JNI crashes JVM (from C) -- openjdk 8

Martijn Verburg martijnverburg at gmail.com
Mon Apr 23 10:35:09 UTC 2018


Hi there,

Welcome to OpenJDK!  The discuss list is more for discussion around OpenJDK
(the project and community).  I'd recommend joining adoption-discuss for
asking questions if you're new to OpenJDK.  Stackoverflow is also a popular
resource for these sorts of questions.

Cheers,
Martijn

On 23 April 2018 at 11:14, Xen <list at xenhideout.nl> wrote:

> Hi,
>
> I am new to using JNI but I don't know where I can get any help.
>
> On my openjdk-8 Linux system, the example at
> https://docs.oracle.com/javase/8/docs/technotes/guides/jni/
> spec/invocation.html
>
> is in C++ but when run the GetStaticMethodID call crashes the JVM.
>
> However when I change it to C, already the FindClass call crashes the JVM.
>
> This fragment includes
>
> JavaVM *jvm;       /* denotes a Java VM */
> JNIEnv *env;       /* pointer to native method interface */
>
> in C++, in C I change it to
>
> JavaVM *jvm;
> JNIEnv env;
>
> to make it compile.
>
> I compile with include dirs of
>
> /usr/lib/jvm/java-8-openjdk-amd64/include and
> /usr/lib/jvm/java-8-openjdk-amd64/include/linux
>
> And link with
>
> /usr/lib/jvm/java-8-openjdk-amd64/jre/lib
> /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/server
>
> and test.c -ljava -ljvm
>
> Consequently, I run with LD_LIBRARY_PATH=...:... with those values.
>
> The program runs, but there is always a JVM crash. I was surprised to see
> it pass the FindClass call with C++.
>
> I have not tested other JDKs or JREs.
>
> The Main class is as simple as it gets, as per the example:
>
> public class Main {
>     public static void test(int i) {
>         System.out.println(i);
>     }
> }
>
> But honestly, the C version always crashes the JVM, no matter what class I
> give it; even non-existent classes always crash it.
>
> I can attach verbosity (-verbose:jni) but it lists no errors anywhere; or
> -Xcheck:jni, but that doesn't change the output.
>
> I can attach the complete output, but I was wondering if this is a known
> issue?
>
> Am I doing something wrong?
>
> This is Oracle's minimal example; the code compiles and can find
> everything.
>
> #include <jni.h>       /* where everything is defined */
>
> int main() {
>     JavaVM *jvm;       /* denotes a Java VM */
>     JNIEnv env;       /* pointer to native method interface */
>     JavaVMInitArgs vm_args; /* JDK/JRE 6 VM initialization arguments */
>     JavaVMOption options[1];
>     options[0].optionString = "-Djava.class.path=/usr/lib/java";
>     vm_args.version = JNI_VERSION_1_6;
>     vm_args.nOptions = 1;
>     vm_args.options = options;
>     vm_args.ignoreUnrecognized = 0;
>     /* load and initialize a Java VM, return a JNI interface
>      * pointer in env */
>
>     JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
>
>     /* invoke the Main.test method using the JNI */
>
>     jclass cls = env->FindClass(&env, "Main");            // <--- C
> crashes here
>     jmethodID mid = env->GetStaticMethodID(&env, cls, "test", "(I)V");
> // <--- C++ here
>     env->CallStaticVoidMethod(cls, mid, 100);             // <--- never
> reached
>     /* We are done. */
>     (*jvm)->DestroyJavaVM(jvm);
> }
>
>
>
> I have tested this on CentOS 7.4 as well, with versions 1.8.0, 1.7.0 and
> 1.6.0, all giving the same error messages:
>
> # A fatal error has been detected by the Java Runtime Environment:
> #
> #  SIGSEGV (0xb) at pc=0x0000000000000000, pid=24945,
> tid=0x00007f5eeab0f740
> #
> # JRE version: OpenJDK Runtime Environment (8.0_161-b14) (build
> 1.8.0_161-b14)
> # Java VM: OpenJDK 64-Bit Server VM (25.161-b14 mixed mode linux-amd64
> compressed oops)
> # Problematic frame:
> # C  0x0000000000000000
>
> The others are the same.


More information about the discuss mailing list