diff --git a/CMakeLists.txt b/CMakeLists.txt index b730256..e64e548 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/dyninstAPI/CMakeLists.txt b/dyninstAPI/CMakeLists.txt new file mode 100644 index 0000000..14c9355 --- /dev/null +++ b/dyninstAPI/CMakeLists.txt @@ -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) diff --git a/dyninstAPI/wrapFunction.cpp b/dyninstAPI/wrapFunction.cpp new file mode 100644 index 0000000..f09e01b --- /dev/null +++ b/dyninstAPI/wrapFunction.cpp @@ -0,0 +1,74 @@ +#include "BPatch.h" +#include "BPatch_function.h" +#include "BPatch_object.h" +#include "Symtab.h" + +#include +#include +#include + +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 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 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]); +}