Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/shenandoah/TestEvilSyncBug.java
32284 views
/*1* Copyright (c) 2016, 2020, Red Hat, Inc. All rights reserved.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation.6*7* This code is distributed in the hope that it will be useful, but WITHOUT8* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or9* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License10* version 2 for more details (a copy is included in the LICENSE file that11* accompanied this code).12*13* You should have received a copy of the GNU General Public License version14* 2 along with this work; if not, write to the Free Software Foundation,15* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.16*17* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA18* or visit www.oracle.com if you need additional information or have any19* questions.20*21*/2223/*24* @test TestEvilSyncBug25* @summary Tests for crash/assert when attaching init thread during shutdown26* @key gc27* @library /testlibrary28*29* @run driver/timeout=480 TestEvilSyncBug30*/3132import java.util.*;33import java.util.concurrent.*;34import java.util.concurrent.locks.*;3536import com.oracle.java.testlibrary.*;3738public class TestEvilSyncBug {3940private static final int NUM_RUNS = 100;4142static Thread[] hooks = new MyHook[10000];4344public static void main(String[] args) throws Exception {45if (args.length > 0) {46test();47} else {48// Use 1/4 of available processors to avoid over-saturation.49int numJobs = Math.max(1, Runtime.getRuntime().availableProcessors() / 4);50ExecutorService pool = Executors.newFixedThreadPool(numJobs);51Future<?>[] fs = new Future<?>[NUM_RUNS];5253for (int c = 0; c < NUM_RUNS; c++) {54Callable<Void> task = () -> {55ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xms128m",56"-Xmx128m",57"-XX:+UnlockExperimentalVMOptions",58"-XX:+UnlockDiagnosticVMOptions",59"-XX:+UseShenandoahGC",60"-XX:ShenandoahGCHeuristics=aggressive",61"TestEvilSyncBug", "test");62OutputAnalyzer output = new OutputAnalyzer(pb.start());63output.shouldHaveExitValue(0);64return null;65};66fs[c] = pool.submit(task);67}6869for (Future<?> f : fs) {70f.get();71}7273pool.shutdown();74pool.awaitTermination(1, TimeUnit.HOURS);75}76}7778private static void test() throws Exception {7980for (int t = 0; t < hooks.length; t++) {81hooks[t] = new MyHook();82}8384ExecutorService service = Executors.newFixedThreadPool(852,86r -> {87Thread t = new Thread(r);88t.setDaemon(true);89return t;90}91);9293List<Future<?>> futures = new ArrayList<>();94for (int c = 0; c < 100; c++) {95Runtime.getRuntime().addShutdownHook(hooks[c]);96final Test[] tests = new Test[1000];97for (int t = 0; t < tests.length; t++) {98tests[t] = new Test();99}100101Future<?> f1 = service.submit(() -> {102Runtime.getRuntime().addShutdownHook(new MyHook());103IntResult2 r = new IntResult2();104for (Test test : tests) {105test.RL_Us(r);106}107});108Future<?> f2 = service.submit(() -> {109Runtime.getRuntime().addShutdownHook(new MyHook());110for (Test test : tests) {111test.WLI_Us();112}113});114115futures.add(f1);116futures.add(f2);117}118119for (Future<?> f : futures) {120f.get();121}122}123124public static class IntResult2 {125int r1, r2;126}127128public static class Test {129final StampedLock lock = new StampedLock();130131int x, y;132133public void RL_Us(IntResult2 r) {134StampedLock lock = this.lock;135long stamp = lock.readLock();136r.r1 = x;137r.r2 = y;138lock.unlock(stamp);139}140141public void WLI_Us() {142try {143StampedLock lock = this.lock;144long stamp = lock.writeLockInterruptibly();145x = 1;146y = 2;147lock.unlock(stamp);148} catch (InterruptedException e) {149throw new RuntimeException(e);150}151}152}153154private static class MyHook extends Thread {155@Override156public void run() {157try {158Thread.sleep(10);159} catch (Exception e) {}160}161}162163}164165166