8334167: Test java/lang/instrument/NativeMethodPrefixApp.java timed out
Reviewed-by: dholmes, sspitsyn, alanb
This commit is contained in:
parent
d90c20c0c7
commit
3babffd400
test/jdk/java/lang/instrument
@ -35,70 +35,69 @@ class NativeMethodPrefixAgent {
|
||||
|
||||
static ClassFileTransformer t0, t1, t2;
|
||||
static Instrumentation inst;
|
||||
private static Throwable agentError;
|
||||
private static Throwable agentError; // to be accessed/updated in a synchronized block
|
||||
|
||||
public static void checkErrors() {
|
||||
private static final String CLASS_TO_TRANSFORM = "NativeMethodPrefixApp$Dummy";
|
||||
|
||||
public static synchronized void checkErrors() {
|
||||
if (agentError != null) {
|
||||
throw new RuntimeException("Agent error", agentError);
|
||||
}
|
||||
}
|
||||
|
||||
private static synchronized void trackError(final Throwable t) {
|
||||
if (agentError == null) {
|
||||
agentError = t;
|
||||
return;
|
||||
}
|
||||
if (agentError != t) {
|
||||
agentError.addSuppressed(t);
|
||||
}
|
||||
}
|
||||
|
||||
static class Tr implements ClassFileTransformer {
|
||||
private static final ClassDesc CD_StringIdCallbackReporter = ClassDesc.ofInternalName("bootreporter/StringIdCallbackReporter");
|
||||
private static final MethodTypeDesc MTD_void_String_int = MethodTypeDesc.of(CD_void, CD_String, CD_int);
|
||||
final String trname;
|
||||
final int transformId;
|
||||
private final String nativeMethodPrefix;
|
||||
|
||||
Tr(int transformId) {
|
||||
this.trname = "tr" + transformId;
|
||||
this.transformId = transformId;
|
||||
this.nativeMethodPrefix = "wrapped_" + trname + "_";
|
||||
}
|
||||
|
||||
public byte[]
|
||||
transform(
|
||||
ClassLoader loader,
|
||||
String className,
|
||||
Class<?> classBeingRedefined,
|
||||
ProtectionDomain protectionDomain,
|
||||
byte[] classfileBuffer) {
|
||||
boolean redef = classBeingRedefined != null;
|
||||
System.out.println(trname + ": " +
|
||||
(redef? "Retransforming " : "Loading ") + className);
|
||||
if (className != null) {
|
||||
try {
|
||||
byte[] newcf = Instrumentor.instrFor(classfileBuffer)
|
||||
.addNativeMethodTrackingInjection(
|
||||
"wrapped_" + trname + "_", (name, h) -> {
|
||||
h.loadConstant(name);
|
||||
h.loadConstant(transformId);
|
||||
h.invokestatic(
|
||||
CD_StringIdCallbackReporter,
|
||||
"tracker",
|
||||
MTD_void_String_int);
|
||||
})
|
||||
.apply();
|
||||
/*** debugging ...
|
||||
if (newcf != null) {
|
||||
String fname = trname + (redef?"_redef" : "") + "/" + className;
|
||||
System.err.println("dumping to: " + fname);
|
||||
write_buffer(fname + "_before.class", classfileBuffer);
|
||||
write_buffer(fname + "_instr.class", newcf);
|
||||
}
|
||||
***/
|
||||
@Override
|
||||
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
|
||||
ProtectionDomain protectionDomain, byte[] classfileBuffer) {
|
||||
|
||||
return redef? null : newcf;
|
||||
} catch (Throwable ex) {
|
||||
if (agentError == null) {
|
||||
agentError = ex;
|
||||
}
|
||||
System.err.println("ERROR: Injection failure: " + ex);
|
||||
ex.printStackTrace();
|
||||
try {
|
||||
// we only transform a specific application class
|
||||
if (!className.equals(CLASS_TO_TRANSFORM)) {
|
||||
return null;
|
||||
}
|
||||
if (classBeingRedefined != null) {
|
||||
return null;
|
||||
}
|
||||
// use a byte code generator which creates wrapper methods,
|
||||
// with a configured native method prefix, for each native method on the
|
||||
// class being transformed
|
||||
final Instrumentor byteCodeGenerator = Instrumentor.instrFor(classfileBuffer)
|
||||
.addNativeMethodTrackingInjection(nativeMethodPrefix,
|
||||
(name, cb) -> {
|
||||
cb.loadConstant(name);
|
||||
cb.loadConstant(transformId);
|
||||
cb.invokestatic(CD_StringIdCallbackReporter,
|
||||
"tracker", MTD_void_String_int);
|
||||
});
|
||||
// generate the bytecode
|
||||
return byteCodeGenerator.apply();
|
||||
} catch (Throwable t) {
|
||||
trackError(t);
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// for debugging
|
||||
|
@ -22,10 +22,7 @@
|
||||
*/
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.lang.management.*;
|
||||
import java.util.zip.CRC32;
|
||||
|
||||
import bootreporter.*;
|
||||
import jdk.test.lib.helpers.ClassFileInstaller;
|
||||
@ -34,27 +31,24 @@ import jdk.test.lib.process.ProcessTools;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 6263319
|
||||
* @bug 6263319 8334167
|
||||
* @summary test setNativeMethodPrefix
|
||||
* @requires ((vm.opt.StartFlightRecording == null) | (vm.opt.StartFlightRecording == false)) & ((vm.opt.FlightRecorder == null) | (vm.opt.FlightRecorder == false))
|
||||
* @modules java.management
|
||||
* java.instrument
|
||||
* @modules java.instrument
|
||||
* @library /test/lib
|
||||
* @build bootreporter.StringIdCallback bootreporter.StringIdCallbackReporter
|
||||
* asmlib.Instrumentor NativeMethodPrefixAgent
|
||||
* @enablePreview
|
||||
* @comment The test uses asmlib/Instrumentor.java which relies on ClassFile API PreviewFeature.
|
||||
* @run driver/timeout=240 NativeMethodPrefixApp roleDriver
|
||||
* @comment The test uses a higher timeout to prevent test timeouts noted in JDK-6528548
|
||||
* @run main/native NativeMethodPrefixApp roleDriver
|
||||
*/
|
||||
public class NativeMethodPrefixApp implements StringIdCallback {
|
||||
|
||||
// This test is fragile like a golden file test.
|
||||
// It assumes that a specific non-native library method will call a specific
|
||||
// native method. The below may need to be updated based on library changes.
|
||||
static String goldenNativeMethodName = "getStartupTime";
|
||||
|
||||
// we expect this native method, which is part of this test's application,
|
||||
// to be instrumented and invoked
|
||||
static String goldenNativeMethodName = "fooBarNativeMethod";
|
||||
static boolean[] gotIt = {false, false, false};
|
||||
private static final String testLibraryPath = System.getProperty("test.nativepath");
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
if (args.length == 1) {
|
||||
@ -69,21 +63,19 @@ public class NativeMethodPrefixApp implements StringIdCallback {
|
||||
launchApp(agentJar);
|
||||
} else {
|
||||
System.err.println("running app");
|
||||
System.loadLibrary("NativeMethodPrefix"); // load the native library
|
||||
new NativeMethodPrefixApp().run();
|
||||
}
|
||||
}
|
||||
|
||||
private static Path createAgentJar() throws Exception {
|
||||
final String testClassesDir = System.getProperty("test.classes");
|
||||
final Path agentJar = Path.of("NativeMethodPrefixAgent.jar");
|
||||
final String manifest = """
|
||||
Manifest-Version: 1.0
|
||||
Premain-Class: NativeMethodPrefixAgent
|
||||
Can-Retransform-Classes: true
|
||||
Can-Set-Native-Method-Prefix: true
|
||||
"""
|
||||
+ "Boot-Class-Path: " + testClassesDir.replace(File.separatorChar, '/') + "/"
|
||||
+ "\n";
|
||||
""";
|
||||
System.out.println("Manifest is:\n" + manifest);
|
||||
// create the agent jar
|
||||
ClassFileInstaller.writeJar(agentJar.getFileName().toString(),
|
||||
@ -97,10 +89,7 @@ public class NativeMethodPrefixApp implements StringIdCallback {
|
||||
final OutputAnalyzer oa = ProcessTools.executeTestJava(
|
||||
"--enable-preview", // due to usage of ClassFile API PreviewFeature in the agent
|
||||
"-javaagent:" + agentJar.toString(),
|
||||
// We disable CheckIntrinsic because the NativeMethodPrefixAgent modifies
|
||||
// the native method names, which then causes a failure in the VM check
|
||||
// for the presence of an intrinsic on a @IntrinsicCandidate native method.
|
||||
"-XX:+UnlockDiagnosticVMOptions", "-XX:-CheckIntrinsics",
|
||||
"-Djava.library.path=" + testLibraryPath,
|
||||
NativeMethodPrefixApp.class.getName());
|
||||
oa.shouldHaveExitValue(0);
|
||||
// make available stdout/stderr in the logs, even in case of successful completion
|
||||
@ -110,14 +99,11 @@ public class NativeMethodPrefixApp implements StringIdCallback {
|
||||
private void run() throws Exception {
|
||||
StringIdCallbackReporter.registerCallback(this);
|
||||
System.err.println("start");
|
||||
|
||||
java.lang.reflect.Array.getLength(new short[5]);
|
||||
RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean();
|
||||
System.err.println(mxbean.getVmVendor());
|
||||
// Simply load a class containing an @IntrinsicCandidate on a native method
|
||||
// to exercise the VM code which verifies the presence of the intrinsic
|
||||
// implementation for that method.
|
||||
System.err.println(new CRC32());
|
||||
final long val = new Dummy().callSomeNativeMethod();
|
||||
if (val != 42) {
|
||||
throw new RuntimeException("unexpected return value " + val
|
||||
+ " from native method, expected 42");
|
||||
}
|
||||
|
||||
NativeMethodPrefixAgent.checkErrors();
|
||||
|
||||
@ -137,4 +123,13 @@ public class NativeMethodPrefixApp implements StringIdCallback {
|
||||
System.err.println("Tracked #" + id + ": " + name);
|
||||
}
|
||||
}
|
||||
|
||||
private static class Dummy {
|
||||
|
||||
private long callSomeNativeMethod() {
|
||||
return fooBarNativeMethod();
|
||||
}
|
||||
|
||||
private native long fooBarNativeMethod();
|
||||
}
|
||||
}
|
||||
|
39
test/jdk/java/lang/instrument/libNativeMethodPrefix.c
Normal file
39
test/jdk/java/lang/instrument/libNativeMethodPrefix.c
Normal file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright (c) 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.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "jni.h"
|
||||
|
||||
JNIEXPORT jlong JNICALL
|
||||
Java_NativeMethodPrefixApp_00024Dummy_fooBarNativeMethod(JNIEnv *env, jclass clazz)
|
||||
{
|
||||
fprintf(stderr, "native method called\n");
|
||||
return 42;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL
|
||||
JNI_OnLoad(JavaVM *vm, void *reserved)
|
||||
{
|
||||
fprintf(stderr, "native library loaded\n");
|
||||
return JNI_VERSION_1_1; // this native library needs the very basic JNI support
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user