Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/StackWalker/LocalsAndOperands.java
41149 views
1
/*
2
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. 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
/*
25
* @test
26
* @bug 8020968 8147039 8156073
27
* @summary Tests for locals and operands
28
* @modules java.base/java.lang:open
29
* @run testng/othervm -Xint -DtestUnused=true LocalsAndOperands
30
* @run testng/othervm -Xcomp LocalsAndOperands
31
*/
32
33
/*
34
* @test
35
* @bug 8020968 8147039 8156073
36
* @modules java.base/java.lang:open
37
* @requires !vm.graal.enabled
38
* @run testng/othervm -Xcomp -XX:-TieredCompilation LocalsAndOperands
39
*/
40
41
import org.testng.annotations.*;
42
import static org.testng.Assert.*;
43
import java.lang.StackWalker.StackFrame;
44
import static java.lang.StackWalker.Option.*;
45
import java.lang.reflect.*;
46
import java.util.*;
47
import java.util.stream.*;
48
49
public class LocalsAndOperands {
50
static final boolean debug = false;
51
static final boolean is32bit;
52
static final boolean testUnused;
53
54
static Class<?> liveStackFrameClass;
55
static Class<?> primitiveSlotClass;
56
static Class<?> primitiveSlot32Class;
57
static Class<?> primitiveSlot64Class;
58
59
static StackWalker extendedWalker;
60
static Method getLocals;
61
static Method getOperands;
62
static Method getMonitors;
63
static Method primitiveSize;
64
static Method primitiveLongValue;
65
static Method primitiveIntValue;
66
static Method getExtendedWalker;
67
68
private static final long LOWER_LONG_VAL = 4L; // Lower bits
69
private static final long UPPER_LONG_VAL = 0x123400000000L; // Upper bits
70
private static final long NEG_LONG_VAL = Long.MIN_VALUE;
71
72
private static final double LOWER_DOUBLE_VAL = Double.longBitsToDouble(0xABCDL);
73
private static final double UPPER_DOUBLE_VAL = Double.longBitsToDouble(0x432100000000L);
74
75
static {
76
try {
77
liveStackFrameClass = Class.forName("java.lang.LiveStackFrame");
78
primitiveSlotClass = Class.forName("java.lang.LiveStackFrame$PrimitiveSlot");
79
primitiveSlot32Class = Class.forName("java.lang.LiveStackFrameInfo$PrimitiveSlot32");
80
primitiveSlot64Class = Class.forName("java.lang.LiveStackFrameInfo$PrimitiveSlot64");
81
82
getLocals = liveStackFrameClass.getDeclaredMethod("getLocals");
83
getLocals.setAccessible(true);
84
85
getOperands = liveStackFrameClass.getDeclaredMethod("getStack");
86
getOperands.setAccessible(true);
87
88
getMonitors = liveStackFrameClass.getDeclaredMethod("getMonitors");
89
getMonitors.setAccessible(true);
90
91
primitiveSize = primitiveSlotClass.getDeclaredMethod("size");
92
primitiveSize.setAccessible(true);
93
94
primitiveLongValue = primitiveSlotClass.getDeclaredMethod("longValue");
95
primitiveLongValue.setAccessible(true);
96
97
primitiveIntValue = primitiveSlotClass.getDeclaredMethod("intValue");
98
primitiveIntValue.setAccessible(true);
99
100
getExtendedWalker = liveStackFrameClass.getMethod("getStackWalker", Set.class);
101
getExtendedWalker.setAccessible(true);
102
extendedWalker = (StackWalker) getExtendedWalker.invoke(null,
103
EnumSet.noneOf(StackWalker.Option.class));
104
105
String dataModel = System.getProperty("sun.arch.data.model");
106
if ("32".equals(dataModel)) {
107
is32bit = true;
108
} else if ("64".equals(dataModel)) {
109
is32bit= false;
110
} else {
111
throw new RuntimeException("Weird data model:" + dataModel);
112
}
113
System.out.println("VM bits: " + dataModel);
114
115
testUnused = System.getProperty("testUnused") != null;
116
} catch (Throwable t) { throw new RuntimeException(t); }
117
}
118
119
/** Helper method to return a StackFrame's locals */
120
static Object[] invokeGetLocals(StackFrame arg) {
121
try {
122
return (Object[]) getLocals.invoke(arg);
123
} catch (Exception e) { throw new RuntimeException(e); }
124
}
125
126
/*****************
127
* DataProviders *
128
*****************/
129
130
/** Calls KnownLocalsTester.testLocals* and provides LiveStackFrames */
131
@DataProvider
132
public static StackFrame[][] knownLocalsProvider() {
133
List<StackFrame[]> list = new ArrayList<>(3);
134
list.add(new KnownLocalsTester().testLocalsKeepAlive());
135
list.add(new KnownLocalsTester().testLocalsKeepAliveArgs(0xA, 'z',
136
"himom", 0x3FF00000000L + 0xFFFF, Math.PI));
137
if (testUnused) {
138
list.add(new KnownLocalsTester().testLocalsUnused());
139
}
140
return list.toArray(new StackFrame[1][1]);
141
}
142
143
/****************
144
* Test methods *
145
****************/
146
147
/**
148
* Check for expected local values in the LiveStackFrame
149
*/
150
@Test(dataProvider = "knownLocalsProvider")
151
public static void checkLocalValues(StackFrame... frames) {
152
dumpFramesIfDebug(frames);
153
try {
154
Stream.of(frames)
155
.filter(f -> KnownLocalsTester.TEST_METHODS.contains(f.getMethodName()))
156
.forEach(LocalsAndOperands::checkFrameLocals);
157
} catch (Exception e) { dumpFramesIfNotDebug(frames); throw e; }
158
}
159
160
/**
161
* Check the locals in the given StackFrame against the expected values.
162
*/
163
private static void checkFrameLocals(StackFrame f) {
164
Object[] expectedArray = KnownLocalsTester.LOCAL_VALUES;
165
Object[] locals = invokeGetLocals(f);
166
167
for (int i = 0; i < locals.length; i++) {
168
Object expected = expectedArray[i];
169
Object observed = locals[i];
170
171
if (expected == null) { /* skip nulls in golden values */
172
continue;
173
} else if (expected instanceof KnownLocalsTester.TwoSlotValue) {
174
// confirm integrity of expected values
175
assertEquals(expectedArray[i+1], null,
176
"Malformed array of expected values - slot after TwoSlotValue should be null");
177
assertLongIsInSlots(locals[i], locals[i+1],
178
((KnownLocalsTester.TwoSlotValue)expected).value);
179
i++; // skip following slot
180
} else if (primitiveSlotClass.isInstance(observed)) { // single slot primitive
181
assertTrue(primitiveValueEquals(observed, expected),
182
"Local value mismatch: local " + i + " value is " +
183
observed + ", expected " + expected);
184
} else if (expected instanceof Class) {
185
assertTrue(((Class)expected).isInstance(observed),
186
"Local value mismatch: local " + i + " expected instancof " +
187
expected + " but got " + observed);
188
} else if (expected instanceof String) {
189
assertEquals(expected, observed, "Local value mismatch: local " +
190
i + " value is " + observed + ", expected " + expected);
191
} else {
192
throw new RuntimeException("Unrecognized expected local value " +
193
i + ": " + expected);
194
}
195
}
196
}
197
198
/**
199
* Sanity check for locals and operands, including testng/jtreg frames
200
* using all StackWalker options.
201
*/
202
@Test
203
public synchronized void fullStackSanityCheck() throws Throwable {
204
if (debug) {
205
System.out.println("Running fullStackSanityCheck");
206
}
207
StackWalker sw = (StackWalker) getExtendedWalker.invoke(null,
208
EnumSet.of(SHOW_HIDDEN_FRAMES, SHOW_REFLECT_FRAMES,
209
RETAIN_CLASS_REFERENCE));
210
sw.forEach(f -> {
211
if (debug) {
212
printLocals(f);
213
} else {
214
try {
215
System.out.println(" " + f + ": " +
216
((Object[]) getLocals.invoke(f)).length + " locals, " +
217
((Object[]) getOperands.invoke(f)).length + " operands, " +
218
((Object[]) getMonitors.invoke(f)).length + " monitors");
219
} catch (IllegalAccessException|InvocationTargetException t) {
220
throw new RuntimeException(t);
221
}
222
}
223
});
224
}
225
226
/**
227
* Test that LiveStackFrames are not provided with the default StackWalker
228
* options.
229
*/
230
@Test
231
public static void noLocalsSanityCheck() {
232
StackWalker sw = StackWalker.getInstance();
233
sw.forEach(f -> {
234
assertFalse(liveStackFrameClass.isInstance(f),
235
"should not be LiveStackFrame");
236
});
237
}
238
239
/**
240
* Class stack-walking methods with a known set of methods and local variables.
241
*/
242
static class KnownLocalsTester {
243
private StackWalker walker;
244
245
KnownLocalsTester() {
246
this.walker = extendedWalker;
247
}
248
249
/**
250
* Perform stackwalk without keeping local variables alive and return an
251
* array of the collected StackFrames
252
*/
253
private synchronized StackFrame[] testLocalsUnused() {
254
// Unused local variables will become dead
255
int x = 0xA;
256
char c = 'z'; // 0x7A
257
String hi = "himom";
258
long l = 0x3FF00000000L + 0xFFFFL;
259
double d = Math.PI;
260
261
return walker.walk(s ->
262
s.filter(f -> TEST_METHODS.contains(f.getMethodName()))
263
.toArray(StackFrame[]::new)
264
);
265
}
266
267
/**
268
* Perform stackwalk, keeping local variables alive, and return a list of
269
* the collected StackFrames
270
*/
271
private synchronized StackFrame[] testLocalsKeepAlive() {
272
int x = 0xA;
273
char c = 'z'; // 0x7A
274
String hi = "himom";
275
long l = 0x3FF00000000L + 0xFFFFL;
276
double d = Math.PI;
277
278
StackFrame[] frames = walker.walk(s ->
279
s.filter(f -> TEST_METHODS.contains(f.getMethodName()))
280
.toArray(StackFrame[]::new)
281
);
282
283
// Use local variables so they stay alive
284
System.out.println("Stayin' alive: "+this+" "+x+" "+c+" "+hi+" "+l+" "+d);
285
return frames;
286
}
287
288
/**
289
* Perform stackwalk, keeping method arguments alive, and return a list of
290
* the collected StackFrames
291
*/
292
private synchronized StackFrame[] testLocalsKeepAliveArgs(int x, char c,
293
String hi, long l,
294
double d) {
295
StackFrame[] frames = walker.walk(s ->
296
s.filter(f -> TEST_METHODS.contains(f.getMethodName()))
297
.toArray(StackFrame[]::new)
298
);
299
300
// Use local variables so they stay alive
301
System.out.println("Stayin' alive: "+this+" "+x+" "+c+" "+hi+" "+l+" "+d);
302
return frames;
303
}
304
305
// An expected two-slot local (i.e. long or double)
306
static class TwoSlotValue {
307
public long value;
308
public TwoSlotValue(long value) { this.value = value; }
309
}
310
311
// Expected values for locals in KnownLocalsTester.testLocals* methods
312
private final static Object[] LOCAL_VALUES = new Object[] {
313
LocalsAndOperands.KnownLocalsTester.class,
314
Integer.valueOf(0xA),
315
Integer.valueOf(0x7A),
316
"himom",
317
new TwoSlotValue(0x3FF00000000L + 0xFFFFL),
318
null, // 2nd slot
319
new TwoSlotValue(Double.doubleToRawLongBits(Math.PI)),
320
null, // 2nd slot
321
Integer.valueOf(0)
322
};
323
324
private final static List<String> TEST_METHODS =
325
List.of("testLocalsUnused",
326
"testLocalsKeepAlive",
327
"testLocalsKeepAliveArgs");
328
}
329
330
/* Simpler tests of long & double arguments */
331
332
@Test
333
public static void testUsedLongArg() throws Exception {
334
usedLong(LOWER_LONG_VAL);
335
usedLong(UPPER_LONG_VAL);
336
usedLong(NEG_LONG_VAL);
337
}
338
339
private static void usedLong(long longArg) throws Exception {
340
StackFrame[] frames = extendedWalker.walk(s ->
341
s.filter(f -> "usedLong".equals(f.getMethodName()))
342
.toArray(StackFrame[]::new)
343
);
344
try {
345
dumpFramesIfDebug(frames);
346
347
Object[] locals = (Object[]) getLocals.invoke(frames[0]);
348
assertLongIsInSlots(locals[0], locals[1], longArg);
349
System.out.println("Stayin' alive: " + longArg);
350
} catch (Exception t) {
351
dumpFramesIfNotDebug(frames);
352
throw t;
353
}
354
}
355
356
@Test
357
public static void testUnusedLongArg() throws Exception {
358
if (testUnused) {
359
unusedLong(NEG_LONG_VAL);
360
}
361
}
362
363
private static void unusedLong(long longArg) throws Exception {
364
StackFrame[] frames = extendedWalker.walk(s ->
365
s.filter(f -> "unusedLong".equals(f.getMethodName()))
366
.toArray(StackFrame[]::new)
367
);
368
try {
369
dumpFramesIfDebug(frames);
370
371
final Object[] locals = (Object[]) getLocals.invoke(frames[0]);
372
assertLongIsInSlots(locals[0], locals[1], NEG_LONG_VAL);
373
} catch (Exception t) {
374
dumpFramesIfNotDebug(frames);
375
throw t;
376
}
377
}
378
379
@Test
380
public static void testUsedDoubleArg() throws Exception {
381
usedDouble(LOWER_DOUBLE_VAL);
382
usedDouble(UPPER_DOUBLE_VAL);
383
}
384
385
private static void usedDouble(double doubleArg) throws Exception {
386
StackFrame[] frames = extendedWalker.walk(s ->
387
s.filter(f -> "usedDouble".equals(f.getMethodName()))
388
.toArray(StackFrame[]::new)
389
);
390
try {
391
dumpFramesIfDebug(frames);
392
393
Object[] locals = (Object[]) getLocals.invoke(frames[0]);
394
assertDoubleIsInSlots(locals[0], locals[1], doubleArg);
395
System.out.println("Stayin' alive: " + doubleArg);
396
} catch (Exception t) {
397
dumpFramesIfNotDebug(frames);
398
throw t;
399
}
400
}
401
402
/*******************
403
* Utility Methods *
404
*******************/
405
406
/**
407
* Print stack trace with locals
408
*/
409
public static void dumpStackWithLocals(StackFrame...frames) {
410
Stream.of(frames).forEach(LocalsAndOperands::printLocals);
411
}
412
413
public static void dumpFramesIfDebug(StackFrame...frames) {
414
if (debug) { dumpStackWithLocals(frames); }
415
}
416
417
public static void dumpFramesIfNotDebug(StackFrame...frames) {
418
if (!debug) { dumpStackWithLocals(frames); }
419
}
420
421
/**
422
* Print the StackFrame and an indexed list of its locals
423
*/
424
public static void printLocals(StackWalker.StackFrame frame) {
425
try {
426
System.out.println("Locals for: " + frame);
427
Object[] locals = (Object[]) getLocals.invoke(frame);
428
for (int i = 0; i < locals.length; i++) {
429
String localStr = null;
430
431
if (primitiveSlot64Class.isInstance(locals[i])) {
432
localStr = String.format("0x%X",
433
(Long)primitiveLongValue.invoke(locals[i]));
434
} else if (primitiveSlot32Class.isInstance(locals[i])) {
435
localStr = String.format("0x%X",
436
(Integer)primitiveIntValue.invoke(locals[i]));
437
} else if (locals[i] != null) {
438
localStr = locals[i].toString();
439
}
440
System.out.format(" local %d: %s type %s\n", i, localStr, type(locals[i]));
441
}
442
443
Object[] operands = (Object[]) getOperands.invoke(frame);
444
for (int i = 0; i < operands.length; i++) {
445
System.out.format(" operand %d: %s type %s%n", i, operands[i],
446
type(operands[i]));
447
}
448
449
Object[] monitors = (Object[]) getMonitors.invoke(frame);
450
for (int i = 0; i < monitors.length; i++) {
451
System.out.format(" monitor %d: %s%n", i, monitors[i]);
452
}
453
} catch (Exception e) { throw new RuntimeException(e); }
454
}
455
456
private static String type(Object o) {
457
try {
458
if (o == null) {
459
return "null";
460
} else if (primitiveSlotClass.isInstance(o)) {
461
int s = (int)primitiveSize.invoke(o);
462
return s + "-byte primitive";
463
} else {
464
return o.getClass().getName();
465
}
466
} catch(Exception e) { throw new RuntimeException(e); }
467
}
468
469
/*
470
* Check if the PrimitiveValue "primVal" contains the specified value,
471
* either a Long or an Integer.
472
*/
473
static boolean primitiveValueEquals(Object primVal, Object expectedVal) {
474
try {
475
if (expectedVal instanceof Long) {
476
assertFalse(is32bit);
477
assertTrue(primitiveSlot64Class.isInstance(primVal));
478
assertTrue(8 == (int)primitiveSize.invoke(primVal));
479
return Objects.equals(primitiveLongValue.invoke(primVal), expectedVal);
480
} else if (expectedVal instanceof Integer) {
481
int expectedInt = (Integer)expectedVal;
482
if (is32bit) {
483
assertTrue(primitiveSlot32Class.isInstance(primVal),
484
"expected a PrimitiveSlot32 on 32-bit VM");
485
assertTrue(4 == (int)primitiveSize.invoke(primVal));
486
return expectedInt == (int)primitiveIntValue.invoke(primVal);
487
} else {
488
assertTrue(primitiveSlot64Class.isInstance(primVal),
489
"expected a PrimitiveSlot64 on 64-bit VM");
490
assertTrue(8 == (int)primitiveSize.invoke(primVal));
491
// Look for int expectedVal in high- or low-order 32 bits
492
long primValLong = (long)primitiveLongValue.invoke(primVal);
493
return (int)(primValLong & 0x00000000FFFFFFFFL) == expectedInt ||
494
(int)(primValLong >>> 32) == expectedInt;
495
}
496
} else {
497
throw new RuntimeException("Called with non-Integer/Long: " + expectedVal);
498
}
499
} catch (IllegalAccessException|InvocationTargetException e) {
500
throw new RuntimeException(e);
501
}
502
503
}
504
505
/*
506
* Assert that the expected 2-slot long value is stored somewhere in the
507
* pair of slots.
508
* Throw exception if long value isn't in the two slots given.
509
* Accounts for 32 vs 64 bit, but is lax on endianness (accepts either)
510
*/
511
static void assertLongIsInSlots(Object primVal0, Object primVal1, long expected) {
512
try {
513
if (is32bit) {
514
int upper = (int)(expected & 0xFFFFFFFFL);
515
int lower = (int)(expected >> 32);
516
517
if (!((primitiveValueEquals(primVal0, upper) &&
518
primitiveValueEquals(primVal1, lower)) ||
519
(primitiveValueEquals(primVal0, lower) &&
520
primitiveValueEquals(primVal1, upper)))) {
521
throw new RuntimeException(String.format("0x%X and 0x%X of 0x%016X not found in 0x%X and 0x%X",
522
upper, lower, expected,
523
(int)primitiveIntValue.invoke(primVal0),
524
(int)primitiveIntValue.invoke(primVal1)));
525
}
526
} else {
527
if (!(primitiveValueEquals(primVal0, expected) ||
528
primitiveValueEquals(primVal1, expected))) {
529
throw new RuntimeException(String.format("0x%016X not found in 0x%016X or 0x%016X",
530
expected,
531
(long)primitiveLongValue.invoke(primVal0),
532
(long)primitiveLongValue.invoke(primVal1)));
533
}
534
}
535
} catch (IllegalAccessException|InvocationTargetException e) {
536
throw new RuntimeException(e);
537
}
538
}
539
540
static void assertDoubleIsInSlots(Object primVal0, Object primVal1, double expected) {
541
assertLongIsInSlots(primVal0, primVal1, Double.doubleToRawLongBits(expected));
542
}
543
}
544
545