Skip to content

Commit 040dd06

Browse files
committed
Frame tests for loading shared objects and fetching symbol address
1 parent 2595a1f commit 040dd06

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

lib/Interpreter/CppInterOpInterpreter.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,17 @@ class Interpreter {
412412
}
413413

414414
CompilationResult loadLibrary(const std::string& filename, bool lookup) {
415+
#ifdef __EMSCRIPTEN__
416+
if (lookup) {
417+
llvm::errs() << "[cppinterop] Warning: 'lookup' has no effect on WASM.\n";
418+
}
419+
// In WASM: directly use Interpreter's LoadDynamicLibrary
420+
if (auto Err = inner->LoadDynamicLibrary(filename.c_str())) {
421+
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "loadLibrary: ");
422+
return kFailure;
423+
}
424+
return kSuccess;
425+
#endif
415426
DynamicLibraryManager* DLM = getDynamicLibraryManager();
416427
std::string canonicalLib;
417428
if (lookup)

unittests/CppInterOp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ if(EMSCRIPTEN)
9797
PUBLIC "SHELL: -s STACK_SIZE=32mb"
9898
PUBLIC "SHELL: -s INITIAL_MEMORY=128mb"
9999
PUBLIC "SHELL: --preload-file ${SYSROOT_PATH}/include@/include"
100+
PUBLIC "SHELL: --preload-file ${CMAKE_CURRENT_BINARY_DIR}/unittests/bin/libTestSharedLib.so@/libTestSharedLib.so"
100101
)
101102
endif()
102103

unittests/CppInterOp/DynamicLibraryManagerTest.cpp

Lines changed: 28 additions & 0 deletions
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)