Move SharedLibraryHandle to utils

This commit is contained in:
Marko Budiselic 2023-04-08 11:39:46 +00:00
parent 49b5343238
commit d3eaff2adb
4 changed files with 82 additions and 21 deletions

View File

@ -1,4 +1,4 @@
// Copyright 2022 Memgraph Ltd.
// Copyright 2023 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
@ -26,6 +26,7 @@
#include "query/procedure/mg_procedure_impl.hpp"
#include "utils/memory.hpp"
#include "utils/rw_lock.hpp"
#include "utils/shared_library_handle.hpp"
class CypherMainVisitorTest;
@ -129,28 +130,10 @@ class ModuleRegistry final {
const std::filesystem::path &InternalModuleDir() const noexcept;
private:
class SharedLibraryHandle {
public:
SharedLibraryHandle(const std::string &shared_library, int mode) : handle_{dlopen(shared_library.c_str(), mode)} {}
SharedLibraryHandle(const SharedLibraryHandle &) = delete;
SharedLibraryHandle(SharedLibraryHandle &&) = delete;
SharedLibraryHandle operator=(const SharedLibraryHandle &) = delete;
SharedLibraryHandle operator=(SharedLibraryHandle &&) = delete;
~SharedLibraryHandle() {
if (handle_) {
dlclose(handle_);
}
}
private:
void *handle_;
};
#if __has_feature(address_sanitizer)
// This is why we need RTLD_NODELETE and we must not use RTLD_DEEPBIND with
// ASAN: https://github.com/google/sanitizers/issues/89
SharedLibraryHandle libstd_handle{"libstdc++.so.6", RTLD_NOW | RTLD_LOCAL | RTLD_NODELETE};
utils::LinuxDLHandle libstd_handle{"libstdc++.so.6", RTLD_NOW | RTLD_LOCAL | RTLD_NODELETE};
#else
// The reason behind opening share library during runtime is to avoid issues
// with loading symbols from stdlib. We have encounter issues with locale
@ -161,7 +144,7 @@ class ModuleRegistry final {
// mentioned library will be first performed in the already existing binded
// libraries and then the global namespace.
// RTLD_DEEPBIND => https://linux.die.net/man/3/dlopen
SharedLibraryHandle libstd_handle{"libstdc++.so.6", RTLD_NOW | RTLD_LOCAL | RTLD_DEEPBIND};
utils::LinuxDLHandle libstd_handle{"libstdc++.so.6", RTLD_NOW | RTLD_LOCAL | RTLD_DEEPBIND};
#endif
std::vector<std::filesystem::path> modules_dirs_;
std::filesystem::path internal_module_dir_;

View File

@ -0,0 +1,46 @@
// Copyright 2023 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
// License, and you may not use this file except in compliance with the Business Source License.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
#include <string>
// https://stackoverflow.com/questions/142508/how-do-i-check-os-with-a-preprocessor-directive
// TODO(gitbuda): Deal with the flags in both: OS case and ASAN case.
namespace memgraph::utils {
#if defined(__linux__) || defined(__APPLE__)
#include <dlfcn.h>
class LinuxDLHandle {
public:
LinuxDLHandle(const std::string &shared_library, int mode) : handle_{dlopen(shared_library.c_str(), mode)} {}
LinuxDLHandle(const LinuxDLHandle &) = delete;
LinuxDLHandle(LinuxDLHandle &&) = delete;
LinuxDLHandle operator=(const LinuxDLHandle &) = delete;
LinuxDLHandle operator=(LinuxDLHandle &&) = delete;
~LinuxDLHandle() {
if (handle_) {
dlclose(handle_);
}
}
private:
void *handle_;
};
#endif
#ifdef _WIN32
#include <windows.h>
class WindowsDLLHandle {};
#endif
} // namespace memgraph::utils

View File

@ -55,3 +55,5 @@ target_link_libraries(${test_prefix}ssl_client mg-communication)
add_manual_test(ssl_server.cpp)
target_link_libraries(${test_prefix}ssl_server mg-communication)
add_manual_test(shared_library_handle.cpp)

View File

@ -0,0 +1,30 @@
// Copyright 2023 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
// License, and you may not use this file except in compliance with the Business Source License.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
#include <iostream>
#include "utils/shared_library_handle.hpp"
using namespace memgraph::utils;
int main(int argc, char *argv[]) {
#if defined(__linux__)
LinuxDLHandle handle("test", 0);
#elif defined(__APPLE__)
LinuxDLHandle handle("test", 0);
#elif defined(_WIN32)
WindowsDLLHandle handle("test", 0);
#else
std::cout << "Unsupportd platform" << std::end;
#endif
return 0;
}