undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
130
test/jdk/java/lang/ClassLoader/deadlock/DelegateTest.java
Normal file
130
test/jdk/java/lang/ClassLoader/deadlock/DelegateTest.java
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
/*
|
||||
* Copyright (c) 2009, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4735126
|
||||
* @summary (cl) ClassLoader.loadClass locks all instances in chain when delegating
|
||||
* @modules java.base/java.lang:open
|
||||
* jdk.compiler
|
||||
* @library /test/lib
|
||||
* @build jdk.test.lib.compiler.CompilerUtils
|
||||
* @run main/othervm -Xlog:class+load DelegateTest one-way
|
||||
* @run main/othervm -Xlog:class+load DelegateTest cross
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import jdk.test.lib.compiler.CompilerUtils;
|
||||
|
||||
public class DelegateTest implements Runnable {
|
||||
|
||||
private static final Path TEST_DIR = Paths.get(System.getProperty("user.dir", "."));
|
||||
private static final Path SRC_DIR = Paths.get(System.getProperty("test.src"), "src");
|
||||
|
||||
private String id;
|
||||
private DelegatingLoader dl;
|
||||
private String startClass;
|
||||
|
||||
private static DelegatingLoader saLoader, sbLoader;
|
||||
|
||||
public static void log(String line) {
|
||||
System.out.println(line);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
if (!CompilerUtils.compile(SRC_DIR, TEST_DIR)) {
|
||||
throw new RuntimeException("Failed to compile "
|
||||
+ SRC_DIR.toAbsolutePath().toString());
|
||||
}
|
||||
|
||||
URL[] url = new URL[1];
|
||||
try {
|
||||
url[0] = new URL("file://" + TEST_DIR + File.separator);
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// Set up Classloader delegation hierarchy
|
||||
saLoader = new DelegatingLoader(url);
|
||||
sbLoader = new DelegatingLoader(url);
|
||||
|
||||
String[] saClasses = { "comSA.SupBob", "comSA.Alice" };
|
||||
String[] sbClasses = { "comSB.SupAlice", "comSB.Bob" };
|
||||
|
||||
saLoader.setDelegate(sbClasses, sbLoader);
|
||||
sbLoader.setDelegate(saClasses, saLoader);
|
||||
|
||||
// test one-way delegate
|
||||
String testType = args[0];
|
||||
if (testType.equals("one-way")) {
|
||||
test("comSA.Alice", "comSA.SupBob");
|
||||
} else if (testType.equals("cross")) {
|
||||
// test cross delegate
|
||||
test("comSA.Alice", "comSB.Bob");
|
||||
} else {
|
||||
System.out.println("ERROR: unsupported - " + testType);
|
||||
}
|
||||
}
|
||||
|
||||
private static void test(String clsForSA, String clsForSB) throws InterruptedException {
|
||||
DelegateTest ia = new DelegateTest("SA", saLoader, clsForSA);
|
||||
DelegateTest ib = new DelegateTest("SB", sbLoader, clsForSB);
|
||||
Thread ta = new Thread(ia);
|
||||
Thread tb = new Thread(ib);
|
||||
ta.start();
|
||||
tb.start();
|
||||
ta.join();
|
||||
tb.join();
|
||||
}
|
||||
|
||||
public static void sleep() {
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
log("Thread interrupted");
|
||||
}
|
||||
}
|
||||
|
||||
private DelegateTest(String id, DelegatingLoader dl, String startClass) {
|
||||
this.id = id;
|
||||
this.dl = dl;
|
||||
this.startClass = startClass;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
log("Spawned thread " + id + " running");
|
||||
try {
|
||||
// To mirror the WAS deadlock, need to ensure class load
|
||||
// is routed via the VM.
|
||||
Class.forName(startClass, true, dl);
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
log("Thread " + id + " terminating");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,93 @@
|
|||
/*
|
||||
* Copyright (c) 2009, 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.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.locks.*;
|
||||
import java.lang.reflect.*;
|
||||
|
||||
public class DelegatingLoader extends URLClassLoader {
|
||||
|
||||
private DelegatingLoader delLoader;
|
||||
private String[] delClasses;
|
||||
|
||||
static {
|
||||
boolean supportParallel = false;
|
||||
try {
|
||||
Class c = Class.forName("java.lang.ClassLoader");
|
||||
Method m = c.getDeclaredMethod("registerAsParallelCapable",
|
||||
new Class[0]);
|
||||
m.setAccessible(true);
|
||||
Object result = (Boolean) m.invoke(null);
|
||||
if (result instanceof Boolean) {
|
||||
supportParallel = ((Boolean) result).booleanValue();
|
||||
} else {
|
||||
// Should never happen
|
||||
System.out.println("Error: ClassLoader.registerAsParallelCapable() did not return a boolean!");
|
||||
System.exit(1);
|
||||
}
|
||||
} catch (NoSuchMethodException nsme) {
|
||||
System.out.println("No ClassLoader.registerAsParallelCapable() API");
|
||||
} catch (NoSuchMethodError nsme2) {
|
||||
System.out.println("No ClassLoader.registerAsParallelCapable() API");
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
// Exit immediately to indicate an error
|
||||
System.exit(1);
|
||||
}
|
||||
System.out.println("Parallel ClassLoader registration: " +
|
||||
supportParallel);
|
||||
}
|
||||
|
||||
public DelegatingLoader(URL urls[]) {
|
||||
super(urls);
|
||||
System.out.println("DelegatingLoader using URL " + urls[0]);
|
||||
}
|
||||
|
||||
public void setDelegate(String[] delClasses, DelegatingLoader delLoader) {
|
||||
this.delClasses = delClasses;
|
||||
this.delLoader = delLoader;
|
||||
}
|
||||
|
||||
public Class loadClass(String className, boolean resolve)
|
||||
throws ClassNotFoundException {
|
||||
for (int i = 0; i < delClasses.length; i++) {
|
||||
if (delClasses[i].equals(className)) {
|
||||
DelegateTest.log("Delegating class loading for " + className);
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException ie) {
|
||||
return null;
|
||||
}
|
||||
return delLoader.loadClass(className, resolve);
|
||||
}
|
||||
}
|
||||
|
||||
DelegateTest.log("Loading local class " + className);
|
||||
// synchronized (getClassLoadingLock(className)) {
|
||||
return super.loadClass(className, resolve);
|
||||
// }
|
||||
}
|
||||
}
|
||||
104
test/jdk/java/lang/ClassLoader/deadlock/GetResource.java
Normal file
104
test/jdk/java/lang/ClassLoader/deadlock/GetResource.java
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
/*
|
||||
* Copyright (c) 2010, 2016, 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.util.Properties;
|
||||
import java.util.concurrent.CyclicBarrier;
|
||||
import java.util.concurrent.BrokenBarrierException;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
|
||||
/* @test
|
||||
* @bug 6977738 8029891
|
||||
* @summary Test ClassLoader.getResource() that should not deadlock
|
||||
# if another thread is holding the system properties object
|
||||
*
|
||||
* @build GetResource
|
||||
* @run main GetResource
|
||||
*/
|
||||
|
||||
public class GetResource {
|
||||
CyclicBarrier go = new CyclicBarrier(2);
|
||||
CyclicBarrier done = new CyclicBarrier(2);
|
||||
Thread t1, t2;
|
||||
public GetResource() {
|
||||
t1 = new Thread() {
|
||||
public void run() {
|
||||
Properties prop = System.getProperties();
|
||||
synchronized (prop) {
|
||||
System.out.println("Thread 1 ready");
|
||||
try {
|
||||
go.await();
|
||||
prop.put("property", "value");
|
||||
prop.store(System.out, "");
|
||||
done.await(); // keep holding the lock until t2 finishes
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (BrokenBarrierException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
System.out.println("Thread 1 exits");
|
||||
}
|
||||
};
|
||||
|
||||
t2 = new Thread() {
|
||||
public void run() {
|
||||
System.out.println("Thread 2 ready");
|
||||
try {
|
||||
go.await(); // wait until t1 holds the lock of the system properties
|
||||
|
||||
URL u1 = Thread.currentThread().getContextClassLoader().getResource("unknownresource");
|
||||
done.await();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (BrokenBarrierException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
System.out.println("Thread 2 exits");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void run() throws Exception {
|
||||
t1.start();
|
||||
t2.start();
|
||||
try {
|
||||
t1.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
try {
|
||||
t2.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
new GetResource().run();
|
||||
}
|
||||
}
|
||||
29
test/jdk/java/lang/ClassLoader/deadlock/src/comSA/Alice.java
Normal file
29
test/jdk/java/lang/ClassLoader/deadlock/src/comSA/Alice.java
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Copyright (c) 2009, 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.
|
||||
*/
|
||||
package comSA;
|
||||
|
||||
public class Alice extends comSB.SupAlice {
|
||||
static {
|
||||
System.out.println("comSA.Alice loaded");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Copyright (c) 2009, 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.
|
||||
*/
|
||||
package comSA;
|
||||
|
||||
public class SupBob {
|
||||
static {
|
||||
System.out.println("comSA.SupBob loaded");
|
||||
}
|
||||
}
|
||||
29
test/jdk/java/lang/ClassLoader/deadlock/src/comSB/Bob.java
Normal file
29
test/jdk/java/lang/ClassLoader/deadlock/src/comSB/Bob.java
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Copyright (c) 2009, 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.
|
||||
*/
|
||||
package comSB;
|
||||
|
||||
public class Bob extends comSA.SupBob {
|
||||
static {
|
||||
System.out.println("comSB.Bob loaded");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Copyright (c) 2009, 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.
|
||||
*/
|
||||
package comSB;
|
||||
|
||||
public class SupAlice {
|
||||
static {
|
||||
System.out.println("comSB.SupAlice loaded");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue