Skip to content
Snippets Groups Projects
Commit 28524968 authored by whh8b's avatar whh8b
Browse files

Add directory for fix_rets.

Former-commit-id: 917655353860a8eeec1837a82a180eaf0e8b15ab
parent 7fa307b5
No related branches found
No related tags found
No related merge requests found
......@@ -573,6 +573,11 @@ tools/cover/Makefile -text
tools/cover/cover.cpp -text
tools/cover/coverage.cpp -text
tools/cover/coverage.h -text
tools/fix_rets/Makefile -text
tools/fix_rets/Makefile.in -text
tools/fix_rets/fix_rets.cpp -text
tools/fix_rets/fix_rets.hpp -text
tools/fix_rets/fix_rets_driver.cpp -text
tools/fptr_shadow/LICENSE.txt -text
tools/fptr_shadow/Makefile -text
tools/fptr_shadow/fptr_shadow_driver.cpp -text
......
PROGS=fix_rets.exe
CXX=g++
CXXFLAGS=
INCLUDE=-I. -I../include -I../xform -I../../beaengine/include -I../../libIRDB/include/ -I../../libMEDSannotation/include/ -I../libtransform/include/ -I../transforms
CXXFLAGS= $(INCLUDE) -Wall
LIBS=-L../../lib -lxform -lIRDB-core -lIRDB-cfg -lBeaEngine_s_d -lpqxx -lMEDSannotation -ltransform# ../transforms/Rewrite_Utility.o
OBJS=fix_rets.o fix_rets_driver.o
programs=fix_rets.exe
.SUFFIXES: .o .c .exe .cpp .hpp
all: $(programs)
@echo "---------------------------------------------"
@echo "- Fix Rets directory -- Build complete -"
@echo "---------------------------------------------"
-include $(OBJS:.o=.d)
%.o: %.cpp
$(CXX) -c $(CXXFLAGS) $*.cpp
@#
@# build dependencies -- http://scottmcpeak.com/autodepend/autodepend.html
@#
$(CXX) -MM $(CXXFLAGS) $*.cpp > $*.d
@cp -f $*.d $*.d.tmp
@sed -e 's/.*://' -e 's/\\$$//' < $*.d.tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $*.d
@rm -f $*.d.tmp
clean:
rm -f *.o core *.exe
$(programs): ../../lib/*.a
fix_rets.exe: $(OBJS)
$(CXX) $(CXXFLAGS) $^ $(INCLUDE) $(LIBS) -o $@
PROGS=fix_rets.exe
CXX=@CXX@
CXXFLAGS=
INCLUDE=-I. -I../include -I../xform -I../../beaengine/include -I../../libIRDB/include/ -I../../libMEDSannotation/include/ -I../libtransform/include/ -I../transforms
CXXFLAGS= @EXTRA_CXXFLAGS@ $(INCLUDE) -Wall
LIBS=-L../../lib -lxform -lIRDB-core -lIRDB-cfg -lBeaEngine_s_d -lpqxx -lMEDSannotation -ltransform# ../transforms/Rewrite_Utility.o
OBJS=fix_rets.o fix_rets_driver.o
programs=fix_rets.exe
.SUFFIXES: .o .c .exe .cpp .hpp
all: $(programs)
@echo "---------------------------------------------"
@echo "- Fix Rets directory -- Build complete -"
@echo "---------------------------------------------"
-include $(OBJS:.o=.d)
%.o: %.cpp
$(CXX) -c $(CXXFLAGS) $*.cpp
@#
@# build dependencies -- http://scottmcpeak.com/autodepend/autodepend.html
@#
$(CXX) -MM $(CXXFLAGS) $*.cpp > $*.d
@cp -f $*.d $*.d.tmp
@sed -e 's/.*://' -e 's/\\$$//' < $*.d.tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $*.d
@rm -f $*.d.tmp
clean:
rm -f *.o core *.exe
$(programs): ../../lib/*.a
fix_rets.exe: $(OBJS)
$(CXX) $(CXXFLAGS) $^ $(INCLUDE) $(LIBS) -o $@
#include "fix_rets.hpp"
#include <assert.h>
using namespace libTransform;
FixRets::FixRets(FileIR_t *p_variantIR) : Transform(NULL, p_variantIR, NULL)
{
}
int FixRets::execute()
{
string register_stack_pointer;
string stack_offset_size;
string pop_insn_assembly, ret_insn_assembly;
if (getFileIR()->GetArchitectureBitWidth() == 64)
{
register_stack_pointer = "rsp";
stack_offset_size = "8";
}
else
{
register_stack_pointer = "esp";
stack_offset_size = "4";
}
pop_insn_assembly = "lea " + register_stack_pointer +
", [" + register_stack_pointer +
"+" + stack_offset_size + "]";
ret_insn_assembly = "jmp [" +
register_stack_pointer +
"-" + stack_offset_size + "]";
cout << "pop_insn_assembly: " << pop_insn_assembly << endl;
cout << "ret_insn_assembly: " << ret_insn_assembly << endl;
for(
set<Function_t*>::const_iterator itf=getFileIR()->GetFunctions().begin();
itf!=getFileIR()->GetFunctions().end();
++itf
)
{
Function_t* func=*itf;
for(
set<Instruction_t*>::const_iterator it=func->GetInstructions().begin();
it!=func->GetInstructions().end();
++it)
{
Instruction_t* insn = *it;
if(insn&& insn->GetAddress())
{
if (insn!=NULL)
{
DISASM disasm;
insn->Disassemble(disasm);
string stack_pointer;
string stack_offset_size;
cout << "Complete instruction: " << disasm.CompleteInstr << "-" << endl;
if (strcmp(disasm.CompleteInstr,"ret "))
continue;
/*
* For 64-bit only at this point.
*/
Instruction_t *pop = NULL;
Instruction_t *ret = insn;
pop = allocateNewInstruction(
insn->GetAddress()->GetFileID(), insn->GetFunction());
setAssembly(pop, pop_insn_assembly);
setAssembly(ret, ret_insn_assembly);
carefullyInsertBefore(ret, pop);
pop->SetFallthrough(ret);
cout << "Fixing a ret!" << endl;
}
}
}
}
return true;
}
#ifndef _LIBTRANSFORM_FIX_RETS_H_
#define _LIBTRANSFORM_FIX_RETS_H_
#include "../../libtransform/include/transform.hpp"
#include "../../libMEDSannotation/include/VirtualOffset.hpp"
using namespace std;
using namespace libIRDB;
class FixRets : public libTransform::Transform
{
public:
FixRets(FileIR_t*p_variantIR);
int execute();
};
#endif
#include <stdlib.h>
#include <fstream>
#include <libIRDB-core.hpp>
#include <libgen.h>
#include "fix_rets.hpp"
using namespace std;
using namespace libIRDB;
void usage(char* name)
{
cerr<<"Usage: "<<name<<" <variant_id>\n";
}
int main(int argc, char **argv)
{
if(argc != 2)
{
usage(argv[0]);
exit(1);
}
string programName(argv[0]);
int variantID = atoi(argv[1]);
VariantID_t *pidp=NULL;
/* setup the interface to the sql server */
pqxxDB_t pqxx_interface;
BaseObj_t::SetInterface(&pqxx_interface);
pidp=new VariantID_t(variantID);
assert(pidp->IsRegistered()==true);
cout<<"ret_shadow_stack.exe started\n";
bool one_success = false;
for(set<File_t*>::iterator it=pidp->GetFiles().begin();
it!=pidp->GetFiles().end();
++it)
{
File_t* this_file = *it;
FileIR_t *firp = new FileIR_t(*pidp, this_file);
cout<<"Transforming "<<this_file->GetURL()<<endl;
assert(firp && pidp);
try
{
FixRets fix_rets(firp);
int success=fix_rets.execute();
if (success)
{
cout<<"Writing changes for "<<this_file->GetURL()<<endl;
one_success = true;
firp->WriteToDB();
delete firp;
}
else
{
cout<<"Skipping (no changes) "<<this_file->GetURL()<<endl;
}
}
catch (DatabaseError_t pnide)
{
cerr << programName << ": Unexpected database error: " << pnide << "file url: " << this_file->GetURL() << endl;
}
catch (...)
{
cerr << programName << ": Unexpected error file url: " << this_file->GetURL() << endl;
}
} // end file iterator
// if any integer transforms for any files succeeded, we commit
if (one_success)
{
cout<<"Commiting changes...\n";
pqxx_interface.Commit();
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment