Blame SOURCES/8174729-pr3336-rh1420518.patch

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