Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frame tests for loading shared objects for the emscripten build #548

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/Interpreter/CppInterOpInterpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,17 @@ class Interpreter {
}

CompilationResult loadLibrary(const std::string& filename, bool lookup) {
#ifdef __EMSCRIPTEN__
if (lookup) {
llvm::errs() << "[cppinterop] Warning: 'lookup' has no effect on WASM.\n";
}
// In WASM: directly use Interpreter's LoadDynamicLibrary
if (auto Err = inner->LoadDynamicLibrary(filename.c_str())) {
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "loadLibrary: ");
return kFailure;
}
return kSuccess;
#endif
DynamicLibraryManager* DLM = getDynamicLibraryManager();
std::string canonicalLib;
if (lookup)
Expand Down
1 change: 1 addition & 0 deletions unittests/CppInterOp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ if(EMSCRIPTEN)
PUBLIC "SHELL: -s STACK_SIZE=32mb"
PUBLIC "SHELL: -s INITIAL_MEMORY=128mb"
PUBLIC "SHELL: --preload-file ${SYSROOT_PATH}/include@/include"
PUBLIC "SHELL: --preload-file ${CMAKE_CURRENT_BINARY_DIR}/unittests/bin/libTestSharedLib.so@/libTestSharedLib.so"
)
endif()

Expand Down
28 changes: 28 additions & 0 deletions unittests/CppInterOp/DynamicLibraryManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,31 @@ TEST(DynamicLibraryManagerTest, Sanity) {
// invalidated...
// EXPECT_FALSE(Cpp::GetFunctionAddress("ret_zero"));
}

TEST(DynamicLibraryManagerTest, BasicSymbolLookup) {
#ifndef EMSCRIPTEN
GTEST_SKIP() << "This test is only intended for Emscripten builds.";
#endif

// 1. Create interpreter
ASSERT_TRUE(Cpp::CreateInterpreter());

// 2. Before loading, the symbol should not exist
EXPECT_FALSE(Cpp::GetFunctionAddress("ret_zero"));

// 3. Load the library manually. Use exact known preload path (MEMFS path)
const char* wasmLibPath = "libTestSharedLib.so"; // Preloaded path in MEMFS
EXPECT_TRUE(Cpp::LoadLibrary(wasmLibPath, false));

// 4. Force engine setup (optional here)
Cpp::Process("");

// 5. Symbol should now be found
void* Addr = Cpp::GetFunctionAddress("ret_zero");
EXPECT_NE(Addr, nullptr) << "Symbol 'ret_zero' not found after dlopen.";

// 6. Optionally, cast and call to test actual function (should return 0)
using RetZeroFn = int (*)();
RetZeroFn Fn = reinterpret_cast<RetZeroFn>(Addr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use reinterpret_cast [cppcoreguidelines-pro-type-reinterpret-cast]

  RetZeroFn Fn = reinterpret_cast<RetZeroFn>(Addr);
                 ^

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: use auto when initializing with a cast to avoid duplicating the type name [modernize-use-auto]

Suggested change
RetZeroFn Fn = reinterpret_cast<RetZeroFn>(Addr);
auto Fn = reinterpret_cast<RetZeroFn>(Addr);

EXPECT_EQ(Fn(), 0);
}
Loading