Skip to content

Commit

Permalink
Do not lift functions that are not in the JSON spec (#102)
Browse files Browse the repository at this point in the history
* Modifies lifting to ignore functions that do not have mapped bytes in the spec

* Moves byte existence and executability check to LifFunction() and adds comments
  • Loading branch information
surovic authored Feb 16, 2021
1 parent 7e2ac5f commit 659ac45
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
35 changes: 28 additions & 7 deletions lib/Lift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,23 +617,44 @@ bool LiftCodeIntoModule(const remill::Arch *arch, const Program &program,

// Lift global variables.
program.ForEachVariable([&](const anvill::GlobalVarDecl *decl) {
const auto addr = decl->address;
const auto name = anvill::CreateVariableName(addr);
const auto gvar = decl->DeclareInModule(name, module);
const auto addr{decl->address};
const auto name{anvill::CreateVariableName(addr)};
const auto gvar{decl->DeclareInModule(name, module)};

// Check if we have mapped bytes
if (!program.FindByte(addr)) {
return true;
}
// Set initializer
auto init = CreateConstFromMemory(addr, decl->type, arch, program, module);
auto init{CreateConstFromMemory(addr, decl->type, arch, program, module)};
gvar->setInitializer(init);

return true;
});

// Lift functions.
program.ForEachFunction([&](const FunctionDecl *decl) {
const auto entry = lifter.LiftFunction(*decl);
DefineNativeToLiftedWrapper(arch, *decl, entry);
// Initialize function entry. This will lift machine code
// into `entry.lifted` if instruction bytes for are
// available and declare `entry.lifted_to_native` and
// `entry.lifted_to_native` wrapper functions that
// are needed for further lifting to native functions.
const auto entry{lifter.LiftFunction(*decl)};

// We have `entry.lifted` available. `entry.lifted`
// will be inlined into `entry.native_to_lifted`.
if (!entry.lifted->isDeclaration()) {
DefineNativeToLiftedWrapper(arch, *decl, entry);
}
// Wrap native functions in a function that lifted
// functions can call. This will result in the
// lifted functions calling the native ones.
DefineLiftedToNativeWrapper(*decl, entry);

// Optimize and inline. After this we should end up
// with only native functions.
OptimizeFunction(entry.native_to_lifted);

// The ritual is done.
return true;
});

Expand Down
5 changes: 5 additions & 0 deletions lib/MCToIRLifter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,11 @@ FunctionEntry MCToIRLifter::LiftFunction(const FunctionDecl &decl) {
if (!entry.native_to_lifted->isDeclaration()) {
return entry;
}
// Check if there's any instruction bytes to lift
if (auto start{program.FindByte(decl.address)};
!start || !start.IsExecutable()) {
return entry;
}

work_list.clear();
addr_to_block.clear();
Expand Down

0 comments on commit 659ac45

Please sign in to comment.