1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.io.IOException; 25 import java.lang.reflect.*; 26 import java.net.URL; 27 import java.net.URLConnection; 28 import java.net.URLStreamHandler; 29 import java.util.concurrent.CountDownLatch; 30 31 /* 32 * @test 33 * @bug 8213942 34 * @summary URLStreamHandler initialization race 35 * @run main/othervm RacyHandler 36 * @run main/othervm RacyHandler 37 * @run main/othervm RacyHandler 38 */ 39 40 /* 41 * This test makes reasonable effort to reproduce the race. 42 * Run repeatedly to ensure correctness. 43 */ 44 public class RacyHandler { 45 static volatile boolean factorySet = false; 46 static int NUM_THREADS = 2; 47 static CountDownLatch cdl = new CountDownLatch(NUM_THREADS + 1); 48 49 public static void main(String[] args) { 50 RacyHandler tester = new RacyHandler(); 51 tester.runTest(); 52 } 53 54 public void runTest() { 55 new Thread(() -> { 56 try { 57 cdl.await(); 58 URL.setURLStreamHandlerFactory(proto -> new CustomHttpHandler()); 59 factorySet = true; 60 } catch (Exception ignore) { } 61 }).start(); 62 cdl.countDown(); 63 64 for (int i = 0; i < NUM_THREADS; i++) { 65 new Thread(() -> { 66 try { 67 cdl.await(); 68 while (!factorySet) { 69 // trigger URL class load 70 getURLStreamHandler(); 71 } 72 } catch (Exception ignore) { } 73 }).start(); 74 cdl.countDown(); 75 } 76 77 // wait for the factory to be set 78 while (!factorySet) { } 79 // The sleep seems to help trigger the failure 80 try { 81 Thread.sleep(500); 82 } catch (InterruptedException ie) { 83 } 84 85 URLStreamHandler httpHandler = getURLStreamHandler(); 86 System.out.println("After setting factory URL handlers: http " + httpHandler); 87 if (!(httpHandler instanceof CustomHttpHandler)) 88 throw new RuntimeException("FAILED: Incorrect handler type"); 89 } 90 91 /* 92 * This is just so we can see what we get for the URLStreamHandler back 93 * from the factory to verify whether it really is using our Handler 94 * or something else... 95 */ 96 public URLStreamHandler getURLStreamHandler() { 97 try { 98 Method method = URL.class.getDeclaredMethod("getURLStreamHandler", 99 String.class); 100 method.setAccessible(true); 101 return (URLStreamHandler) method.invoke(null, "http"); 102 } catch (Exception e) { 103 return null; 104 } 105 } 106 107 class CustomHttpHandler extends URLStreamHandler { 108 @Override 109 protected URLConnection openConnection(URL u) throws IOException { 110 return null; 111 } 112 } 113 }