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

fix #387: fix tls block #379

Merged
merged 15 commits into from
Jan 23, 2024
2 changes: 1 addition & 1 deletion Options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ endif (ENABLE_SQL_IO)
option(ENABLE_ASAN "Enable Address Sanitizer." OFF)
if (ENABLE_ASAN)
message("Address Sanitizer is enabled.")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer -static-libasan")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DENABLE_ASAN -fsanitize=address -fno-omit-frame-pointer -static-libasan")
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个做什么用?

Copy link
Collaborator

Choose a reason for hiding this comment

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

看到下面了

Copy link
Collaborator

Choose a reason for hiding this comment

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

DENABLE_ASAN 改完了删掉吧

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address -static-libasan ")
else (ENABLE_ASAN)
message("Address Sanitizer is disabled.")
Expand Down
116 changes: 84 additions & 32 deletions src/plugin/cpp_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,41 @@ CppPluginManagerImpl::CppPluginManagerImpl(LightningGraph* db, const std::string

CppPluginManagerImpl::~CppPluginManagerImpl() {}

void CppPluginManagerImpl::OpenDynamicLib(const PluginInfoBase* pinfo, DynamicLibinfo &dinfo) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

文件头加点儿注释说明一下tls block的问题吧

using namespace lgraph::dll;
const auto* info = dynamic_cast<const PluginInfo*>(pinfo);
dinfo.lib_handle = LoadDynamicLibrary(info->path);
if (!dinfo.lib_handle) {
auto errMsg = GetLastErrorMsg();
if (errMsg.find("cannot allocate memory in static TLS block") != std::string::npos) {
errMsg += ". Wait for other tasks to finish and try again";
}
throw InputError("Failed to load the DLL: " + errMsg);
}
if (info->has_func) {
dinfo.func = GetDllFunction<PluginFunc*>(dinfo.lib_handle, "Process");
if (!dinfo.func) {
UnloadDynamicLibrary(dinfo.lib_handle);
throw InputError("Failed to get Process() function in the DLL: " + GetLastErrorMsg());
}
} else {
dinfo.get_sig_spec = GetDllFunction<SignatureGetter*>(dinfo.lib_handle, "GetSignature");
dinfo.func_txn = GetDllFunction<PluginFuncInTxn*>(dinfo.lib_handle, "ProcessInTxn");
if (!dinfo.func_txn) {
UnloadDynamicLibrary(dinfo.lib_handle);
throw InputError(
"Failed to get ProcessInTxn() function in the DLL: "
+ GetLastErrorMsg());
}
}
}

void CppPluginManagerImpl::CloseDynamicLib(DynamicLibinfo &dinfo) {
using namespace lgraph::dll;
if (!UnloadDynamicLibrary(dinfo.lib_handle))
throw InternalError("Failed to unload library.");
}

void CppPluginManagerImpl::DoCall(lgraph_api::Transaction* txn,
const std::string& user,
AccessControlledDB* db_with_access_control,
Expand All @@ -45,54 +80,71 @@ void CppPluginManagerImpl::DoCall(lgraph_api::Transaction* txn,

// TODO: support in_process // NOLINT
bool r = false;
const PluginInfo* info = dynamic_cast<const PluginInfo*>(pinfo);
if (info->func) {
PluginFunc* procedure = info->func;
lgraph_api::GraphDB db(db_with_access_control, info->read_only);
DynamicLibinfo info;
OpenDynamicLib(pinfo, info);
if (info.func) {
PluginFunc* procedure = info.func;
lgraph_api::GraphDB db(db_with_access_control, pinfo->read_only);
r = procedure(db, request, output);
} else if (info->func_txn && txn != nullptr) {
PluginFuncInTxn * procedure = info->func_txn;
} else if (info.func_txn && txn != nullptr) {
PluginFuncInTxn * procedure = info.func_txn;
r = procedure(*txn, request, output);
}
CloseDynamicLib(info);

if (!r) throw InputError(FMA_FMT("Plugin returned false. Output: {}.", output));
}

void CppPluginManagerImpl::LoadPlugin(const std::string& user, const std::string& name,
PluginInfoBase* pinfo) {
using namespace lgraph::dll;
PluginInfo* info = dynamic_cast<PluginInfo*>(pinfo);
std::string path = GetPluginPath(name);
info->lib_handle = LoadDynamicLibrary(path);
if (!info->lib_handle) throw InputError("Failed to load the DLL: " + GetLastErrorMsg());
info->get_sig_spec = GetDllFunction<SignatureGetter*>(info->lib_handle, "GetSignature");
// it's ok for plugin which DOES NOT have `GetSignature` function.
// Plugins without `GetSignature` are not guaranteed to call safely in InQueryCall context.
if (!info->get_sig_spec) {
info->func = GetDllFunction<PluginFunc*>(info->lib_handle, "Process");
if (!info->func) {
UnloadDynamicLibrary(info->lib_handle);
throw InputError("Failed to get Process() function in the DLL: " + GetLastErrorMsg());
info->path = GetPluginPath(name);
auto lib_handle = LoadDynamicLibrary(info->path);
if (!lib_handle) {
auto errMsg = GetLastErrorMsg();
if (errMsg.find("cannot allocate memory in static TLS block") != std::string::npos) {
errMsg += ". Wait for other tasks to finish and try again";
}
info->sig_spec = nullptr;
return;
} else {
info->func_txn = GetDllFunction<PluginFuncInTxn*>(info->lib_handle, "ProcessInTxn");
if (!info->func_txn) {
UnloadDynamicLibrary(info->lib_handle);
throw InputError("Failed to get Process() function in the DLL: " + GetLastErrorMsg());
throw InputError("Failed to load the DLL: " + errMsg);
}
{
auto get_sig_spec = GetDllFunction<SignatureGetter*>(lib_handle, "GetSignature");
// it's ok for plugin which DOES NOT have `GetSignature` function.
// Plugins without `GetSignature` are not guaranteed to call safely in InQueryCall context.
if (!get_sig_spec) {
auto func = GetDllFunction<PluginFunc*>(lib_handle, "Process");
if (!func) {
UnloadDynamicLibrary(lib_handle);
throw InputError("Failed to get Process() function in the DLL: " +
GetLastErrorMsg());
}
info->sig_spec = nullptr;
info->has_func = true;
} else {
auto func_txn = GetDllFunction<PluginFuncInTxn*>(lib_handle, "ProcessInTxn");
if (!func_txn) {
UnloadDynamicLibrary(lib_handle);
throw InputError("Failed to get Process() function in the DLL: " +
GetLastErrorMsg());
}
auto sig_spec = std::make_unique<lgraph_api::SigSpec>();
bool r = get_sig_spec(*sig_spec);
if (!r) throw InputError(FMA_FMT("Failed to get Signature"));
info->sig_spec = std::move(sig_spec);
info->has_func = false;
}
auto sig_spec = std::make_unique<lgraph_api::SigSpec>();
bool r = info->get_sig_spec(*sig_spec);
if (!r) throw InputError(FMA_FMT("Failed to get Signature"));
info->sig_spec = std::move(sig_spec);
}
if (!UnloadDynamicLibrary(lib_handle)) {
throw InputError("Failed to unload DLL: " + GetLastErrorMsg());
}
}

void CppPluginManagerImpl::UnloadPlugin(const std::string& user, const std::string& name,
PluginInfoBase* pinfo) {
using namespace lgraph::dll;
PluginInfo* info = dynamic_cast<PluginInfo*>(pinfo);
if (!UnloadDynamicLibrary(info->lib_handle))
throw InternalError("Failed to unload library [{}].", name);
// using namespace lgraph::dll;
// PluginInfo* info = dynamic_cast<PluginInfo*>(pinfo);
// if (!UnloadDynamicLibrary(info->lib_handle))
Copy link
Collaborator

Choose a reason for hiding this comment

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

注释清掉,然后留一个函数头说明一下

// throw InternalError("Failed to unload library [{}].", name);
}
} // namespace lgraph
17 changes: 13 additions & 4 deletions src/plugin/cpp_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,25 @@ namespace lgraph {
class LightningGraph;

class CppPluginManagerImpl : public PluginManagerImplBase {
private:
struct DynamicLibinfo {
lgraph::dll::LibHandle lib_handle;
lgraph_api::Process* func = nullptr;
lgraph_api::ProcessInTxn* func_txn = nullptr;
lgraph_api::GetSignature* get_sig_spec = nullptr;
};

void OpenDynamicLib(const PluginInfoBase* pinfo, DynamicLibinfo &dinfo);
void CloseDynamicLib(DynamicLibinfo &dinfo);

protected:
typedef lgraph_api::Process PluginFunc;
typedef lgraph_api::ProcessInTxn PluginFuncInTxn;
typedef lgraph_api::GetSignature SignatureGetter;

struct PluginInfo : public PluginInfoBase {
lgraph::dll::LibHandle lib_handle;
PluginFunc* func = nullptr;
PluginFuncInTxn* func_txn = nullptr;
SignatureGetter* get_sig_spec = nullptr;
std::string path;
bool has_func = true;
};

LightningGraph* db_;
Expand Down
24 changes: 24 additions & 0 deletions test/test_cpp_procedure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ void build_so() {
"../../test/test_procedures/add_label_v.cpp", LIBLGRAPH);
rt = system(cmd.c_str());
UT_EXPECT_EQ(rt, 0);
cmd = UT_FMT(cmd_f.c_str(), INCLUDE_DIR, DEPS_INCLUDE_DIR, "./bfs.so",
"../../test/test_procedures/bfs.cpp", LIBLGRAPH);
rt = system(cmd.c_str());
UT_EXPECT_EQ(rt, 0);
}

void read_code(const std::string& code_path, std::string& code) {
Expand Down Expand Up @@ -211,6 +215,7 @@ TEST_F(TestCppPlugin, CppPlugin) {
std::string code_zip = "";
std::string code_scan_graph = "";
std::string code_add_label = "";
std::string code_bfs = "";
{
// read file to string
std::string code_so_path = "./sortstr.so";
Expand All @@ -224,12 +229,14 @@ TEST_F(TestCppPlugin, CppPlugin) {
#endif
std::string code_scan_graph_path = "./scan_graph.so";
std::string code_add_label_path = "./add_label.so";
std::string code_bfs_path = "./bfs.so";
build_so();
read_code(code_so_path, code_so);
read_code(code_cpp_path, code_cpp);
read_code(code_zip_path, code_zip);
read_code(code_scan_graph_path, code_scan_graph);
read_code(code_add_label_path, code_add_label);
read_code(code_bfs_path, code_bfs);
UT_EXPECT_NE(code_so, "");
UT_EXPECT_NE(code_cpp, "");
UT_EXPECT_NE(code_zip, "");
Expand Down Expand Up @@ -445,6 +452,23 @@ TEST_F(TestCppPlugin, CppPlugin) {
"#include <stdlib.h>", (plugin::CodeType)6,
"test", true, "v1"));
}
#ifndef ENABLE_ASAN
Copy link
Collaborator

Choose a reason for hiding this comment

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

一般是#ifndef __SANITIZE_ADDRESS__

下面这里有内存问题?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

asan下dlclose并不会真正卸载动态库,这是asan一直就有的问题了:google/sanitizers#89

Copy link
Contributor Author

Choose a reason for hiding this comment

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

我改成__SANITIZE_ADDRESS__吧

{
UT_LOG() << "Testing load many plugins";
for (int i = 0; i < 32; i++) {
UT_LOG() << "try load bfs_" << i;
bool r = false;
UT_EXPECT_NO_THROW(
r = pm.LoadPluginFromCode(lgraph::_detail::DEFAULT_ADMIN_NAME,
"bfs_" + std::to_string(i),
code_bfs, plugin::CodeType::SO,
"bfs v1", true, "v1"));
UT_EXPECT_TRUE(r);
fma_common::SleepS(5);
}
pm.DeleteAllPlugins(lgraph::_detail::DEFAULT_ADMIN_NAME);
}
#endif // ENABLE_ASAN
}
#endif
}
Loading
Loading