< prev index next >

test/hotspot/jtreg/runtime/clinit/ClassInitBarrier.java

Print this page

  1 /*
  2  * Copyright (c) 2019, 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  * @library /test/lib
 27  *
 28  * @requires !vm.graal.enabled
 29  *
 30  * @run main/othervm/native -Xbatch -XX:CompileCommand=dontinline,*::test* -Xint                   -DTHROW=false -Xcheck:jni ClassInitBarrier
 31  * @run main/othervm/native -Xbatch -XX:CompileCommand=dontinline,*::test* -Xint                   -DTHROW=true  -Xcheck:jni ClassInitBarrier
 32  *
 33  * @run main/othervm/native -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:TieredStopAtLevel=1 -DTHROW=false -Xcheck:jni ClassInitBarrier
 34  * @run main/othervm/native -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:TieredStopAtLevel=1 -DTHROW=true  -Xcheck:jni ClassInitBarrier
 35  *
 36  * @run main/othervm/native -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:-TieredCompilation  -DTHROW=false -Xcheck:jni ClassInitBarrier
 37  * @run main/othervm/native -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:-TieredCompilation  -DTHROW=true  -Xcheck:jni ClassInitBarrier
 38  *
 39  * @run main/othervm/native -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:TieredStopAtLevel=1 -DTHROW=false -XX:CompileCommand=dontinline,*::static* -Xcheck:jni ClassInitBarrier
 40  * @run main/othervm/native -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:TieredStopAtLevel=1 -DTHROW=true  -XX:CompileCommand=dontinline,*::static* -Xcheck:jni ClassInitBarrier
 41  * @run main/othervm/native -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:-TieredCompilation  -DTHROW=false -XX:CompileCommand=dontinline,*::static* -Xcheck:jni ClassInitBarrier
 42  * @run main/othervm/native -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:-TieredCompilation  -DTHROW=true  -XX:CompileCommand=dontinline,*::static* -Xcheck:jni ClassInitBarrier
 43  *
 44  * @run main/othervm/native -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:TieredStopAtLevel=1 -DTHROW=false -XX:CompileCommand=exclude,*::static* -Xcheck:jni ClassInitBarrier
 45  * @run main/othervm/native -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:TieredStopAtLevel=1 -DTHROW=true  -XX:CompileCommand=exclude,*::static* -Xcheck:jni ClassInitBarrier
 46  * @run main/othervm/native -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:-TieredCompilation  -DTHROW=false -XX:CompileCommand=exclude,*::static* -Xcheck:jni ClassInitBarrier
 47  * @run main/othervm/native -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:-TieredCompilation  -DTHROW=true  -XX:CompileCommand=exclude,*::static* -Xcheck:jni ClassInitBarrier
 48  */
 49 
 50 import jdk.test.lib.Asserts;
 51 
 52 import java.util.*;
 53 import java.util.concurrent.atomic.AtomicBoolean;
 54 import java.util.concurrent.atomic.AtomicInteger;
 55 import java.util.function.Consumer;
 56 
 57 public class ClassInitBarrier {
 58     static {
 59         System.loadLibrary("ClassInitBarrier");
 60 
 61         if (!init()) {
 62             throw new Error("init failed");
 63         }
 64     }
 65 
 66     static native boolean init();
 67 
 68     static final boolean THROW = Boolean.getBoolean("THROW");
 69 








 70     static class Test {

 71         static class A {
 72             static {
 73                 if (!init(B.class)) {
 74                     throw new Error("init failed");
 75                 }
 76 
 77                 changePhase(Phase.IN_PROGRESS);
 78                 runTests();      // interpreted mode
 79                 warmup();        // trigger compilation
 80                 runTests();      // compiled mode
 81 
 82                 ensureBlocked(); // ensure still blocked
 83                 maybeThrow();    // fail initialization if needed
 84 
 85                 changePhase(Phase.FINISHED);
 86             }
 87 
 88             static              void staticM(Runnable action) { action.run(); }
 89             static synchronized void staticS(Runnable action) { action.run(); }
 90             static native       void staticN(Runnable action);
 91 
 92             static int staticF;
 93 
 94             int f;
 95             void m() {}
 96 
 97             static native boolean init(Class<B> cls);
 98         }
 99 
100         static class B extends A {}
101 
102         static void testInvokeStatic(Runnable action)       { A.staticM(action); }
103         static void testInvokeStaticSync(Runnable action)   { A.staticS(action); }
104         static void testInvokeStaticNative(Runnable action) { A.staticN(action); }
105 
106         static int  testGetStatic(Runnable action)    { int v = A.staticF; action.run(); return v;   }
107         static void testPutStatic(Runnable action)    { A.staticF = 1;     action.run(); }
108         static A    testNewInstanceA(Runnable action) { A obj = new A();   action.run(); return obj; }
109         static B    testNewInstanceB(Runnable action) { B obj = new B();   action.run(); return obj; }
110 
111         static int  testGetField(A recv, Runnable action)      { int v = recv.f; action.run(); return v; }
112         static void testPutField(A recv, Runnable action)      { recv.f = 1;     action.run(); }
113         static void testInvokeVirtual(A recv, Runnable action) { recv.m();       action.run(); }
114 
115         static native void testInvokeStaticJNI(Runnable action);
116         static native void testInvokeStaticSyncJNI(Runnable action);
117         static native void testInvokeStaticNativeJNI(Runnable action);
118 
119         static native int  testGetStaticJNI(Runnable action);
120         static native void testPutStaticJNI(Runnable action);
121         static native A    testNewInstanceAJNI(Runnable action);
122         static native B    testNewInstanceBJNI(Runnable action);
123 
124         static native int  testGetFieldJNI(A recv, Runnable action);
125         static native void testPutFieldJNI(A recv, Runnable action);
126         static native void testInvokeVirtualJNI(A recv, Runnable action);
127 
128         static void runTests() {
129             checkBlockingAction(Test::testInvokeStatic);       // invokestatic
130             checkBlockingAction(Test::testInvokeStaticSync);   // invokestatic
131             checkBlockingAction(Test::testInvokeStaticNative); // invokestatic
132             checkBlockingAction(Test::testGetStatic);          // getstatic
133             checkBlockingAction(Test::testPutStatic);          // putstatic
134             checkBlockingAction(Test::testNewInstanceA);       // new
135 
136             checkNonBlockingAction(Test::testInvokeStaticJNI);       // invokestatic
137             checkNonBlockingAction(Test::testInvokeStaticSyncJNI);   // invokestatic
138             checkNonBlockingAction(Test::testInvokeStaticNativeJNI); // invokestatic
139             checkNonBlockingAction(Test::testGetStaticJNI);          // getstatic
140             checkNonBlockingAction(Test::testPutStaticJNI);          // putstatic
141             checkBlockingAction(Test::testNewInstanceAJNI);          // new
142 
143             A recv = testNewInstanceB(NON_BLOCKING.get());  // trigger B initialization
144             checkNonBlockingAction(Test::testNewInstanceB); // new: NO BLOCKING: same thread: A being initialized, B fully initialized
145 
146             checkNonBlockingAction(recv, Test::testGetField);      // getfield
147             checkNonBlockingAction(recv, Test::testPutField);      // putfield
148             checkNonBlockingAction(recv, Test::testInvokeVirtual); // invokevirtual
149 
150             checkNonBlockingAction(Test::testNewInstanceBJNI);        // new: NO BLOCKING: same thread: A being initialized, B fully initialized
151             checkNonBlockingAction(recv, Test::testGetFieldJNI);      // getfield
152             checkNonBlockingAction(recv, Test::testPutFieldJNI);      // putfield
153             checkNonBlockingAction(recv, Test::testInvokeVirtualJNI); // invokevirtual
154         }
155 
156         static void warmup() {

157             for (int i = 0; i < 20_000; i++) {
158                 testInvokeStatic(      NON_BLOCKING_WARMUP);
159                 testInvokeStaticNative(NON_BLOCKING_WARMUP);
160                 testInvokeStaticSync(  NON_BLOCKING_WARMUP);
161                 testGetStatic(         NON_BLOCKING_WARMUP);
162                 testPutStatic(         NON_BLOCKING_WARMUP);
163                 testNewInstanceA(      NON_BLOCKING_WARMUP);
164                 testNewInstanceB(      NON_BLOCKING_WARMUP);
165 
166                 testGetField(new B(),      NON_BLOCKING_WARMUP);
167                 testPutField(new B(),      NON_BLOCKING_WARMUP);
168                 testInvokeVirtual(new B(), NON_BLOCKING_WARMUP);
169             }
170         }
171 
172         static void run() {
173             execute(ExceptionInInitializerError.class, () -> triggerInitialization(A.class));
174             ensureFinished();
175             runTests(); // after initialization is over
176         }
177     }
178 
179     // ============================================================================================================== //
180 
181     static void execute(Class<? extends Throwable> expectedExceptionClass, Runnable action) {
182         try {
183             action.run();
184             if (THROW) throw failure("no exception thrown");
185         } catch (Throwable e) {
186             if (THROW) {
187                 if (e.getClass() == expectedExceptionClass) {
188                     // expected

248                 if (ex.getClass() != NoClassDefFoundError.class) {
249                     throw new AssertionError(thr + ": wrong exception thrown", ex);
250                 }
251             } else {
252                 if (FAILED_THREADS.containsKey(thr)) {
253                     Throwable ex = FAILED_THREADS.get(thr);
254                     throw new AssertionError(thr + ": exception thrown", ex);
255                 }
256             }
257         }
258         if (THROW) {
259             Asserts.assertEquals(BLOCKING_COUNTER.get(), 0);
260         } else {
261             Asserts.assertEquals(BLOCKING_COUNTER.get(), BLOCKING_ACTIONS.get());
262         }
263 
264         dumpInfo();
265     }
266 
267     interface TestCase0 {
268         void run(Runnable runnable);
269     }
270 
271     interface TestCase1<T> {
272         void run(T arg, Runnable runnable);
273     }
274 
275     enum Phase { BEFORE_INIT, IN_PROGRESS, FINISHED, INIT_FAILURE }
276 
277     static volatile Phase phase = Phase.BEFORE_INIT;
278 
279     static void changePhase(Phase newPhase) {
280         dumpInfo();
281 
282         Phase oldPhase = phase;
283         switch (oldPhase) {
284             case BEFORE_INIT:
285                 Asserts.assertEquals(NON_BLOCKING_ACTIONS.get(), 0);
286                 Asserts.assertEquals(NON_BLOCKING_COUNTER.get(), 0);
287 
288                 Asserts.assertEquals(BLOCKING_ACTIONS.get(),     0);
289                 Asserts.assertEquals(BLOCKING_COUNTER.get(),     0);
290                 break;
291             case IN_PROGRESS:
292                 Asserts.assertEquals(NON_BLOCKING_COUNTER.get(), NON_BLOCKING_ACTIONS.get());

322             if (phase != validPhase) {
323                 throw new AssertionError("NON_BLOCKING: wrong phase: " + phase);
324             }
325         };
326     }
327 
328     @FunctionalInterface
329     interface Factory<V> {
330         V get();
331     }
332 
333     static final AtomicInteger NON_BLOCKING_COUNTER = new AtomicInteger(0);
334     static final AtomicInteger NON_BLOCKING_ACTIONS = new AtomicInteger(0);
335     static final Factory<Runnable> NON_BLOCKING = () -> disposableAction(phase, NON_BLOCKING_COUNTER, NON_BLOCKING_ACTIONS);
336 
337     static final AtomicInteger BLOCKING_COUNTER = new AtomicInteger(0);
338     static final AtomicInteger BLOCKING_ACTIONS = new AtomicInteger(0);
339     static final Factory<Runnable> BLOCKING     = () -> disposableAction(Phase.FINISHED, BLOCKING_COUNTER, BLOCKING_ACTIONS);
340 
341     static void checkBlockingAction(TestCase0 r) {

342         switch (phase) {
343             case IN_PROGRESS: {
344                 // Barrier during class initalization.
345                 r.run(NON_BLOCKING.get());             // initializing thread
346                 checkBlocked(ON_BLOCK, ON_FAILURE, r); // different thread
347                 break;
348             }
349             case FINISHED: {
350                 // No barrier after class initalization is over.
351                 r.run(NON_BLOCKING.get()); // initializing thread
352                 checkNotBlocked(r);        // different thread
353                 break;
354             }
355             case INIT_FAILURE: {
356                 // Exception is thrown after class initialization failed.
357                 TestCase0 test = action -> execute(NoClassDefFoundError.class, () -> r.run(action));
358 
359                 test.run(NON_BLOCKING.get()); // initializing thread
360                 checkNotBlocked(test);        // different thread
361                 break;
362             }
363             default: throw new Error("wrong phase: " + phase);
364         }
365     }
366 
367     static void checkNonBlockingAction(TestCase0 r) {
368         r.run(NON_BLOCKING.get()); // initializing thread
369         checkNotBlocked(r);        // different thread
370     }
371 
372     static <T> void checkNonBlockingAction(T recv, TestCase1<T> r) {
373         r.run(recv, NON_BLOCKING.get());                  // initializing thread
374         checkNotBlocked((action) -> r.run(recv, action)); // different thread
375     }
376 
377     static void checkFailingAction(TestCase0 r) {
378         r.run(NON_BLOCKING.get()); // initializing thread
379         checkNotBlocked(r);        // different thread
380     }
381 
382     static void triggerInitialization(Class<?> cls) {
383         try {
384             Class<?> loadedClass = Class.forName(cls.getName(), true, cls.getClassLoader());
385             if (loadedClass != cls) {
386                 throw new Error("wrong class");
387             }
388         } catch (ClassNotFoundException e) {
389             throw new Error(e);
390         }
391     }
392 
393     static void checkBlocked(Consumer<Thread> onBlockHandler, Thread.UncaughtExceptionHandler onException, TestCase0 r) {
394         Thread thr = new Thread(() -> {
395             try {
396                 r.run(BLOCKING.get());
397                 System.out.println("Thread " + Thread.currentThread() + ": Finished successfully");
398             } catch(Throwable e) {
399                 System.out.println("Thread " + Thread.currentThread() + ": Exception thrown: " + e);
400                 if (!THROW) {
401                     e.printStackTrace();
402                 }
403                 throw e;
404             }
405         } );
406         thr.setUncaughtExceptionHandler(onException);
407 
408         thr.start();
409         try {
410             thr.join(100);
411 
412             dump(thr);
413             if (thr.isAlive()) {
414                 onBlockHandler.accept(thr); // blocked
415             } else {
416                 throw new AssertionError("not blocked");
417             }
418         } catch (InterruptedException e) {
419             throw new Error(e);
420         }
421     }
422 
423     static void checkNotBlocked(TestCase0 r) {
424         final Thread thr = new Thread(() -> r.run(NON_BLOCKING.get()));
425         final Throwable[] ex = new Throwable[1];
426         thr.setUncaughtExceptionHandler((t, e) -> {
427             if (thr != t) {
428                 ex[0] = new Error("wrong thread: " + thr + " vs " + t);
429             } else {
430                 ex[0] = e;
431             }
432         });
433 
434         thr.start();
435         try {
436             thr.join(15_000);
437             if (thr.isAlive()) {
438                 dump(thr);
439                 throw new AssertionError("blocked");
440             }
441         } catch (InterruptedException e) {
442             throw new Error(e);
443         }
444 

  1 /*
  2  * Copyright (c) 2019, 2022, 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  * @library /test/lib
 27  *
 28  * @requires !vm.graal.enabled
 29  *
 30  * @run main/othervm/native -XX:+EnableValhalla -Xbatch -XX:CompileCommand=dontinline,*::test* -Xint                   -DTHROW=false -Xcheck:jni ClassInitBarrier
 31  * @run main/othervm/native -XX:+EnableValhalla -Xbatch -XX:CompileCommand=dontinline,*::test* -Xint                   -DTHROW=true  -Xcheck:jni ClassInitBarrier
 32  *
 33  * @run main/othervm/native -XX:+EnableValhalla -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:TieredStopAtLevel=1 -DTHROW=false -Xcheck:jni ClassInitBarrier
 34  * @run main/othervm/native -XX:+EnableValhalla -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:TieredStopAtLevel=1 -DTHROW=true  -Xcheck:jni ClassInitBarrier
 35  *
 36  * @run main/othervm/native -XX:+EnableValhalla -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:-TieredCompilation  -DTHROW=false -Xcheck:jni ClassInitBarrier
 37  * @run main/othervm/native -XX:+EnableValhalla -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:-TieredCompilation  -DTHROW=true  -Xcheck:jni ClassInitBarrier
 38  *
 39  * @run main/othervm/native -XX:+EnableValhalla -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:TieredStopAtLevel=1 -DTHROW=false -XX:CompileCommand=dontinline,*::static* -Xcheck:jni ClassInitBarrier
 40  * @run main/othervm/native -XX:+EnableValhalla -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:TieredStopAtLevel=1 -DTHROW=true  -XX:CompileCommand=dontinline,*::static* -Xcheck:jni ClassInitBarrier
 41  * @run main/othervm/native -XX:+EnableValhalla -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:-TieredCompilation  -DTHROW=false -XX:CompileCommand=dontinline,*::static* -Xcheck:jni ClassInitBarrier
 42  * @run main/othervm/native -XX:+EnableValhalla -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:-TieredCompilation  -DTHROW=true  -XX:CompileCommand=dontinline,*::static* -Xcheck:jni ClassInitBarrier
 43  *
 44  * @run main/othervm/native -XX:+EnableValhalla -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:TieredStopAtLevel=1 -DTHROW=false -XX:CompileCommand=exclude,*::static* -Xcheck:jni ClassInitBarrier
 45  * @run main/othervm/native -XX:+EnableValhalla -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:TieredStopAtLevel=1 -DTHROW=true  -XX:CompileCommand=exclude,*::static* -Xcheck:jni ClassInitBarrier
 46  * @run main/othervm/native -XX:+EnableValhalla -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:-TieredCompilation  -DTHROW=false -XX:CompileCommand=exclude,*::static* -Xcheck:jni ClassInitBarrier
 47  * @run main/othervm/native -XX:+EnableValhalla -Xbatch -XX:CompileCommand=dontinline,*::test* -XX:-TieredCompilation  -DTHROW=true  -XX:CompileCommand=exclude,*::static* -Xcheck:jni ClassInitBarrier
 48  */
 49 
 50 import jdk.test.lib.Asserts;
 51 
 52 import java.util.*;
 53 import java.util.concurrent.atomic.AtomicBoolean;
 54 import java.util.concurrent.atomic.AtomicInteger;
 55 import java.util.function.Consumer;
 56 
 57 public class ClassInitBarrier {
 58     static {
 59         System.loadLibrary("ClassInitBarrier");
 60 
 61         if (!init()) {
 62             throw new Error("init failed");
 63         }
 64     }
 65 
 66     static native boolean init();
 67 
 68     static final boolean THROW = Boolean.getBoolean("THROW");
 69 
 70     static value class MyValue {
 71         int x = 42;
 72 
 73         void verify() {
 74             Asserts.assertEquals(x, 42);
 75         }
 76     }
 77 
 78     static class Test {
 79 
 80         static class A {
 81             static {
 82                 if (!init(B.class)) {
 83                     throw new Error("init failed");
 84                 }
 85 
 86                 changePhase(Phase.IN_PROGRESS);
 87                 runTests();      // interpreted mode
 88                 warmup();        // trigger compilation
 89                 runTests();      // compiled mode
 90 
 91                 ensureBlocked(); // ensure still blocked
 92                 maybeThrow();    // fail initialization if needed
 93 
 94                 changePhase(Phase.FINISHED);
 95             }
 96 
 97             static              void staticM(Runnable action, MyValue val) { action.run(); val.verify(); }
 98             static synchronized void staticS(Runnable action, MyValue val) { action.run(); val.verify(); }
 99             static native       void staticN(Runnable action, MyValue val);
100 
101             static int staticF;
102 
103             int f;
104             void m() {}
105 
106             static native boolean init(Class<B> cls);
107         }
108 
109         static class B extends A {}
110 
111         static void testInvokeStatic(Runnable action, MyValue val)       { A.staticM(action, val); }
112         static void testInvokeStaticSync(Runnable action, MyValue val)   { A.staticS(action, val); }
113         static void testInvokeStaticNative(Runnable action, MyValue val) { A.staticN(action, val); }
114 
115         static int  testGetStatic(Runnable action, MyValue val)    { int v = A.staticF; action.run(); val.verify(); return v;   }
116         static void testPutStatic(Runnable action, MyValue val)    { A.staticF = 1;     action.run(); val.verify(); }
117         static A    testNewInstanceA(Runnable action, MyValue val) { A obj = new A();   action.run(); val.verify(); return obj; }
118         static B    testNewInstanceB(Runnable action, MyValue val) { B obj = new B();   action.run(); val.verify(); return obj; }
119 
120         static int  testGetField(A recv, Runnable action, MyValue val)      { int v = recv.f; action.run(); val.verify(); return v; }
121         static void testPutField(A recv, Runnable action, MyValue val)      { recv.f = 1;     action.run(); val.verify(); }
122         static void testInvokeVirtual(A recv, Runnable action, MyValue val) { recv.m();       action.run(); val.verify(); }
123 
124         static native void testInvokeStaticJNI(Runnable action, MyValue val);
125         static native void testInvokeStaticSyncJNI(Runnable action, MyValue val);
126         static native void testInvokeStaticNativeJNI(Runnable action, MyValue val);
127 
128         static native int  testGetStaticJNI(Runnable action, MyValue val);
129         static native void testPutStaticJNI(Runnable action, MyValue val);
130         static native A    testNewInstanceAJNI(Runnable action, MyValue val);
131         static native B    testNewInstanceBJNI(Runnable action, MyValue val);
132 
133         static native int  testGetFieldJNI(A recv, Runnable action, MyValue val);
134         static native void testPutFieldJNI(A recv, Runnable action, MyValue val);
135         static native void testInvokeVirtualJNI(A recv, Runnable action, MyValue val);
136 
137         static void runTests() {
138             checkBlockingAction(Test::testInvokeStatic);       // invokestatic
139             checkBlockingAction(Test::testInvokeStaticSync);   // invokestatic
140             checkBlockingAction(Test::testInvokeStaticNative); // invokestatic
141             checkBlockingAction(Test::testGetStatic);          // getstatic
142             checkBlockingAction(Test::testPutStatic);          // putstatic
143             checkBlockingAction(Test::testNewInstanceA);       // new
144 
145             checkNonBlockingAction(Test::testInvokeStaticJNI);       // invokestatic
146             checkNonBlockingAction(Test::testInvokeStaticSyncJNI);   // invokestatic
147             checkNonBlockingAction(Test::testInvokeStaticNativeJNI); // invokestatic
148             checkNonBlockingAction(Test::testGetStaticJNI);          // getstatic
149             checkNonBlockingAction(Test::testPutStaticJNI);          // putstatic
150             checkBlockingAction(Test::testNewInstanceAJNI);          // new
151 
152             A recv = testNewInstanceB(NON_BLOCKING.get(), new MyValue());  // trigger B initialization
153             checkNonBlockingAction(Test::testNewInstanceB); // new: NO BLOCKING: same thread: A being initialized, B fully initialized
154 
155             checkNonBlockingAction(recv, Test::testGetField);      // getfield
156             checkNonBlockingAction(recv, Test::testPutField);      // putfield
157             checkNonBlockingAction(recv, Test::testInvokeVirtual); // invokevirtual
158 
159             checkNonBlockingAction(Test::testNewInstanceBJNI);        // new: NO BLOCKING: same thread: A being initialized, B fully initialized
160             checkNonBlockingAction(recv, Test::testGetFieldJNI);      // getfield
161             checkNonBlockingAction(recv, Test::testPutFieldJNI);      // putfield
162             checkNonBlockingAction(recv, Test::testInvokeVirtualJNI); // invokevirtual
163         }
164 
165         static void warmup() {
166             MyValue val = new MyValue();
167             for (int i = 0; i < 20_000; i++) {
168                 testInvokeStatic(      NON_BLOCKING_WARMUP, val);
169                 testInvokeStaticNative(NON_BLOCKING_WARMUP, val);
170                 testInvokeStaticSync(  NON_BLOCKING_WARMUP, val);
171                 testGetStatic(         NON_BLOCKING_WARMUP, val);
172                 testPutStatic(         NON_BLOCKING_WARMUP, val);
173                 testNewInstanceA(      NON_BLOCKING_WARMUP, val);
174                 testNewInstanceB(      NON_BLOCKING_WARMUP, val);
175 
176                 testGetField(new B(),      NON_BLOCKING_WARMUP, val);
177                 testPutField(new B(),      NON_BLOCKING_WARMUP, val);
178                 testInvokeVirtual(new B(), NON_BLOCKING_WARMUP, val);
179             }
180         }
181 
182         static void run() {
183             execute(ExceptionInInitializerError.class, () -> triggerInitialization(A.class));
184             ensureFinished();
185             runTests(); // after initialization is over
186         }
187     }
188 
189     // ============================================================================================================== //
190 
191     static void execute(Class<? extends Throwable> expectedExceptionClass, Runnable action) {
192         try {
193             action.run();
194             if (THROW) throw failure("no exception thrown");
195         } catch (Throwable e) {
196             if (THROW) {
197                 if (e.getClass() == expectedExceptionClass) {
198                     // expected

258                 if (ex.getClass() != NoClassDefFoundError.class) {
259                     throw new AssertionError(thr + ": wrong exception thrown", ex);
260                 }
261             } else {
262                 if (FAILED_THREADS.containsKey(thr)) {
263                     Throwable ex = FAILED_THREADS.get(thr);
264                     throw new AssertionError(thr + ": exception thrown", ex);
265                 }
266             }
267         }
268         if (THROW) {
269             Asserts.assertEquals(BLOCKING_COUNTER.get(), 0);
270         } else {
271             Asserts.assertEquals(BLOCKING_COUNTER.get(), BLOCKING_ACTIONS.get());
272         }
273 
274         dumpInfo();
275     }
276 
277     interface TestCase0 {
278         void run(Runnable runnable, MyValue val);
279     }
280 
281     interface TestCase1<T> {
282         void run(T arg, Runnable runnable, MyValue val);
283     }
284 
285     enum Phase { BEFORE_INIT, IN_PROGRESS, FINISHED, INIT_FAILURE }
286 
287     static volatile Phase phase = Phase.BEFORE_INIT;
288 
289     static void changePhase(Phase newPhase) {
290         dumpInfo();
291 
292         Phase oldPhase = phase;
293         switch (oldPhase) {
294             case BEFORE_INIT:
295                 Asserts.assertEquals(NON_BLOCKING_ACTIONS.get(), 0);
296                 Asserts.assertEquals(NON_BLOCKING_COUNTER.get(), 0);
297 
298                 Asserts.assertEquals(BLOCKING_ACTIONS.get(),     0);
299                 Asserts.assertEquals(BLOCKING_COUNTER.get(),     0);
300                 break;
301             case IN_PROGRESS:
302                 Asserts.assertEquals(NON_BLOCKING_COUNTER.get(), NON_BLOCKING_ACTIONS.get());

332             if (phase != validPhase) {
333                 throw new AssertionError("NON_BLOCKING: wrong phase: " + phase);
334             }
335         };
336     }
337 
338     @FunctionalInterface
339     interface Factory<V> {
340         V get();
341     }
342 
343     static final AtomicInteger NON_BLOCKING_COUNTER = new AtomicInteger(0);
344     static final AtomicInteger NON_BLOCKING_ACTIONS = new AtomicInteger(0);
345     static final Factory<Runnable> NON_BLOCKING = () -> disposableAction(phase, NON_BLOCKING_COUNTER, NON_BLOCKING_ACTIONS);
346 
347     static final AtomicInteger BLOCKING_COUNTER = new AtomicInteger(0);
348     static final AtomicInteger BLOCKING_ACTIONS = new AtomicInteger(0);
349     static final Factory<Runnable> BLOCKING     = () -> disposableAction(Phase.FINISHED, BLOCKING_COUNTER, BLOCKING_ACTIONS);
350 
351     static void checkBlockingAction(TestCase0 r) {
352         MyValue val = new MyValue();
353         switch (phase) {
354             case IN_PROGRESS: {
355                 // Barrier during class initalization.
356                 r.run(NON_BLOCKING.get(), val);             // initializing thread
357                 checkBlocked(ON_BLOCK, ON_FAILURE, r); // different thread
358                 break;
359             }
360             case FINISHED: {
361                 // No barrier after class initalization is over.
362                 r.run(NON_BLOCKING.get(), val); // initializing thread
363                 checkNotBlocked(r);        // different thread
364                 break;
365             }
366             case INIT_FAILURE: {
367                 // Exception is thrown after class initialization failed.
368                 TestCase0 test = (action, valarg) -> execute(NoClassDefFoundError.class, () -> r.run(action, valarg));
369 
370                 test.run(NON_BLOCKING.get(), val); // initializing thread
371                 checkNotBlocked(test);        // different thread
372                 break;
373             }
374             default: throw new Error("wrong phase: " + phase);
375         }
376     }
377 
378     static void checkNonBlockingAction(TestCase0 r) {
379         r.run(NON_BLOCKING.get(), new MyValue()); // initializing thread
380         checkNotBlocked(r);        // different thread
381     }
382 
383     static <T> void checkNonBlockingAction(T recv, TestCase1<T> r) {
384         r.run(recv, NON_BLOCKING.get(), new MyValue());                  // initializing thread
385         checkNotBlocked((action, val) -> r.run(recv, action, val)); // different thread
386     }
387 
388     static void checkFailingAction(TestCase0 r) {
389         r.run(NON_BLOCKING.get(), new MyValue()); // initializing thread
390         checkNotBlocked(r);        // different thread
391     }
392 
393     static void triggerInitialization(Class<?> cls) {
394         try {
395             Class<?> loadedClass = Class.forName(cls.getName(), true, cls.getClassLoader());
396             if (loadedClass != cls) {
397                 throw new Error("wrong class");
398             }
399         } catch (ClassNotFoundException e) {
400             throw new Error(e);
401         }
402     }
403 
404     static void checkBlocked(Consumer<Thread> onBlockHandler, Thread.UncaughtExceptionHandler onException, TestCase0 r) {
405         Thread thr = new Thread(() -> {
406             try {
407                 r.run(BLOCKING.get(), new MyValue());
408                 System.out.println("Thread " + Thread.currentThread() + ": Finished successfully");
409             } catch(Throwable e) {
410                 System.out.println("Thread " + Thread.currentThread() + ": Exception thrown: " + e);
411                 if (!THROW) {
412                     e.printStackTrace();
413                 }
414                 throw e;
415             }
416         } );
417         thr.setUncaughtExceptionHandler(onException);
418 
419         thr.start();
420         try {
421             thr.join(100);
422 
423             dump(thr);
424             if (thr.isAlive()) {
425                 onBlockHandler.accept(thr); // blocked
426             } else {
427                 throw new AssertionError("not blocked");
428             }
429         } catch (InterruptedException e) {
430             throw new Error(e);
431         }
432     }
433 
434     static void checkNotBlocked(TestCase0 r) {
435         final Thread thr = new Thread(() -> r.run(NON_BLOCKING.get(), new MyValue()));
436         final Throwable[] ex = new Throwable[1];
437         thr.setUncaughtExceptionHandler((t, e) -> {
438             if (thr != t) {
439                 ex[0] = new Error("wrong thread: " + thr + " vs " + t);
440             } else {
441                 ex[0] = e;
442             }
443         });
444 
445         thr.start();
446         try {
447             thr.join(15_000);
448             if (thr.isAlive()) {
449                 dump(thr);
450                 throw new AssertionError("blocked");
451             }
452         } catch (InterruptedException e) {
453             throw new Error(e);
454         }
455 
< prev index next >