如何调用 JNI_CreateJavaVM 而不会出现 Valgrind 错误?

发布于 2024-10-19 21:42:08 字数 2411 浏览 4 评论 0原文

在 valgrind 下编译并运行以下代码时,我始终收到“Invalid write of size 4”错误。有没有一种干净的方法来调用 JNI_CreateJavaVM() 以便 valgrind 不会出现异常?

#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;
}

这是我正在运行的 valgrind 命令:

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

来自我的 loooonng valgrind 日志的单个条目:

==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

谢谢, 陈兹

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把时间冻结 2024-10-26 21:42:08

由于这是 JVM 内的错误,您可以选择抑制它。 Valgrind 可以配置为抑制特定错误。您可以在此处找到更多详细信息

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文