SIGSEGV on JNIEnv->CallStaticObjectMethod

1

I'm trying to create a JThrowable in C code when an error value is returned from a native library. A SIGSEGV is generated when I call (*env)->CallStaticObjectMethod(env, exceptionClass, mid, errCode);.

This is the C code that creates the exception:

jthrowable makeGPIOException(JNIEnv *env, int errCode) {
    printf("makeGPIOException %d\n", errCode);
    jclass exceptionClass = (*env)->FindClass(env, "com/nicholaspjohnson/kpigpio/exceptions/GPIOException");
    printf("excClass %p\n", (void*)&exceptionClass);
    jmethodID mid = (*env)->GetStaticMethodID(env, exceptionClass, "getProperThrowable", "(I)Ljava/lang/Class");
    printf("mid %p\n", (void*)&mid);
    jobject actualExceptionClass = (*env)->CallStaticObjectMethod(env, exceptionClass, mid, errCode);   
    printf("aec %p\n", (void*)&actualExceptionClass);
    jmethodID exceptionConstructorID = (*env)->GetMethodID(env, actualExceptionClass, "<init>", "()V");
    printf("eci %p\n", (void*)&exceptionConstructorID);
    return (*env)->NewObject(env, actualExceptionClass, exceptionConstructorID);
}

And the Kotlin class com.nicholaspjohnson.kpigpio.exceptions.GPIOException

open class GPIOException(reason: String): RuntimeException(reason) {
    companion object {
        @JvmStatic
        fun getProperThrowable(errCode: Int) : Class<out GPIOException> {
            println("getProperThrowable $errCode")
            return when(errCode) {
                //valid exceptions here, removed for clarity
                else -> throw RuntimeException("Bad errCode $errCode")
            }
        }
    }
}

The debug printf's and println's return this message:

makeGPIOException -1
excClass 0xb6340a4c
mid 0xb6340a48

The kotlin code never prints anything, and immediately after the last line the SIGSEGV happens. This is the hs_err_pid.log

#  SIGSEGV (0xb) at pc=0xb66b271c, pid=32064, tid=3056866416
# JRE version: Java(TM) SE Runtime Environment (8.0_65-b17) (build 1.8.0_65-b17)
# Java VM: Java HotSpot(TM) Client VM (25.65-b01 mixed mode linux-arm )
# Problematic frame:
# V  [libjvm.so+0x2f171c]

---------------  T H R E A D  ---------------

Current thread (0xb6106b80):  JavaThread "main" [_thread_in_vm, id=32065, stack(0xb62f2000,0xb6342000)]
siginfo: si_signo: 11 (SIGSEGV), si_code: 1 (SEGV_MAPERR), si_addr: 0x00000000

Top of Stack: (sp=0xb63409b8)
0xb63409b8:   b6106b80 b6106cbc b6340a2c b66ad938
0xb63409c8:   b40015dc b66a11b8 00000000 b6340a34
0xb63409d8:   0000000c 89b37160 aa3a8538 b6106b80
0xb63409e8:   34333662 b6106b80 b6107238 00000617
0xb63409f8:   b6917bc0 00000000 00000000 b6340b04
0xb6340a08:   aa3a8538 b6106b80 b6340a5c b6e0eae0
0xb6340a18:   b66a0e7c b66ad8a4 b66a0e7c 00000000
0xb6340a28:   b6340a5c b40014a4 00000000 b6340a38 

Instructions: (pc=0xb66b271c)
0xb66b26fc:   e3a00003 ebffb390 e3500000 13a03001
0xb66b270c:   15c43000 e1a00004 e8bd8818 e92d4830
0xb66b271c:   e5913000 e1a05002 e5933004 e28db00c
0xb66b272c:   e1d321ba e5933008 e1a04000 e0833102 

Register to memory mapping:

  r0  = 0xb63409fc
0xb63409fc is pointing into the stack for thread: 0xb6106b80

  r1  = 0x00000000
0x00000000 is an unknown value

  r2  = 0xb6340a34
0xb6340a34 is pointing into the stack for thread: 0xb6106b80

  r3  = 0xb6340a34
0xb6340a34 is pointing into the stack for thread: 0xb6106b80

  r4  = 0xb6106b80
0xb6106b80 is a thread

  r5  = 0xb6106cbc
0xb6106cbc is an unknown value

  r6  = 0x00000000
0x00000000 is an unknown value

  r7  = 0x00000000
0x00000000 is an unknown value

  r8  = 0xb6340b04
0xb6340b04 is pointing into the stack for thread: 0xb6106b80

  r9  = 0xaa3a8538
{method} {0xaa3a8538} 'gpioInitialize' '()I' in 'com/nicholaspjohnson/kpigpio/PiGPIO'

  r10 = 0xb6106b80
0xb6106b80 is a thread

  fp  = 0xb6340a2c
0xb6340a2c is pointing into the stack for thread: 0xb6106b80

  r12 = 0x0000000c
0x0000000c is an unknown value

  sp  = 0xb63409b8
0xb63409b8 is pointing into the stack for thread: 0xb6106b80

  lr  = 0xb66ad938
0xb66ad938: <offset 0x2ec938> in /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre/lib/arm/client/libjvm.so at 0xb63c1000

  pc  = 0xb66b271c
0xb66b271c: <offset 0x2f171c> in /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre/lib/arm/client/libjvm.so at 0xb63c1000



Stack: [0xb62f2000,0xb6342000],  sp=0xb63409b8,  free space=314k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
V  [libjvm.so+0x2f171c]

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  com.nicholaspjohnson.kpigpio.PiGPIO.gpioInitialize()I+0
j  com.nicholaspjohnson.ledlights.Main.init()V+13
j  com.nicholaspjohnson.ledlights.Main.main([Ljava/lang/String;)V+23
v  ~StubRoutines::call_stub

---------------  P R O C E S S  ---------------

Java Threads: ( => current thread )
  0xb6180d20 JavaThread "Service Thread" daemon [_thread_blocked, id=32071, stack(0xac645000,0xac695000)]
  0xb617dcb8 JavaThread "C1 CompilerThread0" daemon [_thread_blocked, id=32070, stack(0xac695000,0xac715000)]
  0xb617c7e0 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=32069, stack(0xac715000,0xac765000)]
  0xb6161198 JavaThread "Finalizer" daemon [_thread_blocked, id=32068, stack(0xaca12000,0xaca62000)]
  0xb615fd88 JavaThread "Reference Handler" daemon [_thread_blocked, id=32067, stack(0xaca62000,0xacab2000)]
=>0xb6106b80 JavaThread "main" [_thread_in_vm, id=32065, stack(0xb62f2000,0xb6342000)]

Other Threads:
  0xb615cfd8 VMThread [stack: 0xacab2000,0xacb32000] [id=32066]
  0xb61827b0 WatcherThread [stack: 0xac5c5000,0xac645000] [id=32072]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Internal exceptions (10 events):
Event: 7.321 Thread 0xb6106b80 Exception <a 'java/lang/ArrayIndexOutOfBoundsException'> (0xad36f280) thrown at [/HUDSON/workspace/8-2-build-glinux-arm-vfp-hflt/jdk8u65/4987/hotspot/src/share/vm/runtime/sharedRuntime.cpp, line 605]
Event: 7.324 Thread 0xb6106b80 Exception <a 'java/lang/ArrayIndexOutOfBoundsException'> (0xad370388) thrown at [/HUDSON/workspace/8-2-build-glinux-arm-vfp-hflt/jdk8u65/4987/hotspot/src/share/vm/runtime/sharedRuntime.cpp, line 605]
Event: 7.330 Thread 0xb6106b80 Exception <a 'java/lang/ArrayIndexOutOfBoundsException'> (0xad371798) thrown at [/HUDSON/workspace/8-2-build-glinux-arm-vfp-hflt/jdk8u65/4987/hotspot/src/share/vm/runtime/sharedRuntime.cpp, line 605]
Event: 7.419 Thread 0xb6106b80 Exception <a 'java/lang/ArrayIndexOutOfBoundsException'> (0xad393a90) thrown at [/HUDSON/workspace/8-2-build-glinux-arm-vfp-hflt/jdk8u65/4987/hotspot/src/share/vm/runtime/sharedRuntime.cpp, line 605]
Event: 7.421 Thread 0xb6106b80 Exception <a 'java/lang/ArrayIndexOutOfBoundsException'> (0xad394ae0) thrown at [/HUDSON/workspace/8-2-build-glinux-arm-vfp-hflt/jdk8u65/4987/hotspot/src/share/vm/runtime/sharedRuntime.cpp, line 605]
Event: 7.425 Thread 0xb6106b80 Exception <a 'java/lang/ArrayIndexOutOfBoundsException'> (0xad395ff8) thrown at [/HUDSON/workspace/8-2-build-glinux-arm-vfp-hflt/jdk8u65/4987/hotspot/src/share/vm/runtime/sharedRuntime.cpp, line 605]
Event: 7.508 Thread 0xb6106b80 Exception <a 'java/lang/ArrayIndexOutOfBoundsException'> (0xad3b80b8) thrown at [/HUDSON/workspace/8-2-build-glinux-arm-vfp-hflt/jdk8u65/4987/hotspot/src/share/vm/runtime/sharedRuntime.cpp, line 605]
Event: 7.564 Thread 0xb6106b80 Exception <a 'java/lang/ArrayIndexOutOfBoundsException'> (0xad3c61f0) thrown at [/HUDSON/workspace/8-2-build-glinux-arm-vfp-hflt/jdk8u65/4987/hotspot/src/share/vm/runtime/sharedRuntime.cpp, line 605]
Event: 7.941 Thread 0xb6106b80 Exception <a 'java/lang/NoSuchMethodError': org.slf4j.impl.StaticMarkerBinder.getSingleton()Lorg/slf4j/impl/StaticMarkerBinder;> (0xad23dc40) thrown at [/HUDSON/workspace/8-2-build-glinux-arm-vfp-hflt/jdk8u65/4987/hotspot/src/share/vm/interpreter/linkReso
Event: 8.148 Thread 0xb6106b80 Exception <a 'java/lang/NoSuchMethodError': getProperThrowable> (0xad2c7910) thrown at [/HUDSON/workspace/8-2-build-glinux-arm-vfp-hflt/jdk8u65/4987/hotspot/src/share/vm/prims/jni.cpp, line 1559]

Events (10 events):
Event: 8.118 loading class org/apache/logging/log4j/core/layout/ByteBufferDestinationHelper
Event: 8.118 loading class org/apache/logging/log4j/core/layout/ByteBufferDestinationHelper done
Event: 8.126 loading class com/nicholaspjohnson/kpigpio/PiGPIO
Event: 8.126 loading class com/nicholaspjohnson/kpigpio/PiGPIO done
Event: 8.131 loading class java/lang/ClassLoaderHelper
Event: 8.132 loading class java/lang/ClassLoaderHelper done
Event: 8.139 loading class com/nicholaspjohnson/kpigpio/exceptions/GPIOException
Event: 8.139 loading class com/nicholaspjohnson/kpigpio/exceptions/GPIOException done
Event: 8.142 loading class com/nicholaspjohnson/kpigpio/exceptions/GPIOException$Companion
Event: 8.142 loading class com/nicholaspjohnson/kpigpio/exceptions/GPIOException$Companion done

VM Arguments:
java_command: com.nicholaspjohnson.ledlights.Main
java_class_path (initial): ledlights.main.jar
Launcher Type: SUN_STANDARD

Environment Variables:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
SHELL=/bin/bash

Signal Handlers:
SIGSEGV: [libjvm.so+0x506638], sa_mask[0]=11111111011111111101111111111110, sa_flags=SA_RESTART|SA_SIGINFO
SIGBUS: [libjvm.so+0x506638], sa_mask[0]=11111111011111111101111111111110, sa_flags=SA_RESTART|SA_SIGINFO
SIGFPE: [libjvm.so+0x3fd4ec], sa_mask[0]=11111111011111111101111111111110, sa_flags=SA_RESTART|SA_SIGINFO
SIGPIPE: [libjvm.so+0x3fd4ec], sa_mask[0]=11111111011111111101111111111110, sa_flags=SA_RESTART|SA_SIGINFO
SIGXFSZ: [libjvm.so+0x3fd4ec], sa_mask[0]=11111111011111111101111111111110, sa_flags=SA_RESTART|SA_SIGINFO
SIGILL: [libjvm.so+0x3fd4ec], sa_mask[0]=11111111011111111101111111111110, sa_flags=SA_RESTART|SA_SIGINFO
SIGUSR1: SIG_DFL, sa_mask[0]=00000000000000000000000000000000, sa_flags=none
SIGUSR2: [libjvm.so+0x3fd650], sa_mask[0]=00000000000000000000000000000000, sa_flags=SA_RESTART|SA_SIGINFO
SIGHUP: [libjvm.so+0x3fda84], sa_mask[0]=11111111011111111101111111111110, sa_flags=SA_RESTART|SA_SIGINFO
SIGINT: [libjvm.so+0x3fda84], sa_mask[0]=11111111011111111101111111111110, sa_flags=SA_RESTART|SA_SIGINFO
SIGTERM: [libjvm.so+0x3fda84], sa_mask[0]=11111111011111111101111111111110, sa_flags=SA_RESTART|SA_SIGINFO
SIGQUIT: [libjvm.so+0x3fda84], sa_mask[0]=11111111011111111101111111111110, sa_flags=SA_RESTART|SA_SIGINFO


---------------  S Y S T E M  ---------------

OS:PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

uname:Linux 4.14.71+ #1145 Fri Sep 21 15:06:38 BST 2018 armv6l
libc:glibc 2.24 NPTL 2.24 
rlimit: STACK 8192k, CORE 0k, NPROC 3400, NOFILE 1048576, AS infinity
load average:0.24 0.15 0.46

Memory: 4k page, physical 443880k(246264k free), swap 102396k(88316k free)

vm_info: Java HotSpot(TM) Client VM (25.65-b01) for linux-arm-vfp-hflt JRE (1.8.0_65-b17), built on Oct  6 2015 16:19:04 by "java_re" with gcc 4.7.2 20120910 (prerelease)

time: Fri Aug 23 01:02:36 2019
elapsed time: 8 seconds (0d 0h 0m 8s)


Thank you for any help in advance!

c
kotlin
raspberry-pi
java-native-interface
segmentation-fault
asked on Stack Overflow Aug 23, 2019 by Nick Johnson

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0