Skip to content

Commit c67fc73

Browse files
author
Serguei Spitsyn
committed
8320189: vmTestbase/nsk/jvmti/scenarios/bcinstr/BI02/bi02t001 memory corruption when using -Xcheck:jni
Reviewed-by: lmesnik, amenkov
1 parent d9d00d3 commit c67fc73

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/bcinstr/BI02/bi02t001/bi02t001.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2004, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2004, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -49,7 +49,7 @@ const char* CLASS_NAME = "nsk/jvmti/scenarios/bcinstr/BI02/bi02t001a";
4949
/** callback functions **/
5050

5151
static void JNICALL
52-
ClassFileLoadHook(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
52+
ClassFileLoadHook(jvmtiEnv *jvmti, JNIEnv *jni,
5353
jclass class_being_redefined, jobject loader,
5454
const char* name, jobject protection_domain,
5555
jint class_data_len, const unsigned char* class_data,
@@ -61,18 +61,7 @@ ClassFileLoadHook(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
6161

6262
if (class_being_redefined == nullptr) {
6363
/* sent by class load */
64-
65-
if (!NSK_JNI_VERIFY(jni_env, (*new_class_data_len =
66-
jni_env->GetArrayLength(classBytes)) > 0)) {
67-
nsk_jvmti_setFailStatus();
68-
return;
69-
}
70-
71-
if (!NSK_JNI_VERIFY(jni_env, (*new_class_data = (unsigned char*)
72-
jni_env->GetByteArrayElements(classBytes, nullptr)) != nullptr)) {
73-
nsk_jvmti_setFailStatus();
74-
return;
75-
}
64+
*new_class_data = jni_array_to_jvmti_allocated(jvmti, jni, classBytes, new_class_data_len);
7665
}
7766
}
7867
}

test/hotspot/jtreg/vmTestbase/nsk/jvmti/scenarios/bcinstr/BI03/bi03t001/bi03t001.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2004, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2004, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -49,7 +49,7 @@ const char* CLASS_NAME = "nsk/jvmti/scenarios/bcinstr/BI03/bi03t001a";
4949
/** callback functions **/
5050

5151
static void JNICALL
52-
ClassFileLoadHook(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
52+
ClassFileLoadHook(jvmtiEnv *jvmti, JNIEnv *jni,
5353
jclass class_being_redefined, jobject loader,
5454
const char* name, jobject protection_domain,
5555
jint class_data_len, const unsigned char* class_data,
@@ -62,17 +62,7 @@ ClassFileLoadHook(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
6262
if (class_being_redefined == nullptr) {
6363
/* sent by class load */
6464

65-
if (!NSK_JNI_VERIFY(jni_env, (*new_class_data_len =
66-
jni_env->GetArrayLength(classBytes)) > 0)) {
67-
nsk_jvmti_setFailStatus();
68-
return;
69-
}
70-
71-
if (!NSK_JNI_VERIFY(jni_env, (*new_class_data = (unsigned char*)
72-
jni_env->GetByteArrayElements(classBytes, nullptr)) != nullptr)) {
73-
nsk_jvmti_setFailStatus();
74-
return;
75-
}
65+
*new_class_data = jni_array_to_jvmti_allocated(jvmti, jni, classBytes, new_class_data_len);
7666
}
7767
}
7868
}

test/lib/jdk/test/lib/jvmti/jvmti_common.hpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -447,7 +447,27 @@ static jthread get_current_thread(jvmtiEnv *jvmti, JNIEnv* jni) {
447447
return thread;
448448
}
449449

450+
/* Used in a couple of nsk/jvmti/scenarios tests to convert jbyteArray to a JVMTI allocated */
451+
static unsigned char* jni_array_to_jvmti_allocated(jvmtiEnv *jvmti, JNIEnv *jni, jbyteArray arr, jint* len_ptr) {
452+
unsigned char* new_arr = nullptr;
450453

454+
jint len = jni->GetArrayLength(arr);
455+
if (len <= 0) {
456+
fatal(jni, "JNI GetArrayLength returned a non-positive value");
457+
}
458+
jbyte* jni_arr = jni->GetByteArrayElements(arr, nullptr);
459+
if (jni_arr == nullptr) {
460+
fatal(jni, "JNI GetByteArrayElements returned nullptr");
461+
}
462+
jvmtiError err = jvmti->Allocate(len, &new_arr);
463+
check_jvmti_status(jni, err, "JVMTI Allocate returned an error code");
464+
465+
memcpy(new_arr, jni_arr, (size_t)len);
466+
jni->ReleaseByteArrayElements(arr, jni_arr, JNI_ABORT);
467+
468+
*len_ptr = len;
469+
return new_arr;
470+
}
451471

452472
/* Commonly used helper functions */
453473
const char*

0 commit comments

Comments
 (0)