Skip to content

Commit 946c6cc

Browse files
committed
8334167: Test java/lang/instrument/NativeMethodPrefixApp.java timed out
Reviewed-by: dholmes, sspitsyn Backport-of: 3babffd
1 parent 6a6591e commit 946c6cc

File tree

3 files changed

+105
-63
lines changed

3 files changed

+105
-63
lines changed

test/jdk/java/lang/instrument/NativeMethodPrefixAgent.java

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,70 +35,69 @@ class NativeMethodPrefixAgent {
3535

3636
static ClassFileTransformer t0, t1, t2;
3737
static Instrumentation inst;
38-
private static Throwable agentError;
38+
private static Throwable agentError; // to be accessed/updated in a synchronized block
3939

40-
public static void checkErrors() {
40+
private static final String CLASS_TO_TRANSFORM = "NativeMethodPrefixApp$Dummy";
41+
42+
public static synchronized void checkErrors() {
4143
if (agentError != null) {
4244
throw new RuntimeException("Agent error", agentError);
4345
}
4446
}
4547

48+
private static synchronized void trackError(final Throwable t) {
49+
if (agentError == null) {
50+
agentError = t;
51+
return;
52+
}
53+
if (agentError != t) {
54+
agentError.addSuppressed(t);
55+
}
56+
}
57+
4658
static class Tr implements ClassFileTransformer {
4759
private static final ClassDesc CD_StringIdCallbackReporter = ClassDesc.ofInternalName("bootreporter/StringIdCallbackReporter");
4860
private static final MethodTypeDesc MTD_void_String_int = MethodTypeDesc.of(CD_void, CD_String, CD_int);
4961
final String trname;
5062
final int transformId;
63+
private final String nativeMethodPrefix;
5164

5265
Tr(int transformId) {
5366
this.trname = "tr" + transformId;
5467
this.transformId = transformId;
68+
this.nativeMethodPrefix = "wrapped_" + trname + "_";
5569
}
5670

57-
public byte[]
58-
transform(
59-
ClassLoader loader,
60-
String className,
61-
Class<?> classBeingRedefined,
62-
ProtectionDomain protectionDomain,
63-
byte[] classfileBuffer) {
64-
boolean redef = classBeingRedefined != null;
65-
System.out.println(trname + ": " +
66-
(redef? "Retransforming " : "Loading ") + className);
67-
if (className != null) {
68-
try {
69-
byte[] newcf = Instrumentor.instrFor(classfileBuffer)
70-
.addNativeMethodTrackingInjection(
71-
"wrapped_" + trname + "_", (name, h) -> {
72-
h.loadConstant(name);
73-
h.loadConstant(transformId);
74-
h.invokestatic(
75-
CD_StringIdCallbackReporter,
76-
"tracker",
77-
MTD_void_String_int);
78-
})
79-
.apply();
80-
/*** debugging ...
81-
if (newcf != null) {
82-
String fname = trname + (redef?"_redef" : "") + "/" + className;
83-
System.err.println("dumping to: " + fname);
84-
write_buffer(fname + "_before.class", classfileBuffer);
85-
write_buffer(fname + "_instr.class", newcf);
86-
}
87-
***/
88-
89-
return redef? null : newcf;
90-
} catch (Throwable ex) {
91-
if (agentError == null) {
92-
agentError = ex;
93-
}
94-
System.err.println("ERROR: Injection failure: " + ex);
95-
ex.printStackTrace();
71+
@Override
72+
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
73+
ProtectionDomain protectionDomain, byte[] classfileBuffer) {
74+
75+
try {
76+
// we only transform a specific application class
77+
if (!className.equals(CLASS_TO_TRANSFORM)) {
78+
return null;
79+
}
80+
if (classBeingRedefined != null) {
9681
return null;
9782
}
83+
// use a byte code generator which creates wrapper methods,
84+
// with a configured native method prefix, for each native method on the
85+
// class being transformed
86+
final Instrumentor byteCodeGenerator = Instrumentor.instrFor(classfileBuffer)
87+
.addNativeMethodTrackingInjection(nativeMethodPrefix,
88+
(name, cb) -> {
89+
cb.loadConstant(name);
90+
cb.loadConstant(transformId);
91+
cb.invokestatic(CD_StringIdCallbackReporter,
92+
"tracker", MTD_void_String_int);
93+
});
94+
// generate the bytecode
95+
return byteCodeGenerator.apply();
96+
} catch (Throwable t) {
97+
trackError(t);
98+
return null;
9899
}
99-
return null;
100100
}
101-
102101
}
103102

104103
// for debugging

test/jdk/java/lang/instrument/NativeMethodPrefixApp.java

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
*/
2323

2424

25-
import java.io.File;
2625
import java.nio.file.Path;
27-
import java.lang.management.*;
2826

2927
import bootreporter.*;
3028
import jdk.test.lib.helpers.ClassFileInstaller;
@@ -33,27 +31,24 @@
3331

3432
/*
3533
* @test
36-
* @bug 6263319
34+
* @bug 6263319 8334167
3735
* @summary test setNativeMethodPrefix
3836
* @requires ((vm.opt.StartFlightRecording == null) | (vm.opt.StartFlightRecording == false)) & ((vm.opt.FlightRecorder == null) | (vm.opt.FlightRecorder == false))
39-
* @modules java.management
40-
* java.instrument
37+
* @modules java.instrument
4138
* @library /test/lib
4239
* @build bootreporter.StringIdCallback bootreporter.StringIdCallbackReporter
4340
* asmlib.Instrumentor NativeMethodPrefixAgent
4441
* @enablePreview
4542
* @comment The test uses asmlib/Instrumentor.java which relies on ClassFile API PreviewFeature.
46-
* @run driver/timeout=240 NativeMethodPrefixApp roleDriver
47-
* @comment The test uses a higher timeout to prevent test timeouts noted in JDK-6528548
43+
* @run main/native NativeMethodPrefixApp roleDriver
4844
*/
4945
public class NativeMethodPrefixApp implements StringIdCallback {
5046

51-
// This test is fragile like a golden file test.
52-
// It assumes that a specific non-native library method will call a specific
53-
// native method. The below may need to be updated based on library changes.
54-
static String goldenNativeMethodName = "getStartupTime";
55-
47+
// we expect this native method, which is part of this test's application,
48+
// to be instrumented and invoked
49+
static String goldenNativeMethodName = "fooBarNativeMethod";
5650
static boolean[] gotIt = {false, false, false};
51+
private static final String testLibraryPath = System.getProperty("test.nativepath");
5752

5853
public static void main(String[] args) throws Exception {
5954
if (args.length == 1) {
@@ -68,21 +63,19 @@ public static void main(String[] args) throws Exception {
6863
launchApp(agentJar);
6964
} else {
7065
System.err.println("running app");
66+
System.loadLibrary("NativeMethodPrefix"); // load the native library
7167
new NativeMethodPrefixApp().run();
7268
}
7369
}
7470

7571
private static Path createAgentJar() throws Exception {
76-
final String testClassesDir = System.getProperty("test.classes");
7772
final Path agentJar = Path.of("NativeMethodPrefixAgent.jar");
7873
final String manifest = """
7974
Manifest-Version: 1.0
8075
Premain-Class: NativeMethodPrefixAgent
8176
Can-Retransform-Classes: true
8277
Can-Set-Native-Method-Prefix: true
83-
"""
84-
+ "Boot-Class-Path: " + testClassesDir.replace(File.separatorChar, '/') + "/"
85-
+ "\n";
78+
""";
8679
System.out.println("Manifest is:\n" + manifest);
8780
// create the agent jar
8881
ClassFileInstaller.writeJar(agentJar.getFileName().toString(),
@@ -96,6 +89,7 @@ private static void launchApp(final Path agentJar) throws Exception {
9689
final OutputAnalyzer oa = ProcessTools.executeTestJava(
9790
"--enable-preview", // due to usage of ClassFile API PreviewFeature in the agent
9891
"-javaagent:" + agentJar.toString(),
92+
"-Djava.library.path=" + testLibraryPath,
9993
NativeMethodPrefixApp.class.getName());
10094
oa.shouldHaveExitValue(0);
10195
// make available stdout/stderr in the logs, even in case of successful completion
@@ -105,10 +99,11 @@ private static void launchApp(final Path agentJar) throws Exception {
10599
private void run() throws Exception {
106100
StringIdCallbackReporter.registerCallback(this);
107101
System.err.println("start");
108-
109-
java.lang.reflect.Array.getLength(new short[5]);
110-
RuntimeMXBean mxbean = ManagementFactory.getRuntimeMXBean();
111-
System.err.println(mxbean.getVmVendor());
102+
final long val = new Dummy().callSomeNativeMethod();
103+
if (val != 42) {
104+
throw new RuntimeException("unexpected return value " + val
105+
+ " from native method, expected 42");
106+
}
112107

113108
NativeMethodPrefixAgent.checkErrors();
114109

@@ -128,4 +123,13 @@ public void tracker(String name, int id) {
128123
System.err.println("Tracked #" + id + ": " + name);
129124
}
130125
}
126+
127+
private static class Dummy {
128+
129+
private long callSomeNativeMethod() {
130+
return fooBarNativeMethod();
131+
}
132+
133+
private native long fooBarNativeMethod();
134+
}
131135
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2024, 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+
#include <stdio.h>
25+
#include "jni.h"
26+
27+
JNIEXPORT jlong JNICALL
28+
Java_NativeMethodPrefixApp_00024Dummy_fooBarNativeMethod(JNIEnv *env, jclass clazz)
29+
{
30+
fprintf(stderr, "native method called\n");
31+
return 42;
32+
}
33+
34+
JNIEXPORT jint JNICALL
35+
JNI_OnLoad(JavaVM *vm, void *reserved)
36+
{
37+
fprintf(stderr, "native library loaded\n");
38+
return JNI_VERSION_1_1; // this native library needs the very basic JNI support
39+
}

0 commit comments

Comments
 (0)