Browse Source

windows dlopen

pull/16077/head
zhaodezan 4 years ago
parent
commit
3be8c4714a
3 changed files with 50 additions and 26 deletions
  1. +45
    -15
      mindspore/lite/src/common/loader_util.cc
  2. +2
    -6
      mindspore/lite/src/common/loader_util.h
  3. +3
    -5
      mindspore/lite/test/ut/src/loader_util_test.cc

+ 45
- 15
mindspore/lite/src/common/loader_util.cc View File

@@ -18,43 +18,73 @@
#include <string.h> #include <string.h>
#include <climits> #include <climits>
#include "include/errorcode.h" #include "include/errorcode.h"
#include "src/common/log_util.h"

#ifndef _WIN32 #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 mindspore {
namespace lite { 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; return RET_ERROR;
} }
char resolved_path[PATH_MAX]; 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; 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) { if (handler_ == nullptr) {
MS_LOG(ERROR) << "open path failed";
LOG_ERROR("open path failed");
return RET_ERROR; return RET_ERROR;
} }
return RET_OK; 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_); auto close_res = dlclose(handler_);
if (close_res != 0) { if (close_res != 0) {
MS_LOG(ERROR) << "can not close handler";
LOG_ERROR("can not close handler");
return RET_ERROR; 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; return RET_OK;
} }


} // namespace lite } // namespace lite
} // namespace mindspore } // namespace mindspore

#endif

+ 2
- 6
mindspore/lite/src/common/loader_util.h View File

@@ -17,15 +17,12 @@
#ifndef MINDSPORE_LITE_SRC_COMMON_LOADER_UTIL_H_ #ifndef MINDSPORE_LITE_SRC_COMMON_LOADER_UTIL_H_
#define MINDSPORE_LITE_SRC_COMMON_LOADER_UTIL_H_ #define MINDSPORE_LITE_SRC_COMMON_LOADER_UTIL_H_


#ifndef _WIN32
#include <dlfcn.h>

namespace mindspore { namespace mindspore {
namespace lite { namespace lite {


class SoLoader {
class DynamicLibraryLoader {
public: public:
int Open(const char *so_path, int mode = RTLD_LAZY);
int Open(const char *lib_path);
void *GetFunc(const char *func_name); void *GetFunc(const char *func_name);
int Close(); int Close();


@@ -37,4 +34,3 @@ class SoLoader {
} // namespace mindspore } // namespace mindspore


#endif #endif
#endif

+ 3
- 5
mindspore/lite/test/ut/src/loader_util_test.cc 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;} int add(int a, int b) {return a + b;}
use this command to generate so file: use this command to generate so file:
gcc add.cc -fPIC -shared -o libadd.so gcc add.cc -fPIC -shared -o libadd.so
@@ -32,14 +32,12 @@ class LoaderUtilTest : public mindspore::CommonTest {
nm -D libadd.so nm -D libadd.so
*/ */
TEST_F(LoaderUtilTest, TestAdd) { TEST_F(LoaderUtilTest, TestAdd) {
#ifndef _WIN32
lite::SoLoader loader;
lite::DynamicLibraryLoader loader;
loader.Open("./libadd.so"); loader.Open("./libadd.so");
int (*add)(int a, int b); 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); int res = add(7, 8);
loader.Close(); loader.Close();
ASSERT_EQ(15, res); ASSERT_EQ(15, res);
#endif
} }
} // namespace mindspore } // namespace mindspore

Loading…
Cancel
Save