File: MaterializeVirtualObjectTest.java

package info (click to toggle)
openjdk-25 25.0.1%2B8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 825,408 kB
  • sloc: java: 5,585,680; cpp: 1,333,948; xml: 1,321,242; ansic: 488,034; asm: 404,003; objc: 21,088; sh: 15,106; javascript: 13,265; python: 8,319; makefile: 2,518; perl: 357; awk: 351; pascal: 103; exp: 83; sed: 72; jsp: 24
file content (351 lines) | stat: -rw-r--r-- 19,070 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8136421
 *
 * @requires vm.jvmci & vm.compMode == "Xmixed"
 * @requires vm.opt.final.EliminateAllocations == true
 * @requires vm.opt.StressUnstableIfTraps == null | !vm.opt.StressUnstableIfTraps
 *
 * @comment no "-Xcomp -XX:-TieredCompilation" combination allowed until JDK-8140018 is resolved
 * @requires vm.opt.TieredCompilation == null | vm.opt.TieredCompilation == true
 *
 * @library / /test/lib
 * @library ../common/patches
 * @library /testlibrary/asm
 * @modules java.base/jdk.internal.misc
 * @modules jdk.internal.vm.ci/jdk.vm.ci.hotspot
 *          jdk.internal.vm.ci/jdk.vm.ci.code
 *          jdk.internal.vm.ci/jdk.vm.ci.code.stack
 *          jdk.internal.vm.ci/jdk.vm.ci.meta
 *
 * @build jdk.internal.vm.ci/jdk.vm.ci.hotspot.CompilerToVMHelper jdk.test.whitebox.WhiteBox
 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
 * @run main/othervm -Xbatch -Xbootclasspath/a:.
 *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
 *                   -XX:CompileCommand=exclude,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::check
 *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame
 *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame2
 *                   -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::recurse
 *                   -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame3
 *                   -XX:+DoEscapeAnalysis
 *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.materializeFirst=true
 *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=false
 *                   compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
 * @run main/othervm -Xbatch -Xbootclasspath/a:.
 *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
 *                   -XX:CompileCommand=exclude,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::check
 *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame
 *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame2
 *                   -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::recurse
 *                   -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame3
 *                   -XX:+DoEscapeAnalysis
 *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.materializeFirst=false
 *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=false
 *                   compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
 * @run main/othervm -Xbatch -Xbootclasspath/a:.
 *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
 *                   -XX:CompileCommand=exclude,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::check
 *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame
 *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame2
 *                   -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::recurse
 *                   -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame3
 *                   -XX:+DoEscapeAnalysis
 *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.materializeFirst=true
 *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=true
 *                   compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
 * @run main/othervm -Xbatch -Xbootclasspath/a:.
 *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
 *                   -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
 *                   -XX:CompileCommand=exclude,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::check
 *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame
 *                   -XX:CompileCommand=dontinline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame2
 *                   -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::recurse
 *                   -XX:CompileCommand=inline,compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest::testFrame3
 *                   -XX:+DoEscapeAnalysis
 *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.materializeFirst=false
 *                   -Dcompiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate=true
 *                   compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest
 */

package compiler.jvmci.compilerToVM;

import compiler.jvmci.common.CTVMUtilities;
import compiler.testlibrary.CompilerUtils;
import compiler.whitebox.CompilerWhiteBoxTest;
import jdk.test.lib.Asserts;
import jdk.vm.ci.code.stack.InspectedFrame;
import jdk.vm.ci.hotspot.CompilerToVMHelper;
import jdk.vm.ci.hotspot.HotSpotStackFrameReference;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jtreg.SkippedException;
import jdk.test.whitebox.WhiteBox;

import java.lang.reflect.Method;

public class MaterializeVirtualObjectTest {
    private static final WhiteBox WB;
    private static final boolean INVALIDATE;
    private static final int COMPILE_THRESHOLD;
    private static final Method MATERIALIZED_METHOD;
    private static final Method NOT_MATERIALIZED_METHOD;
    private static final Method FRAME3_METHOD;
    private static final ResolvedJavaMethod MATERIALIZED_RESOLVED;
    private static final ResolvedJavaMethod NOT_MATERIALIZED_RESOLVED;
    private static final ResolvedJavaMethod FRAME2_RESOLVED;
    private static final ResolvedJavaMethod FRAME3_RESOLVED;
    private static final boolean MATERIALIZE_FIRST;

    static {
        Method method1;
        Method method2;
        WB = WhiteBox.getWhiteBox();
        try {
            method1 = MaterializeVirtualObjectTest.class.getDeclaredMethod("testFrame",
                    String.class, int.class);
            method2 = MaterializeVirtualObjectTest.class.getDeclaredMethod("testFrame2",
                    String.class, int.class);
            FRAME3_METHOD = MaterializeVirtualObjectTest.class.getDeclaredMethod("testFrame3",
                    Helper.class, int.class);
        } catch (NoSuchMethodException e) {
            throw new Error("Can't get executable for test method", e);
        }
        ResolvedJavaMethod resolved1;
        resolved1 = CTVMUtilities.getResolvedMethod(method1);
        FRAME2_RESOLVED = CTVMUtilities.getResolvedMethod(method2);
        FRAME3_RESOLVED = CTVMUtilities.getResolvedMethod(FRAME3_METHOD);
        INVALIDATE = Boolean.getBoolean(
                "compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.invalidate");
        COMPILE_THRESHOLD = CompilerWhiteBoxTest.THRESHOLD;
        MATERIALIZE_FIRST = Boolean.getBoolean(
                "compiler.jvmci.compilerToVM.MaterializeVirtualObjectTest.materializeFirst");
        MATERIALIZED_RESOLVED = MATERIALIZE_FIRST ? resolved1 : FRAME2_RESOLVED;
        NOT_MATERIALIZED_RESOLVED = MATERIALIZE_FIRST ? FRAME2_RESOLVED : resolved1;
        MATERIALIZED_METHOD = MATERIALIZE_FIRST ? method1 : method2;
        NOT_MATERIALIZED_METHOD = MATERIALIZE_FIRST ? method2 : method1;
    }

    public static void main(String[] args) {
        int levels[] = CompilerUtils.getAvailableCompilationLevels();
        // we need compilation level 4 to use EscapeAnalysis
        if (levels.length < 1 || levels[levels.length - 1] != 4) {
            throw new SkippedException("Test needs compilation level 4");
        }

        try {
            new MaterializeVirtualObjectTest().test();
        } catch (MaterializationNotSupported e) {
            Asserts.assertTrue(Thread.currentThread().isVirtual());
        }
    }

    private static String getName() {
        return "CASE: invalidate=" + INVALIDATE + ", materializedMethod="
                + (MATERIALIZE_FIRST ? "testFrame" : "testFrame2")
                + ", notMaterializedMethod="
                + (MATERIALIZE_FIRST ? "testFrame2" : "testFrame");
    }

    private void test() {
        Asserts.assertFalse(WB.isMethodCompiled(MATERIALIZED_METHOD),
                getName() + " : materialized method is compiled");
        Asserts.assertFalse(WB.isMethodCompiled(NOT_MATERIALIZED_METHOD),
                getName() + " : not materialized method is compiled");
        for (int i = 0; i < CompilerWhiteBoxTest.THRESHOLD; i++) {
            testFrame("someString", i);
        }
        Asserts.assertTrue(WB.isMethodCompiled(MATERIALIZED_METHOD), getName()
                + " : materialized method not compiled");
        Asserts.assertTrue(WB.isMethodCompiled(NOT_MATERIALIZED_METHOD),
                getName() + " : not materialized method not compiled");
        testFrame("someString", /* materialize */ CompilerWhiteBoxTest.THRESHOLD);

        // run second test types
        for (int i = 0; i < CompilerWhiteBoxTest.THRESHOLD; i++) {
            testFrame("someString", i);
        }
        Asserts.assertTrue(WB.isMethodCompiled(MATERIALIZED_METHOD), getName()
                + " : materialized method not compiled");
        Asserts.assertTrue(WB.isMethodCompiled(NOT_MATERIALIZED_METHOD),
                getName() + " : not materialized method not compiled");
        testFrame("someString", /* materialize */ CompilerWhiteBoxTest.THRESHOLD + 1);
    }

    private void testFrame(String str, int iteration) {
        Helper helper = new Helper(str);
        testFrame2(str, iteration);
        Asserts.assertTrue((helper.string != null) && (this != null)
                && (helper != null), String.format("%s : some locals are null", getName()));
    }

    private void testFrame2(String str, int iteration) {
        Helper helper = new Helper(str);
        Helper helper2 = new Helper("bar");
        testFrame3(helper, iteration);
        Asserts.assertTrue((helper.string != null) && (this != null) && helper.string == str
                && (helper != null), String.format("%s : some locals are null", getName()));
        Asserts.assertTrue((helper2.string != null) && (this != null)
                && (helper2 != null), String.format("%s : some locals are null", getName()));
    }

    private void testFrame3(Helper outerHelper, int iteration) {
        Helper innerHelper = new Helper("foo");
        recurse(2, iteration);
        Asserts.assertTrue((innerHelper.string != null) && (this != null)
                && (innerHelper != null), String.format("%s : some locals are null", getName()));
        Asserts.assertTrue((outerHelper.string != null) && (this != null)
                && (outerHelper != null), String.format("%s : some locals are null", getName()));
    }

    private void recurse(int depth, int iteration) {
        if (depth == 0) {
            check(iteration);
        } else {
            Integer s = new Integer(depth);
            recurse(depth - 1, iteration);
            Asserts.assertEQ(s.intValue(), depth,
                    String.format("different values: %s != %s", s.intValue(), depth));
        }
    }

    private static void materializeVirtualObjects(InspectedFrame f, boolean invalidateCode) {
        try {
            f.materializeVirtualObjects(invalidateCode);
        } catch (IllegalArgumentException e) {
            throw new MaterializationNotSupported(e);
        }
    }

    private void checkStructure(boolean materialize) {
        boolean[] framesSeen = new boolean[2];
        Object[] helpers = new Object[1];
        CompilerToVMHelper.iterateFrames(
            new ResolvedJavaMethod[] {FRAME3_RESOLVED},
            null, /* any */
            0,
            f -> {
                if (!framesSeen[1]) {
                    Asserts.assertTrue(f.isMethod(FRAME3_RESOLVED),
                            "Expected testFrame3 first");
                    framesSeen[1] = true;
                    Asserts.assertTrue(f.getLocal(0) != null, "this should not be null");
                    Asserts.assertTrue(f.getLocal(1) != null, "outerHelper should not be null");
                    Asserts.assertTrue(f.getLocal(3) != null, "innerHelper should not be null");
                    Asserts.assertEQ(((Helper) f.getLocal(3)).string, "foo", "innerHelper.string should be foo");
                    helpers[0] = f.getLocal(1);
                    if (materialize) {
                        materializeVirtualObjects(f, false);
                    }
                    return null; //continue
                } else {
                    Asserts.assertFalse(framesSeen[0], "frame3 can not have been seen");
                    Asserts.assertTrue(f.isMethod(FRAME2_RESOLVED),
                            "Expected testFrame2 second");
                    framesSeen[0] = true;
                    Asserts.assertTrue(f.getLocal(0) != null, "this should not be null");
                    Asserts.assertTrue(f.getLocal(1) != null, "str should not be null");
                    Asserts.assertTrue(f.getLocal(3) != null, "helper should not be null");
                    Asserts.assertTrue(f.getLocal(4) != null, "helper2 should not be null");
                    Asserts.assertEQ(((Helper) f.getLocal(3)).string, f.getLocal(1), "helper.string should be the same as str");
                    Asserts.assertEQ(((Helper) f.getLocal(4)).string, "bar", "helper2.string should be foo");
                    if (!materialize) {
                        Asserts.assertEQ(f.getLocal(3), helpers[0], "helper should be the same as frame3's outerHelper");
                    }
                    return f; // stop
                }
            });
        Asserts.assertTrue(framesSeen[1], "frame3 should have been seen");
        Asserts.assertTrue(framesSeen[0], "frame2 should have been seen");
    }

    private void check(int iteration) {
        // Materialize virtual objects on last invocation
        if (iteration == COMPILE_THRESHOLD) {
            // get frames and check not-null
            HotSpotStackFrameReference materialized = CompilerToVMHelper.iterateFrames(
                new ResolvedJavaMethod[] {MATERIALIZED_RESOLVED},
                null /* any */,
                0,
                f -> (HotSpotStackFrameReference) f);
            Asserts.assertNotNull(materialized, getName()
                    + " : got null frame for materialized method");
            Asserts.assertTrue(materialized.isMethod(MATERIALIZED_RESOLVED),
                "Expected materialized method but got " + materialized);
            InspectedFrame notMaterialized = CompilerToVMHelper.iterateFrames(
                new ResolvedJavaMethod[] {NOT_MATERIALIZED_RESOLVED},
                null /* any */,
                0,
                f -> f);
            Asserts.assertNE(materialized, notMaterialized,
                    "Got same frame pointer for both tested frames");
            Asserts.assertTrue(notMaterialized.isMethod(NOT_MATERIALIZED_RESOLVED),
                "Expected notMaterialized method but got " + notMaterialized);
            Asserts.assertNotNull(notMaterialized, getName()
                    + " : got null frame for not materialized method");
            Asserts.assertTrue(WB.isMethodCompiled(MATERIALIZED_METHOD), getName()
                + " : materialized method not compiled");
            Asserts.assertTrue(WB.isMethodCompiled(NOT_MATERIALIZED_METHOD),
                getName() + " : not materialized method not compiled");
            // check that frames has virtual objects before materialization stage
            Asserts.assertTrue(materialized.hasVirtualObjects(), getName()
                    + ": materialized frame has no virtual object before materialization");
            Asserts.assertTrue(notMaterialized.hasVirtualObjects(), getName()
                    + ": notMaterialized frame has no virtual object before materialization");
            // materialize
            materializeVirtualObjects(materialized, INVALIDATE);
            // check that only not materialized frame has virtual objects
            Asserts.assertFalse(materialized.hasVirtualObjects(), getName()
                    + " : materialized has virtual object after materialization");
            Asserts.assertTrue(notMaterialized.hasVirtualObjects(), getName()
                    + " : notMaterialized has no virtual object after materialization");
            // check that materialized frame was deoptimized in case invalidate=true
            Asserts.assertEQ(WB.isMethodCompiled(MATERIALIZED_METHOD), !INVALIDATE, getName()
                    + " : materialized method has unexpected compiled status");
            // check that not materialized frame wasn't deoptimized
            Asserts.assertTrue(WB.isMethodCompiled(NOT_MATERIALIZED_METHOD), getName()
                    + " : not materialized method has unexpected compiled status");
        } else if (iteration == COMPILE_THRESHOLD + 1) {
            checkStructure(false);
            checkStructure(true);
        }
    }

    private class Helper {
        public String string;

        public Helper(String s) {
            this.string = s;
        }
    }

    static class MaterializationNotSupported extends RuntimeException {
        public MaterializationNotSupported(Throwable cause) {
            super(cause);
        }
    }
}