|
2 | 2 |
|
3 | 3 | #include "clang/Interpreter/CppInterOp.h" |
4 | 4 |
|
| 5 | +#include "llvm/Support/FileSystem.h" |
| 6 | +#include "llvm/Support/Path.h" |
| 7 | + |
| 8 | +// This function isn't referenced outside its translation unit, but it |
| 9 | +// can't use the "static" keyword because its address is used for |
| 10 | +// GetMainExecutable (since some platforms don't support taking the |
| 11 | +// address of main, and some platforms can't implement GetMainExecutable |
| 12 | +// without being given the address of a function in the main executable). |
| 13 | +std::string GetExecutablePath(const char* Argv0) { |
| 14 | + // This just needs to be some symbol in the binary; C++ doesn't |
| 15 | + // allow taking the address of ::main however. |
| 16 | + void* MainAddr = (void*)intptr_t(GetExecutablePath); |
| 17 | + return llvm::sys::fs::getMainExecutable(Argv0, MainAddr); |
| 18 | +} |
| 19 | + |
5 | 20 | TEST(DynamicLibraryManagerTest, Sanity) { |
6 | 21 | EXPECT_TRUE(Cpp::CreateInterpreter()); |
7 | | - EXPECT_TRUE(Cpp::LoadLibrary("TestSharedLib")); |
| 22 | + EXPECT_FALSE(Cpp::GetFunctionAddress("ret_zero")); |
| 23 | + |
| 24 | + std::string BinaryPath = GetExecutablePath(/*Argv0=*/nullptr); |
| 25 | + llvm::StringRef Dir = llvm::sys::path::parent_path(BinaryPath); |
| 26 | + Cpp::AddSearchPath(Dir.str().c_str()); |
| 27 | + |
| 28 | + // FIXME: dlsym on mach-o takes the C-level name, however, the macho-o format |
| 29 | + // adds an additional underscore (_) prefix to the lowered names. Figure out |
| 30 | + // how to harmonize that API. |
| 31 | +#ifdef __APPLE__ |
| 32 | + std::string PathToTestSharedLib = |
| 33 | + Cpp::SearchLibrariesForSymbol("_ret_zero", /*system_search=*/false); |
| 34 | +#else |
| 35 | + std::string PathToTestSharedLib = |
| 36 | + Cpp::SearchLibrariesForSymbol("ret_zero", /*system_search=*/false); |
| 37 | +#endif // __APPLE__ |
| 38 | + |
| 39 | + EXPECT_STRNE("", PathToTestSharedLib.c_str()) |
| 40 | + << "Cannot find: '" << PathToTestSharedLib << "' in '" << Dir.str() |
| 41 | + << "'"; |
| 42 | + |
| 43 | + EXPECT_TRUE(Cpp::LoadLibrary(PathToTestSharedLib.c_str())); |
| 44 | + EXPECT_TRUE(Cpp::GetFunctionAddress("ret_zero")); |
| 45 | + Cpp::UnloadLibrary("TestSharedLib"); |
| 46 | + // We have no reliable way to check if it was unloaded because posix does not |
| 47 | + // require the library to be actually unloaded but just the handle to be |
| 48 | + // invalidated... |
| 49 | + // EXPECT_FALSE(Cpp::GetFunctionAddress("ret_zero")); |
8 | 50 | } |
0 commit comments