Skip to content

Commit 6b666b2

Browse files
anutosh491mcbarton
authored andcommitted
Frame tests for loading shared objects and fetching symbol address
1 parent e1ace51 commit 6b666b2

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

lib/Interpreter/CppInterOpInterpreter.h

+11
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,17 @@ class Interpreter {
400400
}
401401

402402
CompilationResult loadLibrary(const std::string& filename, bool lookup) {
403+
#ifdef __EMSCRIPTEN__
404+
if (lookup) {
405+
llvm::errs() << "[cppinterop] Warning: 'lookup' has no effect on WASM.\n";
406+
}
407+
// In WASM: directly use Interpreter's LoadDynamicLibrary
408+
if (auto Err = inner->LoadDynamicLibrary(filename.c_str())) {
409+
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "loadLibrary: ");
410+
return kFailure;
411+
}
412+
return kSuccess;
413+
#endif
403414
DynamicLibraryManager* DLM = getDynamicLibraryManager();
404415
std::string canonicalLib;
405416
if (lookup)

unittests/CppInterOp/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ if(EMSCRIPTEN)
9191
PUBLIC "SHELL: -s STACK_SIZE=32mb"
9292
PUBLIC "SHELL: -s INITIAL_MEMORY=128mb"
9393
PUBLIC "SHELL: --preload-file ${SYSROOT_PATH}/include@/include"
94+
PUBLIC "SHELL: --preload-file ${CMAKE_CURRENT_BINARY_DIR}/unittests/bin/libTestSharedLib.so@/libTestSharedLib.so"
9495
)
9596
endif()
9697

unittests/CppInterOp/DynamicLibraryManagerTest.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,31 @@ TEST(DynamicLibraryManagerTest, Sanity) {
5757
// invalidated...
5858
// EXPECT_FALSE(Cpp::GetFunctionAddress("ret_zero"));
5959
}
60+
61+
TEST(DynamicLibraryManagerTest, BasicSymbolLookup) {
62+
#ifndef EMSCRIPTEN
63+
GTEST_SKIP() << "This test is only intended for Emscripten builds.";
64+
#endif
65+
66+
// 1. Create interpreter
67+
ASSERT_TRUE(Cpp::CreateInterpreter());
68+
69+
// 2. Before loading, the symbol should not exist
70+
EXPECT_FALSE(Cpp::GetFunctionAddress("ret_zero"));
71+
72+
// 3. Load the library manually. Use exact known preload path (MEMFS path)
73+
const char* wasmLibPath = "libTestSharedLib.so"; // Preloaded path in MEMFS
74+
EXPECT_TRUE(Cpp::LoadLibrary(wasmLibPath, false));
75+
76+
// 4. Force engine setup (optional here)
77+
Cpp::Process("");
78+
79+
// 5. Symbol should now be found
80+
void* Addr = Cpp::GetFunctionAddress("ret_zero");
81+
EXPECT_NE(Addr, nullptr) << "Symbol 'ret_zero' not found after dlopen.";
82+
83+
// 6. Optionally, cast and call to test actual function (should return 0)
84+
using RetZeroFn = int (*)();
85+
RetZeroFn Fn = reinterpret_cast<RetZeroFn>(Addr);
86+
EXPECT_EQ(Fn(), 0);
87+
}

0 commit comments

Comments
 (0)