!16077 support windows dlopen

From: @zhaodezan
Reviewed-by: @jpc_chenjianping,@zhanghaibo5
Signed-off-by: @jpc_chenjianping
This commit is contained in:
mindspore-ci-bot 2021-05-09 06:09:52 +08:00 committed by Gitee
commit 4f10496408
3 changed files with 50 additions and 26 deletions

View File

@ -18,43 +18,73 @@
#include <string.h>
#include <climits>
#include "include/errorcode.h"
#include "src/common/log_util.h"
#ifndef _WIN32
#include <dlfcn.h>
#else
#include <windows.h>
#endif
#define LOG_ERROR(content, args...) \
{ printf("[ERROR] %s|%d|%s: " #content "\r\n", __FILE__, __LINE__, __func__, ##args); }
namespace mindspore {
namespace lite {
int SoLoader::Open(const char *so_path, int mode) {
if ((strlen(so_path)) >= PATH_MAX) {
MS_LOG(ERROR) << "path is too long";
int DynamicLibraryLoader::Open(const char *lib_path) {
if ((strlen(lib_path)) >= PATH_MAX) {
LOG_ERROR("path is too long");
return RET_ERROR;
}
char resolved_path[PATH_MAX];
auto resolve_res = realpath(so_path, resolved_path);
if (resolve_res == nullptr) {
MS_LOG(ERROR) << "path not exist";
#ifndef _WIN32
char *real_path = realpath(lib_path, resolved_path);
#else
char *real_path = _fullpath(resolved_path, lib_path, 1024);
#endif
if (real_path == nullptr) {
LOG_ERROR("path not exist");
return RET_ERROR;
}
handler_ = dlopen(so_path, mode);
#ifndef _WIN32
handler_ = dlopen(lib_path, RTLD_LAZY);
#else
handler_ = LoadLibrary(lib_path);
#endif
if (handler_ == nullptr) {
MS_LOG(ERROR) << "open path failed";
LOG_ERROR("open path failed");
return RET_ERROR;
}
return RET_OK;
}
void *SoLoader::GetFunc(const char *func_name) { return dlsym(handler_, func_name); }
void *DynamicLibraryLoader::GetFunc(const char *func_name) {
#ifndef _WIN32
return dlsym(handler_, func_name);
#else
auto func = GetProcAddress(reinterpret_cast<HINSTANCE__ *>(handler_), func_name);
return reinterpret_cast<void *>(func);
#endif
}
int SoLoader::Close() {
int DynamicLibraryLoader::Close() {
#ifndef _WIN32
auto close_res = dlclose(handler_);
if (close_res != 0) {
MS_LOG(ERROR) << "can not close handler";
LOG_ERROR("can not close handler");
return RET_ERROR;
}
#else
auto close_res = FreeLibrary(reinterpret_cast<HINSTANCE__ *>(handler_));
if (close_res == 0) {
LOG_ERROR("can not close handler");
return RET_ERROR;
}
#endif
return RET_OK;
}
} // namespace lite
} // namespace mindspore
#endif

View File

@ -17,15 +17,12 @@
#ifndef MINDSPORE_LITE_SRC_COMMON_LOADER_UTIL_H_
#define MINDSPORE_LITE_SRC_COMMON_LOADER_UTIL_H_
#ifndef _WIN32
#include <dlfcn.h>
namespace mindspore {
namespace lite {
class SoLoader {
class DynamicLibraryLoader {
public:
int Open(const char *so_path, int mode = RTLD_LAZY);
int Open(const char *lib_path);
void *GetFunc(const char *func_name);
int Close();
@ -37,4 +34,3 @@ class SoLoader {
} // namespace mindspore
#endif
#endif

View File

@ -24,7 +24,7 @@ class LoaderUtilTest : public mindspore::CommonTest {
};
/*
in file add.cc, the code is:
in file add.c, the code is:
int add(int a, int b) {return a + b;}
use this command to generate so file:
gcc add.cc -fPIC -shared -o libadd.so
@ -32,14 +32,12 @@ class LoaderUtilTest : public mindspore::CommonTest {
nm -D libadd.so
*/
TEST_F(LoaderUtilTest, TestAdd) {
#ifndef _WIN32
lite::SoLoader loader;
lite::DynamicLibraryLoader loader;
loader.Open("./libadd.so");
int (*add)(int a, int b);
add = (int (*)(int, int))loader.GetFunc("_Z3addii");
add = (int (*)(int, int))loader.GetFunc("add");
int res = add(7, 8);
loader.Close();
ASSERT_EQ(15, res);
#endif
}
} // namespace mindspore