Vraie gestion des erreurs

This commit is contained in:
Horde Nicolas 2021-07-11 15:27:07 +02:00
parent eaadfa4bc8
commit 73df49a5ba
1 changed files with 67 additions and 88 deletions

153
ia86.cpp
View File

@ -322,7 +322,7 @@ Desassembler::Desassembler(TextWindow *log) : log(log)
{ {
err = cs_open(CS_ARCH_X86, CS_MODE_16, &handle); err = cs_open(CS_ARCH_X86, CS_MODE_16, &handle);
if (err != CS_ERR_OK) if (err != CS_ERR_OK)
Error("Désassembleur - initialisation....................[ERREUR]"); throw Error("Désassembleur - initialisation....................[ERREUR]");
log->append("Désassembleur - initialisation....................[ OK ]"); log->append("Désassembleur - initialisation....................[ OK ]");
} }
catch(exception const& e) catch(exception const& e)
@ -337,10 +337,10 @@ void Desassembler::Desassemble(uint8_t *content, uint32_t address,uint32_t size,
{ {
srcsize=cs_disasm(handle, content, size, address, 0, &insn); srcsize=cs_disasm(handle, content, size, address, 0, &insn);
if (srcsize == 0) if (srcsize == 0)
Error("Désassembleur - Désassemblage.....................[ERREUR]"); throw Error("Désassembleur - désassemblage.....................[ERREUR]");
else else
{ {
if (debug) log->append("Désassemblage - Désassemblage.....................[ "+to_string(srcsize)+"l ]"); if (debug) log->append("Désassemblage - désassemblage.....................[ "+to_string(srcsize)+"l ]");
unasm->src.clear(); unasm->src.clear();
unasm->pos.clear(); unasm->pos.clear();
for (size_t j = 0; j < srcsize; j++) for (size_t j = 0; j < srcsize; j++)
@ -377,7 +377,7 @@ Assembler::Assembler(TextWindow *log) : log(log)
{ {
err = ks_open(KS_ARCH_X86, KS_MODE_16, &ks); err = ks_open(KS_ARCH_X86, KS_MODE_16, &ks);
if (err != KS_ERR_OK) if (err != KS_ERR_OK)
Error("Assembleur - initialisation.......................[ERREUR]"); throw Error("Assembleur - initialisation.......................[ERREUR]");
log->append("Assembleur - initialisation.......................[ OK ]"); log->append("Assembleur - initialisation.......................[ OK ]");
} }
catch(exception const& e) catch(exception const& e)
@ -423,13 +423,8 @@ std::vector<Code> Assembler::MultiAssemble(std::string source,uint32_t address)
if (code->src.size()>0) if (code->src.size()>0)
mcode.push_back(*code); mcode.push_back(*code);
for(size_t i=0;i<mcode.size();i++) for(size_t i=0;i<mcode.size();i++)
{
//log->append(mcode[i].src);
mcode[i].assembled=false;
mcode[i].loaded=false;
this->Assemble(&mcode[i]); this->Assemble(&mcode[i]);
if (debug) log->append("Section N°"+std::to_string(i)+" : "+intToHexString(mcode[i].address,8)+" -> "+to_string(mcode[i].size)+" octets"); log->append("Assembleur - assemblage...........................[ OK ]");
}
return mcode; return mcode;
} }
catch(exception const& e) catch(exception const& e)
@ -451,7 +446,8 @@ void Assembler::Assemble(Code *code)
{ {
code->size=0; code->size=0;
code->assembled=false; code->assembled=false;
Error("Assembleur - assemblage...........................[ERREUR]"); code->loaded=false;
throw Error("Assembleur - assemblage...........................[ERREUR]");
} }
else else
code->assembled=true; code->assembled=true;
@ -483,7 +479,7 @@ std::string VMEngine::getFlags()
int eflags=0; int eflags=0;
err = uc_reg_read(uc, UC_X86_REG_EFLAGS, &eflags); err = uc_reg_read(uc, UC_X86_REG_EFLAGS, &eflags);
if (err != UC_ERR_OK) if (err != UC_ERR_OK)
Error("VM IA86 - voir EFLAGS.............................[ERREUR]"); throw Error("VM IA86 - voir EFLAGS.............................[ERREUR]");
std::stringstream out; std::stringstream out;
out << " CF:" << std::dec << ((eflags & 0x0001)); out << " CF:" << std::dec << ((eflags & 0x0001));
if (rights > 8) if (rights > 8)
@ -539,7 +535,7 @@ std::string VMEngine::getRegs()
} }
err = uc_reg_read_batch(uc, regsi836, ptrs, sizeof(regsi836)); err = uc_reg_read_batch(uc, regsi836, ptrs, sizeof(regsi836));
if (err > 0) { if (err > 0) {
Error("VM IA86 - voir REGISTRES..........................[ERREUR]"); throw Error("VM IA86 - voir REGISTRES..........................[ERREUR]");
return ""; return "";
} }
std::stringstream out; std::stringstream out;
@ -627,7 +623,7 @@ void VMEngine::Init()
{ {
err = uc_open(UC_ARCH_X86, UC_MODE_16, &uc); err = uc_open(UC_ARCH_X86, UC_MODE_16, &uc);
if (err != UC_ERR_OK) if (err != UC_ERR_OK)
Error("VM IA86 - initilisation...........................[ERREUR]"); throw Error("VM IA86 - initilisation...........................[ERREUR]");
log->append("VM IA86 - initilisation...........................[ OK ]"); log->append("VM IA86 - initilisation...........................[ OK ]");
} }
catch(exception const& e) catch(exception const& e)
@ -654,13 +650,15 @@ void VMEngine::Close()
void VMEngine::Halt() void VMEngine::Halt()
{ {
if (this->executed) if (this->executed)
log->append("VM IA86 - arret...................................[ERREUR]"); log->append("VM IA86 - arret...................................[ INFO ]");
this->executed=false; this->executed=false;
} }
void VMEngine::Unconfigure() void VMEngine::Unconfigure()
{ {
this->Halt(); this->Halt();
if (this->initialized)
log->append("VM IA86 - déconfiguration.........................[ INFO ]");
this->initialized=false; this->initialized=false;
} }
@ -690,7 +688,7 @@ std::vector<std::array<std::string, 5>> VMEngine::getInstr(int segment, int addr
err = uc_mem_read(uc, begin, code, 500); err = uc_mem_read(uc, begin, code, 500);
if (err) if (err)
{ {
Error("VM IA86 - cache instructions......................[ERREUR]"); throw Error("VM IA86 - cache instructions......................[ERREUR]");
} }
bufferaddress=begin; bufferaddress=begin;
} }
@ -699,7 +697,7 @@ std::vector<std::array<std::string, 5>> VMEngine::getInstr(int segment, int addr
{ {
unasmer.Desassemble(code, address, 500, &unasm); unasmer.Desassemble(code, address, 500, &unasm);
if (unasm.src.size()==0) if (unasm.src.size()==0)
Error("VM IA86 - cache instructions......................[ERREUR]"); throw Error("VM IA86 - cache instructions......................[ERREUR]");
crc_old=crc; crc_old=crc;
} }
int line=0; int line=0;
@ -746,13 +744,21 @@ void VMEngine::Configure(State *init, std::string code)
//log->append("Mappage de la mémoire virtuelle"); //log->append("Mappage de la mémoire virtuelle");
uc_mem_map(uc, 0, 1 * 1024 * 1024, UC_PROT_ALL); uc_mem_map(uc, 0, 1 * 1024 * 1024, UC_PROT_ALL);
for(size_t i=0;i<mcode.size();i++) for(size_t i=0;i<mcode.size();i++)
SetMem(&mcode[i]); {
if (mcode[i].assembled)
SetMem(&mcode[i]);
else
throw Error("VM IA86 - code non assemblé...................[ERREUR]");
if (debug) log->append("Section N°"+std::to_string(i)+" : "+intToHexString(mcode[i].address,8)+" -> "+to_string(mcode[i].size)+" octets");
}
status=verify(); status=verify();
if (status==0) if (status==0)
{
this->initialized=true; this->initialized=true;
SetRegs(init);
}
else else
this->initialized=false; this->initialized=false;
SetRegs(init);
} }
catch(exception const& e) catch(exception const& e)
{ {
@ -788,7 +794,7 @@ uint32_t VMEngine::getEIP()
int eip; int eip;
err = uc_reg_read(uc, UC_X86_REG_EIP, &eip); err = uc_reg_read(uc, UC_X86_REG_EIP, &eip);
if (err != UC_ERR_OK) if (err != UC_ERR_OK)
Error("VM IA86 - voir EIP................................[ERREUR]"); throw Error("VM IA86 - voir EIP................................[ERREUR]");
return eip; return eip;
} }
@ -797,7 +803,7 @@ uint16_t VMEngine::getCS()
int cs; int cs;
err = uc_reg_read(uc, UC_X86_REG_CS, &cs); err = uc_reg_read(uc, UC_X86_REG_CS, &cs);
if (err != UC_ERR_OK) if (err != UC_ERR_OK)
Error("VM IA86 - voir CS.................................[ERREUR]"); throw Error("VM IA86 - voir CS.................................[ERREUR]");
return cs; return cs;
} }
@ -806,7 +812,7 @@ uint16_t VMEngine::getDS()
int ds; int ds;
err = uc_reg_read(uc, UC_X86_REG_DS, &ds); err = uc_reg_read(uc, UC_X86_REG_DS, &ds);
if (err != UC_ERR_OK) if (err != UC_ERR_OK)
Error("VM IA86 - voir DS.................................[ERREUR]"); throw Error("VM IA86 - voir DS.................................[ERREUR]");
return ds; return ds;
} }
@ -815,7 +821,7 @@ uint16_t VMEngine::getES()
int es; int es;
err = uc_reg_read(uc, UC_X86_REG_ES, &es); err = uc_reg_read(uc, UC_X86_REG_ES, &es);
if (err != UC_ERR_OK) if (err != UC_ERR_OK)
Error("VM IA86 - voir ES.................................[ERREUR]"); throw Error("VM IA86 - voir ES.................................[ERREUR]");
return es; return es;
} }
@ -824,7 +830,7 @@ uint16_t VMEngine::getSS()
int ss; int ss;
err = uc_reg_read(uc, UC_X86_REG_SS, &ss); err = uc_reg_read(uc, UC_X86_REG_SS, &ss);
if (err != UC_ERR_OK) if (err != UC_ERR_OK)
Error("VM IA86 - voir SS.................................[ERREUR]"); throw Error("VM IA86 - voir SS.................................[ERREUR]");
return ss; return ss;
} }
@ -835,7 +841,7 @@ void VMEngine::SetMem(Code *code)
if (err) if (err)
{ {
code->loaded=false; code->loaded=false;
Error("VM IA86 - copie mémoire...........................[ERREUR]"); throw Error("VM IA86 - copie mémoire...........................[ERREUR]");
return; return;
} }
code->loaded=true; code->loaded=true;
@ -847,7 +853,7 @@ void VMEngine::SetRegs(State *init)
out << "VM IA86 - configuration initiale..................[ OK ]"; out << "VM IA86 - configuration initiale..................[ OK ]";
err = uc_reg_write(uc, UC_X86_REG_EIP, &init->dump.regs.eip); err = uc_reg_write(uc, UC_X86_REG_EIP, &init->dump.regs.eip);
if (err != UC_ERR_OK) if (err != UC_ERR_OK)
Error("VM IA86 - configuration initiale EIP..............[ERREUR]"); throw Error("VM IA86 - configuration initiale EIP..............[ERREUR]");
else else
if (init->dump.regs.eip != 0x00000000) if (init->dump.regs.eip != 0x00000000)
if ((init->dump.regs.eip & 0xFFFF0000) == 0x00000000) if ((init->dump.regs.eip & 0xFFFF0000) == 0x00000000)
@ -856,7 +862,7 @@ void VMEngine::SetRegs(State *init)
out << "EIP=" << std::uppercase << std::setfill('0') << std::setw(8) << std::hex << init->dump.regs.eip << " "; out << "EIP=" << std::uppercase << std::setfill('0') << std::setw(8) << std::hex << init->dump.regs.eip << " ";
err = uc_reg_write(uc, UC_X86_REG_EDI, &init->dump.regs.edi); err = uc_reg_write(uc, UC_X86_REG_EDI, &init->dump.regs.edi);
if (err != UC_ERR_OK) if (err != UC_ERR_OK)
Error("VM IA86 - configuration initiale EDI..............[ERREUR]"); throw Error("VM IA86 - configuration initiale EDI..............[ERREUR]");
else else
if (init->dump.regs.edi != 0x00000000) if (init->dump.regs.edi != 0x00000000)
if ((init->dump.regs.edi & 0xFFFF0000) == 0x00000000) if ((init->dump.regs.edi & 0xFFFF0000) == 0x00000000)
@ -865,7 +871,7 @@ void VMEngine::SetRegs(State *init)
out << "EDI=" << std::uppercase << std::setfill('0') << std::setw(8) << std::hex << init->dump.regs.edi << " "; out << "EDI=" << std::uppercase << std::setfill('0') << std::setw(8) << std::hex << init->dump.regs.edi << " ";
err = uc_reg_write(uc, UC_X86_REG_ESI, &init->dump.regs.esi); err = uc_reg_write(uc, UC_X86_REG_ESI, &init->dump.regs.esi);
if (err != UC_ERR_OK) if (err != UC_ERR_OK)
Error("VM IA86 - configuration initiale ESI..............[ERREUR]"); throw Error("VM IA86 - configuration initiale ESI..............[ERREUR]");
else else
if (init->dump.regs.esi != 0x00000000) if (init->dump.regs.esi != 0x00000000)
if ((init->dump.regs.esi & 0xFFFF0000) == 0x00000000) if ((init->dump.regs.esi & 0xFFFF0000) == 0x00000000)
@ -874,7 +880,7 @@ void VMEngine::SetRegs(State *init)
out << "ESI=" << std::uppercase << std::setfill('0') << std::setw(8) << std::hex << init->dump.regs.esi << " "; out << "ESI=" << std::uppercase << std::setfill('0') << std::setw(8) << std::hex << init->dump.regs.esi << " ";
err = uc_reg_write(uc, UC_X86_REG_EBP, &init->dump.regs.ebp); err = uc_reg_write(uc, UC_X86_REG_EBP, &init->dump.regs.ebp);
if (err != UC_ERR_OK) if (err != UC_ERR_OK)
Error("VM IA86 - configuration initiale EBP..............[ERREUR]"); throw Error("VM IA86 - configuration initiale EBP..............[ERREUR]");
else else
if (init->dump.regs.ebp != 0x00000000) if (init->dump.regs.ebp != 0x00000000)
if ((init->dump.regs.ebp & 0xFFFF0000) == 0x00000000) if ((init->dump.regs.ebp & 0xFFFF0000) == 0x00000000)
@ -883,7 +889,7 @@ void VMEngine::SetRegs(State *init)
out << "EBP=" << std::uppercase << std::setfill('0') << std::setw(8) << std::hex << init->dump.regs.ebp << " "; out << "EBP=" << std::uppercase << std::setfill('0') << std::setw(8) << std::hex << init->dump.regs.ebp << " ";
err = uc_reg_write(uc, UC_X86_REG_ESP, &init->dump.regs.esp); err = uc_reg_write(uc, UC_X86_REG_ESP, &init->dump.regs.esp);
if (err != UC_ERR_OK) if (err != UC_ERR_OK)
Error("VM IA86 - configuration initiale ESP..............[ERREUR]"); throw Error("VM IA86 - configuration initiale ESP..............[ERREUR]");
else else
if (init->dump.regs.esp != 0x00000000) if (init->dump.regs.esp != 0x00000000)
if ((init->dump.regs.esp & 0xFFFF0000) == 0x00000000) if ((init->dump.regs.esp & 0xFFFF0000) == 0x00000000)
@ -892,7 +898,7 @@ void VMEngine::SetRegs(State *init)
out << "ESP=" << std::uppercase << std::setfill('0') << std::setw(8) << std::hex << init->dump.regs.esp << " "; out << "ESP=" << std::uppercase << std::setfill('0') << std::setw(8) << std::hex << init->dump.regs.esp << " ";
err = uc_reg_write(uc, UC_X86_REG_EBX, &init->dump.regs.ebx); err = uc_reg_write(uc, UC_X86_REG_EBX, &init->dump.regs.ebx);
if (err != UC_ERR_OK) if (err != UC_ERR_OK)
Error("VM IA86 - configuration initiale EBX..............[ERREUR]"); throw Error("VM IA86 - configuration initiale EBX..............[ERREUR]");
else else
if (init->dump.regs.ebx != 0x00000000) if (init->dump.regs.ebx != 0x00000000)
if ((init->dump.regs.ebx & 0xFFFF0000) == 0x00000000) if ((init->dump.regs.ebx & 0xFFFF0000) == 0x00000000)
@ -901,7 +907,7 @@ void VMEngine::SetRegs(State *init)
out << "EBX=" << std::uppercase << std::setfill('0') << std::setw(8) << std::hex << init->dump.regs.ebx << " "; out << "EBX=" << std::uppercase << std::setfill('0') << std::setw(8) << std::hex << init->dump.regs.ebx << " ";
err = uc_reg_write(uc, UC_X86_REG_EDX, &init->dump.regs.edx); err = uc_reg_write(uc, UC_X86_REG_EDX, &init->dump.regs.edx);
if (err != UC_ERR_OK) if (err != UC_ERR_OK)
Error("VM IA86 - configuration initiale EDX..............[ERREUR]"); throw Error("VM IA86 - configuration initiale EDX..............[ERREUR]");
else else
if (init->dump.regs.edx != 0x00000000) if (init->dump.regs.edx != 0x00000000)
if ((init->dump.regs.edx & 0xFFFF0000) == 0x00000000) if ((init->dump.regs.edx & 0xFFFF0000) == 0x00000000)
@ -910,7 +916,7 @@ void VMEngine::SetRegs(State *init)
out << "EDX=" << std::uppercase << std::setfill('0') << std::setw(8) << std::hex << init->dump.regs.edx << " "; out << "EDX=" << std::uppercase << std::setfill('0') << std::setw(8) << std::hex << init->dump.regs.edx << " ";
err = uc_reg_write(uc, UC_X86_REG_ECX, &init->dump.regs.ecx); err = uc_reg_write(uc, UC_X86_REG_ECX, &init->dump.regs.ecx);
if (err != UC_ERR_OK) if (err != UC_ERR_OK)
Error("VM IA86 - configuration initiale ECX..............[ERREUR]"); throw Error("VM IA86 - configuration initiale ECX..............[ERREUR]");
else else
if (init->dump.regs.ecx != 0x00000000) if (init->dump.regs.ecx != 0x00000000)
if ((init->dump.regs.ecx & 0xFFFF0000) == 0x00000000) if ((init->dump.regs.ecx & 0xFFFF0000) == 0x00000000)
@ -919,7 +925,7 @@ void VMEngine::SetRegs(State *init)
out << "ECX=" << std::uppercase << std::setfill('0') << std::setw(8) << std::hex << init->dump.regs.ecx << " "; out << "ECX=" << std::uppercase << std::setfill('0') << std::setw(8) << std::hex << init->dump.regs.ecx << " ";
err = uc_reg_write(uc, UC_X86_REG_EAX, &init->dump.regs.eax); err = uc_reg_write(uc, UC_X86_REG_EAX, &init->dump.regs.eax);
if (err != UC_ERR_OK) if (err != UC_ERR_OK)
Error("VM IA86 - configuration initiale EAX..............[ERREUR]"); throw Error("VM IA86 - configuration initiale EAX..............[ERREUR]");
else else
if (init->dump.regs.eax != 0x00000000) if (init->dump.regs.eax != 0x00000000)
if ((init->dump.regs.eax & 0xFFFF0000) == 0x00000000) if ((init->dump.regs.eax & 0xFFFF0000) == 0x00000000)
@ -933,17 +939,20 @@ void VMEngine::Run(uint32_t end,uint64_t timeout)
{ {
try try
{ {
err=uc_emu_start(uc, this->getCurrent(), end, timeout, 0); if (verify()==0 && this->initialized)
if (err)
{ {
this->Halt(); err=uc_emu_start(uc, this->getCurrent(), end, timeout, 0);
Error("VM IA86 - execution...............................[ERREUR]"); if (err)
} {
else this->Halt();
{ throw Error("VM IA86 - execution...............................[ERREUR]");
if (!this->executed) }
log->append("VM IA86 - execution...............................[ INFO ]"); else
this->executed="true"; {
if (!this->executed)
log->append("VM IA86 - execution...............................[ INFO ]");
this->executed="true";
}
} }
} }
catch(exception const& e) catch(exception const& e)
@ -983,13 +992,13 @@ void Menu::initCore()
log.append("Application - charge scénarios....................[ OK ]"); log.append("Application - charge scénarios....................[ OK ]");
log.append("-={ "+ scenario.title+" }=-"); log.append("-={ "+ scenario.title+" }=-");
if (scenario.levels.size()>0) if (scenario.levels.size()>0)
loadLevel(); loadLevel();
maxi(); maxi();
} }
else else
{ {
log.append("Application - charge scénarios....................[ERREUR]");
vm.Unconfigure(); vm.Unconfigure();
log.append("Application - charge scénarios....................[ERREUR]");
mini(); mini();
} }
} }
@ -1016,7 +1025,6 @@ void Menu::mini()
void Menu::maxi() void Menu::maxi()
{ {
AdjustWindows();
Options.show(); Options.show();
Tools.show(); Tools.show();
Window.show(); Window.show();
@ -1225,6 +1233,7 @@ void Menu::onClose (finalcut::FCloseEvent* ev)
void Menu::loadLevel() void Menu::loadLevel()
{ {
vm.Unconfigure();
log.append("Application - charge niveau.......................[ INFO ]"); log.append("Application - charge niveau.......................[ INFO ]");
view.setText("Objectif: "+scenario.levels[scenar.getselected()].title); view.setText("Objectif: "+scenario.levels[scenar.getselected()].title);
view.clear(); view.clear();
@ -1233,8 +1242,8 @@ void Menu::loadLevel()
tuto.append(scenario.levels[scenar.getselected()].tutorial); tuto.append(scenario.levels[scenar.getselected()].tutorial);
edit.set(scenario.levels[scenar.getselected()].code); edit.set(scenario.levels[scenar.getselected()].code);
debug.clear(); debug.clear();
vm.Unconfigure();
vm.setRights(scenario.levels[scenar.getselected()].rights); vm.setRights(scenario.levels[scenar.getselected()].rights);
AdjustWindows();
} }
void Menu::end() void Menu::end()
@ -1273,8 +1282,11 @@ void Menu::showInstr()
{ {
try try
{ {
debug.set(vm.getInstr(vm.getCS(),vm.getEIP(),debug.getHeight()-3)); if (vm.isInitialized())
debug.setmark(vm.getLine()); {
debug.set(vm.getInstr(vm.getCS(),vm.getEIP(),debug.getHeight()-3));
debug.setmark(vm.getLine());
}
} }
catch(exception const& e) catch(exception const& e)
{ {
@ -1321,37 +1333,15 @@ void Menu::exec()
{ {
if (!vm.isInitialized()) if (!vm.isInitialized())
compile(); compile();
if (vm.verify()==1) vm.Run(0xFFFF,0);
{ showInstr();
finalcut::FMessageBox::error(this, "Vous devez compiler le source d'abord !");
return;
}
else if (vm.verify()==2)
{
finalcut::FMessageBox::error(this, "Une erreur de chargement a eu lieu vers la VM !");
return;
}
else
vm.Run(0xFFFF,0);
showInstr();
} }
void Menu::trace() void Menu::trace()
{ {
if (!vm.isInitialized()) if (!vm.isInitialized())
compile(); compile();
if (vm.verify()==1) vm.Run(vm.getNextInstr(),0);
{
finalcut::FMessageBox::error(this, "Vous devez compiler le source d'abord !");
return;
}
else if (vm.verify()==2)
{
finalcut::FMessageBox::error(this, "Une erreur de chargement a eu lieu vers la VM !");
return;
}
else
vm.Run(vm.getNextInstr(),0);
showInstr(); showInstr();
} }
@ -1359,18 +1349,7 @@ void Menu::step()
{ {
if (!vm.isInitialized()) if (!vm.isInitialized())
compile(); compile();
if (vm.verify()==1) vm.Run(vm.getNextInstr(),0);
{
finalcut::FMessageBox::error(this, "Vous devez compiler le source d'abord !");
return;
}
else if (vm.verify()==2)
{
finalcut::FMessageBox::error(this, "Une erreur de chargement a eu lieu vers la VM !");
return;
}
else
vm.Run(vm.getNextInstr(),0);
showInstr(); showInstr();
} }