Skip to content

Commit

Permalink
Add static function to retrieve public non-runtime functions from a b…
Browse files Browse the repository at this point in the history
…inary
  • Loading branch information
fwsGonzo committed Aug 2, 2024
1 parent ffcd987 commit 2754701
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/sandbox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Sandbox : public Node
void set_program(Ref<ELFResource> program);
Ref<ELFResource> get_program();
PackedStringArray get_functions() const;
static PackedStringArray get_functions_from_binary(const PackedByteArray& binary);
// Make a function call to a function in the guest by its name.
Variant vmcall(const Variant **args, GDExtensionInt arg_count, GDExtensionCallError &error);
Variant vmcall_address(gaddr_t address, const Variant **args, GDExtensionInt arg_count, GDExtensionCallError &error);
Expand Down
19 changes: 19 additions & 0 deletions src/sandbox_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,22 @@ PackedStringArray Sandbox::get_functions() const {
}
return array;
}

PackedStringArray Sandbox::get_functions_from_binary(const PackedByteArray &binary) {
const auto binary_view = std::string_view{ (const char *)binary.ptr(), static_cast<size_t>(binary.size()) };
PackedStringArray array;

// Instantiate Machine without loading the ELF
machine_t machine{ binary_view, riscv::MachineOptions<RISCV_ARCH>{
.load_program = false, // Do not load the ELF program.
} };

// Get all unmangled public functions from the guest program.
for (auto &functions : machine.memory.all_unmangled_function_symbols()) {
// Exclude functions that belong to the C/C++ runtime, as well as compiler-generated functions.
if (exclude_functions.count(functions) == 0) {
array.append(String(std::string(functions).c_str()));
}
}
return array;
}

0 comments on commit 2754701

Please sign in to comment.