File: SetLocalWhileThreadInNative.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (198 lines) | stat: -rw-r--r-- 7,603 bytes parent folder | download | duplicates (7)
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
/*
 * Copyright (c) 2018 SAP SE. 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 8212928
 * @summary With NeedsDeoptSuspend == true the assertion in compiledVFrame::update_deferred_value() fails, because the frame is not deoptimized.
 * @author Richard Reingruber richard DOT reingruber AT sap DOT com
 *
 * @library /test/lib
 *
 * @run build TestScaffold VMConnection TargetListener TargetAdapter
 * @run main jdk.test.lib.FileInstaller compilerDirectives.json compilerDirectives.json
 * @run compile -g SetLocalWhileThreadInNative.java
 * @run driver SetLocalWhileThreadInNative -Xbatch -XX:-TieredCompilation -XX:CICompilerCount=1 -XX:+UnlockDiagnosticVMOptions -XX:CompilerDirectivesFile=compilerDirectives.json
 */

import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import com.sun.jdi.*;
import com.sun.jdi.event.*;

import jdk.test.lib.Asserts;

/********** target program **********/

class SetLocalWhileThreadInNativeTarget {

    public static final String name = SetLocalWhileThreadInNativeTarget.class.getName();
    public static FileInputStream fis;
    public static int count;
    public static int bytesRead;


    // Let D (debugger) be a JVMTI agent that updates a local of a compiled frame F owned by
    // thread T. In this test F corresponds to dontinline_testMethod.
    //
    // Issue: The VM thread executes an VM op on behalf of D to set the local in F. Doing so it
    // requests the deoptimization of F and then calls compiledVFrame::update_deferred_value(),
    // where frame::is_deoptimized_frame() returns false causing an assertion failure on SPARC where
    // NeedsDeoptSuspend is true.
    //
    // Analysis: The deoptimization of F is requested, while T is in the native method
    // java.io.FileInputStream::read0() and F is the direct caller of read0(). This is a special
    // case with NeedsDeoptSuspend in frame::deoptimize(). Effectively the deoptimization is not
    // done synchronously, instead T deoptimizes F at a later point upon return from the native
    // method.
    public static int dontinline_testMethod() {
        int zero = 0;
        int val = 0;
        try {
            val = fis.read(); // Will be inlined. Calls native method java.io.FileInputStream::read0()
            count++;
        } catch (IOException e) { /* ignored */ }
        return val + zero;
    }

    public static void main(String[] args) {
        System.out.println(name + " is up and running.");
        fis = new FileInputStream(FileDescriptor.in);
        bytesRead=0;
        while (true) {
            int val = dontinline_testMethod();
            if (val == SetLocalWhileThreadInNative.STOP) {
                System.out.println("Debuggee: received STOP message");
                System.exit(0);
            }
            bytesRead++;
            if ((bytesRead & ((1L << 14)-1)) == 0) {
                System.out.println("Called test method " + bytesRead + " times");
            }
        }
    }
}

 /********** test program **********/

public class SetLocalWhileThreadInNative extends TestScaffold {
    public static final int MESSAGE_COUNT = 10000;
    public static final String MESSAGE    = "0123456789";
    public static final int MESSAGE_SIZE  = MESSAGE.length();
    public static final int TOTAL_BYTES   = MESSAGE_COUNT * MESSAGE_SIZE;
    public static final int STOP = 255;

    ReferenceType mainClass;
    ThreadReference mainThread;

    SetLocalWhileThreadInNative (String args[]) {
        super(args);
    }

    public static void main(String[] args)
        throws Exception
    {
        new SetLocalWhileThreadInNative (args).startTests();
    }

    /********** test core **********/

    protected void runTests()
        throws Exception
    {
        String targetProgName = SetLocalWhileThreadInNativeTarget.class.getName();
        String testName = getClass().getSimpleName();

        // Start debuggee and obtain reference to main thread an main class
        BreakpointEvent bpe = startToMain(targetProgName);
        mainClass = bpe.location().declaringType();
        mainThread = bpe.thread();

        // Resume debuggee send some bytes
        vm().resume();
        OutputStream os = vm().process().getOutputStream();
        byte[] ba = MESSAGE.getBytes();
        for (int i = 0; i < MESSAGE_COUNT; i++) {
            os.write(ba);
        }
        os.flush();

        // Wait for the debugee to read all the bytes.
        int bytesRead = 0;
        Field bytesReadField = mainClass.fieldByName("bytesRead");
        do {
            bytesRead = ((PrimitiveValue)mainClass.getValue(bytesReadField)).intValue();
            System.out.println("debugee has read " + bytesRead + " of " + TOTAL_BYTES);
            Thread.sleep(500);
        } while (bytesRead < TOTAL_BYTES);

        // By now  dontinline_testMethod() will be compiled. The debugee will be blocked in java.io.FileInputStream::read0().
        // Now set local variable in dontinline_testMethod().
        vm().suspend();
        System.out.println("Debuggee Stack:");
        List<StackFrame> stack_frames = mainThread.frames();
        int i = 0;
        for (StackFrame ff : stack_frames) {
            System.out.println("frame[" + i++ +"]: " + ff.location().method());
        }
        StackFrame frame = mainThread.frame(2);
        Asserts.assertEQ(frame.location().method().toString(), "SetLocalWhileThreadInNativeTarget.dontinline_testMethod()");
        List<LocalVariable> localVars = frame.visibleVariables();
        boolean changedLocal = false;
        boolean caughtOFE = false;
        for (LocalVariable lv : localVars) {
            if (lv.name().equals("zero")) {
                try {
                    frame.setValue(lv, vm().mirrorOf(0)); // triggers deoptimization!
                    changedLocal = true;
                } catch (OpaqueFrameException e) {
                    caughtOFE = true;
                }
            }
        }
        boolean isVirtualThread = DebuggeeWrapper.isVirtual();
        Asserts.assertTrue(caughtOFE == isVirtualThread);
        Asserts.assertTrue(changedLocal == !isVirtualThread);

        // signal stop
        os.write(STOP);
        os.flush();


        // resume the target listening for events
        listenUntilVMDisconnect();


        // deal with results of test if anything has called failure("foo")
        // testFailed will be true
        if (!testFailed) {
            println(testName + ": passed");
        } else {
            throw new Exception(testName + ": failed");
        }
    }
}