Java JNI 在某些具有特定签名的 C 函数上崩溃

标签 java c java-native-interface swig

我正在测试我的 JNI dll,但在对基于 C 的函数执行某些调用时遇到系统崩溃。有些调用有效(参见下面的 getSession 1-5),有些会导致崩溃(参见下面的 getSession 6-8)。以下是我的测试结果列表。我已经从 C 函数中删除了所有逻辑,只返回一个 int。关于如何进一步解决此问题的任何想法?

我的 JNI 和 C 库 dll 正在 Linux 32 位上交叉编译,使用这些 make file contents .这些问题不会出现在 Linux 上,只会出现在 Windows XP 上。我正在使用 SWIG 生成我的 JNI。

造成崩溃的模式似乎是 C 函数的指针参数多于非指针参数。如果非指针参数与指针参数的比率相等或非指针参数 > 指针参数或只有一个指针参数而没有任何其他参数,则不会发生崩溃。

Java JNI 签名:

public final static native int getSession1(String jarg1); //works
public final static native int getSession2(int jarg1); //works
public final static native int getSession3(String jarg1, int jarg2); // works
public final static native int getSession4(int jarg1, int jarg2); //works
public final static native int getSession5(int jarg1, String jarg2, String jarg3, int jarg4); //works

public final static native int getSession6(String jarg1, String jarg2); //causes system failure, see below for stack details
public final static native int getSession7(String jarg1, String jarg2, int jarg3, String jarg4); //causes system failure
public final static native int getSession8(String jarg1, String jarg2, String jarg3); //causes system failure

C 函数 header 签名:

int getSession1(char *arg1); //works
int getSession2(int arg1); //works
int getSession3(char *ag1, int arg2); //works
int getSession4(int arg1, int arg2); //works
int getSession5(int arg1, char *agr2, char *arg3, int arg4); //works

int getSession6(char *arg1, char *arg2); //crashes
int getSession7(char *arg1, char *arg2, int arg3, char *arg4); //crashes
int getSession8(char *arg1, char *arg2, char *arg3);

JRE 系统故障日志(为清楚起见删除了一些内容):

#
# A fatal error has been detected by the Java Runtime Environment
#
#  EXCEPTION_SINGLE_STEP (0x80000004) at pc=0x7c90eaf0, pid=2704, tid=2396
#
# JRE version: 6.0_30-b12
# Java VM: Java HotSpot(TM) Client VM (20.5-b03 mixed mode, sharing windows-x86 )
# Problematic frame:
# C  [ntdll.dll+0xeaf0]

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

Current thread (0x003a6400):  JavaThread "main" [_thread_in_native, id=2396, stack(0x00990000,0x009e0000)]

siginfo: ExceptionCode=0x80000004

Registers:
EAX=0x003a654c, EBX=0x3278fc38, ECX=0x009df9a8, EDX=0x00000000
ESP=0x009df984, EBP=0x009dfcbc, ESI=0x3278fc38, EDI=0x003a6400
EIP=0x7c90eaf0, EFLAGS=0x00204602

Top of Stack: (sp=0x009df984)
0x009df984:   009df98c 009df9a8 c0000005 00000000
0x009df994:   00000000 009dfcc7 00000002 00000001
0x009df9a4:   0000003e 0001003f 00000000 00000000
0x009df9b4:   00000000 00000000 00000000 00000000
0x009df9c4:   ffff027f ffff0000 ffffffff 00a0fb5a
0x009df9d4:   073c001b 009dfc90 ffff0023 0008fb30
0x009df9e4:   7c911538 2ca81596 00120003 2d280000
0x009df9f4:   00030000 7c910732 00000004 26400003 

Instructions: (pc=0x7c90eaf0)
0x7c90ead0:   83 c4 04 5a 64 a1 18 00 00 00 8b 40 30 8b 40 2c
0x7c90eae0:   ff 14 90 33 c9 33 d2 cd 2b cc 8b ff 8b 4c 24 04
0x7c90eaf0:   8b 1c 24 51 53 e8 c7 8c 02 00 0a c0 74 0c 5b 59
0x7c90eb00:   6a 00 51 e8 11 eb ff ff eb 0b 5b 59 6a 00 51 53 


Register to memory mapping:

EAX=0x003a654c is an unknown value
EBX=0x3278fc38 is an oop
{method} 
 - klass: {other class}
ECX=0x009df9a8 is pointing into the stack for thread: 0x003a6400
EDX=0x00000000 is an unknown value
ESP=0x009df984 is pointing into the stack for thread: 0x003a6400
EBP=0x009dfcbc is pointing into the stack for thread: 0x003a6400
ESI=0x3278fc38 is an oop
{method} 
 - klass: {other class}
EDI=0x003a6400 is a thread


Stack: [0x00990000,0x009e0000],  sp=0x009df984,  free space=318k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [ntdll.dll+0xeaf0]  KiUserExceptionDispatcher+0x4

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  com.test.jni.SampleJNI.getSession6(Ljava/lang/String;Ljava/lang/String;)I+0
j  com.test.jni.Sample.getSession6(Ljava/lang/String;Ljava/lang/String;)I+2
j  com.test.jni.Example.main([Ljava/lang/String;)V+605
v  ~StubRoutines::call_stub

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

Java Threads: ( => current thread )
  0x02c48c00 JavaThread "Low Memory Detector" daemon [_thread_blocked, id=2748, stack(0x02ec0000,0x02f10000)]
  0x02c43800 JavaThread "C1 CompilerThread0" daemon [_thread_blocked, id=2372, stack(0x02e70000,0x02ec0000)]
  0x02c41400 JavaThread "Attach Listener" daemon [_thread_blocked, id=2484, stack(0x02e20000,0x02e70000)]
  0x02c3fc00 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=3800, stack(0x02dd0000,0x02e20000)]
  0x02c3bc00 JavaThread "Finalizer" daemon [_thread_blocked, id=2512, stack(0x02d80000,0x02dd0000)]
  0x02c37000 JavaThread "Reference Handler" daemon [_thread_blocked, id=3716, stack(0x02d30000,0x02d80000)]
=>0x003a6400 JavaThread "main" [_thread_in_native, id=2396, stack(0x00990000,0x009e0000)]

Other Threads:
  0x02bfa400 VMThread [stack: 0x02ce0000,0x02d30000] [id=2052]
  0x02c4bc00 WatcherThread [stack: 0x02f10000,0x02f60000] [id=3304]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap
 def new generation   total 4864K, used 495K [0x22970000, 0x22eb0000, 0x27e10000)
  eden space 4352K,  11% used [0x22970000, 0x229ebf60, 0x22db0000)
  from space 512K,   0% used [0x22db0000, 0x22db0000, 0x22e30000)
  to   space 512K,   0% used [0x22e30000, 0x22e30000, 0x22eb0000)
 tenured generation   total 10880K, used 0K [0x27e10000, 0x288b0000, 0x32770000)
   the space 10880K,   0% used [0x27e10000, 0x27e10000, 0x27e10200, 0x288b0000)
 compacting perm gen  total 12288K, used 156K [0x32770000, 0x33370000, 0x36770000)
   the space 12288K,   1% used [0x32770000, 0x327970a0, 0x32797200, 0x33370000)
    ro space 10240K,  51% used [0x36770000, 0x36c9da30, 0x36c9dc00, 0x37170000)
    rw space 12288K,  55% used [0x37170000, 0x37809b50, 0x37809c00, 0x37d70000)

Code Cache  [0x00a00000, 0x00a70000, 0x02a00000)
 total_blobs=136 nmethods=5 adapters=68 free_code_cache=33101824 largest_free_block=0

Dynamic libraries:

VM Arguments:


Environment Variables:



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

OS: Windows XP Build 2600 Service Pack 2

CPU:total 4 (2 cores per cpu, 2 threads per core) family 6 model 28 stepping 2, cmov, cx8, fxsr, mmx, sse, sse2, sse3, ssse3, ht

Memory: 4k page, physical 1038156k(614332k free), swap 1713032k(1398020k free)

vm_info: Java HotSpot(TM) Client VM (20.5-b03) for windows-x86 JRE (1.6.0_30-b12), built on Nov 10 2011 01:51:00 by "java_re" with MS VC++ 7.1 (VS2003)

time: Thu Jan 05 15:06:54 2012
elapsed time: 0 seconds

最佳答案

猜猜这是上面代码片段中 getSession7 和 getSession6 签名互换的拼写错误?从堆栈跟踪“getSession7”中查看签名似乎只需要 2 个参数 getSession7(Ljava/lang/String;Ljava/lang/String;)

关于Java JNI 在某些具有特定签名的 C 函数上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8752194/

相关文章:

java - 从服务更新 textView 的最简单方法是什么?

java - 线程声明

java - 尝试使用 JNI : libjvm. 在 C++ 代码中使用 jar 时出错,因此未找到

java - System.load 给出不满意的链接错误

java - 动态绑定(bind)适用于方法。为什么不为其他成员[变量]?

java - Jmeter 2.11 : Getting Peer Not Authenticated

c - 在 C/C++ 中只获取一个简单的数字

c - 从 C 向 GenServer 发送消息

c - 如何根据给定地址初始化结构体?

android - 每次调用 JNI 方法时附加值