File: SaveResolutionErrorTest.java

package info (click to toggle)
openjdk-21 21.0.10~4ea-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 823,260 kB
  • sloc: java: 5,638,490; xml: 1,643,607; cpp: 1,298,236; ansic: 421,374; asm: 404,850; objc: 21,018; sh: 15,085; javascript: 11,217; python: 6,895; makefile: 2,368; perl: 357; awk: 351; sed: 172; jsp: 24
file content (176 lines) | stat: -rw-r--r-- 6,407 bytes parent folder | download | duplicates (10)
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
/*
 * Copyright (c) 2021, 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 8262046
 * @summary JVMS 5.4.3 If an attempt by the Java Virtual Machine to resolve a symbolic reference fails
 *          because an error is thrown that is an instance of LinkageError (or a subclass), then subsequent
 *          attempts to resolve the reference always fail with the same error that was thrown as a result of
 *          the initial resolution attempt.
 * @run main/othervm SaveResolutionErrorTest
 */

public class SaveResolutionErrorTest {
    static byte classfile_for_Tester[];
    static byte classfile_for_Loadee[];

    public static void main(java.lang.String[] args) throws Exception {
        ClassLoader appLoader = SaveResolutionErrorTest.class.getClassLoader();
        classfile_for_Tester = appLoader.getResourceAsStream("SaveResolutionErrorTest$Tester.class").readAllBytes();
        classfile_for_Loadee = appLoader.getResourceAsStream("SaveResolutionErrorTest$Loadee.class").readAllBytes();

        long started = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            System.out.println("Test: " + i);
            MyLoader loader = new MyLoader(appLoader);
            loader.doTest();

            if (System.currentTimeMillis() - started > 100000) {
                break;
            }
        }
        System.out.println("Succeed");
    }

    public static class Tester extends Thread {
        static int errorCount = 0;
        synchronized static void incrError() {
            ++ errorCount;
        }

        public volatile static boolean go = false;

        public static void doit() throws Exception {
            System.out.println(Tester.class.getClassLoader());

            Thread t1 = new Tester();
            Thread t2 = new Tester();

            t1.start();
            t2.start();

            go = true;

            t1.join();
            t2.join();


            System.out.println("errorCount = " + errorCount);

            if (errorCount != 0 && errorCount != 2) {
                throw new RuntimeException("errorCount should be 0 or 2 but is " + errorCount);
            }
        }

        static int foobar;
        static boolean debug = false;

        public void run() {
            while (!go) { Thread.onSpinWait(); }

            try {
                // The first thread who tries to resolve the CP entry for the "Loadee" class
                // should (most likely) result in a resolution error. This error, if it has
                // happened, must also be reported by the second thread.
                //
                // In some rare conditions, the resolution may succeed. In this case, both
                // threads must succeed. The reasons is:
                //
                //     The first thread who tries to resolve the CP entry gets a bad class, but
                //     when it tries to update the CP entry, the second thread has already succeeded in
                //     loading the class and has set the entry's tag to JVM_CONSTANT_Class.

                foobar += Loadee.value;
            } catch (Throwable t) {
                Throwable cause = t.getCause();
                if (cause != null) {
                    String s = cause.toString();
                    if (s.equals(t.toString())) { // wrong cause
                        t.printStackTrace();
                        throw new RuntimeException("wrong cause");
                    }
                }
                if (debug) {
                    System.out.println(t);
                } else {
                    synchronized (Tester.class) {
                        // Enable this block to see the stack trace
                        System.out.println("------");
                        t.printStackTrace();
                        System.out.println("");
                    }
                }
                incrError();
            }
        }
    }

    public static class Loadee {
        static int value = 1234;
    }

    static class MyLoader extends ClassLoader {
        static int count;
        static byte[] badclass = {1, 2, 3, 4, 5, 6, 7, 8};

        static {
            registerAsParallelCapable();
        }

        ClassLoader parent;

        MyLoader(ClassLoader parent) {
            this.parent = parent;
        }

        synchronized boolean hack() {
            ++ count;
            if ((count % 2) == 1) {
                return true;
            } else {
                return false;
            }
        }

        public Class loadClass(String name) throws ClassNotFoundException {
            if (name.equals("SaveResolutionErrorTest$Loadee")) {
                if (hack()) {
                    return defineClass(name, badclass, 0, badclass.length);
                } else {
                    return defineClass(name, classfile_for_Loadee, 0, classfile_for_Loadee.length);
                }
            }
            if (name.equals("SaveResolutionErrorTest$Tester")) {
                return defineClass(name, classfile_for_Tester, 0, classfile_for_Tester.length);
            }
            return parent.loadClass(name);
        }

        void doTest() throws Exception {
            Class c = Class.forName("SaveResolutionErrorTest$Tester", /*init=*/true, this);
            java.lang.reflect.Method m = c.getMethod("doit");
            m.invoke(null);
        }
    }
}