C ++ JNI在执行任何IO / Streams时崩溃/挂起

时间:2016-09-26 18:06:20

标签: java c++ java-native-interface

在尝试编译要在JAVA端使用的C ++代码时,我似乎遇到了一个大问题。当我尝试做任何IO / Stream / Manipulation相关的事情时,JVM似乎崩溃了。

有时,就像我运行应用程序时每10次一样,JNI 100%使用所有IO内容。然后剩下的时间JVM崩溃或挂起。

我正在使用: Windows 10 x64,JAVA jdk1.8.0_102,Netbeans 8.1,g ++,附加选项:-D__int64 = int64_t

当我这样做时,JNI工作正常:

JNIEXPORT jstring JNICALL Java_Main_nativePrint
  (JNIEnv *env, jobject)
{
    return env->NewStringUTF("HELLO");
}

JNIEXPORT jstring JNICALL Java_Main_nativePrint
  (JNIEnv *env, jobject)
{
    double var = std::sqrt(100);
    return env->NewStringUTF("HELLO");
}

当我尝试合并printfstd::stringstreamsprintfcoutputs等内容时,JVM会崩溃。甚至实例化std::stringstream似乎让我崩溃了。我假设其他类型的操作函数也会让我崩溃:

Java_Main_nativePrint(JNIEnv *env, jobject instance)
{
    int variable = 100;
    char str[15];

    //CRASHES ON HERE
    sprintf(str, "%d", variable);
    //CRASHES ON HERE IF ABOVE COMMENTED OUT OR USING ANOTHER CHAR ARRAY.
    puts(str); //CRASHES ON HERE

    return env->NewStringUTF(str);
}

JNIEXPORT jstring JNICALL Java_Main_nativePrint
  (JNIEnv *env, jobject)
{
    double res = std::sqrt(100);

    //CRASHES HERE ON INSTANTATION OF stringstream
    std::stringstream ss;
    //ss << std::sqrt(100);
    //const char* str = ss.str().c_str();

    return env->NewStringUTF("HELLO");
}

我似乎对这个人有类似的问题:

JNI with c++ keeps crashing on cout, printf and method calls

这是我的转储:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000180117a37, pid=6064, tid=0x0000000000002e40
#
# JRE version: Java(TM) SE Runtime Environment (8.0_102-b14) (build 1.8.0_102-b14)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.102-b14 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [cygwin1.dll+0xd7a37]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

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

Current thread (0x0000000002e70800):  JavaThread "main" [_thread_in_native, id=11840, stack(0x0000000002d70000,0x0000000002e70000)]

siginfo: ExceptionCode=0xc0000005, writing address 0x0000000000000000

Registers:
RAX=0x0000000000000000, RBX=0x0000000002e6f328, RCX=0x00000003fec8bfe0, RDX=0x00000003fec5ee60
RSP=0x0000000002e6f100, RBP=0x0000000000000018, RSI=0x0000000002e6f258, RDI=0x0000000002f7835d
R8 =0x0000000002e6f298, R9 =0x0000000002d67610, R10=0x0000000002e70000, R11=0x00000003fec5ee08
R12=0x0000000000000000, R13=0x000000001bf903f0, R14=0x0000000002e6f420, R15=0x0000000002e70800
RIP=0x0000000180117a37, EFLAGS=0x0000000000010202

Top of Stack: (sp=0x0000000002e6f100)
0x0000000002e6f100:   00000001800f0390 0000000180117a4b
0x0000000002e6f110:   0000000002d6eee0 0000000002d6eee8
0x0000000002e6f120:   0000000002e70800 000000005b94a34e
0x0000000002e6f130:   0000000002e6f328 00000003fec62d16
0x0000000002e6f140:   000000005bd50088 0000005000000000
0x0000000002e6f150:   0000000000000015 0000000000000000
0x0000000002e6f160:   0000000002e6f1f0 0000000002e6f258
0x0000000002e6f170:   0000000002f7835d 00000003fec58d59
0x0000000002e6f180:   0000000000000000 0000000000000100
0x0000000002e6f190:   0000000000000002 0000000002d6ebf0
0x0000000002e6f1a0:   0000000002e70800 000000001bf903f0
0x0000000002e6f1b0:   0000000020200021 0000000002f7835d
0x0000000002e6f1c0:   0000000002e6f250 00000005c9f710e4
0x0000000002e6f1d0:   0000000000000010 0000000000000008
0x0000000002e6f1e0:   0000000002e70800 0000000002f93b63
0x0000000002e6f1f0:   000000001bf903f0 0000000002e70800 

Instructions: (pc=0x0000000180117a37)
0x0000000180117a17:   04 f3 90 eb e3 48 c7 c0 08 00 00 00 49 0f c1 82
0x0000000180117a27:   50 e4 ff ff 4c 8d 1d 19 00 00 00 4c 87 5c 24 08
0x0000000180117a37:   4c 89 18 41 ff 82 44 e4 ff ff 41 ff 8a 4c e4 ff
0x0000000180117a47:   ff 58 ff e0 65 4c 8b 14 25 08 00 00 00 41 bb 01 


Register to memory mapping:

RAX=0x0000000000000000 is an unknown value
RBX=0x0000000002e6f328 is pointing into the stack for thread: 0x0000000002e70800
RCX=0x00000003fec8bfe0 is an unknown value
RDX=0x00000003fec5ee60 is an unknown value
RSP=0x0000000002e6f100 is pointing into the stack for thread: 0x0000000002e70800
RBP=0x0000000000000018 is an unknown value
RSI=0x0000000002e6f258 is pointing into the stack for thread: 0x0000000002e70800
RDI=0x0000000002f7835d is at code_begin+2301 in an Interpreter codelet
invoke return entry points  [0x0000000002f77a60, 0x0000000002f78440]  2528 bytes
R8 =0x0000000002e6f298 is pointing into the stack for thread: 0x0000000002e70800
R9 =0x0000000002d67610 is an unknown value
R10=0x0000000002e70000 is pointing into the stack for thread: 0x0000000002e70800
R11=0x00000003fec5ee08 is an unknown value
R12=0x0000000000000000 is an unknown value
R13={method} {0x000000001bf903f8} 'nativePrint' '()Ljava/lang/String;' in 'Main'
R14=0x0000000002e6f420 is pointing into the stack for thread: 0x0000000002e70800
R15=0x0000000002e70800 is a thread


Stack: [0x0000000002d70000,0x0000000002e70000],  sp=0x0000000002e6f100,  free space=1020k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [cygwin1.dll+0xd7a37]
C  [cygwin1.dll+0xb038f]

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  Main.nativePrint()Ljava/lang/String;+0
j  Main.main([Ljava/lang/String;)V+18
v  ~StubRoutines::call_stub

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

Java Threads: ( => current thread )
  0x000000001dea8800 JavaThread "Service Thread" daemon [_thread_blocked, id=3408, stack(0x000000001e280000,0x000000001e380000)]
  0x000000001c538800 JavaThread "C1 CompilerThread2" daemon [_thread_blocked, id=5372, stack(0x000000001dd80000,0x000000001de80000)]
  0x000000001c535800 JavaThread "C2 CompilerThread1" daemon [_thread_blocked, id=5400, stack(0x000000001dc80000,0x000000001dd80000)]
  0x000000001c531000 JavaThread "C2 CompilerThread0" daemon [_thread_blocked, id=5124, stack(0x000000001db80000,0x000000001dc80000)]
  0x000000001c52f000 JavaThread "Attach Listener" daemon [_thread_blocked, id=2432, stack(0x000000001da80000,0x000000001db80000)]
  0x000000001c52e000 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=336, stack(0x000000001d980000,0x000000001da80000)]
  0x0000000002f6a800 JavaThread "Finalizer" daemon [_thread_blocked, id=11892, stack(0x000000001d7a0000,0x000000001d8a0000)]
  0x0000000002f61000 JavaThread "Reference Handler" daemon [_thread_blocked, id=11932, stack(0x000000001d6a0000,0x000000001d7a0000)]
=>0x0000000002e70800 JavaThread "main" [_thread_in_native, id=11840, stack(0x0000000002d70000,0x0000000002e70000)]

Other Threads:
  0x000000001c4f8000 VMThread [stack: 0x000000001d5a0000,0x000000001d6a0000] [id=13280]
  0x000000001ded3000 WatcherThread [stack: 0x000000001e380000,0x000000001e480000] [id=11656]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap:
 PSYoungGen      total 76288K, used 2621K [0x000000076b500000, 0x0000000770a00000, 0x00000007c0000000)
  eden space 65536K, 4% used [0x000000076b500000,0x000000076b78f5e8,0x000000076f500000)
  from space 10752K, 0% used [0x000000076ff80000,0x000000076ff80000,0x0000000770a00000)
  to   space 10752K, 0% used [0x000000076f500000,0x000000076f500000,0x000000076ff80000)
 ParOldGen       total 175104K, used 0K [0x00000006c1e00000, 0x00000006cc900000, 0x000000076b500000)
  object space 175104K, 0% used [0x00000006c1e00000,0x00000006c1e00000,0x00000006cc900000)
 Metaspace       used 2586K, capacity 4486K, committed 4864K, reserved 1056768K
  class space    used 282K, capacity 386K, committed 512K, reserved 1048576K

Card table byte_map: [0x0000000012330000,0x0000000012b30000] byte_map_base: 0x000000000ed21000

Marking Bits: (ParMarkBitMap*) 0x000000005bf8a6c0
 Begin Bits: [0x0000000013480000, 0x0000000017408000)
 End Bits:   [0x0000000017408000, 0x000000001b390000)

Polling page: 0x0000000002c00000

CodeCache: size=245760Kb used=1102Kb max_used=1105Kb free=244657Kb
 bounds [0x0000000002f70000, 0x00000000031e0000, 0x0000000011f70000]
 total_blobs=254 nmethods=23 adapters=145
 compilation: enabled

Compilation events (10 events):
Event: 0.060 Thread 0x000000001c538800   18  s    3       java.lang.StringBuffer::append (13 bytes)
Event: 0.060 Thread 0x000000001c535800   20       4       java.lang.String::charAt (29 bytes)
Event: 0.060 Thread 0x000000001c538800 nmethod 18 0x0000000003080210 code [0x00000000030803c0, 0x0000000003080af8]
Event: 0.060 Thread 0x000000001c538800   21       3       java.lang.AbstractStringBuilder::append (50 bytes)
Event: 0.061 Thread 0x000000001c538800 nmethod 21 0x0000000003080ed0 code [0x00000000030810a0, 0x00000000030817f8]
Event: 0.061 Thread 0x000000001c538800   22       3       java.lang.String::indexOf (7 bytes)
Event: 0.061 Thread 0x000000001c535800 nmethod 20 0x00000000030847d0 code [0x0000000003084920, 0x00000000030849f8]
Event: 0.061 Thread 0x000000001c538800 nmethod 22 0x00000000030843d0 code [0x0000000003084540, 0x0000000003084748]
Event: 0.064 Thread 0x000000001c538800   23       3       java.util.Arrays::copyOfRange (63 bytes)
Event: 0.064 Thread 0x000000001c538800 nmethod 23 0x0000000003082950 code [0x0000000003082b60, 0x0000000003083698]

GC Heap History (0 events):
No events

Deoptimization events (0 events):
No events

Internal exceptions (2 events):
Event: 0.025 Thread 0x0000000002e70800 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.defineClass(Ljava/lang/String;[BII)Ljava/lang/Class; name or signature does not match> (0x000000076b507c78) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u102\7268\hotspot\s�4�B��?
Event: 0.025 Thread 0x0000000002e70800 Exception <a 'java/lang/NoSuchMethodError': Method sun.misc.Unsafe.prefetchRead(Ljava/lang/Object;J)V name or signature does not match> (0x000000076b507f60) thrown at [C:\re\workspace\8-2-build-windows-amd64-cygwin\jdk8u102\7268\hotspot\src\share\vm\prims\

Events (10 events):
Event: 0.063 loading class java/security/UnresolvedPermission
Event: 0.063 loading class java/security/UnresolvedPermission done
Event: 0.064 loading class java/security/BasicPermissionCollection
Event: 0.064 loading class java/security/BasicPermissionCollection done
Event: 0.064 loading class sun/launcher/LauncherHelper$FXHelper
Event: 0.064 loading class sun/launcher/LauncherHelper$FXHelper done
Event: 0.064 loading class java/lang/Class$MethodArray
Event: 0.064 loading class java/lang/Class$MethodArray done
Event: 0.064 loading class java/lang/Void
Event: 0.064 loading class java/lang/Void done


Dynamic libraries:
0x00007ff77e020000 - 0x00007ff77e057000     C:\Program Files\Java\jdk1.8.0_102\bin\java.exe
0x00007ffb13b20000 - 0x00007ffb13ce1000     C:\Windows\SYSTEM32\ntdll.dll
0x00007ffb13110000 - 0x00007ffb131bd000     C:\Windows\system32\KERNEL32.DLL
0x00007ffb10220000 - 0x00007ffb10408000     C:\Windows\system32\KERNELBASE.dll
0x00007ffb13910000 - 0x00007ffb139b7000     C:\Windows\system32\ADVAPI32.dll
0x00007ffb13870000 - 0x00007ffb1390d000     C:\Windows\system32\msvcrt.dll
0x00007ffb12d30000 - 0x00007ffb12d8b000     C:\Windows\system32\sechost.dll
0x00007ffb12f80000 - 0x00007ffb1309c000     C:\Windows\system32\RPCRT4.dll
0x00007ffb13390000 - 0x00007ffb134e6000     C:\Windows\system32\USER32.dll
0x00007ffb12df0000 - 0x00007ffb12f76000     C:\Windows\system32\GDI32.dll
0x00007ffb0bbb0000 - 0x00007ffb0be24000     C:\Windows\WinSxS\amd64_microsoft.windows.common-controls_6595b64144ccf1df_6.0.10586.589_none_a2ddb3caa539acce\COMCTL32.dll
0x00007ffb10ff0000 - 0x00007ffb1126d000     C:\Windows\system32\combase.dll
0x00007ffb10600000 - 0x00007ffb1066a000     C:\Windows\system32\bcryptPrimitives.dll
0x00007ffb13ad0000 - 0x00007ffb13b0b000     C:\Windows\system32\IMM32.DLL
0x000000005b690000 - 0x000000005b761000     C:\Program Files\Java\jdk1.8.0_102\jre\bin\msvcr100.dll
0x000000005b770000 - 0x000000005c00a000     C:\Program Files\Java\jdk1.8.0_102\jre\bin\server\jvm.dll
0x00007ffb13b10000 - 0x00007ffb13b18000     C:\Windows\system32\PSAPI.DLL
0x00007ffb0bee0000 - 0x00007ffb0beea000     C:\Windows\SYSTEM32\VERSION.dll
0x00007ffb0cf30000 - 0x00007ffb0cf39000     C:\Windows\SYSTEM32\WSOCK32.dll
0x00007ffb0e7e0000 - 0x00007ffb0e803000     C:\Windows\SYSTEM32\WINMM.dll
0x00007ffb134f0000 - 0x00007ffb1355b000     C:\Windows\system32\WS2_32.dll
0x0000000002c40000 - 0x0000000002c6c000     C:\Windows\SYSTEM32\WINMMBASE.dll
0x00007ffb101d0000 - 0x00007ffb10213000     C:\Windows\system32\cfgmgr32.dll
0x000000005cd10000 - 0x000000005cd1f000     C:\Program Files\Java\jdk1.8.0_102\jre\bin\verify.dll
0x000000005cad0000 - 0x000000005caf9000     C:\Program Files\Java\jdk1.8.0_102\jre\bin\java.dll
0x000000005ccf0000 - 0x000000005cd06000     C:\Program Files\Java\jdk1.8.0_102\jre\bin\zip.dll
0x00007ffb116c0000 - 0x00007ffb12c1c000     C:\Windows\system32\SHELL32.dll
0x00007ffb10840000 - 0x00007ffb10e85000     C:\Windows\system32\windows.storage.dll
0x00007ffb13330000 - 0x00007ffb13382000     C:\Windows\system32\shlwapi.dll
0x00007ffb10170000 - 0x00007ffb1017f000     C:\Windows\system32\kernel.appcore.dll
0x00007ffb10430000 - 0x00007ffb104e5000     C:\Windows\system32\shcore.dll
0x00007ffb10180000 - 0x00007ffb101cb000     C:\Windows\system32\powrprof.dll
0x00007ffb10140000 - 0x00007ffb10154000     C:\Windows\system32\profapi.dll
0x00000005c9f70000 - 0x00000005c9f88000     C:\Users\Moe\Desktop\Projects\IntelligentCloud\IntelligentCloudLibrary\build\libs\main\shared\main.dll
0x00000003ff750000 - 0x00000003ff76a000     C:\cygwin64\bin\cyggcc_s-seh-1.dll
0x00000003febd0000 - 0x00000003fed22000     C:\cygwin64\bin\cygstdc++-6.dll
0x0000000180040000 - 0x0000000180610000     C:\cygwin64\bin\cygwin1.dll
0x00007ffb0cb00000 - 0x00007ffb0cc8c000     C:\Windows\SYSTEM32\dbghelp.dll

VM Arguments:
jvm_args: -Dfile.encoding=windows-1252 -Duser.country=US -Duser.language=en -Duser.variant 
java_command: Main
java_class_path (initial): C:\Users\Moe\Desktop\Projects\IntelligentCloud\IntelligentCloudApp\build\classes\main;C:\Users\Moe\Desktop\Projects\IntelligentCloud\IntelligentCloudApp\build\resources\main
Launcher Type: SUN_STANDARD

Environment Variables:
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_102
PATH=C:\cygwin64\bin;C:\\PROGRA~1\\rti_connext_dds-5.2.3\;C:\\PROGRA~1\\rti_connext_dds-5.2.3\\bin;C:\Program Files\ArrayFire\v3\lib;C:\Program Files\Java\jdk1.8.0_102\bin
USERNAME=Moe
OS=Windows_NT
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 94 Stepping 3, GenuineIntel



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

OS: Windows 10.0 , 64 bit Build 10586 (10.0.10586.589)

CPU:total 4 (4 cores per cpu, 1 threads per core) family 6 model 94 stepping 3, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3, sse4.1, sse4.2, popcnt, avx, avx2, aes, clmul, erms, rtm, 3dnowpref, lzcnt, tsc, tscinvbit, bmi1, bmi2, adx

Memory: 4k page, physical 16647436k(9117744k free), swap 19137804k(9821056k free)

vm_info: Java HotSpot(TM) 64-Bit Server VM (25.102-b14) for windows-amd64 JRE (1.8.0_102-b14), built on Jun 22 2016 13:15:21 by "java_re" with MS VC++ 10.0 (VS2010)

time: Mon Sep 26 13:31:32 2016
elapsed time: 0 seconds (0d 0h 0m 0s)

1 个答案:

答案 0 :(得分:2)

@RustyX可能是正确的。如果有的话,在单个进程中混合使用多个C ++运行时库的可能性不大。

JVM似乎依赖于Microsoft C ++运行时。添加GNU C ++运行时并不可行。 MinGW的这个页面解决了一些问题:

  

Interoperability of Libraries Created by Different Compiler Brands

     

...

     

即使GNU g ++现在可以链接MSVC C ++库,也可以生成   MSVC ++兼容的库/ DLL,这并不意味着它们会   由于C ++的动态特性,能够在运行时工作。一些   可能的原因是:

     
      
  • 简单名称修改问题,可以通过显式.def文件来规避。
  •   
  • 需要正确编译器选项的不同结构对齐问题(-mms-bitfields,...)。
  •   
  • 基础异常和内存模型的基本冲突:      
        
    • MSVC DLL中的new / delete或malloc / free不会与Cygwin newlib new / delete或malloc / free合作。一个人无法释放空间   这是在一个使用不同的new / malloc的函数中分配的。
    •   
    • MSVC DLL引发的异常不会被Cygwin可执行文件捕获,反之亦然。
    •   
    • 缓慢的GNU SJLJ异常模型(在GCC-3.x及更早版本中使用)与MSVC ++模型兼容,但与新的DWARF2模型兼容(   将由GCC-4.x使用,将是不兼容的。
    •   
  •   

解决不同new / deletemalloc / free实施之间的内存管理兼容性问题和冲突,即使不是不可能,也会很困难。