Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ add_subdirectory(codeCoverage)
add_subdirectory(dataflowAPI)
add_subdirectory(disassemble)
add_subdirectory(DynC)
add_subdirectory(dyninstAPI)
add_subdirectory(instrumentAFunction)
add_subdirectory(instrumentMemoryAccess)
add_subdirectory(insertSnippet)
Expand Down
5 changes: 5 additions & 0 deletions dyninstAPI/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
project(dyninstAPI LANGUAGES CXX)

add_executable(wrapFunction wrapFunction.cpp)
target_compile_options(wrapFunction PRIVATE ${EXAMPLES_WARNING_FLAGS})
target_link_libraries(wrapFunction Dyninst::dyninstAPI)
74 changes: 74 additions & 0 deletions dyninstAPI/wrapFunction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "BPatch.h"
#include "BPatch_function.h"
#include "BPatch_object.h"
#include "Symtab.h"

#include <iostream>
#include <string>
#include <vector>

namespace st = Dyninst::SymtabAPI;

/* The instrumented binary should have these two functions defined:
*
* void* origMalloc(unsigned long size);
* void* fastMalloc(unsigned long size);
*
*/

char const* orig_func = "origMalloc";
char const* replacement_func = "fastMalloc";

int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "Usage: " << argv[0] << " file\n";
return -1;
}

BPatch bpatch;
BPatch_binaryEdit* appBin = bpatch.openBinary(argv[1]);

if(!appBin) {
std::cerr << "Unable to open '" << argv[1] << "'\n";
return -1;
}

BPatch_image* appImage = appBin->getImage();

auto find_func = [appImage](std::string const& name) -> BPatch_function* {
std::vector<BPatch_function*> funcs;
appImage->findFunction("", funcs);
if(funcs.size() != 1) {
std::cerr << "Unable to find '" << name << "'\n";
return nullptr;
}
return funcs[0];
};

BPatch_function* original_malloc = find_func(orig_func);
if(!original_malloc) {
return -1;
}

BPatch_function* fast_malloc = find_func(replacement_func);
if(!fast_malloc) {
return -1;
}

auto* symtab = st::convert(fast_malloc->getModule())->exec();

std::vector<st::Symbol*> syms;
symtab->findSymbol(syms, orig_func,
st::Symbol::ST_UNKNOWN, // Don’t specify type
st::NameType::prettyName, // Look for demangled symbol name
false, // Not regular expression
false, // Don’t check case
true); // Include undefined symbols

if(syms.size() != 1) {
std::cerr << "Unable to find symbol for '" << orig_func << "'\n";
return -1;
}

appBin->wrapFunction(original_malloc, fast_malloc, syms[0]);
}