valgrinding GCJ Java code

Tom Schutter tom@platte.com
Thu Jun 17 17:08:00 GMT 2004


I have a mixed Java/C app that I am trying to run valgrind 
(http://valgrind.kde.org) on.  The object of this exercise is to find a 
difficult-to-reproduce crash that is related to JNI and garbage 
collection.  Most of the C code is valgrind clean, it is when it is used 
from JNI that I am having problems.

It is obvious to me that valgrind will not be able to run a standard 
JVM, so I started looking into GCJ.  GCJ will generate standard object 
code that I should be able to run valgrind on.  But I am stuck at the 
starting gate.

Valgrind hangs when I try to initialize the GCJ Java runtime.  It hangs 
with both of the alternatives that GCJ provides, namely JvCreateJavaVM() 
and JNI_CreateJavaVM(), although I suspect that one is just a wrapper 
around the other.

Debian testing/unstable
Linux rota 2.6.3-1-686 #2 Tue Feb 24 20:24:38 EST 2004 i686 GNU/Linux
ii  valgrind       2.1.1-3        A memory debugger for x86-linux
ii  gcj-3.4        3.4.0-1        The GNU compiler for Java(TM)
ii  gcc-3.4        3.4.0-1        The GNU C compiler

I have two easy test cases that follow.  Both generated executables that 
run, but when run under valgrind, they hang (using 100% cpu) when 
initializing the Java runtime.

/* vg_cni.cc
  * compile with:
  *   gcc-3.4 vg_cni.cc -o vg_cni -lgcj
  */

#include <gcj/cni.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
   jint cni_error;

   /* Initialize the Java runtime. */
   printf("%s:%i\n", __FILE__, __LINE__);
   cni_error = JvCreateJavaVM(NULL);
   if (cni_error < 0) {
     printf(
       "ERROR: Unable to initialize the Java runtime, (%i)\n",
       (int) cni_error
     );
     return(EXIT_FAILURE);
   }

   printf("%s:%i\n", __FILE__, __LINE__);
   JvAttachCurrentThread(NULL, NULL);

   printf("%s:%i\n", __FILE__, __LINE__);
   JvDetachCurrentThread();

   printf("%s:%i\n", __FILE__, __LINE__);
   argc = argc;
   argv = argv;
   return(EXIT_SUCCESS);
}


/* vg_jni.c
  * compile with:
  *   gcc-3.4 vg_jni.c -o vg_jni -lgcj
  */

#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {
   JavaVMInitArgs vm_args;
   JNIEnv *env = NULL;
   JavaVM *jvm = NULL;
   jint jni_error;

   /* Setup the vm_args structure. */
   memset(&vm_args, 0, sizeof(vm_args));
   vm_args.version = JNI_VERSION_1_2;

   /* Create the Java Virtual Machine. */
   printf("%s:%i\n", __FILE__, __LINE__);
   jni_error = JNI_CreateJavaVM(&jvm, (void **) &env, &vm_args);
   if (jni_error < 0) {
     printf(
       "ERROR: Unable to create Java Virtual Machine, (%i)\n",
       (int) jni_error
     );
     return(EXIT_FAILURE);
   }

   printf("%s:%i\n", __FILE__, __LINE__);
   if ((*env)->ExceptionOccurred(env)) {
     printf("exception occurred %s:%i\n", __FILE__, __LINE__);
   }

   printf("%s:%i\n", __FILE__, __LINE__);
   (*jvm)->DestroyJavaVM(jvm);

   printf("%s:%i\n", __FILE__, __LINE__);
   argc = argc;
   argv = argv;
   return(EXIT_SUCCESS);
}


$ valgrind vg_cni
==5831== Memcheck, a memory error detector for x86-linux.
==5831== Copyright (C) 2002-2004, and GNU GPL'd, by Julian Seward.
==5831== Using valgrind-2.1.1, a program supervision framework for 
x86-linux.
==5831== Copyright (C) 2000-2004, and GNU GPL'd, by Julian Seward.
==5831== For more details, rerun with: -v
==5831==
vg_cni.cc:14
==5831== Syscall param sigaction(act) contains uninitialised or 
unaddressable byte(s)
==5831==    at 0x3CA36025: __libc_sigaction (in /lib/tls/libc-2.3.2.so)
==5831==  Address 0x4FFFE830 is on thread 1's stack
==5831==
==5831== Syscall param sigaction(act) contains uninitialised or 
unaddressable byte(s)
==5831==    at 0x3CA36025: __libc_sigaction (in /lib/tls/libc-2.3.2.so)
==5831==    by 0x7FFFFFFB: ???
==5831==  Address 0x4FFFE780 is on thread 1's stack
==5831==
==5831== Conditional jump or move depends on uninitialised value(s)
==5831==    at 0x3C646535: GC_push_all_eager (in /usr/lib/libgcj.so.5.0.0)
==5831==    by 0x3C6478C9: GC_push_current_stack (in 
/usr/lib/libgcj.so.5.0.0)
==5831==    by 0x3C64E12D: GC_generic_push_regs (in 
/usr/lib/libgcj.so.5.0.0)
==5831==    by 0x3C6479FD: GC_push_roots (in /usr/lib/libgcj.so.5.0.0)
==5831==
==5831== Conditional jump or move depends on uninitialised value(s)
==5831==    at 0x3C64653A: GC_push_all_eager (in /usr/lib/libgcj.so.5.0.0)
==5831==    by 0x3C6478C9: GC_push_current_stack (in 
/usr/lib/libgcj.so.5.0.0)
==5831==    by 0x3C64E12D: GC_generic_push_regs (in 
/usr/lib/libgcj.so.5.0.0)
==5831==    by 0x3C6479FD: GC_push_roots (in /usr/lib/libgcj.so.5.0.0)
==5831==
==5831== Conditional jump or move depends on uninitialised value(s)
==5831==    at 0x3C646535: GC_push_all_eager (in /usr/lib/libgcj.so.5.0.0)
==5831==    by 0x3C6465B9: GC_push_all_stack (in /usr/lib/libgcj.so.5.0.0)
==5831==    by 0x3C64DB96: GC_push_all_stacks (in /usr/lib/libgcj.so.5.0.0)
==5831==    by 0x3C649815: GC_default_push_other_roots (in 
/usr/lib/libgcj.so.5.0.0)
==5831==
==5831== Syscall param sigaction(act) contains uninitialised or 
unaddressable byte(s)
==5831==    at 0x3CAE551D: syscall (in /lib/tls/libc-2.3.2.so)
==5831==    by 0x8048736: JvCreateJavaVM(void*) (in 
/home/tom/tmp/valgrind_gcj/vg_cni)
==5831==    by 0x8048670: main (in /home/tom/tmp/valgrind_gcj/vg_cni)
==5831==  Address 0x4FFFE9B0 is on thread 1's stack
==5831==
==5831== Conditional jump or move depends on uninitialised value(s)
==5831==    at 0x3C64D25C: GC_pthread_create (in /usr/lib/libgcj.so.5.0.0)
==5831==    by 0x3C472162: _Jv_ThreadStart(java::lang::Thread*, 
_Jv_Thread_t*, void (*)(java::lang::Thread*)) (in /usr/lib/libgcj.so.5.0.0)
==5831==    by 0x3C49E6A6: java::lang::Thread::start() (in 
/usr/lib/libgcj.so.5.0.0)
==5831==    by 0x3C473E09: _Jv_CreateJavaVM(void*) (in 
/usr/lib/libgcj.so.5.0.0)==5831==
==5831== Thread 2:
==5831== Conditional jump or move depends on uninitialised value(s)
==5831==    at 0x3CB4CFF1: thread_wrapper (vg_libpthread.c:824)
==5831==    by 0xB800FACC: do__quit (vg_scheduler.c:1792)
==5831==
==5831== Thread 2:
==5831== thread_wrapper: invalid attr->__detachstate
==5831==    at 0x3CB4C00F: pthread_error (vg_libpthread.c:355)
==5831==    by 0x3CB4D000: thread_wrapper (vg_libpthread.c:826)
==5831==    by 0xB800FACC: do__quit (vg_scheduler.c:1792)
==5831==
==5831== Thread 2:
==5831== Conditional jump or move depends on uninitialised value(s)
==5831==    at 0x3CB4D004: thread_wrapper (vg_libpthread.c:827)
==5831==    by 0xB800FACC: do__quit (vg_scheduler.c:1792)
------------------------hangs here-----------------------------

-- 
Tom Schutter (mailto:tom@platte.com)
Platte River Associates, Inc. (http://www.platte.com)



More information about the Java mailing list