Path: blob/master/test/jdk/java/lang/RuntimeTests/loadLibrary/LoadLibraryTest.java
41153 views
/*1* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.2* Copyright (c) 2019, Azul Systems, Inc. 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/**25* @test26* @bug 823158427* @library /test/lib28* @run main/othervm LoadLibraryTest29*/3031import java.nio.file.FileSystems;32import java.nio.file.Files;33import java.nio.file.Paths;34import java.nio.file.Path;35import java.net.MalformedURLException;36import java.net.URLClassLoader;37import java.net.URL;3839import jdk.test.lib.compiler.CompilerUtils;4041public class LoadLibraryTest {42static Thread thread1 = null;43static Thread thread2 = null;4445static volatile boolean thread1Ready = false;4647private static final String TEST_SRC = System.getProperty("test.src");48private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");49private static final Path CLS_DIR = Paths.get("classes");5051static TestClassLoader loader;52static void someLibLoad() {53try {54/*55FileSystems.getDefault();5657// jdk/jdk: loads directly from Bootstrap Classloader (doesn't take lock on Runtime)58java.net.NetworkInterface.getNetworkInterfaces();5960*/61Class c = Class.forName("Target2", true, loader);62} catch (Exception e) {63throw new RuntimeException(e);64}65}6667static class TestClassLoader extends URLClassLoader {68boolean passed = false;6970public boolean passed() {71return passed;72}7374TestClassLoader() throws MalformedURLException {75super(new URL[] { new URL("file://" + CLS_DIR.toAbsolutePath().toString() + '/') });76}7778public String findLibrary(String name) {79System.out.println("findLibrary " + name);8081if ("someLibrary".equals(name)) {82try {83synchronized(thread1) {84while(!thread1Ready) {85thread1.wait();86}87thread1.notifyAll();88}8990Thread.sleep(10000);9192System.out.println("Thread2 load");93someLibLoad();9495// no deadlock happened96passed = true;97} catch (Exception e) {98throw new RuntimeException(e);99}100return null;101}102103return super.findLibrary(name);104}105}106107108public static void main(String[] args) throws Exception {109loader = new TestClassLoader();110111if (!CompilerUtils.compile(SRC_DIR, CLS_DIR)) {112throw new Exception("Can't compile");113}114115thread1 = new Thread() {116public void run() {117try {118synchronized(this) {119thread1Ready = true;120thread1.notifyAll();121thread1.wait();122}123} catch(InterruptedException e) {124throw new RuntimeException(e);125}126127System.out.println("Thread1 load");128someLibLoad();129};130};131132thread2 = new Thread() {133public void run() {134try {135Class c = Class.forName("Target", true, loader);136System.out.println(c);137} catch (Exception e) {138throw new RuntimeException(e);139}140};141};142143thread1.setDaemon(true);144thread2.setDaemon(true);145146thread1.start();147thread2.start();148149thread1.join();150thread2.join();151152if (!loader.passed()) {153throw new RuntimeException("FAIL");154}155}156}157158159