Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/GetLocalVariable/libGetLocalWithoutSuspendTest.cpp
41153 views
1
/*
2
* Copyright (c) 2020 SAP SE. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
#include <stdio.h>
25
#include <string.h>
26
#include "jvmti.h"
27
#include "jni.h"
28
29
30
//
31
// Please also read the @comment in GetLocalWithoutSuspendTest.java
32
//
33
34
extern "C" {
35
36
////////////////////////////////////////////////////
37
// BEGIN: Shared Variables
38
// The following variables are shared between agent and target thread
39
40
// glws_monitor is used to synchronize access to shared variables.
41
static jrawMonitorID glws_monitor;
42
43
// Target thread for agent operations.
44
static jthread target_thread = NULL;
45
46
// Depth of the frame for GetLocalObject() call by the agent thread. It is set by the target thread.
47
// -1 is the signal to shut down.
48
static int depth_for_get_local = 0;
49
50
enum TestState {
51
Initial,
52
53
TargetInNative, // The agent waits for the target thread to reach
54
// the native method notifyAgentToGetLocal. Then it
55
// reads depth_for_get_local and changes the state
56
// to AgentInGetLocal. After that it
57
// calls GetLocalObject().
58
59
AgentInGetLocal, // The target thread waits for the agent to call
60
// GetLocalObject(). When this state is reached it
61
// resets the state to Initial and returns from
62
// native after a short spin wait racing the agent
63
// thread doing the unsafe stack walk.
64
65
ShutDown,
66
67
Terminated
68
};
69
70
// Current test state. It is used to synchronize agent and target thread execution.
71
static TestState test_state;
72
73
// END: Shared Variables
74
////////////////////////////////////////////////////
75
76
77
// Dummy counter used in spin wait. It is declared volatile to prevent the compiler
78
// from eliminating the whole spin loop.
79
static volatile int dummy_counter = 0;
80
81
// Makes a string of the argument (which is not macro-expanded)
82
#define STR(a) #a
83
84
// Makes a string of the macro expansion of a
85
#define XSTR(a) STR(a)
86
87
#define AT_LINE " ERROR at line " XSTR(__LINE__)
88
89
static jvmtiEnv* jvmti = NULL;
90
91
static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);
92
93
static char* GetErrorMessage(jvmtiEnv *jvmti, jvmtiError errCode) {
94
char *errMsg;
95
jvmtiError result = jvmti->GetErrorName(errCode, &errMsg);
96
return result == JVMTI_ERROR_NONE ? errMsg : NULL;
97
}
98
99
static void ShowErrorMessage(jvmtiEnv *jvmti, jvmtiError errCode, const char *message) {
100
char* errMsg = GetErrorMessage(jvmti, errCode);
101
102
if (errMsg != NULL) {
103
fprintf(stderr, "AGENT: %s: %s (%d)\n", message, errMsg, errCode);
104
jvmti->Deallocate((unsigned char *)errMsg);
105
} else {
106
fprintf(stderr, "AGENT: %s (%d)\n", message, errCode);
107
}
108
}
109
110
static void monitor_enter(jvmtiEnv* jvmti, JNIEnv* env, const char* loc) {
111
jvmtiError err = jvmti->RawMonitorEnter(glws_monitor);
112
if (err != JVMTI_ERROR_NONE) {
113
ShowErrorMessage(jvmti, err, loc);
114
env->FatalError(loc);
115
}
116
}
117
118
static void monitor_exit(jvmtiEnv* jvmti, JNIEnv* env, const char* loc) {
119
jvmtiError err = jvmti->RawMonitorExit(glws_monitor);
120
if (err != JVMTI_ERROR_NONE) {
121
ShowErrorMessage(jvmti, err, loc);
122
env->FatalError(loc);
123
}
124
}
125
126
static void monitor_wait(jvmtiEnv* jvmti, JNIEnv* env, const char* loc) {
127
jvmtiError err = jvmti->RawMonitorWait(glws_monitor, 0);
128
if (err != JVMTI_ERROR_NONE) {
129
ShowErrorMessage(jvmti, err, loc);
130
env->FatalError(loc);
131
}
132
}
133
134
static void monitor_notify(jvmtiEnv* jvmti, JNIEnv* env, const char* loc) {
135
jvmtiError err = jvmti->RawMonitorNotify(glws_monitor);
136
if (err != JVMTI_ERROR_NONE) {
137
ShowErrorMessage(jvmti, err, loc);
138
env->FatalError(loc);
139
}
140
}
141
142
static void monitor_destroy(jvmtiEnv* jvmti, JNIEnv* env, const char* loc) {
143
jvmtiError err = jvmti->DestroyRawMonitor(glws_monitor);
144
if (err != JVMTI_ERROR_NONE) {
145
ShowErrorMessage(jvmti, err, loc);
146
env->FatalError(loc);
147
}
148
}
149
150
// Perform GetLocalObject() at the requested depth while target thread is running.
151
// Note that the JVMTI spec does not require to suspend the target thread.
152
void test_GetLocalObject(jvmtiEnv* jvmti, JNIEnv* env, int depth) {
153
jvmtiError err;
154
jobject obj;
155
char* errMsg;
156
157
printf("AGENT: calling GetLocalObject() with depth %d\n", depth);
158
err = jvmti->GetLocalObject(target_thread, depth, 0, &obj);
159
errMsg = GetErrorMessage(jvmti, err);
160
printf("AGENT: GetLocalObject() result code %s (%d)\n", errMsg != NULL ? errMsg : "N/A", err);
161
if (errMsg != NULL) {
162
jvmti->Deallocate((unsigned char *)errMsg);
163
}
164
fflush(stdout);
165
166
// If the target thread wins the race we can get errors because we
167
// don't find a frame at the given depth or we find a non-java frame
168
// there (e.g. native frame). This is expected.
169
// JVMTI_ERROR_INVALID_SLOT can occur also because the target thread is
170
// running and the GetLocalObject() call might coincidentally refer to the
171
// frame of a static method without parameters.
172
if (err != JVMTI_ERROR_NONE &&
173
err != JVMTI_ERROR_NO_MORE_FRAMES &&
174
err != JVMTI_ERROR_OPAQUE_FRAME &&
175
err != JVMTI_ERROR_INVALID_SLOT) {
176
ShowErrorMessage(jvmti, err, "AgentThreadLoop: error in JVMTI GetLocalObject");
177
env->FatalError("AgentThreadLoop: error in JVMTI GetLocalObject\n");
178
}
179
}
180
181
// Function holding the main loop for the test agent thread.
182
//
183
// The agent does the following in each loop iteration:
184
//
185
// - Wait for the target thread either to start a new test iteration or to
186
// signal shutdown.
187
//
188
// Shutdown is signalled by setting test_state to ShutDown. The agent reacts
189
// to it by changing test_state to Terminated and then exits.
190
//
191
// In the case of a new test iteration the target thread builds a deep call
192
// stack and then calls the native method notifyAgentToGetLocal(). While in
193
// native code its stack is walkable. It sets the shared variable test_state
194
// to TargetInNative and then uses the glws_monitor to send the
195
// notification to the agent thread.
196
//
197
// - Read the shared variable depth_for_get_local which was set by the target
198
// thread before sending the notification.
199
//
200
// - Set test_state to AgentInGetLocal and notify the target thread.
201
//
202
// - Perform the JVMTI GetLocal call at depth_for_get_local racing the target
203
// thread returning from the native call making its stack not walkable. The VM
204
// will crash if this happens while the stack is walked to find the frame for
205
// the GetLocal operation. The deeper the frame the more likely the crash
206
// because the stack walk takes longer.
207
//
208
JNIEXPORT void JNICALL
209
AgentThreadLoop(jvmtiEnv * jvmti, JNIEnv* env, void * arg) {
210
jvmtiError err;
211
jvmtiThreadInfo thread_info;
212
213
// Wait until target_thread is set by target thread.
214
monitor_enter(jvmti, env, AT_LINE);
215
while (target_thread == NULL) {
216
monitor_wait(jvmti, env, AT_LINE);
217
}
218
monitor_exit(jvmti, env, AT_LINE);
219
220
err = jvmti->GetThreadInfo(target_thread, &thread_info);
221
if (err != JVMTI_ERROR_NONE) {
222
ShowErrorMessage(jvmti, err, "AgentThreadLoop: error in JVMTI GetThreadInfo");
223
env->FatalError("AgentThreadLoop: error in JVMTI GetThreadInfo\n");
224
}
225
226
printf("AGENT: AgentThreadLoop thread started. Polling thread '%s' for local variables\n",
227
thread_info.name);
228
jvmti->Deallocate((unsigned char *) thread_info.name);
229
230
do {
231
int depth;
232
233
monitor_enter(jvmti, env, AT_LINE);
234
235
// Wait for java part to build large stack and then become stack walk
236
// save by calling the native method notifyAgentToGetLocal or to signal
237
// shutdown.
238
while (test_state != TargetInNative) {
239
if (test_state == ShutDown) {
240
test_state = Terminated;
241
monitor_notify(jvmti, env, AT_LINE);
242
monitor_exit(jvmti, env, AT_LINE);
243
return;
244
}
245
monitor_wait(jvmti, env, AT_LINE);
246
}
247
depth = depth_for_get_local;
248
249
// Notify target thread that this thread is about to query the local value.
250
test_state = AgentInGetLocal;
251
monitor_notify(jvmti, env, AT_LINE);
252
253
monitor_exit(jvmti, env, AT_LINE);
254
255
// Now get the local object from the target thread's stack.
256
test_GetLocalObject(jvmti, env, depth);
257
} while (true);
258
259
printf("AGENT: AgentThreadLoop thread: exiting\n");
260
}
261
262
// Called by target thread after building a large stack.
263
// By calling this native method, the thread's stack becomes walkable.
264
// It notifies the agent to do the GetLocalObject() call and then races
265
// it to make its stack not walkable by returning from the native call.
266
JNIEXPORT void JNICALL
267
Java_GetLocalWithoutSuspendTest_notifyAgentToGetLocal(JNIEnv *env, jclass cls, jint depth, jint waitCycles) {
268
monitor_enter(jvmti, env, AT_LINE);
269
270
// Set depth_for_get_local and notify agent that the target thread is ready for the GetLocalObject() call
271
depth_for_get_local = depth;
272
test_state = TargetInNative;
273
274
monitor_notify(jvmti, env, AT_LINE);
275
276
// Wait for agent thread to read depth_for_get_local and do the GetLocalObject() call
277
while (test_state != AgentInGetLocal) {
278
monitor_wait(jvmti, env, AT_LINE);
279
}
280
281
// Reset state to Initial
282
test_state = Initial;
283
284
monitor_exit(jvmti, env, AT_LINE);
285
286
// Wait a little until agent thread is in unsafe stack walk.
287
// This needs to be a spin wait or sleep because we cannot get a notification
288
// from there.
289
while (--waitCycles > 0) {
290
dummy_counter++;
291
}
292
}
293
294
// Called by target thread to signal shutdown. The target thread waits for the
295
// agent's acknowledge by changing test_state to Terminated.
296
JNIEXPORT void JNICALL
297
Java_GetLocalWithoutSuspendTest_shutDown(JNIEnv *env, jclass cls) {
298
monitor_enter(jvmti, env, AT_LINE);
299
300
// Notify agent thread to shut down
301
test_state = ShutDown;
302
monitor_notify(jvmti, env, AT_LINE);
303
304
// Wait for agent to terminate
305
while (test_state != Terminated) {
306
monitor_wait(jvmti, env, AT_LINE);
307
}
308
309
monitor_exit(jvmti, env, AT_LINE);
310
311
// Destroy glws_monitor
312
monitor_destroy(jvmti, env, AT_LINE);
313
}
314
315
// Called by target thread to provide agent with its thread object
316
JNIEXPORT void JNICALL
317
Java_GetLocalWithoutSuspendTest_setTargetThread(JNIEnv *env, jclass cls, jthread target) {
318
monitor_enter(jvmti, env, AT_LINE);
319
320
target_thread = env->NewGlobalRef(target);
321
322
monitor_notify(jvmti, env, AT_LINE);
323
324
monitor_exit(jvmti, env, AT_LINE);
325
}
326
327
void JNICALL VMInit(jvmtiEnv *jvmti, JNIEnv *env, jthread thr) {
328
jvmtiError err;
329
jobject agent_thread_name;
330
jclass thread_clas;
331
jmethodID thread_ctro;
332
jthread agent_thread;
333
334
printf("AGENT: VM init event\n");
335
printf("AGENT: Start new thread that performs GetLocalObject calls on a running target thread\n");
336
337
agent_thread_name = env->NewStringUTF("GetLocalWithoutSuspendTest Agent Thread");
338
if (agent_thread_name == NULL) {
339
env->FatalError("VMInit: NewStringUTF failed\n");
340
}
341
342
thread_clas = env->FindClass("java/lang/Thread");
343
if (agent_thread_name == NULL) {
344
env->FatalError("VMInit: java.lang.Thread class not found\n");
345
}
346
347
thread_ctro = env->GetMethodID(thread_clas, "<init>", "(Ljava/lang/String;)V");
348
if (thread_ctro == NULL) {
349
env->FatalError("VMInit: failed to get ID for the Thread ctor\n");
350
}
351
352
agent_thread = (jthread) env->NewObject(thread_clas, thread_ctro, agent_thread_name);
353
if (agent_thread == NULL) {
354
env->FatalError("VMInit: Failed to allocate thread object\n");
355
}
356
357
err = jvmti->RunAgentThread(agent_thread, &AgentThreadLoop, NULL,
358
JVMTI_THREAD_NORM_PRIORITY);
359
if (err != JVMTI_ERROR_NONE) {
360
ShowErrorMessage(jvmti, err, "VMInit: failed to start GetLocalWithoutSuspendTest thread");
361
return;
362
}
363
}
364
365
JNIEXPORT jint JNICALL
366
Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
367
printf("AGENT: Agent_OnLoad started.\n");
368
return Agent_Initialize(jvm, options, reserved);
369
}
370
371
JNIEXPORT jint JNICALL
372
Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
373
printf("AGENT: Agent_OnAttach started.");
374
return Agent_Initialize(jvm, options, reserved);
375
}
376
377
JNIEXPORT jint JNICALL
378
JNI_OnLoad(JavaVM *jvm, void *reserved) {
379
jint res;
380
JNIEnv *env;
381
382
printf("AGENT: JNI_OnLoad started.");
383
res = jvm->GetEnv((void **) &env, JNI_VERSION_9);
384
if (res != JNI_OK || env == NULL) {
385
fprintf(stderr, "Error: GetEnv call failed(%d)!\n", res);
386
return JNI_ERR;
387
}
388
389
return JNI_VERSION_9;
390
}
391
392
static
393
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
394
jint res;
395
jvmtiError err;
396
jvmtiCapabilities caps;
397
jvmtiEventCallbacks callbacks;
398
399
printf("AGENT: Agent_Initialize started\n");
400
401
memset(&caps, 0, sizeof(caps));
402
memset(&callbacks, 0, sizeof(callbacks));
403
404
res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_9);
405
if (res != JNI_OK || jvmti == NULL) {
406
fprintf(stderr, "Error: GetEnv(JVMTI_VERSION_9) call failed(%d)!\n", res);
407
return JNI_ERR;
408
}
409
410
caps.can_access_local_variables = 1;
411
412
err = jvmti->AddCapabilities(&caps);
413
if (err != JVMTI_ERROR_NONE) {
414
ShowErrorMessage(jvmti, err, "Agent_OnLoad: error in JVMTI AddCapabilities");
415
return JNI_ERR;
416
}
417
418
err = jvmti->GetCapabilities(&caps);
419
if (err != JVMTI_ERROR_NONE) {
420
ShowErrorMessage(jvmti, err, "Agent_OnLoad: error in JVMTI GetCapabilities");
421
return JNI_ERR;
422
}
423
424
if (!caps.can_access_local_variables) {
425
fprintf(stderr, "Warning: Access to local variables is not implemented\n");
426
return JNI_ERR;
427
}
428
429
callbacks.VMInit = &VMInit;
430
err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
431
if (err != JVMTI_ERROR_NONE) {
432
ShowErrorMessage(jvmti, err, "Agent_OnLoad: error in JVMTI SetEventCallbacks");
433
return JNI_ERR;
434
}
435
436
err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL);
437
if (err != JVMTI_ERROR_NONE) {
438
ShowErrorMessage(jvmti, err, "Agent_OnLoad: error in JVMTI SetEventNotificationMode");
439
return JNI_ERR;
440
}
441
442
err = jvmti->CreateRawMonitor("GetLocalWithoutSuspend Test Monitor", &glws_monitor);
443
444
printf("AGENT: Agent_Initialize finished\n");
445
return JNI_OK;
446
}
447
448
}
449
450