Skip to content

Commit 192962a

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

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

lib/Interpreter/CppInterOpInterpreter.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,18 @@ 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(),
422+
"loadLibrary: ");
423+
return kFailure;
424+
}
425+
return kSuccess;
426+
#endif
415427
DynamicLibraryManager* DLM = getDynamicLibraryManager();
416428
std::string canonicalLib;
417429
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}/TestSharedLib/unittests/bin/Release/libTestSharedLib.so@/libTestSharedLib.so"
100101
)
101102
endif()
102103

unittests/CppInterOp/DynamicLibraryManagerTest.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,25 @@ 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+
ASSERT_TRUE(Cpp::CreateInterpreter());
67+
EXPECT_FALSE(Cpp::GetFunctionAddress("ret_zero"));
68+
69+
// Load the library manually. Use known preload path (MEMFS path)
70+
const char* wasmLibPath = "libTestSharedLib.so"; // Preloaded path in MEMFS
71+
EXPECT_TRUE(Cpp::LoadLibrary(wasmLibPath, false));
72+
73+
Cpp::Process("");
74+
75+
void* Addr = Cpp::GetFunctionAddress("ret_zero");
76+
EXPECT_NE(Addr, nullptr) << "Symbol 'ret_zero' not found after dlopen.";
77+
78+
using RetZeroFn = int (*)();
79+
auto Fn = reinterpret_cast<RetZeroFn>(Addr);
80+
EXPECT_EQ(Fn(), 0);
81+
}

0 commit comments

Comments
 (0)