Skip to content

Commit

Permalink
Add test for multiple CUs per Symtab module (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
hainest authored Oct 10, 2023
1 parent 2910dcd commit 08337ce
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions symtab/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_executable(st_insertion_operators insertion_operators.cpp)
target_link_libraries(st_insertion_operators PRIVATE Dyninst::symtabAPI)

add_subdirectory(enum)
add_subdirectory(module)

add_executable(symtab_includes includes.cpp)
target_link_libraries(symtab_includes PRIVATE Dyninst::symtabAPI)
Expand Down
17 changes: 17 additions & 0 deletions symtab/module/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
project(module)

add_executable(module_parser parser.cpp)
target_link_libraries(module_parser PRIVATE Dyninst::symtabAPI)
add_test(NAME module_parser COMMAND module_parser)

add_library(lib1func1 OBJECT lib1.cpp)
target_compile_definitions(lib1func1 PRIVATE FUNC1)
target_compile_options(lib1func1 PRIVATE -g)

add_library(lib1func2 OBJECT lib1.cpp)
target_compile_definitions(lib1func2 PRIVATE FUNC2)
target_compile_options(lib1func2 PRIVATE -g)

add_library(moduletest SHARED lib2.cpp)
target_link_libraries(moduletest PRIVATE lib1func1 lib1func2)
target_compile_options(moduletest PRIVATE -g)
7 changes: 7 additions & 0 deletions symtab/module/lib1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifdef FUNC1
extern "C" void lib1func1() {}
#endif

#ifdef FUNC2
extern "C" void lib1func2() {}
#endif
2 changes: 2 additions & 0 deletions symtab/module/lib2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extern "C" void lib2func1() {}
extern "C" void lib2func2() {}
83 changes: 83 additions & 0 deletions symtab/module/parser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "Module.h"
#include "Symtab.h"
#include <boost/filesystem/path.hpp>
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

namespace st = Dyninst::SymtabAPI;
namespace bf = boost::filesystem;

int main() {
st::Symtab *obj{};
if (!st::Symtab::openFile(obj, "libmoduletest.so")) {
std::cerr << "Unable to open 'libmoduletest.so'\n";
return -1;
}

constexpr auto num_expected = 4;

std::vector<st::Module *> modules;
obj->getAllModules(modules);

if (modules.size() != num_expected) {
std::cerr << "Found " << modules.size() << " modules, but expected "
<< num_expected << '\n';
return -1;
}

std::unordered_set<std::string> expected{"lib1.cpp", "lib2.cpp",
"libmoduletest.so"};

for (auto *m : modules) {
auto const &basename = bf::path(m->fileName()).stem();
auto const &extension = bf::path(m->fileName()).extension();
auto const &name = basename.string() + extension.string();
if (expected.count(name) == 0) {
std::cerr << "Module '" << m->fileName() << "' not found.\n";
return -1;
}
}

std::vector<st::Function *> funcs;
obj->getAllFunctions(funcs);

std::unordered_map<std::string, std::string> funcs_by_mod{
{"lib1func1", "lib1.cpp"},
{"lib1func2", "lib1.cpp"},
{"lib2func1", "lib2.cpp"},
{"lib2func2", "lib2.cpp"},
};

auto find_func = [&funcs](std::string const &name) -> st::Function* {
auto i = std::find_if(funcs.begin(), funcs.end(), [&name](st::Function *f) {
return f->getName() == name;
});
if (i != funcs.end())
return *i;
return nullptr;
};

auto find_mod = [&funcs_by_mod](st::Function *f, std::string const &name) {
auto *m = f->getModule();
auto const &basename = bf::path(m->fileName()).stem();
auto const &extension = bf::path(m->fileName()).extension();
auto const &file = basename.string() + extension.string();
return file == name;
};

for (auto const &fm : funcs_by_mod) {
auto *f = find_func(fm.first);
if (!f) {
std::cerr << "symtab doesn't contain function '" << fm.first << "'\n";
return -1;
}
if (!find_mod(f, fm.second)) {
std::cerr << "Module '" << fm.second << "' doesn't contain function '"
<< fm.first << "'\n";
return -1;
}
}
}

0 comments on commit 08337ce

Please sign in to comment.