From e6785ea641959a5c9a63218a2849ee1e82190f67 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Thu, 12 Sep 2024 19:50:02 +0300 Subject: [PATCH 1/4] Fix VM loader --- interop/src/cpp/common-interop.cc | 34 ++++++++++++++++++++++++++++ interop/src/cpp/vmloader.cc | 37 +++++++++++++++---------------- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/interop/src/cpp/common-interop.cc b/interop/src/cpp/common-interop.cc index 9241f7bc7..995fdb013 100644 --- a/interop/src/cpp/common-interop.cc +++ b/interop/src/cpp/common-interop.cc @@ -15,7 +15,9 @@ #include #include + #include "common-interop.h" +#include "dynamic-loader.h" #if KOALA_INTEROP_PROFILER #include "profiler.h" @@ -255,3 +257,35 @@ void impl_StopGroupedLog(KInt index) { stopGroupedLog(index); } KOALA_INTEROP_V1(StopGroupedLog, KInt) + + +typedef KInt (*LoadVirtualMachine_t)(KInt vmKind, const char* appClassPath, const char* appLibPath); +typedef KInt (*StartApplication_t)(); +typedef KInt (*RunApplication_t)(const KInt arg0, const KInt arg1); + +void* getImpl(const char* path, const char* name) { + static void* lib = nullptr; + if (!lib) lib = loadLibrary(std::string(path)); + return findSymbol(lib, name); +} + +KInt impl_LoadVirtualMachine(KInt vmKind, const KStringPtr& appClassPath, const KStringPtr& appLibPath) { + static LoadVirtualMachine_t impl = nullptr; + if (!impl) impl = reinterpret_cast(getImpl(appClassPath.c_str(), "LoadVirtualMachine")); + return impl(vmKind, appClassPath.c_str(), appLibPath.c_str()); +} +KOALA_INTEROP_3(LoadVirtualMachine, KInt, KInt, KStringPtr, KStringPtr) + +KInt impl_StartApplication() { + static StartApplication_t impl = nullptr; + if (!impl) impl = reinterpret_cast(getImpl(nullptr, "StartApplication")); + return impl(); +} +KOALA_INTEROP_0(StartApplication, KInt) + +KInt impl_RunApplication(const KInt arg0, const KInt arg1) { + static RunApplication_t impl = nullptr; + if (!impl) impl = reinterpret_cast(getImpl(nullptr, "RunApplication")); + return impl(arg0, arg1); +} +KOALA_INTEROP_2(RunApplication, KInt, KInt, KInt) diff --git a/interop/src/cpp/vmloader.cc b/interop/src/cpp/vmloader.cc index b15d14d35..0eac4f20a 100644 --- a/interop/src/cpp/vmloader.cc +++ b/interop/src/cpp/vmloader.cc @@ -15,8 +15,11 @@ #include #include + #include "dynamic-loader.h" -#include "common-interop.h" +#include "koala-types.h" + +// DO NOT USE KOALA INTEROP MECHANISMS IN THIS FILE! #ifdef KOALA_JNI #include "jni.h" @@ -26,7 +29,7 @@ #include "etsapi.h" #endif -#ifdef KOALA_LINUX +#if defined(KOALA_LINUX) || defined(KOALA_MACOS) #include "sys/stat.h" #include "dirent.h" #endif @@ -44,7 +47,7 @@ struct VMLibInfo { const VMLibInfo javaVMLib = { .sdkPath = getenv("JAVA_HOME"), .platform = - #ifdef KOALA_LINUX + #if defined(KOALA_LINUX) || defined(KOALA_MACOS) "lib/server" #elif KOALA_WINDOWS "bin/server" @@ -63,6 +66,8 @@ const VMLibInfo pandaVMLib = { .platform = #ifdef KOALA_LINUX "linux_host_tools/lib" + #elif KOALA_MACOS + "macos_host_tools/lib" #elif KOALA_OHOS "ohos_arm64/lib" #else @@ -113,7 +118,7 @@ VMEntry g_vmEntry; typedef int (*createVM_t)(void** pVM, void** pEnv, void* vmInitArgs); -KInt impl_LoadVirtualMachine(KInt vmKind, const KStringPtr& appClassPath, const KStringPtr& appLibPath) { +extern "C" KInt LoadVirtualMachine(KInt vmKind, const char* appClassPath, const char* appLibPath) { const VMLibInfo* thisVM = #ifdef KOALA_JNI (vmKind == JAVA_VM_KIND) ? &javaVMLib : @@ -143,7 +148,7 @@ KInt impl_LoadVirtualMachine(KInt vmKind, const KStringPtr& appClassPath, const void* vm = nullptr; void* env = nullptr; - VMInitArgs* vmInitArgs = + VMInitArgs* vmInitArgs = #ifdef KOALA_JNI (vmKind == JAVA_VM_KIND) ? (VMInitArgs*)&javaVMArgs : #endif @@ -161,8 +166,8 @@ KInt impl_LoadVirtualMachine(KInt vmKind, const KStringPtr& appClassPath, const std::vector javaVMOptions; if (vmKind == JAVA_VM_KIND) { javaVMOptions = { - {(char*)strdup((std::string("-Djava.class.path=") + appClassPath.c_str()).c_str())}, - {(char*)strdup((std::string("-Djava.library.path=") + appLibPath.c_str()).c_str())}, + {(char*)strdup((std::string("-Djava.class.path=") + appClassPath).c_str())}, + {(char*)strdup((std::string("-Djava.library.path=") + appLibPath).c_str())}, }; vmInitArgs->nOptions = javaVMOptions.size(); vmInitArgs->options = (void*)(javaVMOptions.data()); @@ -195,7 +200,7 @@ KInt impl_LoadVirtualMachine(KInt vmKind, const KStringPtr& appClassPath, const g_vmEntry.vmKind = PANDA_VM_KIND; } #endif - + int res = createVM(&vm, &env, vmInitArgs); if (res != 0) { fprintf(stderr, "Error creating a VM\n"); @@ -204,7 +209,6 @@ KInt impl_LoadVirtualMachine(KInt vmKind, const KStringPtr& appClassPath, const g_vmEntry.env = env; return 0; } -KOALA_INTEROP_3(LoadVirtualMachine, KInt, KInt, KStringPtr, KStringPtr) struct AppInfo { const char* className; @@ -234,7 +238,7 @@ const AppInfo pandaAppInfo = { }; #endif -KInt impl_StartApplication() { +extern "C" KInt StartApplication() { const AppInfo* appInfo = #ifdef KOALA_JNI (g_vmEntry.vmKind == JAVA_VM_KIND) ? &javaAppInfo : @@ -243,7 +247,7 @@ KInt impl_StartApplication() { (g_vmEntry.vmKind == PANDA_VM_KIND) ? &pandaAppInfo : #endif nullptr; - + if (!appInfo) { fprintf(stderr, "No appInfo provided for VM kind %d (recompile vmloader.cc with the missing flags)\n", g_vmEntry.vmKind); return -1; @@ -273,12 +277,8 @@ KInt impl_StartApplication() { #endif return -1; } -KOALA_INTEROP_0(StartApplication, KInt) -KInt impl_RunApplication( - const KInt arg0, - const KInt arg1 -) { +extern "C" KInt RunApplication(const KInt arg0, const KInt arg1) { #ifdef KOALA_JNI if (g_vmEntry.vmKind == JAVA_VM_KIND) { JNIEnv* jEnv = (JNIEnv*)(g_vmEntry.env); @@ -313,13 +313,12 @@ KInt impl_RunApplication( #endif return -1; } -KOALA_INTEROP_2(RunApplication, KInt, KInt, KInt) void traverseDir(std::string root, std::vector& paths, int depth) { if (depth >= 50) { return; } - #ifdef KOALA_LINUX +#if defined(KOALA_LINUX) || defined(KOALA_MACOS) DIR* d = opendir(root.c_str()); struct dirent* ent = NULL; struct stat statbuf; @@ -337,5 +336,5 @@ void traverseDir(std::string root, std::vector& paths, int depth) { traverseDir(filepath, paths, depth + 1); } } - #endif +#endif } -- Gitee From 4f563552af7e155bafb39b6868912f96d039ec53 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Thu, 12 Sep 2024 19:53:20 +0300 Subject: [PATCH 2/4] Fix typo --- interop/src/cpp/common-interop.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/interop/src/cpp/common-interop.cc b/interop/src/cpp/common-interop.cc index 995fdb013..cad162a17 100644 --- a/interop/src/cpp/common-interop.cc +++ b/interop/src/cpp/common-interop.cc @@ -265,7 +265,12 @@ typedef KInt (*RunApplication_t)(const KInt arg0, const KInt arg1); void* getImpl(const char* path, const char* name) { static void* lib = nullptr; - if (!lib) lib = loadLibrary(std::string(path)); + if (!lib) { + lib = loadLibrary(std::string(path) + libName("vmloader")); + if (!lib) { + fprintf(stderr, "Ensure vmloader library was built\n"); + } + } return findSymbol(lib, name); } -- Gitee From 30fad77b2aa87c04160d87a18c8a56da67a8db85 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Thu, 12 Sep 2024 22:25:56 +0300 Subject: [PATCH 3/4] Update --- interop/src/cpp/common-interop.cc | 11 ++++++----- interop/src/cpp/dynamic-loader.h | 8 ++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/interop/src/cpp/common-interop.cc b/interop/src/cpp/common-interop.cc index cad162a17..508879312 100644 --- a/interop/src/cpp/common-interop.cc +++ b/interop/src/cpp/common-interop.cc @@ -266,9 +266,10 @@ typedef KInt (*RunApplication_t)(const KInt arg0, const KInt arg1); void* getImpl(const char* path, const char* name) { static void* lib = nullptr; if (!lib) { - lib = loadLibrary(std::string(path) + libName("vmloader")); + auto name = std::string(path) + "/" + libName("vmloader"); + lib = loadLibrary(name); if (!lib) { - fprintf(stderr, "Ensure vmloader library was built\n"); + fprintf(stderr, "Ensure vmloader library at $s was built\n", name.c_str()); } } return findSymbol(lib, name); @@ -276,21 +277,21 @@ void* getImpl(const char* path, const char* name) { KInt impl_LoadVirtualMachine(KInt vmKind, const KStringPtr& appClassPath, const KStringPtr& appLibPath) { static LoadVirtualMachine_t impl = nullptr; - if (!impl) impl = reinterpret_cast(getImpl(appClassPath.c_str(), "LoadVirtualMachine")); + if (!impl) impl = reinterpret_cast(getImpl(appClassPath.c_str(), "_LoadVirtualMachine")); return impl(vmKind, appClassPath.c_str(), appLibPath.c_str()); } KOALA_INTEROP_3(LoadVirtualMachine, KInt, KInt, KStringPtr, KStringPtr) KInt impl_StartApplication() { static StartApplication_t impl = nullptr; - if (!impl) impl = reinterpret_cast(getImpl(nullptr, "StartApplication")); + if (!impl) impl = reinterpret_cast(getImpl(nullptr, "_StartApplication")); return impl(); } KOALA_INTEROP_0(StartApplication, KInt) KInt impl_RunApplication(const KInt arg0, const KInt arg1) { static RunApplication_t impl = nullptr; - if (!impl) impl = reinterpret_cast(getImpl(nullptr, "RunApplication")); + if (!impl) impl = reinterpret_cast(getImpl(nullptr, "_RunApplication")); return impl(arg0, arg1); } KOALA_INTEROP_2(RunApplication, KInt, KInt, KInt) diff --git a/interop/src/cpp/dynamic-loader.h b/interop/src/cpp/dynamic-loader.h index 58832528e..a19c5361a 100644 --- a/interop/src/cpp/dynamic-loader.h +++ b/interop/src/cpp/dynamic-loader.h @@ -68,6 +68,14 @@ inline std::string libName(const char* lib) { return result; } +inline std::string symbolName(const char* symbol) { + std::string result; +#ifdef KOALA_MACOS + return "_" + std::string(symbol); +#else + return std::string(symbol); +#endif +} #else #error "Unknown platform" #endif -- Gitee From f4df46f63b5cc6daef218ef77f14d2d54e55fbc2 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Mon, 16 Sep 2024 17:53:03 +0300 Subject: [PATCH 4/4] Fix typo --- interop/src/cpp/common-interop.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interop/src/cpp/common-interop.cc b/interop/src/cpp/common-interop.cc index 508879312..59ef2b5f4 100644 --- a/interop/src/cpp/common-interop.cc +++ b/interop/src/cpp/common-interop.cc @@ -269,7 +269,7 @@ void* getImpl(const char* path, const char* name) { auto name = std::string(path) + "/" + libName("vmloader"); lib = loadLibrary(name); if (!lib) { - fprintf(stderr, "Ensure vmloader library at $s was built\n", name.c_str()); + fprintf(stderr, "Ensure vmloader library at %s was built\n", name.c_str()); } } return findSymbol(lib, name); -- Gitee