Skip to content

Commit 2c22d28

Browse files
author
Andrew Lu
committed
8311301: MethodExitTest may fail with stack buffer overrun
Backport-of: 3d813ae39f4422dd47473608eb8911e2483c6c32
1 parent 77331ae commit 2c22d28

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

test/hotspot/jtreg/serviceability/jvmti/vthread/MethodExitTest/libMethodExitTest.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2023, 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
@@ -122,7 +122,9 @@ clear_breakpoint(JNIEnv *jni, const char *methodName,
122122
set_or_clear_breakpoint(jni, JNI_FALSE, methodName, klass, methods, method_count);
123123
}
124124

125-
static long tls_data = 0;
125+
static void* tls_data = 0;
126+
static const void* const tls_data1 = (const void*)0x111;
127+
static const void* const tls_data2 = (const void*)0x222;
126128

127129
static void
128130
breakpoint_hit1(jvmtiEnv *jvmti, JNIEnv* jni,
@@ -149,14 +151,14 @@ breakpoint_hit1(jvmtiEnv *jvmti, JNIEnv* jni,
149151
// Test GetThreadLocalStorage for carrier thread.
150152
LOG("Hit #1: Breakpoint: %s: checking GetThreadLocalStorage on carrier thread: %p\n",
151153
mname, (void*)cthread);
152-
err = jvmti->GetThreadLocalStorage(cthread, (void**)&tls_data);
154+
err = jvmti->GetThreadLocalStorage(cthread, &tls_data);
153155
check_jvmti_status(jni, err, "Breakpoint: error in JVMTI GetThreadLocalStorage");
154156

155-
if (tls_data != 111) {
157+
if (tls_data != tls_data1) {
156158
passed = JNI_FALSE;
157-
LOG("FAILED: GetThreadLocalStorage for carrier thread returned value: %d, expected 111\n\n", (int)tls_data);
159+
LOG("FAILED: GetThreadLocalStorage for carrier thread returned value: %p, expected %p\n\n", tls_data, tls_data1);
158160
} else {
159-
LOG("GetThreadLocalStorage for carrier thread returned value %d as expected\n\n", (int)tls_data);
161+
LOG("GetThreadLocalStorage for carrier thread returned value %p as expected\n\n", tls_data);
160162
}
161163
{
162164
jmethodID method = NULL;
@@ -225,14 +227,14 @@ breakpoint_hit2(jvmtiEnv *jvmti, JNIEnv* jni,
225227
// Test GetThreadLocalStorage for virtual thread.
226228
LOG("Hit #2: Breakpoint: %s: checking GetThreadLocalStorage on virtual thread: %p\n",
227229
mname, (void*)thread);
228-
err = jvmti->GetThreadLocalStorage(thread, (void**)&tls_data);
230+
err = jvmti->GetThreadLocalStorage(thread, &tls_data);
229231
check_jvmti_status(jni, err, "Breakpoint: error in JVMTI GetThreadLocalStorage");
230232

231-
if (tls_data != 222) {
233+
if (tls_data != tls_data2) {
232234
passed = JNI_FALSE;
233-
LOG("FAILED: GetThreadLocalStorage for virtual thread returned value: %d, expected 222\n\n", (int)tls_data);
235+
LOG("FAILED: GetThreadLocalStorage for virtual thread returned value: %p, expected %p\n\n", tls_data, tls_data2);
234236
} else {
235-
LOG("GetThreadLocalStorage for virtual thread returned value %d as expected\n\n", (int)tls_data);
237+
LOG("GetThreadLocalStorage for virtual thread returned value %p as expected\n\n", tls_data);
236238
}
237239
}
238240

@@ -380,26 +382,26 @@ ThreadStart(jvmtiEnv *jvmti, JNIEnv* jni, jthread cthread) {
380382
return; // avoid failures with JVMTI_ERROR_WRONG_PHASE
381383
}
382384
char* tname = get_thread_name(jvmti, jni, cthread);
383-
long loc_tls_data = 0;
385+
void* loc_tls_data = 0;
384386
jvmtiError err;
385387

386388
RawMonitorLocker rml(jvmti, jni, event_mon);
387389

388390
LOG("\nThreadStart: cthread: %p, name: %s\n", (void*)cthread, tname);
389391

390392
// Test SetThreadLocalStorage for carrier thread.
391-
err = jvmti->SetThreadLocalStorage(cthread, (void*)111);
393+
err = jvmti->SetThreadLocalStorage(cthread, tls_data1);
392394
check_jvmti_status(jni, err, "ThreadStart: error in JVMTI SetThreadLocalStorage");
393395

394396
// Test GetThreadLocalStorage for carrier thread.
395-
err = jvmti->GetThreadLocalStorage(cthread, (void**)&loc_tls_data);
397+
err = jvmti->GetThreadLocalStorage(cthread, &loc_tls_data);
396398
check_jvmti_status(jni, err, "ThreadStart: error in JVMTI GetThreadLocalStorage");
397399

398-
if (loc_tls_data != 111) {
400+
if (loc_tls_data != tls_data1) {
399401
passed = JNI_FALSE;
400-
LOG("ThreadStart: FAILED: GetThreadLocalStorage for carrier thread returned value: %d, expected 111\n\n", (int)loc_tls_data);
402+
LOG("ThreadStart: FAILED: GetThreadLocalStorage for carrier thread returned value: %p, expected %p\n\n", loc_tls_data, tls_data1);
401403
} else {
402-
LOG("ThreadStart: GetThreadLocalStorage for carrier thread returned value %d as expected\n\n", (int)loc_tls_data);
404+
LOG("ThreadStart: GetThreadLocalStorage for carrier thread returned value %p as expected\n\n", loc_tls_data);
403405
}
404406
deallocate(jvmti, jni, (void*)tname);
405407
}
@@ -419,7 +421,7 @@ VirtualThreadStart(jvmtiEnv *jvmti, JNIEnv* jni, jthread vthread) {
419421
LOG("\nVirtualThreadStart: %s thread: %p, name: %s\n", virt, (void*)vthread, tname);
420422

421423
// Test SetThreadLocalStorage for virtual thread.
422-
err = jvmti->SetThreadLocalStorage(vthread, (void*)222);
424+
err = jvmti->SetThreadLocalStorage(vthread, tls_data2);
423425
check_jvmti_status(jni, err, "VirtualThreadMount: error in JVMTI SetThreadLocalStorage");
424426

425427
deallocate(jvmti, jni, (void*)tname);
@@ -463,7 +465,7 @@ VirtualThreadMount(jvmtiEnv *jvmti, ...) {
463465
print_frame_event_info(jvmti, jni, thread, method, "VirtualThreadMount", vthread_mounted_count);
464466

465467
// Test SetThreadLocalStorage for virtual thread.
466-
err = jvmti->SetThreadLocalStorage(thread, (void*)222);
468+
err = jvmti->SetThreadLocalStorage(thread, tls_data2);
467469
check_jvmti_status(jni, err, "VirtualThreadMount: error in JVMTI SetThreadLocalStorage");
468470

469471
deallocate(jvmti, jni, (void*)mname);

0 commit comments

Comments
 (0)