|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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 | +import java.lang.reflect.Method; |
| 25 | +import java.net.URL; |
| 26 | +import java.net.URLClassLoader; |
| 27 | +import java.util.ArrayList; |
| 28 | + |
| 29 | +/** |
| 30 | + * @test |
| 31 | + * @bug 8340313 |
| 32 | + * @summary Test that concurrent patching of oop immediates is thread-safe in C1. |
| 33 | + * @run main/othervm/timeout=480 -Xcomp -XX:CompileCommand=compileonly,TestConcurrentPatching::* -XX:TieredStopAtLevel=1 TestConcurrentPatching |
| 34 | + */ |
| 35 | + |
| 36 | +class MyClass { } |
| 37 | + |
| 38 | +class Holder { |
| 39 | + public static final MyClass OBJ1 = null; |
| 40 | + public static final MyClass OBJ2 = null; |
| 41 | + public static final MyClass OBJ3 = null; |
| 42 | + public static final MyClass OBJ4 = null; |
| 43 | + public static final MyClass OBJ5 = null; |
| 44 | + public static final MyClass OBJ6 = null; |
| 45 | + public static final MyClass OBJ7 = null; |
| 46 | + public static final MyClass OBJ8 = null; |
| 47 | + public static final MyClass OBJ9 = null; |
| 48 | + public static final MyClass OBJ10 = null; |
| 49 | + public static final MyClass OBJ11 = null; |
| 50 | + public static final MyClass OBJ12 = null; |
| 51 | + public static final MyClass OBJ13 = null; |
| 52 | + public static final MyClass OBJ14 = null; |
| 53 | + public static final MyClass OBJ15 = null; |
| 54 | + public static final MyClass OBJ16 = null; |
| 55 | + public static final MyClass OBJ17 = null; |
| 56 | + public static final MyClass OBJ18 = null; |
| 57 | + public static final MyClass OBJ19 = null; |
| 58 | + public static final MyClass OBJ20 = null; |
| 59 | +} |
| 60 | + |
| 61 | +public class TestConcurrentPatching { |
| 62 | + // Increase to 100_000 for a good chance of reproducing the issue with a single run |
| 63 | + static final int ITERATIONS = 1000; |
| 64 | + |
| 65 | + static Object field; |
| 66 | + |
| 67 | + // 'Holder' class is unloaded on first execution and therefore field |
| 68 | + // accesses require patching when the method is C1 compiled (with -Xcomp). |
| 69 | + public static void test() { |
| 70 | + field = Holder.OBJ1; |
| 71 | + field = Holder.OBJ2; |
| 72 | + field = Holder.OBJ3; |
| 73 | + field = Holder.OBJ4; |
| 74 | + field = Holder.OBJ5; |
| 75 | + field = Holder.OBJ6; |
| 76 | + field = Holder.OBJ7; |
| 77 | + field = Holder.OBJ8; |
| 78 | + field = Holder.OBJ9; |
| 79 | + field = Holder.OBJ10; |
| 80 | + field = Holder.OBJ11; |
| 81 | + field = Holder.OBJ12; |
| 82 | + field = Holder.OBJ13; |
| 83 | + field = Holder.OBJ14; |
| 84 | + field = Holder.OBJ15; |
| 85 | + field = Holder.OBJ16; |
| 86 | + field = Holder.OBJ17; |
| 87 | + field = Holder.OBJ18; |
| 88 | + field = Holder.OBJ19; |
| 89 | + field = Holder.OBJ20; |
| 90 | + } |
| 91 | + |
| 92 | + // Appendix of invokedynamic call sites is unloaded on first execution and |
| 93 | + // therefore requires patching when the method is C1 compiled (with -Xcomp). |
| 94 | + public static void testIndy() throws Throwable { |
| 95 | + field = (Runnable) () -> { }; |
| 96 | + field = (Runnable) () -> { }; |
| 97 | + field = (Runnable) () -> { }; |
| 98 | + field = (Runnable) () -> { }; |
| 99 | + field = (Runnable) () -> { }; |
| 100 | + field = (Runnable) () -> { }; |
| 101 | + field = (Runnable) () -> { }; |
| 102 | + field = (Runnable) () -> { }; |
| 103 | + field = (Runnable) () -> { }; |
| 104 | + field = (Runnable) () -> { }; |
| 105 | + } |
| 106 | + |
| 107 | + // Run 'test' by multiple threads to trigger concurrent patching of field accesses |
| 108 | + static void runWithThreads(Method method) { |
| 109 | + ArrayList<Thread> threads = new ArrayList<>(); |
| 110 | + for (int threadIdx = 0; threadIdx < 10; threadIdx++) { |
| 111 | + threads.add(new Thread(() -> { |
| 112 | + try { |
| 113 | + method.invoke(null); |
| 114 | + } catch (Exception e) { |
| 115 | + throw new IllegalStateException(e); |
| 116 | + } |
| 117 | + })); |
| 118 | + } |
| 119 | + threads.forEach(Thread::start); |
| 120 | + threads.forEach(t -> { |
| 121 | + try { |
| 122 | + t.join(); |
| 123 | + } catch (Throwable e) { |
| 124 | + throw new IllegalStateException(e); |
| 125 | + } |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + public static void main(String[] args) throws Exception { |
| 130 | + Class<?> thisClass = TestConcurrentPatching.class; |
| 131 | + ClassLoader defaultLoader = thisClass.getClassLoader(); |
| 132 | + URL classesDir = thisClass.getProtectionDomain().getCodeSource().getLocation(); |
| 133 | + |
| 134 | + // Load the test class multiple times with a separate class loader to make sure |
| 135 | + // that the 'Holder' class is unloaded for each compilation of method 'test' |
| 136 | + // and that the appendix of the invokedynamic call site is unloaded for each |
| 137 | + // compilation of method 'testIndy'. |
| 138 | + for (int i = 0; i < ITERATIONS; ++i) { |
| 139 | + URLClassLoader myLoader = URLClassLoader.newInstance(new URL[] {classesDir}, defaultLoader.getParent()); |
| 140 | + Class<?> testClass = Class.forName(thisClass.getCanonicalName(), true, myLoader); |
| 141 | + runWithThreads(testClass.getDeclaredMethod("test")); |
| 142 | + runWithThreads(testClass.getDeclaredMethod("testIndy")); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments