Skip to content

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

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

Merged
merged 2 commits into from
May 21, 2025
Merged
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
13 changes: 13 additions & 0 deletions lib/Interpreter/CppInterOpInterpreter.h
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
#include "clang/AST/DeclarationName.h"
#include "clang/AST/GlobalDecl.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendOptions.h"
#include "clang/Lex/Preprocessor.h"
@@ -36,6 +37,7 @@
#include "llvm/Support/Error.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TargetParser/Triple.h"

#include <utility>
#include <vector>
@@ -412,6 +414,17 @@
}

CompilationResult loadLibrary(const std::string& filename, bool lookup) {
llvm::Triple triple(getCompilerInstance()->getTargetOpts().Triple);
if (triple.isWasm()) {
// On WASM, dlopen-style canonical lookup has no effect.
if (auto Err = inner->LoadDynamicLibrary(filename.c_str())) {
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(),

Check warning on line 421 in lib/Interpreter/CppInterOpInterpreter.h

Codecov / codecov/patch

lib/Interpreter/CppInterOpInterpreter.h#L420-L421

Added lines #L420 - L421 were not covered by tests
"loadLibrary: ");
return kFailure;
}
return kSuccess;

Check warning on line 425 in lib/Interpreter/CppInterOpInterpreter.h

Codecov / codecov/patch

lib/Interpreter/CppInterOpInterpreter.h#L423-L425

Added lines #L423 - L425 were not covered by tests
}

DynamicLibraryManager* DLM = getDynamicLibraryManager();
std::string canonicalLib;
if (lookup)
1 change: 1 addition & 0 deletions unittests/CppInterOp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -97,6 +97,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}/TestSharedLib/unittests/bin/Release/libTestSharedLib.so@/libTestSharedLib.so"
)
endif()

27 changes: 27 additions & 0 deletions unittests/CppInterOp/DynamicLibraryManagerTest.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "gtest/gtest.h"

#include "clang/Basic/Version.h"
#include "clang/Interpreter/CppInterOp.h"

#include "llvm/Support/FileSystem.h"
@@ -57,3 +58,29 @@ 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.";
#else
#if CLANG_VERSION_MAJOR < 20
GTEST_SKIP() << "Support for loading shared libraries was added in LLVM 20.";
#endif
#endif

ASSERT_TRUE(Cpp::CreateInterpreter());
EXPECT_FALSE(Cpp::GetFunctionAddress("ret_zero"));

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

Cpp::Process("");

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

using RetZeroFn = int (*)();
auto Fn = reinterpret_cast<RetZeroFn>(Addr);
EXPECT_EQ(Fn(), 0);
}