9

When compiling and running the following code under valgrind, I consistently get "Invalid write of size 4" errors. Is there a clean way of calling JNI_CreateJavaVM() so that valgrind doesn't throw a fit?

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

int main()
{
    JavaVMInitArgs * vm_args = calloc(1, sizeof(JavaVMInitArgs));
    JavaVM * jvm = NULL;
    JNIEnv * env = NULL;

    vm_args->version = JNI_VERSION_1_6;
    vm_args->nOptions = 0;
    vm_args->options = NULL;

    JNI_CreateJavaVM(&jvm, (void **)&env, vm_args);

    return 0;
}

Here is the valgrind command I am running:

valgrind --tool=memcheck --leak-check=yes --num-callers=20 --smc-check=all ./test

A single entry from my loooonng valgrind log:

==9004== Invalid write of size 4
==9004==    at 0x4D5A3C8: ???
==9004==    by 0x4D512CB: ???
==9004==    by 0x423374F: JavaCalls::call_helper(JavaValue*, methodHandle*, JavaCallArguments*, Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004==    by 0x4361B67: os::os_exception_wrapper(void (*)(JavaValue*, methodHandle*, JavaCallArguments*, Thread*), JavaValue*, methodHandle*, JavaCallArguments*, Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004==    by 0x42335AE: JavaCalls::call(JavaValue*, methodHandle, JavaCallArguments*, Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004==    by 0x420F8C5: instanceKlass::call_class_initializer_impl(instanceKlassHandle, Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004==    by 0x420E650: instanceKlass::initialize_impl(instanceKlassHandle, Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004==    by 0x420DB97: instanceKlass::initialize(Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004==    by 0x420E8AB: instanceKlass::initialize_impl(instanceKlassHandle, Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004==    by 0x420DB97: instanceKlass::initialize(Thread*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004==    by 0x440D660: Threads::create_vm(JavaVMInitArgs*, bool*) (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004==    by 0x4265395: JNI_CreateJavaVM (in /usr/java/jdk1.6.0_21/jre/lib/i386/client/libjvm.so)
==9004==    by 0x804845F: main (jvm.c:15)
==9004==  Address 0xbeb54078 is not stack'd, malloc'd or (recently) free'd

Thanks, Chenz

Crazy Chenz
  • 12,650
  • 12
  • 50
  • 62

1 Answers1

3

Since this is an error within the JVM you can just choose to supress it. Valgrind can be configured to supress specific errors. You can find more details here

Raam
  • 10,296
  • 3
  • 26
  • 27
  • The previous answer is correct -- the JVM does many things that valgrind does not approve of. You will need to suppress those errors, while not suppressing errors from the code you want valgrind to check. That is a tedious process of trial and error unfortunately. Using suppressions generated by valgrind can make that easier, but it is by no means painless. If you end up with "too many callers in stack trace" from valgrind, see this post: http://stackoverflow.com/a/11040043/203044 – BillT Jun 14 '12 at 19:33