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:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,9 @@
/*
*%E
*
* Simple "no-op" class used by CircularityErrorTest.sh to create a circularity
* condition.
*
*/
public interface A {
}

View file

@ -0,0 +1,9 @@
/*
*%E
*
* Simple "no-op" class used by CircularityErrorTest.sh to create a circularity
* condition.
*
*/
public interface A extends B {
}

View file

@ -0,0 +1,45 @@
/*
* Copyright (c) 2005, 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.
*/
/*
*
*
* Used by run_tests.sh
*
* Simple agent that makes Instrumentation instance available to other classes.
*/
import java.lang.instrument.Instrumentation;
public class Agent {
static Instrumentation inst;
public static void premain(String args, Instrumentation ins) {
inst = ins;
}
public static Instrumentation getInstrumentation() {
return inst;
}
}

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 2005, 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.
*/
/*
*
*
* Used by run_tests.sh
*
* An agent "supporting" class that will be loaded by the system class loader.
*/
public class AgentSupport {
}

View file

@ -0,0 +1,36 @@
/*
* Copyright (c) 2005, 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.
*/
/*
*
*
* Used by DynamicTest - simple Application with one method that returns the
* number of messages printed.
*/
public class Application {
public int doSomething() {
System.out.println("Bring us up to warp speed!");
return 1;
}
}

View file

@ -0,0 +1,9 @@
/*
*%E
*
* Simple "no-op" class used by CircularityErrorTest.sh to create a circularity
* condition.
*
*/
public interface B extends A {
}

View file

@ -0,0 +1,9 @@
/*
*%E
*
* Simple "no-op" class used by CircularityErrorTest.sh to create a circularity
* condition.
*
*/
public interface B {
}

View file

@ -0,0 +1,90 @@
/*
* Copyright (c) 2005, 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.
*/
/*
*
*
* Basic unit test for Instrumentation appendToBootstrapClassLoaderSearch and
* appendToSystemClassLoaderSearch methods:
*
* 1. Adds a jar file to the bootstrap class loader search; Loads a class known
* to be in the jar file and checks that it was loaded by the bootstrap class
* loader.
*
* 2. Adds a jar file to the system class loader search; Loads a class known
* to be in the jar file and checks that it was indeed loaded by the system
* class loader.
*/
import java.lang.instrument.Instrumentation;
import java.util.jar.JarFile;
import java.io.IOException;
public class BasicTest {
// count of failures
static int failures = 0;
// check that the given class is loaded by the given loader
static void checkLoadedByLoader(String name, ClassLoader loader) {
try {
Class cl = Class.forName(name);
ClassLoader actual = cl.getClassLoader();
String loaderName = (actual == null) ? "boot class loader" : actual.toString();
if (actual != loader) {
System.out.println("FAIL: " + name + " incorrectly loaded by: " + loaderName);
failures++;
} else {
System.out.println("PASS: " + name + " loaded by: " + loaderName);
}
} catch (Exception x) {
System.out.println("FAIL: Unable to load " + name + ":" + x);
failures++;
}
}
public static void main(String args[]) throws IOException {
JarFile bootclasses = new JarFile("BootSupport.jar");
JarFile agentclasses = new JarFile("AgentSupport.jar");
Instrumentation ins = Agent.getInstrumentation();
// Test 1: Add BootSupport.jar to boot class path and check that
// BootSupport is loaded by the bootstrap class loader
ins.appendToBootstrapClassLoaderSearch(bootclasses);
checkLoadedByLoader("BootSupport", null);
// Test 2: Add AgentSupport.jar to the system class path and check that
// AgentSupport is loaded by the system class loader.
try {
ins.appendToSystemClassLoaderSearch(agentclasses);
checkLoadedByLoader("AgentSupport", ClassLoader.getSystemClassLoader());
} catch (UnsupportedOperationException x) {
System.out.println("System class loader does not support adding to class path");
}
// throw exception if a test failed
if (failures > 0) {
throw new RuntimeException(failures + " test(s) failed.");
}
}
}

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 2005, 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.
*/
/*
*
*
* Used by run_tests.sh
*
* An agent "supporting" class that will be loaded by the bootstrap class loader.
*/
public class BootSupport {
}

View file

@ -0,0 +1,64 @@
/*
* Copyright (c) 2005, 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.
*/
/*
*
*
* Unit test for Instrumentation appendToBootstrapClassLoaderSearch:
*
* 1. Reference class A. Resolving this class should fail with
* ClassCircularityError.
*
* 2. Add JAR file to boot class path which contains a "good"
* version of A.
*
* 3. Re-run the code for 1 again - it should fail with
* ClassCircularityError again.
*/
import java.lang.instrument.Instrumentation;
import java.util.jar.JarFile;
public class CircularityErrorTest {
static Instrumentation ins;
static void resolve() {
try {
Class c = A.class;
throw new RuntimeException("Test failed - class A loaded by: " +
c.getClassLoader());
} catch (ClassCircularityError e) {
System.err.println(e);
}
}
public static void main(String args[]) throws Exception {
resolve();
ins.appendToBootstrapClassLoaderSearch(new JarFile("A.jar"));
resolve();
}
public static void premain(String args, Instrumentation i) {
ins = i;
}
}

View file

@ -0,0 +1,80 @@
#
# Copyright (c) 2005, 2024, 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 6173575
# @summary Unit tests for appendToBootstrapClassLoaderSearch and
# appendToSystemClasLoaderSearch methods.
#
# @run shell/timeout=240 CircularityErrorTest.sh
if [ "${TESTSRC}" = "" ]
then
echo "TESTSRC not set. Test cannot execute. Failed."
exit 1
fi
if [ "${COMPILEJAVA}" = "" ]
then
COMPILEJAVA="${TESTJAVA}"
fi
. ${TESTSRC}/CommonSetup.sh
# Setup to create circularity condition
# B extends A
# This yields us A.jar (containing A.class) and B.keep (class file)
rm -f "${TESTCLASSES}"/A.java "${TESTCLASSES}"/B.java
cp "${TESTSRC}"/A.1 "${TESTCLASSES}"/A.java
cp "${TESTSRC}"/B.1 "${TESTCLASSES}"/B.java
(cd "${TESTCLASSES}"; \
$JAVAC ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} A.java B.java; \
$JAVAC ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} -d . "${TESTSRC}"/CircularityErrorTest.java; \
$JAR ${TESTTOOLVMOPTS} cf A.jar A.class; \
rm -f A.class; mv B.class B.keep)
# A extends B
# This yields us A.class
rm -f "${TESTCLASSES}"/A.java "${TESTCLASSES}"/B.java
cp "${TESTSRC}"/A.2 "${TESTCLASSES}"/A.java
cp "${TESTSRC}"/B.2 "${TESTCLASSES}"/B.java
(cd "${TESTCLASSES}"; \
$JAVAC ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} A.java B.java; rm -f B.class A.java B.java)
# Move B.keep to B.class creates the A extends B and
# B extends A condition.
(cd "${TESTCLASSES}"; mv B.keep B.class)
# Create the manifest
MANIFEST="${TESTCLASSES}"/agent.mf
rm -f "${MANIFEST}"
echo "Premain-Class: CircularityErrorTest" > "${MANIFEST}"
# Setup test case as an agent
$JAR ${TESTTOOLVMOPTS} -cfm "${TESTCLASSES}"/CircularityErrorTest.jar "${MANIFEST}" \
-C "${TESTCLASSES}" CircularityErrorTest.class
# Finally we run the test
(cd "${TESTCLASSES}";
$JAVA ${TESTVMOPTS} ${TESTJAVAOPTS} -javaagent:CircularityErrorTest.jar CircularityErrorTest)

View file

@ -0,0 +1,119 @@
/*
* Copyright (c) 2005, 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.
*/
/*
*
*
* Unit test for Instrumentation appendToSystemClassLoaderSearch. This
* test does the following:
*
* 1. Creates a class loader to load class Foo. Execute Foo.doSomething
* which references a missing class Bar. The doSomething method
* should fail with NoClassDefFoundError.
*
* 2. Add Bar.jar to the system class path. Bar.jar contains Bar.
*
* 3. Create another class loader to load Foo. Execute Foo.doSomething.
* doSomething will load Bar.
*
* 4. Re-execute the first Foo's doSomething - it should fail a second
* time because the attempt to resolve Bar must fail with the same
* error as the first attempt.
*
* 5. De-reference both class loaders and execute System.gc(). We can't
* assert that the Foo classes will be unloaded but it serves to
* exercise the unload code path in HotSpot.
*/
import java.lang.instrument.Instrumentation;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.util.jar.JarFile;
public class ClassUnloadTest {
static Instrumentation ins;
public static void main(String args[]) throws Exception {
String dir = args[0] + File.separator;
String jar = dir + args[1];
System.out.println(jar);
URL u = (new File(dir)).toURL();
URL urls[] = { u };
// This should fail as Bar is not available
Invoker i1 = new Invoker(urls, "Foo", "doSomething");
Boolean result = (Boolean)i1.invoke((Object)null);
if (result.booleanValue()) {
throw new RuntimeException("Test configuration error - doSomething should not succeed");
}
// put Bar on the system class path
ins.appendToSystemClassLoaderSearch( new JarFile(jar) );
// This should fail even though Bar is now available
result = (Boolean)i1.invoke((Object)null);
if (result.booleanValue()) {
throw new RuntimeException("Test configuration error - doSomething should not succeed");
}
// This should succeed because this is a different Foo
Invoker i2 = new Invoker(urls, "Foo", "doSomething");
result = (Boolean)i2.invoke((Object)null);
if (!result.booleanValue()) {
throw new RuntimeException("Test configuration error - doSomething did not succeed");
}
// Exercise some class unloading
i1 = i2 = null;
System.gc();
}
static class Invoker {
URLClassLoader cl;
Method m;
public Invoker(URL urls[], String cn, String mn, Class ... params)
throws ClassNotFoundException, NoSuchMethodException
{
cl = new URLClassLoader(urls);
Class c = Class.forName("Foo", true, cl);
m = c.getDeclaredMethod(mn, params);
}
public Object invoke(Object ... args)
throws IllegalAccessException, InvocationTargetException
{
return m.invoke(args);
}
}
public static void premain(String args, Instrumentation i) {
ins = i;
}
}

View file

@ -0,0 +1,84 @@
#
# Copyright (c) 2005, 2024, 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 6173575
# @summary Unit tests for appendToBootstrapClassLoaderSearch and
# appendToSystemClasLoaderSearch methods.
#
# @build ClassUnloadTest
# @run shell ClassUnloadTest.sh
if [ "${TESTSRC}" = "" ]
then
echo "TESTSRC not set. Test cannot execute. Failed."
exit 1
fi
. ${TESTSRC}/CommonSetup.sh
# Create Foo and Bar
# Foo has a reference to Bar but we deleted Bar so that
# a NoClassDefFoundError will be thrown when Foo tries to
# resolve the reference to Bar
OTHERDIR="${TESTCLASSES}"/other
mkdir "${OTHERDIR}"
FOO="${OTHERDIR}"/Foo.java
BAR="${OTHERDIR}"/Bar.java
rm -f "${FOO}" "${BAR}"
cat << EOF > "${FOO}"
public class Foo {
public static boolean doSomething() {
try {
Bar b = new Bar();
return true;
} catch (NoClassDefFoundError x) {
return false;
}
}
}
EOF
echo "public class Bar { }" > "${BAR}"
(cd "${OTHERDIR}"; \
$JAVAC ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} Foo.java Bar.java; \
$JAR ${TESTTOOLVMOPTS} cf "${OTHERDIR}"/Bar.jar Bar.class; \
rm -f Bar.class)
# Create the manifest
MANIFEST="${TESTCLASSES}"/agent.mf
rm -f "${MANIFEST}"
echo "Premain-Class: ClassUnloadTest" > "${MANIFEST}"
# Setup test case as an agent
$JAR ${TESTTOOLVMOPTS} -cfm "${TESTCLASSES}"/ClassUnloadTest.jar "${MANIFEST}" \
-C "${TESTCLASSES}" ClassUnloadTest.class
# Finally we run the test
(cd "${TESTCLASSES}"; \
$JAVA ${TESTVMOPTS} ${TESTJAVAOPTS} -Xlog:class+unload \
-javaagent:ClassUnloadTest.jar ClassUnloadTest "${OTHERDIR}" Bar.jar)

View file

@ -0,0 +1,94 @@
#!/bin/sh
#
# Copyright (c) 2005, 2020, 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.
#
#
# Common setup for unit tests. Setups up the following variables:
#
# PS - path sep.
# FS - file sep.
# JAVA - java cmd.
# JAVAC - javac cmd.
# JAR - jar cmd.
OS=`uname -s`
case "$OS" in
Linux )
PS=":"
FS="/"
;;
Darwin )
PS=":"
FS="/"
;;
AIX )
PS=":"
FS="/"
;;
Windows*)
PS=";"
OS="Windows"
FS="\\"
;;
CYGWIN*)
PS=";"
OS="Windows"
FS="\\"
isCygwin=true
;;
* )
echo "Unrecognized system!"
exit 1;
;;
esac
if [ "${TESTJAVA}" = "" ]
then
echo "TESTJAVA not set. Test cannot execute. Failed."
exit 1
fi
if [ "${COMPILEJAVA}" = "" ]
then
COMPILEJAVA="${TESTJAVA}"
fi
echo "COMPILEJAVA=${COMPILEJAVA}"
if [ "${TESTSRC}" = "" ]
then
echo "TESTSRC not set. Test cannot execute. Failed."
exit 1
fi
if [ "${TESTCLASSES}" = "" ]
then
echo "TESTCLASSES not set. Test cannot execute. Failed."
exit 1
fi
JAVA="${TESTJAVA}/bin/java"
JAVAC="${COMPILEJAVA}/bin/javac"
JAR="${COMPILEJAVA}/bin/jar"

View file

@ -0,0 +1,84 @@
/*
* Copyright (c) 2005, 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.
*/
/*
*
*
* Unit test for Instrumentation appendToBootstrapClassLoaderSearch method.
* The test works as follows:
*
* 1. Load class "Application" and execute the doSomething() method
*
* 2. Append Tracer.jar to the boot class path - Tracer.jar has a single
* class org.tools.Tracer.
*
* 3. Redefine class "Application" - the redefined version has an instrumented
* version of doSomething() that invokes a method in org.tools.Tracer.
*
* 4. Re-execute doSomething() - this should provoke the loading of org.tools.Tracer
* from the jar file. If updated version of doSomething() executes then test
* passes.
*/
import java.lang.instrument.*;
import java.util.jar.JarFile;
import java.io.*;
public class DynamicTest {
public static void main(String args[]) throws Exception {
// Load Application
Application app = new Application();
if (app.doSomething() != 1) {
throw new RuntimeException("Test configuration error - doSomething should return 1");
}
// Add org.tools.Tracer package to the boot class path
JarFile jf = new JarFile("Tracer.jar");
Agent.getInstrumentation().appendToBootstrapClassLoaderSearch(jf);
// Redefine Application with the instrumented version
File f = new File("InstrumentedApplication.bytes");
int len = (int)f.length();
byte[] def = new byte[len];
FileInputStream fis = new FileInputStream(f);
int nread = 0;
do {
int n = fis.read(def, nread, len-nread);
if (n > 0) {
nread += n;
}
} while (nread < len);
ClassDefinition classDefs = new ClassDefinition(Application.class, def);
Agent.getInstrumentation().redefineClasses(new ClassDefinition[] { classDefs } );
// Re-execute doSomething() - should get 3 messages printed
int res = app.doSomething();
if (res != 3) {
throw new RuntimeException("FAIL: redefined Application returned: " + res);
}
System.out.println("PASS: Test passed.");
}
}

View file

@ -0,0 +1,38 @@
/*
* Copyright (c) 2005, 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.
*/
/*
*
*
* Used by DynamicTest - simple Application with one method that returns the
* number of messages printed. This is the "instrumented" form.
*/
public class Application {
public int doSomething() {
org.tools.Tracer.trace("MethodEnter");
System.out.println("Bring us up to warp speed!");
org.tools.Tracer.trace("MethodExit");
return 3;
}
}

View file

@ -0,0 +1,91 @@
/*
* Copyright (c) 2005, 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.
*/
/*
*
*
* Basic unit test for Instrumentation appendToBootstrapClassLoaderSearch and
* appendToSystemClassLoaderSearch methods:
*
* 1. Attempt to load a class that does not exist.
*
* 2. Add a jar file to the class path with the missing class.
*
* 3. Attempt to load the class again - CNF should be thrown as the JVMS requires that
* the error from the first resolution attempt should be thrown.
*/
import java.lang.instrument.Instrumentation;
import java.util.jar.JarFile;
import java.io.IOException;
public class PrematureLoadTest {
static int failures = 0;
public static void main(String args[]) throws IOException {
try {
new BootSupport();
throw new RuntimeException("Test configuration error - BootSupport loaded unexpectedly!");
} catch (NoClassDefFoundError x) {
}
try {
new AgentSupport();
throw new RuntimeException("Test configuration error - AgentSupport loaded unexpectedly!");
} catch (NoClassDefFoundError x) {
}
JarFile bootclasses = new JarFile("BootSupport.jar");
JarFile agentclasses = new JarFile("AgentSupport.jar");
Instrumentation ins = Agent.getInstrumentation();
ins.appendToBootstrapClassLoaderSearch(bootclasses);
try {
new BootSupport();
System.out.println("FAIL: BootSupport resolved");
failures++;
} catch (NoClassDefFoundError x) {
System.out.println("PASS: BootSupport failed to resolve");
}
try {
ins.appendToSystemClassLoaderSearch(agentclasses);
try {
new AgentSupport();
System.out.println("FAIL: AgentSupport resolved");
failures++;
} catch (NoClassDefFoundError x) {
System.out.println("PASS: AgentSupport failed to resolve");
}
} catch (UnsupportedOperationException x) {
System.out.println("System class loader does not support adding to class path");
}
if (failures > 0) {
throw new RuntimeException(failures + " test(s) failed.");
}
}
}

View file

@ -0,0 +1,47 @@
/*
* Copyright (c) 2005, 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.
*/
/*
*
*
* Used by the unit tests for the Instrument appendToBootstrapClassLoaderSearch
* tests. This class is a "supporting" class which the agent puts on the boot
* class path.
*/
package org.tools;
public class Tracer {
static {
ClassLoader cl = Tracer.class.getClassLoader();
if (cl != null) {
throw new RuntimeException("Tracer loaded by: " + cl);
}
System.out.println("Tracer loaded by boot class loader.");
}
public static void trace(String msg) {
System.out.println(msg);
}
}

View file

@ -0,0 +1,4 @@
Manifest-Version: 1.0
Premain-Class: Agent
Can-Redefine-Classes: true

View file

@ -0,0 +1,98 @@
#!/bin/sh
#
# Copyright (c) 2005, 2024, 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 6173575 6388987
# @summary Unit tests for appendToBootstrapClassLoaderSearch and
# appendToSystemClasLoaderSearch methods.
#
# @build Agent AgentSupport BootSupport BasicTest PrematureLoadTest DynamicTest
# @run shell/timeout=240 run_tests.sh
if [ "${TESTSRC}" = "" ]
then
echo "TESTSRC not set. Test cannot execute. Failed."
exit 1
fi
. ${TESTSRC}/CommonSetup.sh
# Simple tests
echo "Creating jar files for simple tests..."
cd ${TESTCLASSES}
"$JAR" ${TESTTOOLVMOPTS} -cfm Agent.jar "${TESTSRC}"/manifest.mf Agent.class
"$JAR" ${TESTTOOLVMOPTS} -cf AgentSupport.jar AgentSupport.class
"$JAR" ${TESTTOOLVMOPTS} -cf BootSupport.jar BootSupport.class
"$JAR" ${TESTTOOLVMOPTS} -cf SimpleTests.jar BasicTest.class PrematureLoadTest.class
failures=0
go() {
echo ''
sh -xc "$JAVA ${TESTVMOPTS} ${TESTJAVAOPTS} -javaagent:Agent.jar -classpath SimpleTests.jar $1 $2 $3" 2>&1
if [ $? != 0 ]; then failures=`expr $failures + 1`; fi
}
go BasicTest
go PrematureLoadTest
# Functional tests
echo ''
echo "Setup for functional tests..."
# Create org.tools.Tracer in temp directory so that it's not seen on the
# system class path
mkdir tmp
"${JAVAC}" ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} -d tmp "${TESTSRC}"/Tracer.java
(cd tmp; "${JAR}" ${TESTTOOLVMOPTS} cf ../Tracer.jar org/tools/Tracer.class)
# InstrumentedApplication is Application+instrmentation - don't copy as
# we don't want the original file permission
cat "${TESTSRC}"/InstrumentedApplication.java > ./Application.java
"${JAVAC}" ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} -classpath Tracer.jar -d . Application.java
mv Application.class InstrumentedApplication.bytes
cp "${TESTSRC}"/Application.java .
"${JAVAC}" ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} -d . Application.java
sh -xc "$JAVA ${TESTVMOPTS} ${TESTJAVAOPTS} -classpath . -javaagent:Agent.jar DynamicTest" 2>&1
if [ $? != 0 ]; then failures=`expr $failures + 1`; fi
#
# Results
#
echo ''
if [ $failures -gt 0 ];
then echo "$failures test(s) failed";
else echo "All test(s) passed"; fi
exit $failures