1 |
# HG changeset patch |
2 |
# User adinn |
3 |
# Date 1487931564 0 |
4 |
# Fri Feb 24 10:19:24 2017 +0000 |
5 |
# Node ID d41592af9af3790fe5eee30ce686d85cff09c942 |
6 |
# Parent 1ac9b0f1bf17fc5935bfa8250550eabc2ffb6785 |
7 |
8174729, PR3336, RH1420518: Race Condition in java.lang.reflect.WeakCache |
8 |
Summary: Race can occur between Proxy.getProxyClass and Proxy.isProxyClass |
9 |
Reviewed-by: mchung |
10 |
|
11 |
diff --git a/src/share/classes/java/lang/reflect/WeakCache.java b/src/share/classes/java/lang/reflect/WeakCache.java |
12 |
--- openjdk/jdk/src/share/classes/java/lang/reflect/WeakCache.java |
13 |
+++ openjdk/jdk/src/share/classes/java/lang/reflect/WeakCache.java |
14 |
@@ -239,11 +239,11 @@ |
15 |
// wrap value with CacheValue (WeakReference) |
16 |
CacheValue<V> cacheValue = new CacheValue<>(value); |
17 |
|
18 |
+ // put into reverseMap |
19 |
+ reverseMap.put(cacheValue, Boolean.TRUE); |
20 |
+ |
21 |
// try replacing us with CacheValue (this should always succeed) |
22 |
- if (valuesMap.replace(subKey, this, cacheValue)) { |
23 |
- // put also in reverseMap |
24 |
- reverseMap.put(cacheValue, Boolean.TRUE); |
25 |
- } else { |
26 |
+ if (!valuesMap.replace(subKey, this, cacheValue)) { |
27 |
throw new AssertionError("Should not reach here"); |
28 |
} |
29 |
|
30 |
diff --git a/test/java/lang/reflect/Proxy/ProxyRace.java b/test/java/lang/reflect/Proxy/ProxyRace.java |
31 |
new file mode 100644 |
32 |
--- /dev/null |
33 |
+++ openjdk/jdk/test/java/lang/reflect/Proxy/ProxyRace.java |
34 |
@@ -0,0 +1,88 @@ |
35 |
+/* |
36 |
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
37 |
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
38 |
+ * |
39 |
+ * This code is free software; you can redistribute it and/or modify it |
40 |
+ * under the terms of the GNU General Public License version 2 only, as |
41 |
+ * published by the Free Software Foundation. |
42 |
+ * |
43 |
+ * This code is distributed in the hope that it will be useful, but WITHOUT |
44 |
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
45 |
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
46 |
+ * version 2 for more details (a copy is included in the LICENSE file that |
47 |
+ * accompanied this code). |
48 |
+ * |
49 |
+ * You should have received a copy of the GNU General Public License version |
50 |
+ * 2 along with this work; if not, write to the Free Software Foundation, |
51 |
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
52 |
+ * |
53 |
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
54 |
+ * or visit www.oracle.com if you need additional information or have any |
55 |
+ * questions. |
56 |
+ */ |
57 |
+ |
58 |
+import java.lang.reflect.Proxy; |
59 |
+import java.util.concurrent.ExecutorService; |
60 |
+import java.util.concurrent.Executors; |
61 |
+import java.util.concurrent.Phaser; |
62 |
+import java.util.concurrent.TimeUnit; |
63 |
+import java.util.concurrent.atomic.AtomicInteger; |
64 |
+ |
65 |
+/** |
66 |
+ * @test |
67 |
+ * @bug 8174729 |
68 |
+ * @summary Proxy.getProxyClass() / Proxy.isProxyClass() race detector |
69 |
+ * @run main ProxyRace |
70 |
+ * @author plevart |
71 |
+ */ |
72 |
+ |
73 |
+public class ProxyRace { |
74 |
+ |
75 |
+ static final int threads = 8; |
76 |
+ |
77 |
+ static volatile ClassLoader classLoader; |
78 |
+ static volatile boolean terminate; |
79 |
+ static final AtomicInteger racesDetected = new AtomicInteger(); |
80 |
+ |
81 |
+ public static void main(String[] args) throws Exception { |
82 |
+ |
83 |
+ Phaser phaser = new Phaser(threads) { |
84 |
+ @Override |
85 |
+ protected boolean onAdvance(int phase, int registeredParties) { |
86 |
+ // install new ClassLoader on each advance |
87 |
+ classLoader = new CL(); |
88 |
+ return terminate; |
89 |
+ } |
90 |
+ }; |
91 |
+ |
92 |
+ ExecutorService exe = Executors.newFixedThreadPool(threads); |
93 |
+ |
94 |
+ for (int i = 0; i < threads; i++) { |
95 |
+ exe.execute(() -> { |
96 |
+ while (phaser.arriveAndAwaitAdvance() >= 0) { |
97 |
+ Class<?> proxyClass = Proxy.getProxyClass(classLoader, Runnable.class); |
98 |
+ if (!Proxy.isProxyClass(proxyClass)) { |
99 |
+ racesDetected.incrementAndGet(); |
100 |
+ } |
101 |
+ } |
102 |
+ }); |
103 |
+ } |
104 |
+ |
105 |
+ Thread.sleep(5000L); |
106 |
+ |
107 |
+ terminate = true; |
108 |
+ exe.shutdown(); |
109 |
+ exe.awaitTermination(5L, TimeUnit.SECONDS); |
110 |
+ |
111 |
+ System.out.println(racesDetected.get() + " races detected"); |
112 |
+ if (racesDetected.get() != 0) { |
113 |
+ throw new RuntimeException(racesDetected.get() + " races detected"); |
114 |
+ } |
115 |
+ } |
116 |
+ |
117 |
+ static class CL extends ClassLoader { |
118 |
+ public CL() { |
119 |
+ super(ClassLoader.getSystemClassLoader()); |
120 |
+ } |
121 |
+ } |
122 |
+} |