From c87d5333e671a7df0ca576aba766a0e57571160b Mon Sep 17 00:00:00 2001 From: masesdevelopers <94312179+masesdevelopers@users.noreply.github.com> Date: Thu, 12 Dec 2024 03:22:59 +0100 Subject: [PATCH] Adding more steps in test function --- .github/workflows/build.yaml | 6 +++- tests/native/CreateVM.cpp | 61 ++++++++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 3b9b6e0638..60d41c76c4 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -80,11 +80,15 @@ jobs: cd ${{ github.workspace }}/tests/native g++ -I $JAVA_HOME/include -I $JAVA_HOME/include/darwin -L $JAVA_HOME/lib/server -ljvm CreateVM.cpp -o CreateVM.out + - name: Export location + shell: bash + run: echo "DYLD_LIBRARY_PATH=$(echo $JAVA_HOME/lib/server)" >> $GITHUB_ENV + - name: Execute CreateVM shell: bash run: | + export echo "GITHUB_ENV_LD_LIBRARY_PATH=$(echo $JAVA_HOME/lib/server)" >> $GITHUB_ENV - export LD_LIBRARY_PATH=/Users/runner/hostedtoolcache/Java_Temurin-Hotspot_jdk/17.0.13-11/x64/Contents/Home/lib/server export DYLD_LIBRARY_PATH=/Users/runner/hostedtoolcache/Java_Temurin-Hotspot_jdk/17.0.13-11/x64/Contents/Home/lib/server export ${{ github.workspace }}/tests/native/CreateVM.out diff --git a/tests/native/CreateVM.cpp b/tests/native/CreateVM.cpp index d36247666d..db0330f0f2 100644 --- a/tests/native/CreateVM.cpp +++ b/tests/native/CreateVM.cpp @@ -3,22 +3,79 @@ int main() { - const int arguments = 1; + const int arguments = 4; std::cout << "Starting JNI_CreateJavaVM test" << std::endl; JavaVM *jvm; /* denotes a Java VM */ JNIEnv *env; /* pointer to native method interface */ + + int numVms; + + jint ret = JNI_GetCreatedJavaVMs(&jvm, 1, &numVms); + if (ret != JNI_OK) + { + switch (ret) + { + case -1: std::cout << "failed - unknown error"; << std::endl; return; + case -2: std::cout << "failed - thread detached from the VM"; << std::endl; return; + case -3: std::cout << "failed - JNI version error"; << std::endl; return; + case -4: std::cout << "failed - not enough memory"; << std::endl; return; + case -5: std::cout << "failed - VM already created"; << std::endl; return; + case -6: std::cout << "failed - invalid arguments"; << std::endl; return; + default: break; + } + } + + if (numVms >= 0) + { + std::cout << "JNI_GetCreatedJavaVMs returned with an already created JVM"; << std::endl; + return; + } + JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */ + + ret = JNI_GetDefaultJavaVMInitArgs(&vm_args); + if (ret != JNI_OK) + { + switch (ret) + { + case -1: std::cout << "failed - unknown error"; << std::endl; return; + case -2: std::cout << "failed - thread detached from the VM"; << std::endl; return; + case -3: std::cout << "failed - JNI version error"; << std::endl; return; + case -4: std::cout << "failed - not enough memory"; << std::endl; return; + case -5: std::cout << "failed - VM already created"; << std::endl; return; + case -6: std::cout << "failed - invalid arguments"; << std::endl; return; + default: break; + } + } + JavaVMOption* options = new JavaVMOption[arguments]; options[0].optionString = "-Djava.class.path=/usr/lib/java"; + options[1].optionString = "--add-opens=java.base/java.nio=ALL-UNNAMED"; + options[2].optionString = "-Xmx4G"; + options[3].optionString = "-Xms1G"; vm_args.version = JNI_VERSION_1_6; vm_args.nOptions = arguments; vm_args.options = options; vm_args.ignoreUnrecognized = true; /* load and initialize a Java VM, return a JNI interface * pointer in env */ - JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); + ret = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); + if (ret != JNI_OK) + { + switch (ret) + { + case -1: std::cout << "failed - unknown error"; << std::endl; return; + case -2: std::cout << "failed - thread detached from the VM"; << std::endl; return; + case -3: std::cout << "failed - JNI version error"; << std::endl; return; + case -4: std::cout << "failed - not enough memory"; << std::endl; return; + case -5: std::cout << "failed - VM already created"; << std::endl; return; + case -6: std::cout << "failed - invalid arguments"; << std::endl; return; + default: break; + } + } + delete[] options; /* invoke the Main.test method using the JNI */ // jclass cls = env->FindClass("Main");