Skip to content

Commit 659ac45

Browse files
authored
Do not lift functions that are not in the JSON spec (#102)
* 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
1 parent 7e2ac5f commit 659ac45

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

lib/Lift.cpp

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -617,23 +617,44 @@ bool LiftCodeIntoModule(const remill::Arch *arch, const Program &program,
617617

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

624+
// Check if we have mapped bytes
625+
if (!program.FindByte(addr)) {
626+
return true;
627+
}
624628
// Set initializer
625-
auto init = CreateConstFromMemory(addr, decl->type, arch, program, module);
629+
auto init{CreateConstFromMemory(addr, decl->type, arch, program, module)};
626630
gvar->setInitializer(init);
627-
628631
return true;
629632
});
630633

631634
// Lift functions.
632635
program.ForEachFunction([&](const FunctionDecl *decl) {
633-
const auto entry = lifter.LiftFunction(*decl);
634-
DefineNativeToLiftedWrapper(arch, *decl, entry);
636+
// Initialize function entry. This will lift machine code
637+
// into `entry.lifted` if instruction bytes for are
638+
// available and declare `entry.lifted_to_native` and
639+
// `entry.lifted_to_native` wrapper functions that
640+
// are needed for further lifting to native functions.
641+
const auto entry{lifter.LiftFunction(*decl)};
642+
643+
// We have `entry.lifted` available. `entry.lifted`
644+
// will be inlined into `entry.native_to_lifted`.
645+
if (!entry.lifted->isDeclaration()) {
646+
DefineNativeToLiftedWrapper(arch, *decl, entry);
647+
}
648+
// Wrap native functions in a function that lifted
649+
// functions can call. This will result in the
650+
// lifted functions calling the native ones.
635651
DefineLiftedToNativeWrapper(*decl, entry);
652+
653+
// Optimize and inline. After this we should end up
654+
// with only native functions.
636655
OptimizeFunction(entry.native_to_lifted);
656+
657+
// The ritual is done.
637658
return true;
638659
});
639660

lib/MCToIRLifter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,11 @@ FunctionEntry MCToIRLifter::LiftFunction(const FunctionDecl &decl) {
455455
if (!entry.native_to_lifted->isDeclaration()) {
456456
return entry;
457457
}
458+
// Check if there's any instruction bytes to lift
459+
if (auto start{program.FindByte(decl.address)};
460+
!start || !start.IsExecutable()) {
461+
return entry;
462+
}
458463

459464
work_list.clear();
460465
addr_to_block.clear();

0 commit comments

Comments
 (0)