From 6661ea505ab6562fe8738024cde9ac2be12a125d Mon Sep 17 00:00:00 2001 From: bdr7fv <bdr7fv@git.zephyr-software.com> Date: Tue, 29 May 2012 01:55:01 +0000 Subject: [PATCH] More comment cleanup for PNTransformDriver.cpp Former-commit-id: b785fed302e131895847afcec75f73311832f45c --- tools/transforms/PNTransformDriver.cpp | 240 ------------------------- 1 file changed, 240 deletions(-) diff --git a/tools/transforms/PNTransformDriver.cpp b/tools/transforms/PNTransformDriver.cpp index b737a87f5..2e803127d 100644 --- a/tools/transforms/PNTransformDriver.cpp +++ b/tools/transforms/PNTransformDriver.cpp @@ -1338,247 +1338,7 @@ inline bool PNTransformDriver::Instruction_Rewrite(PNStackLayout *layout, Instru return true; } -/* -//TODO: this is a naive rewrite, more analysis is needed -//TODO: check if pmatch actually has a match -bool PNTransformDriver::Rewrite(PNStackLayout *layout, Function_t *func) -{ - //TODO: handle this better - assert(layout != NULL); - assert(func != NULL); - - cerr<<"PNTransformDriver: Rewriting Function = "<<func->GetName()<<endl; - - int max = PNRegularExpressions::MAX_MATCHES; - regmatch_t pmatch[max]; - memset(pmatch, 0,sizeof(regmatch_t) * max); - - //TODO: if no stack allocation is seen before a stack access, ignore, don't abort? - - //rewrite instructions - bool stack_alloc = false; - for( - set<Instruction_t*>::const_iterator it=func->GetInstructions().begin(); - it!=func->GetInstructions().end(); - ++it - ) - { - Instruction_t* instr=*it; - string matched =""; - string disasm_str = ""; - DISASM disasm; - - instr->Disassemble(disasm); - disasm_str = disasm.CompleteInstr; - - cerr << "PNTransformDriver: Looking at Instruction = " << disasm_str << endl; - - //the disassmebly of lea has extra tokens not accepted by nasm, remove those tokens - if(regexec(&(pn_regex.regex_lea_hack), disasm_str.c_str(), max, pmatch, 0)==0) - { - cerr<<"PNTransformDriver: Transforming LEA Instruction"<<endl; - matched = ""; - for (int k = 1; k < 5; ++k) - { - if (pmatch[k].rm_so >= 0 && pmatch[k].rm_eo >= 0) - { - int mlen = pmatch[k].rm_eo - pmatch[k].rm_so; - matched.append(disasm_str.substr(pmatch[k].rm_so,mlen)); - } - } - disasm_str = matched; - cerr<<"PNTransformDriver: New LEA Instruction = "<<disasm_str<<endl; - matched = ""; - } - - - if(regexec(&(pn_regex.regex_stack_alloc), disasm_str.c_str(), 5, pmatch, 0)==0) - { - cerr << "PNTransformDriver: Transforming Stack Alloc"<<endl; - - //TODO: what should I do in this case? - //TODO: transform the instruction but never decrease the size of the allocation - if(stack_alloc) - { - cerr <<"PNTransformDriver: Stack Alloc Previously Found, Ignoring Instruction"<<endl; - continue; - } - - stringstream ss; - ss << hex << layout->GetAlteredAllocSize(); - - disasm_str = "sub esp, 0x"+ss.str(); - - cerr<<"PNTransformDriver: New Instruction = "<<disasm_str<<endl; - undo_list[instr] = instr->GetDataBits(); - if(!instr->Assemble(disasm_str)) - return false; - - stack_alloc = true; - } - else if(regexec(&(pn_regex.regex_esp_only), disasm_str.c_str(), max, pmatch, 0)==0) - { - cerr<<"PNTransformDriver: Transforming ESP Only Instruction ([esp])"<<endl; - - PNRange *closest = layout->GetClosestRangeESP(0); - - if(closest == NULL) - { - //There should always be a closet range to esp+0 - assert(false); - } - - int new_offset = closest->GetDisplacement(); - - assert(new_offset >= 0); - - if(new_offset == 0) - { - cerr<<"PNTransformDriver: Displacement of [esp] is Zero, Ignoring Transformation"<<endl; - continue; - } - - stringstream ss; - ss<<hex<<new_offset; - - matched = "esp+0x"+ss.str(); - int mlen = pmatch[1].rm_eo - pmatch[1].rm_so; - disasm_str.replace(pmatch[1].rm_so,mlen,matched); - - cerr<<"PNTransformDriver: New Instruction = "<<disasm_str<<endl; - undo_list[instr] = instr->GetDataBits(); - if(!instr->Assemble(disasm_str.c_str())) - return false; - - } -//TODO: the regular expression order does matter, scaled must come first, change the regex so this doesn't matter - else if(regexec(&(pn_regex.regex_esp_scaled), disasm_str.c_str(), 5, pmatch, 0)==0 || - regexec(&(pn_regex.regex_esp_direct), disasm_str.c_str(), 5, pmatch, 0)==0) - { - cerr<<"PNTransformDriver: Transforming ESP Relative Instruction"<<endl; - int mlen = pmatch[1].rm_eo - pmatch[1].rm_so; - matched = disasm_str.substr(pmatch[1].rm_so,mlen); - - // extract displacement - int offset = strtol(matched.c_str(),NULL,0); - - //TODO: I don't think this can happen but just in case - assert(offset >= 0); - - int new_offset = layout->GetNewOffsetESP(offset); - - stringstream ss; - ss<<hex<<new_offset; - - matched = "0x"+ss.str(); - - disasm_str.replace(pmatch[1].rm_so,mlen,matched); - - cerr<<"PNTransformDriver: New Instruction = "<<disasm_str<<endl; - undo_list[instr] = instr->GetDataBits(); - if(!instr->Assemble(disasm_str.c_str())) - return false; - } - //TODO: the regular expression order does matter, scaled must come first, change the regex so this doesn't matter - else if(regexec(&(pn_regex.regex_ebp_scaled), disasm_str.c_str(), 5, pmatch, 0)==0 || - regexec(&(pn_regex.regex_ebp_direct), disasm_str.c_str(), 5, pmatch, 0)==0) - { - cerr<<"PNTransformDriver: Transforming EBP Relative Instruction"<<endl; - - int mlen = pmatch[1].rm_eo - pmatch[1].rm_so; - matched = disasm_str.substr(pmatch[1].rm_so,mlen); - - // extract displacement - int offset = strtol(matched.c_str(),NULL,0); - - cerr<<"PNTransformDriver: Offset = "<<offset<<endl; - - int new_offset = layout->GetNewOffsetEBP(offset); - - if(new_offset == offset) - { - cerr<<"PNTransformDriver: No offset transformation necessary, skipping instruction"<<endl; - continue; - } - - stringstream ss; - ss<<hex<<new_offset; - - matched = "0x"+ss.str(); - - disasm_str.replace(pmatch[1].rm_so,mlen,matched); - - cerr<<"PNTransformDriver: New Instruction = "<<disasm_str<<endl; - undo_list[instr] = instr->GetDataBits(); - if(!instr->Assemble(disasm_str.c_str())) - return false; - - } - - else if(regexec(&(pn_regex.regex_stack_dealloc), disasm_str.c_str(), 5, pmatch, 0)==0) - { - cerr<<"PNTransformDriver: Transforming Stack Dealloc Instruction"<<endl; - - //Check if the dealloc amount is 0. In unoptimized code, sometimes the - //compiler will reset esp, and then add 0 to esp - //In this case, do not deallocate the stack - - int mlen = pmatch[1].rm_eo - pmatch[1].rm_so; - matched = disasm_str.substr(pmatch[1].rm_so,mlen); - - // extract displacement - int offset = strtol(matched.c_str(),NULL,0); - - cerr<<"PNTransformDriver: Dealloc Amount = "<<offset<<endl; - - if(offset == 0) - { - cerr<<"PNTransformDriver: Dealloc of 0 detected, ignoring instruction"<<endl; - continue; - } - - stringstream ss; - ss << hex <<layout->GetAlteredAllocSize(); - - disasm_str = "add esp, 0x"+ss.str(); - - undo_list[instr] = instr->GetDataBits(); - cerr<<"PNTransformDriver: New Instruction = "<<disasm_str<<endl; - if (!instr->Assemble(disasm_str)) - return false; - } - else - cerr<<"PNTransformDriver: No Pattern Match"<<endl; - } - - //If you get here assume the transform was successfully made - return true; -} - -*/ - - /* -void PNTransformDriver::undo(map<Instruction_t*, string> undo_list, Function_t *func) -{ - //rollback any changes - cerr<<"PNTransformDriver: Undo Transform: "<<undo_list.size()<<" instructions to rollback for function "<<func->GetName()<<endl; - for( - map<Instruction_t*, std::string>::const_iterator mit=undo_list.begin(); - mit != undo_list.end(); - ++mit) - { - Instruction_t* insn = mit->first; - std::string dataBits = mit->second; - - DISASM disasm; - insn->Disassemble(disasm); - insn->SetDataBits(dataBits); - } - - undo_list.clear(); -} -*/ //TODO: there is a memory leak, I need to write a undo_list clear to properly cleanup //void PNTransformDriver::undo(map<Instruction_t*, Instruction_t*> undo_list, Function_t *func) -- GitLab