Path: blob/master/test/hotspot/jtreg/gc/shenandoah/TestEvilSyncBug.java
40942 views
/*1* Copyright (c) 2016, 2020, Red Hat, Inc. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324/*25* @test TestEvilSyncBug26* @summary Tests for crash/assert when attaching init thread during shutdown27* @requires vm.gc.Shenandoah28* @library /test/lib29* @modules java.base/jdk.internal.misc30* java.management31* @run driver/timeout=480 TestEvilSyncBug32*/3334import java.util.*;35import java.util.concurrent.*;36import java.util.concurrent.locks.*;3738import jdk.test.lib.process.ProcessTools;39import jdk.test.lib.process.OutputAnalyzer;4041public class TestEvilSyncBug {4243private static final int NUM_RUNS = 100;4445static Thread[] hooks = new MyHook[10000];4647public static void main(String[] args) throws Exception {48if (args.length > 0) {49test();50} else {51// Use 1/4 of available processors to avoid over-saturation.52int numJobs = Math.max(1, Runtime.getRuntime().availableProcessors() / 4);53ExecutorService pool = Executors.newFixedThreadPool(numJobs);54Future<?>[] fs = new Future<?>[NUM_RUNS];5556for (int c = 0; c < NUM_RUNS; c++) {57Callable<Void> task = () -> {58ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xms128m",59"-Xmx128m",60"-XX:+UnlockExperimentalVMOptions",61"-XX:+UnlockDiagnosticVMOptions",62"-XX:+UseShenandoahGC",63"-XX:ShenandoahGCHeuristics=aggressive",64"TestEvilSyncBug", "test");65OutputAnalyzer output = new OutputAnalyzer(pb.start());66output.shouldHaveExitValue(0);67return null;68};69fs[c] = pool.submit(task);70}7172for (Future<?> f : fs) {73f.get();74}7576pool.shutdown();77pool.awaitTermination(1, TimeUnit.HOURS);78}79}8081private static void test() throws Exception {8283for (int t = 0; t < hooks.length; t++) {84hooks[t] = new MyHook();85}8687ExecutorService service = Executors.newFixedThreadPool(882,89r -> {90Thread t = new Thread(r);91t.setDaemon(true);92return t;93}94);9596List<Future<?>> futures = new ArrayList<>();97for (int c = 0; c < 100; c++) {98Runtime.getRuntime().addShutdownHook(hooks[c]);99final Test[] tests = new Test[1000];100for (int t = 0; t < tests.length; t++) {101tests[t] = new Test();102}103104Future<?> f1 = service.submit(() -> {105Runtime.getRuntime().addShutdownHook(new MyHook());106IntResult2 r = new IntResult2();107for (Test test : tests) {108test.RL_Us(r);109}110});111Future<?> f2 = service.submit(() -> {112Runtime.getRuntime().addShutdownHook(new MyHook());113for (Test test : tests) {114test.WLI_Us();115}116});117118futures.add(f1);119futures.add(f2);120}121122for (Future<?> f : futures) {123f.get();124}125}126127public static class IntResult2 {128int r1, r2;129}130131public static class Test {132final StampedLock lock = new StampedLock();133134int x, y;135136public void RL_Us(IntResult2 r) {137StampedLock lock = this.lock;138long stamp = lock.readLock();139r.r1 = x;140r.r2 = y;141lock.unlock(stamp);142}143144public void WLI_Us() {145try {146StampedLock lock = this.lock;147long stamp = lock.writeLockInterruptibly();148x = 1;149y = 2;150lock.unlock(stamp);151} catch (InterruptedException e) {152throw new RuntimeException(e);153}154}155}156157private static class MyHook extends Thread {158@Override159public void run() {160try {161Thread.sleep(10);162} catch (Exception e) {}163}164}165166}167168169