# HG changeset patch # User rkennke # Date 1477930754 -3600 # Mon Oct 31 17:19:14 2016 +0100 # Node ID 93a34924066519aae0acc00520f36f92ea77293f # Parent f66cf3bcac8eea7d88eff50f6cdf4ae74e91e708 [mq]: syncbug.patch diff --git a/src/share/vm/gc/shenandoah/shenandoahConcurrentThread.cpp b/src/share/vm/gc/shenandoah/shenandoahConcurrentThread.cpp --- a/src/share/vm/gc/shenandoah/shenandoahConcurrentThread.cpp +++ b/src/share/vm/gc/shenandoah/shenandoahConcurrentThread.cpp @@ -109,6 +109,7 @@ } if (heap->is_evacuation_in_progress()) { + MutexLocker mu(Threads_lock); heap->set_evacuation_in_progress(false); } heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::reset_bitmaps); diff --git a/src/share/vm/runtime/thread.cpp b/src/share/vm/runtime/thread.cpp --- a/src/share/vm/runtime/thread.cpp +++ b/src/share/vm/runtime/thread.cpp @@ -1971,6 +1971,8 @@ // The dirty card queue should have been constructed with its // active field set to true. assert(dirty_queue.is_active(), "dirty card queue should be active"); + + _evacuation_in_progress = _evacuation_in_progress_global; } bool JavaThread::evacuation_in_progress() const { @@ -1982,8 +1984,9 @@ } void JavaThread::set_evacuation_in_progress_all_threads(bool in_prog) { + assert(Threads_lock->owned_by_self(), "must hold Threads_lock"); _evacuation_in_progress_global = in_prog; - for (JavaThread* t = Threads::first(); t; t = t->next()) { + for (JavaThread* t = Threads::first(); t != NULL; t = t->next()) { t->set_evacuation_in_progress(in_prog); } } diff --git a/test/gc/shenandoah/EvilSyncBug.java b/test/gc/shenandoah/EvilSyncBug.java new file mode 100644 --- /dev/null +++ b/test/gc/shenandoah/EvilSyncBug.java @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2016 Red Hat, Inc. and/or its affiliates. + * + * 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 EvilSyncBug + * @summary Tests for crash/assert when attaching init thread during shutdown + * @key gc + * @library /test/lib + * @modules java.base/jdk.internal.misc + * java.management + * @run driver EvilSyncBug + */ + +import java.util.*; +import java.util.concurrent.*; +import java.util.concurrent.locks.*; + +import jdk.test.lib.process.ProcessTools; +import jdk.test.lib.process.OutputAnalyzer; + +public class EvilSyncBug { + + private static final int NUM_RUNS = 200; + + static Thread[] hooks = new MyHook[10000]; + + public static void main(String[] args) throws Exception { + if (args.length > 0) { + test(); + } else { + for (int i = 0; i < NUM_RUNS; i++) { + ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xmx128m", + "-Xmx128m", + "-XX:+UseShenandoahGC", + "-XX:ShenandoahGCHeuristics=aggressive", + "-XX:+ShenandoahStoreCheck", + "EvilSyncBug", "test"); + OutputAnalyzer output = new OutputAnalyzer(pb.start()); + output.shouldHaveExitValue(0); + } + } + } + + private static void test() throws Exception { + + for (int t = 0; t < hooks.length; t++) { + hooks[t] = new MyHook(); + } + + ExecutorService service = Executors.newFixedThreadPool( + 2, + r -> { + Thread t = new Thread(r); + t.setDaemon(true); + return t; + } + ); + + List> futures = new ArrayList<>(); + for (int c = 0; c < 100; c++) { + Runtime.getRuntime().addShutdownHook(hooks[c]); + final Test[] tests = new Test[1000]; + for (int t = 0; t < tests.length; t++) { + tests[t] = new Test(); + } + + Future f1 = service.submit(() -> { + Runtime.getRuntime().addShutdownHook(new MyHook()); + IntResult2 r = new IntResult2(); + for (Test test : tests) { + test.RL_Us(r); + } + }); + Future f2 = service.submit(() -> { + Runtime.getRuntime().addShutdownHook(new MyHook()); + for (Test test : tests) { + test.WLI_Us(); + } + }); + + futures.add(f1); + futures.add(f2); + } + + for (Future f : futures) { + f.get(); + } + } + + public static class IntResult2 { + int r1, r2; + } + + public static class Test { + final StampedLock lock = new StampedLock(); + + int x, y; + + public void RL_Us(IntResult2 r) { + StampedLock lock = this.lock; + long stamp = lock.readLock(); + r.r1 = x; + r.r2 = y; + lock.unlock(stamp); + } + + public void WLI_Us() { + try { + StampedLock lock = this.lock; + long stamp = lock.writeLockInterruptibly(); + x = 1; + y = 2; + lock.unlock(stamp); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } + + private static class MyHook extends Thread { + @Override + public void run() { + try { + Thread.sleep(10); + } catch (Exception e) {} + } + } + +} +