JVM crashes when using JNI

P

P. Ajay Prakash

Hi Everybody,

I am using the JNI functionality of Java to call methods of the Lucent
RTP stack. These methods contain calls to JNI functions such as :

GetStaticIntField()
GetObjectClass(), etc.
(i will include a native method implementation inside which i am
getting the JVM crashes).

On separate executions of the Java Application, the crash is occurring
at different points (but within the same native function call).
This is highlighted by the printf() statements i have placed at
various points of the code.
-------------------------------------------------------------------------------

Below is the JNI signature of the function in question :

JNIEXPORT void JNICALL
Java_SATestTool_MGRTPSocketThread_ReceiveRTPPacket(JNIEnv *env, jclass
cls, jlong cid, jint socket)

------------------------------------------------------------------------------

And the body of the function :

JNIEXPORT void JNICALL
Java_SATestTool_MGRTPSocketThread_ReceiveRTPPacket(JNIEnv *env, jclass
cls, jlong cid, jint socket)
{
printf("Just entered ReceiveRTPPacket() \n");
rtperror err;
u_int16 port;

char *addr = (char *) malloc(sizeof(char) * 4096);
err = RTPSessionGetReceiveAddr(cid, addr, &port);
printf("*****************AFTER call to
RTPSessionGetReceiveAddr()\n");

if(err != RTP_OK)
{
printf("get Receive Addr FAILED!\n");
}
else
{
printf("The port numbert associated with this context
is : %u\n", port);
}

//obtain the references to mRTPMessage, mDataBuffer and
mBufLength


jfieldID fid1 = (*env)->GetStaticFieldID(env, cls,
"mRTPMessage", "LSATestTool/RTPMessage;");
jobject message = (*env)->GetStaticObjectField(env, cls,
fid1);

jfieldID fid2 = (*env)->GetStaticFieldID(env, cls,
"mDataBuffer", "[C");
jobject data = (*env)->GetStaticObjectField(env, cls, fid2);

jfieldID fid3 = (*env)->GetStaticFieldID(env, cls,
"mBufLength", "I");
jobject length = (*env)->GetStaticObjectField(env, cls, fid3);

err = RTPReceive(cid, socket, data, &length);

if(err != RTP_OK)
{
if(err == RTP_UNKNOWN_CONTEXT)
{
printf("RTP_UNKNOWN_CONTEXT\n");
}
else if(err == RTP_CANT_CALL_FUNCTION)
{
printf("RTP_CANT_CALL_FUNCTION\n");
}
else if(err == RTP_SOCKET_MISMATCH)
{
printf("RTP_SOCKET_MISMATCH\n");
}
else if(err == RTP_INSUFFICIENT_BUFFER)
{
printf("RTP_INSUFFICIENT_BUFFER\n");
}
else if(err == RTP_BAD_PACKET_FORMAT)
{
printf("RTP_BAD_PACKET_FORMAT\n");
}
else if(err == RTP_PACKET_LOOPBACK)
{
printf("RTP_PACKET_LOOPBACK\n");
}
printf("Error occurred during RTPReceive()\n");
return;
}
else
{
printf("After RTPReceive() was SUCCESSFUL\n");
}

rtp_packet packet = RTPGetRTPPacket(data, length);
printf("After RTPGetRTPPacket() \n");

char marker = (packet.RTP_header)->mpt & 0x128;
char mark=0;
if(marker == 0)
mark = 0;
else
mark = 1;

printf("After setting the value of mark temporarily \n");

jclass class = (*env)->GetObjectClass(env, message);
printf("After obtaining class of MESSAGE\n");

jmethodID mid1 = (*env)->GetMethodID(env, class,
"setRtpMarker", "(C)V");
printf("After obtaining MID-ONE\n");
(*env)->CallVoidMethod(env, message, mid1, mark);
printf("After setting MARKER\n");

short codec = (packet.RTP_header)->mpt & 0x127;

jmethodID mid2 = (*env)->GetMethodID(env, class,
"setOutputCodecType", "(S)V");
(*env)->CallVoidMethod(env, message, mid2, codec);
printf("After setting CODEC\n");

jmethodID mid3 = (*env)->GetMethodID(env, class, "setPayload",
"([C)V");
(*env)->CallVoidMethod(env, message, mid3, packet.payload);
printf("After setting PAYLOAD\n");

jmethodID mid4 = (*env)->GetMethodID(env, class,
"setDataLength", "(I)V");
(*env)->CallVoidMethod(env, message, mid4,
packet.payload_len);
printf("After setting LENGTH\n");

(*env)->SetStaticObjectField(env, cls, fid1, message);
printf("After setting STATIC message\n");
(*env)->SetStaticObjectField(env, cls, fid2, data);
printf("After setting STATIC data\n");
(*env)->SetStaticObjectField(env, cls, fid3, length);
printf("After setting STATIC length\n");

printf("After setting in the parent class\n");

//(*env)->ReleaseStringUTFChars(env, socket, sock);
}

-------------------------------------------------------------------------------

The output on 3 separate executions are :

1)

Just entered ReceiveRTPPacket()
*****************AFTER call to RTPSessionGetReceiveAddr()
The port numbert associated with this context is : 1044
After RTPReceive() was SUCCESSFUL
After RTPGetRTPPacket()
After setting the value of mark temporarily
After obtaining class of MESSAGE
After obtaining MID-ONE
After setting MARKER
After setting CODEC
After setting PAYLOAD
After setting LENGTH
After setting STATIC message
After setting STATIC data

2)

Just entered ReceiveRTPPacket()
*****************AFTER call to RTPSessionGetReceiveAddr()
The port numbert associated with this context is : 1044
After RTPReceive() was SUCCESSFUL
After RTPGetRTPPacket()
After setting the value of mark temporarily

3)

Just entered ReceiveRTPPacket()
*****************AFTER call to RTPSessionGetReceiveAddr()
The port numbert associated with this context is : 1044
After RTPReceive() was SUCCESSFUL
After RTPGetRTPPacket()
After setting the value of mark temporarily
After obtaining class of MESSAGE
After obtaining MID-ONE
After setting MARKER
After setting CODEC
After setting PAYLOAD
After setting LENGTH

-------------------------------------------------------------------------------

The Error output by the JRE is :

Unexpected Signal : 11 occurred at PC=0x401EF295
Function=(null)+0x401EF295
Library=/usr/java/j2sdk1.4.2_04/jre/lib/i386/client/libjvm.so

NOTE: We are unable to locate the function name symbol for the error
just occurred. Please refer to release documentation for
possible
reason and solutions.


Current Java thread:
at SATestTool.MGRTPSocketThread.ReceiveRTPPacket(Native
Method)
- locked <0x488b6940> (a java.lang.Class)
at SATestTool.MGRTPSocketThread.receiveMessage(MGRTPSocketThread.java:266)
at SATestTool.MGRTPSocketThread.run(MGRTPSocketThread.java:426)
at java.lang.Thread.run(Thread.java:534)

Dynamic libraries:
08048000-0804e000 r-xp 00000000 03:03 998146
/usr/java/j2sdk1.4.2_04/bin/java
0804e000-0804f000 rw-p 00005000 03:03 998146
/usr/java/j2sdk1.4.2_04/bin/java
40000000-40015000 r-xp 00000000 03:03 1095617 /lib/ld-2.3.2.so
40015000-40016000 rw-p 00014000 03:03 1095617 /lib/ld-2.3.2.so
40017000-4001b000 rw-s 00000000 03:03 474660
/tmp/hsperfdata_ajay/22947
4001b000-4001e000 r--s 00000000 03:03 1014884
/usr/java/j2sdk1.4.2_04/jre/lib/ext/dnsns.jar
4001e000-40028000 r-xp 00000000 03:03 915763
/lib/tls/libpthread-0.29.so
40028000-40029000 rw-p 0000a000 03:03 915763
/lib/tls/libpthread-0.29.so
4002b000-4002d000 r-xp 00000000 03:03 1095628 /lib/libdl-2.3.2.so
4002d000-4002e000 rw-p 00002000 03:03 1095628 /lib/libdl-2.3.2.so
4002f000-4042a000 r-xp 00000000 03:03 916229
/usr/java/j2sdk1.4.2_04/jre/lib/i386/client/libjvm.so
4042a000-40445000 rw-p 003fa000 03:03 916229
/usr/java/j2sdk1.4.2_04/jre/lib/i386/client/libjvm.so
40458000-4046a000 r-xp 00000000 03:03 1095632 /lib/libnsl-2.3.2.so
4046a000-4046b000 rw-p 00011000 03:03 1095632 /lib/libnsl-2.3.2.so
4046d000-4048e000 r-xp 00000000 03:03 915761
/lib/tls/libm-2.3.2.so
4048e000-4048f000 rw-p 00020000 03:03 915761
/lib/tls/libm-2.3.2.so
4048f000-40497000 r-xp 00000000 03:03 949477
/usr/java/j2sdk1.4.2_04/jre/lib/i386/native_threads/libhpi.so
40497000-40498000 rw-p 00007000 03:03 949477
/usr/java/j2sdk1.4.2_04/jre/lib/i386/native_threads/libhpi.so
40498000-404a3000 r-xp 00000000 03:03 1095638
/lib/libnss_files-2.3.2.so
404a3000-404a4000 rw-p 0000a000 03:03 1095638
/lib/libnss_files-2.3.2.so
404a4000-404b4000 r-xp 00000000 03:03 1211043
/usr/java/j2sdk1.4.2_04/jre/lib/i386/libverify.so
404b4000-404b6000 rw-p 0000f000 03:03 1211043
/usr/java/j2sdk1.4.2_04/jre/lib/i386/libverify.so
404b6000-404d6000 r-xp 00000000 03:03 1211029
/usr/java/j2sdk1.4.2_04/jre/lib/i386/libjava.so
404d6000-404d8000 rw-p 0001f000 03:03 1211029
/usr/java/j2sdk1.4.2_04/jre/lib/i386/libjava.so
404d8000-404ec000 r-xp 00000000 03:03 1211044
/usr/java/j2sdk1.4.2_04/jre/lib/i386/libzip.so
404ec000-404ef000 rw-p 00013000 03:03 1211044
/usr/java/j2sdk1.4.2_04/jre/lib/i386/libzip.so
404ef000-41e8f000 r--s 00000000 03:03 50152
/usr/java/j2sdk1.4.2_04/jre/lib/rt.jar
41ed9000-41eef000 r--s 00000000 03:03 50151
/usr/java/j2sdk1.4.2_04/jre/lib/sunrsasign.jar
41eef000-41fca000 r--s 00000000 03:03 50150
/usr/java/j2sdk1.4.2_04/jre/lib/jsse.jar
41fca000-41fdb000 r--s 00000000 03:03 50142
/usr/java/j2sdk1.4.2_04/jre/lib/jce.jar
41ff9000-41fff000 r-xp 00000000 03:03 1211041
/usr/java/j2sdk1.4.2_04/jre/lib/i386/libnio.so
41fff000-42000000 rw-p 00005000 03:03 1211041
/usr/java/j2sdk1.4.2_04/jre/lib/i386/libnio.so
42000000-4212e000 r-xp 00000000 03:03 915759
/lib/tls/libc-2.3.2.so
4212e000-42131000 rw-p 0012e000 03:03 915759
/lib/tls/libc-2.3.2.so
42133000-4268c000 r--s 00000000 03:03 50143
/usr/java/j2sdk1.4.2_04/jre/lib/charsets.jar
44734000-44737000 r-xp 00000000 03:05 65040
/export/home/ajay/TestTool/IntSATestTool/native/libSATestTool.so
44737000-44738000 rw-p 00003000 03:05 65040
/export/home/ajay/TestTool/IntSATestTool/native/libSATestTool.so
44738000-4473e000 r-xp 00000000 03:05 211158
/export/home/ajay/TestTool/IntSATestTool/cots/rtpstack/obj/librtpunix.so
4473e000-4473f000 rw-p 00005000 03:05 211158
/export/home/ajay/TestTool/IntSATestTool/cots/rtpstack/obj/librtpunix.so
4c922000-4cb22000 r--p 00000000 03:03 523277
/usr/lib/locale/locale-archive
4cd22000-4cd2f000 r--s 00000000 03:03 1014885
/usr/java/j2sdk1.4.2_04/jre/lib/ext/ldapsec.jar
4cd2f000-4cdeb000 r--s 00000000 03:03 1014892
/usr/java/j2sdk1.4.2_04/jre/lib/ext/localedata.jar
4cdeb000-4ce07000 r--s 00000000 03:03 1014887
/usr/java/j2sdk1.4.2_04/jre/lib/ext/sunjce_provider.jar
4d007000-4d017000 r-xp 00000000 03:03 1211040
/usr/java/j2sdk1.4.2_04/jre/lib/i386/libnet.so
4d017000-4d018000 rw-p 0000f000 03:03 1211040
/usr/java/j2sdk1.4.2_04/jre/lib/i386/libnet.so
4d118000-4d127000 r-xp 00000000 03:05 211197
/export/home/ajay/TestTool/IntSATestTool/cots/rtpstack/obj/librtp.so
4d127000-4d128000 rw-p 0000f000 03:05 211197
/export/home/ajay/TestTool/IntSATestTool/cots/rtpstack/obj/librtp.so

Heap at VM Abort:
Heap
def new generation total 576K, used 71K [0x44740000, 0x447e0000,
0x44c20000)
eden space 512K, 9% used [0x44740000, 0x4474bf00, 0x447c0000)
from space 64K, 37% used [0x447c0000, 0x447c6008, 0x447d0000)
to space 64K, 0% used [0x447d0000, 0x447d0000, 0x447e0000)
tenured generation total 1408K, used 253K [0x44c20000, 0x44d80000,
0x48740000)
the space 1408K, 18% used [0x44c20000, 0x44c5f7b8, 0x44c5f800,
0x44d80000)
compacting perm gen total 4096K, used 2887K [0x48740000, 0x48b40000,
0x4c740000)
the space 4096K, 70% used [0x48740000, 0x48a11d88, 0x48a11e00,
0x48b40000)

Local Time = Mon Jul 26 17:06:51 2004
Elapsed Time = 102
#
# HotSpot Virtual Machine Error : 11
# Error ID : 4F530E43505002EF
# Please report this error at
# http://java.sun.com/cgi-bin/bugreport.cgi
#
# Java VM: Java HotSpot(TM) Client VM (1.4.2_04-b05 mixed mode)
#
# An error report file has been saved as hs_err_pid22947.log.
# Please refer to the file for further information.
#
Aborted

-------------------------------------------------------------------------------

The environment specification :

1. Platform : Linux sa-tt1 2.4.20-8 #1 Thu Mar 13 17:54:28 EST 2003
i686 i686 i386 GNU/Linux

2. OS version : Red Hat Linux release 9 (Shrike)

3. Java Version :
java version "1.4.2_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)

4. Third party libraries used : Lucent RTP Stack
------------------------------------------------------------------------------
I would appreciate it very much if anyone could give any pointers to
as to what might be the problem.

Thanks and Regards,
Ajay
 
K

Kevin McMurtrie

Hi Everybody,

I am using the JNI functionality of Java to call methods of the Lucent
RTP stack. These methods contain calls to JNI functions such as :

GetStaticIntField()
GetObjectClass(), etc.
(i will include a native method implementation inside which i am
getting the JVM crashes).

On separate executions of the Java Application, the crash is occurring
at different points (but within the same native function call).
This is highlighted by the printf() statements i have placed at
various points of the code.
------------------------------------------------------------------------------
-

Below is the JNI signature of the function in question :

JNIEXPORT void JNICALL
Java_SATestTool_MGRTPSocketThread_ReceiveRTPPacket(JNIEnv *env, jclass
cls, jlong cid, jint socket)

------------------------------------------------------------------------------

And the body of the function :

JNIEXPORT void JNICALL
Java_SATestTool_MGRTPSocketThread_ReceiveRTPPacket(JNIEnv *env, jclass
cls, jlong cid, jint socket)
{
printf("Just entered ReceiveRTPPacket() \n");
rtperror err;
u_int16 port;

char *addr = (char *) malloc(sizeof(char) * 4096);
err = RTPSessionGetReceiveAddr(cid, addr, &port);
printf("*****************AFTER call to
RTPSessionGetReceiveAddr()\n");

if(err != RTP_OK)
{
printf("get Receive Addr FAILED!\n");
}
else
{
printf("The port numbert associated with this context
is : %u\n", port);
}

//obtain the references to mRTPMessage, mDataBuffer and
mBufLength


jfieldID fid1 = (*env)->GetStaticFieldID(env, cls,
"mRTPMessage", "LSATestTool/RTPMessage;");
jobject message = (*env)->GetStaticObjectField(env, cls,
fid1);

jfieldID fid2 = (*env)->GetStaticFieldID(env, cls,
"mDataBuffer", "[C");
jobject data = (*env)->GetStaticObjectField(env, cls, fid2);

jfieldID fid3 = (*env)->GetStaticFieldID(env, cls,
"mBufLength", "I");
jobject length = (*env)->GetStaticObjectField(env, cls, fid3);

err = RTPReceive(cid, socket, data, &length);

if(err != RTP_OK)
{
if(err == RTP_UNKNOWN_CONTEXT)
{
printf("RTP_UNKNOWN_CONTEXT\n");
}
else if(err == RTP_CANT_CALL_FUNCTION)
{
printf("RTP_CANT_CALL_FUNCTION\n");
}
else if(err == RTP_SOCKET_MISMATCH)
{
printf("RTP_SOCKET_MISMATCH\n");
}
else if(err == RTP_INSUFFICIENT_BUFFER)
{
printf("RTP_INSUFFICIENT_BUFFER\n");
}
else if(err == RTP_BAD_PACKET_FORMAT)
{
printf("RTP_BAD_PACKET_FORMAT\n");
}
else if(err == RTP_PACKET_LOOPBACK)
{
printf("RTP_PACKET_LOOPBACK\n");
}
printf("Error occurred during RTPReceive()\n");
return;
}
else
{
printf("After RTPReceive() was SUCCESSFUL\n");
}

rtp_packet packet = RTPGetRTPPacket(data, length);
printf("After RTPGetRTPPacket() \n");

char marker = (packet.RTP_header)->mpt & 0x128;
char mark=0;
if(marker == 0)
mark = 0;
else
mark = 1;

printf("After setting the value of mark temporarily \n");

jclass class = (*env)->GetObjectClass(env, message);
printf("After obtaining class of MESSAGE\n");

jmethodID mid1 = (*env)->GetMethodID(env, class,
"setRtpMarker", "(C)V");
printf("After obtaining MID-ONE\n");
(*env)->CallVoidMethod(env, message, mid1, mark);
printf("After setting MARKER\n");

short codec = (packet.RTP_header)->mpt & 0x127;

jmethodID mid2 = (*env)->GetMethodID(env, class,
"setOutputCodecType", "(S)V");
(*env)->CallVoidMethod(env, message, mid2, codec);
printf("After setting CODEC\n");

jmethodID mid3 = (*env)->GetMethodID(env, class, "setPayload",
"([C)V");
(*env)->CallVoidMethod(env, message, mid3, packet.payload);
printf("After setting PAYLOAD\n");

jmethodID mid4 = (*env)->GetMethodID(env, class,
"setDataLength", "(I)V");
(*env)->CallVoidMethod(env, message, mid4,
packet.payload_len);
printf("After setting LENGTH\n");

(*env)->SetStaticObjectField(env, cls, fid1, message);
printf("After setting STATIC message\n");
(*env)->SetStaticObjectField(env, cls, fid2, data);
printf("After setting STATIC data\n");
(*env)->SetStaticObjectField(env, cls, fid3, length);
printf("After setting STATIC length\n");

printf("After setting in the parent class\n");

//(*env)->ReleaseStringUTFChars(env, socket, sock);
}

You're mixing up Java object references with native primitives. The
actual Java implementation of object references varies but it's
extremely unlikely for your code to work on any JVM. You need to call
the JNI conversion utilities to produce native primitives:

GetObjectArrayElement
SetObjectArrayElement
GetCharArrayElements / ReleaseCharArrayElements
GetIntArrayElements / ReleaseIntArrayElements
GetStringUTFChars / ReleaseStringUTFChars

etc.

The output on 3 separate executions are :

[snip]

I would appreciate it very much if anyone could give any pointers to
as to what might be the problem.

Thanks and Regards,
Ajay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,283
Latest member
JoannaGrif

Latest Threads

Top