|
|
e24496 |
# HG changeset patch
|
|
|
e24496 |
# User adinn
|
|
|
e24496 |
# Date 1487931564 0
|
|
|
e24496 |
# Fri Feb 24 10:19:24 2017 +0000
|
|
|
e24496 |
# Node ID d41592af9af3790fe5eee30ce686d85cff09c942
|
|
|
e24496 |
# Parent 1ac9b0f1bf17fc5935bfa8250550eabc2ffb6785
|
|
|
e24496 |
8174729, PR3336, RH1420518: Race Condition in java.lang.reflect.WeakCache
|
|
|
e24496 |
Summary: Race can occur between Proxy.getProxyClass and Proxy.isProxyClass
|
|
|
e24496 |
Reviewed-by: mchung
|
|
|
e24496 |
|
|
|
e24496 |
diff --git a/src/share/classes/java/lang/reflect/WeakCache.java b/src/share/classes/java/lang/reflect/WeakCache.java
|
|
|
e24496 |
--- openjdk/jdk/src/share/classes/java/lang/reflect/WeakCache.java
|
|
|
e24496 |
+++ openjdk/jdk/src/share/classes/java/lang/reflect/WeakCache.java
|
|
|
e24496 |
@@ -239,11 +239,11 @@
|
|
|
e24496 |
// wrap value with CacheValue (WeakReference)
|
|
|
e24496 |
CacheValue<V> cacheValue = new CacheValue<>(value);
|
|
|
e24496 |
|
|
|
e24496 |
+ // put into reverseMap
|
|
|
e24496 |
+ reverseMap.put(cacheValue, Boolean.TRUE);
|
|
|
e24496 |
+
|
|
|
e24496 |
// try replacing us with CacheValue (this should always succeed)
|
|
|
e24496 |
- if (valuesMap.replace(subKey, this, cacheValue)) {
|
|
|
e24496 |
- // put also in reverseMap
|
|
|
e24496 |
- reverseMap.put(cacheValue, Boolean.TRUE);
|
|
|
e24496 |
- } else {
|
|
|
e24496 |
+ if (!valuesMap.replace(subKey, this, cacheValue)) {
|
|
|
e24496 |
throw new AssertionError("Should not reach here");
|
|
|
e24496 |
}
|
|
|
e24496 |
|
|
|
e24496 |
diff --git a/test/java/lang/reflect/Proxy/ProxyRace.java b/test/java/lang/reflect/Proxy/ProxyRace.java
|
|
|
e24496 |
new file mode 100644
|
|
|
e24496 |
--- /dev/null
|
|
|
e24496 |
+++ openjdk/jdk/test/java/lang/reflect/Proxy/ProxyRace.java
|
|
|
e24496 |
@@ -0,0 +1,88 @@
|
|
|
e24496 |
+/*
|
|
|
e24496 |
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
|
|
|
e24496 |
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
e24496 |
+ *
|
|
|
e24496 |
+ * This code is free software; you can redistribute it and/or modify it
|
|
|
e24496 |
+ * under the terms of the GNU General Public License version 2 only, as
|
|
|
e24496 |
+ * published by the Free Software Foundation.
|
|
|
e24496 |
+ *
|
|
|
e24496 |
+ * This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
e24496 |
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
e24496 |
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
e24496 |
+ * version 2 for more details (a copy is included in the LICENSE file that
|
|
|
e24496 |
+ * accompanied this code).
|
|
|
e24496 |
+ *
|
|
|
e24496 |
+ * You should have received a copy of the GNU General Public License version
|
|
|
e24496 |
+ * 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
e24496 |
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
e24496 |
+ *
|
|
|
e24496 |
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
e24496 |
+ * or visit www.oracle.com if you need additional information or have any
|
|
|
e24496 |
+ * questions.
|
|
|
e24496 |
+ */
|
|
|
e24496 |
+
|
|
|
e24496 |
+import java.lang.reflect.Proxy;
|
|
|
e24496 |
+import java.util.concurrent.ExecutorService;
|
|
|
e24496 |
+import java.util.concurrent.Executors;
|
|
|
e24496 |
+import java.util.concurrent.Phaser;
|
|
|
e24496 |
+import java.util.concurrent.TimeUnit;
|
|
|
e24496 |
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
e24496 |
+
|
|
|
e24496 |
+/**
|
|
|
e24496 |
+ * @test
|
|
|
e24496 |
+ * @bug 8174729
|
|
|
e24496 |
+ * @summary Proxy.getProxyClass() / Proxy.isProxyClass() race detector
|
|
|
e24496 |
+ * @run main ProxyRace
|
|
|
e24496 |
+ * @author plevart
|
|
|
e24496 |
+ */
|
|
|
e24496 |
+
|
|
|
e24496 |
+public class ProxyRace {
|
|
|
e24496 |
+
|
|
|
e24496 |
+ static final int threads = 8;
|
|
|
e24496 |
+
|
|
|
e24496 |
+ static volatile ClassLoader classLoader;
|
|
|
e24496 |
+ static volatile boolean terminate;
|
|
|
e24496 |
+ static final AtomicInteger racesDetected = new AtomicInteger();
|
|
|
e24496 |
+
|
|
|
e24496 |
+ public static void main(String[] args) throws Exception {
|
|
|
e24496 |
+
|
|
|
e24496 |
+ Phaser phaser = new Phaser(threads) {
|
|
|
e24496 |
+ @Override
|
|
|
e24496 |
+ protected boolean onAdvance(int phase, int registeredParties) {
|
|
|
e24496 |
+ // install new ClassLoader on each advance
|
|
|
e24496 |
+ classLoader = new CL();
|
|
|
e24496 |
+ return terminate;
|
|
|
e24496 |
+ }
|
|
|
e24496 |
+ };
|
|
|
e24496 |
+
|
|
|
e24496 |
+ ExecutorService exe = Executors.newFixedThreadPool(threads);
|
|
|
e24496 |
+
|
|
|
e24496 |
+ for (int i = 0; i < threads; i++) {
|
|
|
e24496 |
+ exe.execute(() -> {
|
|
|
e24496 |
+ while (phaser.arriveAndAwaitAdvance() >= 0) {
|
|
|
e24496 |
+ Class proxyClass = Proxy.getProxyClass(classLoader, Runnable.class);
|
|
|
e24496 |
+ if (!Proxy.isProxyClass(proxyClass)) {
|
|
|
e24496 |
+ racesDetected.incrementAndGet();
|
|
|
e24496 |
+ }
|
|
|
e24496 |
+ }
|
|
|
e24496 |
+ });
|
|
|
e24496 |
+ }
|
|
|
e24496 |
+
|
|
|
e24496 |
+ Thread.sleep(5000L);
|
|
|
e24496 |
+
|
|
|
e24496 |
+ terminate = true;
|
|
|
e24496 |
+ exe.shutdown();
|
|
|
e24496 |
+ exe.awaitTermination(5L, TimeUnit.SECONDS);
|
|
|
e24496 |
+
|
|
|
e24496 |
+ System.out.println(racesDetected.get() + " races detected");
|
|
|
e24496 |
+ if (racesDetected.get() != 0) {
|
|
|
e24496 |
+ throw new RuntimeException(racesDetected.get() + " races detected");
|
|
|
e24496 |
+ }
|
|
|
e24496 |
+ }
|
|
|
e24496 |
+
|
|
|
e24496 |
+ static class CL extends ClassLoader {
|
|
|
e24496 |
+ public CL() {
|
|
|
e24496 |
+ super(ClassLoader.getSystemClassLoader());
|
|
|
e24496 |
+ }
|
|
|
e24496 |
+ }
|
|
|
e24496 |
+}
|