# HG changeset patch # User adinn # Date 1500150407 -3600 # Node ID 8f1c0d565d1322e0b1357c59b8a4fbb0bd429b48 # Parent 3cf8f6451be37527de7d073383d1b60021a5c22d 8174729: Race Condition in java.lang.reflect.WeakCache Summary: Race can occur between Proxy.getProxyClass and Proxy.isProxyClass Reviewed-by: mchung diff -r 3cf8f6451be3 -r 8f1c0d565d13 src/share/classes/java/lang/reflect/WeakCache.java --- a/src/share/classes/java/lang/reflect/WeakCache.java Fri Jul 14 18:46:12 2017 +0100 +++ b/src/share/classes/java/lang/reflect/WeakCache.java Sat Jul 15 21:26:47 2017 +0100 @@ -253,11 +253,11 @@ // wrap value with CacheValue (WeakReference) CacheValue cacheValue = new CacheValue<>(value); + // put into reverseMap + reverseMap.put(cacheValue, Boolean.TRUE); + // try replacing us with CacheValue (this should always succeed) - if (valuesMap.replace(subKey, this, cacheValue)) { - // put also in reverseMap - reverseMap.put(cacheValue, Boolean.TRUE); - } else { + if (!valuesMap.replace(subKey, this, cacheValue)) { throw new AssertionError("Should not reach here"); } diff -r 3cf8f6451be3 -r 8f1c0d565d13 test/java/lang/reflect/Proxy/ProxyRace.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/java/lang/reflect/Proxy/ProxyRace.java Sat Jul 15 21:26:47 2017 +0100 @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * 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. + */ + +import java.lang.reflect.Proxy; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Phaser; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * @test + * @bug 8174729 + * @summary Proxy.getProxyClass() / Proxy.isProxyClass() race detector + * @run main ProxyRace + * @author plevart + */ + +public class ProxyRace { + + static final int threads = 8; + + static volatile ClassLoader classLoader; + static volatile boolean terminate; + static final AtomicInteger racesDetected = new AtomicInteger(); + + public static void main(String[] args) throws Exception { + + final Phaser phaser = new Phaser(threads) { + @Override + protected boolean onAdvance(int phase, int registeredParties) { + // install new ClassLoader on each advance + classLoader = new CL(); + return terminate; + } + }; + + ExecutorService exe = Executors.newFixedThreadPool(threads); + + for (int i = 0; i < threads; i++) { + exe.execute(new Runnable() { + @Override + public void run() { + while (phaser.arriveAndAwaitAdvance() >= 0) { + Class proxyClass = Proxy.getProxyClass(classLoader, Runnable.class); + if (!Proxy.isProxyClass(proxyClass)) { + racesDetected.incrementAndGet(); + } + } + } + }); + } + + Thread.sleep(5000L); + + terminate = true; + exe.shutdown(); + exe.awaitTermination(5L, TimeUnit.SECONDS); + + System.out.println(racesDetected.get() + " races detected"); + if (racesDetected.get() != 0) { + throw new RuntimeException(racesDetected.get() + " races detected"); + } + } + + static class CL extends ClassLoader { + public CL() { + super(ClassLoader.getSystemClassLoader()); + } + } +}