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

Replace std::string pointer by reference and remove unused function DLSym #193

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions lib/Interpreter/DynamicLibraryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ namespace Cpp {
// TODO: !permanent case

std::string errMsg;
DyLibHandle dyLibHandle = platform::DLOpen(canonicalLoadedLib, &errMsg);
DyLibHandle dyLibHandle = platform::DLOpen(canonicalLoadedLib, errMsg);
if (!dyLibHandle) {
// We emit callback to LibraryLoadingFailed when we get error with error message.
//TODO: Implement callbacks
Expand Down Expand Up @@ -403,7 +403,7 @@ namespace Cpp {
// TODO: !permanent case

std::string errMsg;
platform::DLClose(dyLibHandle, &errMsg);
platform::DLClose(dyLibHandle, errMsg);
if (!errMsg.empty()) {
LLVM_DEBUG(dbgs() << "cling::DynamicLibraryManager::unloadLibrary(): "
<< errMsg << '\n');
Expand Down
57 changes: 21 additions & 36 deletions lib/Interpreter/Paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,53 +106,38 @@ namespace platform {
return std::string(Buffer.str());
}

#if defined(LLVM_ON_UNIX)
static void DLErr(std::string* Err) {
if (!Err)
return;
if (const char* DyLibError = ::dlerror())
*Err = DyLibError;
#if defined(_WIN32)
void* DLOpen(const std::string& Path, std::string& Err) {
auto lib = llvm::sys::DynamicLibrary::getLibrary(Path.c_str(), &Err);
return lib.getOSSpecificHandle();
}

void* DLOpen(const std::string& Path, std::string* Err /* = nullptr */) {
void* Lib = ::dlopen(Path.c_str(), RTLD_LAZY | RTLD_GLOBAL);
DLErr(Err);
return Lib;
void DLClose(void* Lib, std::string& Err) {
auto dl = llvm::sys::DynamicLibrary(Lib);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a test where we dlopen a library with standard dlopen and not DLOpen and try to close it with DLClose?

llvm::sys::DynamicLibrary::closeLibrary(dl);
if (Err.empty()) {
Err = std::string();
}
}
#elif defined(LLVM_ON_UNIX)
static void DLErr(std::string& Err) {
SAtacker marked this conversation as resolved.
Show resolved Hide resolved
if (const char* DyLibError = ::dlerror())
Err = std::string(DyLibError);
}

void* DLSym(const std::string& Name, std::string* Err /* = nullptr*/) {
if (void* Self = ::dlopen(nullptr, RTLD_GLOBAL)) {
// get dlopen error if there is one
DLErr(Err);
void* Sym = ::dlsym(Self, Name.c_str());
// overwrite error if dlsym caused one
void* DLOpen(const std::string& Path, std::string& Err) {
void* Lib = ::dlopen(Path.c_str(), RTLD_LAZY | RTLD_GLOBAL);
if (Lib == nullptr) {
DLErr(Err);
// only get dlclose error if dlopen & dlsym haven't emited one
DLClose(Self, Err && Err->empty() ? Err : nullptr);
return Sym;
return nullptr;
}
DLErr(Err);
return nullptr;
return Lib;
}

void DLClose(void* Lib, std::string* Err /* = nullptr*/) {
void DLClose(void* Lib, std::string& Err) {
::dlclose(Lib);
DLErr(Err);
}
#elif defined(_WIN32)

void* DLOpen(const std::string& Path, std::string* Err /* = nullptr */) {
auto lib = llvm::sys::DynamicLibrary::getLibrary(Path.c_str(), Err);
return lib.getOSSpecificHandle();
}

void DLClose(void* Lib, std::string* Err /* = nullptr*/) {
auto dl = llvm::sys::DynamicLibrary(Lib);
llvm::sys::DynamicLibrary::closeLibrary(dl);
if (Err) {
*Err = std::string();
}
}
#endif

} // namespace platform
Expand Down
6 changes: 2 additions & 4 deletions lib/Interpreter/Paths.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ std::string NormalizePath(const std::string& Path);
///
/// \returns the library handle
///
void* DLOpen(const std::string& Path, std::string* Err = nullptr);

void* DLSym(const std::string& Name, std::string* Err = nullptr);
void* DLOpen(const std::string& Path, std::string& Err);

///\brief Close a handle to a shared library.
///
Expand All @@ -58,7 +56,7 @@ void* DLSym(const std::string& Name, std::string* Err = nullptr);
///
/// \returns the library handle
///
void DLClose(void* Lib, std::string* Err = nullptr);
void DLClose(void* Lib, std::string& Err);
} // namespace platform

///\brief Replace all $TOKENS in a string with environent variable values.
Expand Down
17 changes: 10 additions & 7 deletions unittests/CppInterOp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ export_executable_symbols(CppInterOpTests)

unset(LLVM_LINK_COMPONENTS)

add_cppinterop_unittest(DynamicLibraryManagerTests DynamicLibraryManagerTest.cpp)
target_link_libraries(DynamicLibraryManagerTests
PRIVATE
clangCppInterOp
)
if(USE_REPL)
add_cppinterop_unittest(DynamicLibraryManagerTests DynamicLibraryManagerTest.cpp)
target_link_libraries(DynamicLibraryManagerTests
PRIVATE
clangCppInterOp
)

set_output_directory(DynamicLibraryManagerTests BINARY_DIR ${CMAKE_BINARY_DIR}/unittests/bin/$<CONFIG>/)

set_output_directory(DynamicLibraryManagerTests BINARY_DIR ${CMAKE_BINARY_DIR}/unittests/bin/$<CONFIG>/)
add_dependencies(DynamicLibraryManagerTests TestSharedLib)
endif()

add_dependencies(DynamicLibraryManagerTests TestSharedLib)
#export_executable_symbols_for_plugins(TestSharedLib)
add_subdirectory(TestSharedLib)
13 changes: 13 additions & 0 deletions unittests/CppInterOp/DynamicLibraryManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"

#include "../../lib/Interpreter/Paths.h"

// This function isn't referenced outside its translation unit, but it
// can't use the "static" keyword because its address is used for
// GetMainExecutable (since some platforms don't support taking the
Expand Down Expand Up @@ -40,6 +42,17 @@ TEST(DynamicLibraryManagerTest, Sanity) {
<< "Cannot find: '" << PathToTestSharedLib << "' in '" << Dir.str()
<< "'";

// DLOPEN DLCLOSE Test
std::string err = "";
auto* dlopen_handle = Cpp::utils::platform::DLOpen(PathToTestSharedLib, err);
EXPECT_TRUE(dlopen_handle) << "Error occurred: " << err << "\n";
Cpp::utils::platform::DLClose(dlopen_handle, err);
EXPECT_TRUE(err.empty()) << "Error occurred: " << err << "\n";
Cpp::utils::platform::DLOpen("missing", err);
EXPECT_TRUE(err.find("no such file") != std::string::npos ||
err.find("No such file") != std::string::npos);
// DLOPEN DLCLOSE Test end

EXPECT_TRUE(Cpp::LoadLibrary(PathToTestSharedLib.c_str()));
// Force ExecutionEngine to be created.
Cpp::Process("");
Expand Down
Loading