Skip to content

Commit a934df7

Browse files
committed
Fix test agent dependes on target's library
The agent should not depend on library loaded by its target program.
1 parent 592d027 commit a934df7

File tree

7 files changed

+94
-60
lines changed

7 files changed

+94
-60
lines changed

substratevm/mx.substratevm/mx_substratevm.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,7 @@ def build_and_test_java_agent_image(native_image, args):
18521852
agent_test_classpath = join('com', 'oracle', 'svm', 'test', 'javaagent', 'agent' + str(i))
18531853
class_list = [join('com', 'oracle', 'svm', 'test', 'javaagent', 'agent' + str(i), f) for f in os.listdir(agent_test_classpath) if os.path.isfile(os.path.join(agent_test_classpath, f)) and f.endswith(".class")]
18541854
class_list.append(join('com', 'oracle', 'svm', 'test', 'javaagent', 'AgentPremainHelper.class'))
1855+
class_list.append(join('com', 'oracle', 'svm', 'test', 'javaagent', 'AssertInAgent.class'))
18551856
mx.run([mx.get_jdk().jar, 'cmf', join(test_classpath, 'resources', 'javaagent' + str(i), 'MANIFEST.MF'), agent] + class_list, cwd = test_classpath)
18561857
agents.append(agent)
18571858
os.chdir(current_dir)

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/api/HostVM.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,6 @@ public boolean enableReachableInCurrentLayer() {
391391
return false;
392392
}
393393

394-
public boolean isFromJavaAgent(@SuppressWarnings("unused") Class<?> clazz) {
395-
return false;
396-
}
397-
398394
/**
399395
* Helpers to determine what analysis actions should be taken for a given Multi-Method version.
400396
*/

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/infrastructure/WrappedConstantPool.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -120,26 +120,7 @@ public JavaMethod lookupMethod(int cpi, int opcode) {
120120
@Override
121121
public JavaMethod lookupMethod(int cpi, int opcode, ResolvedJavaMethod caller) {
122122
try {
123-
JavaMethod ret = universe.lookupAllowUnresolved(wrapped.lookupMethod(cpi, opcode, OriginalMethodProvider.getOriginalMethod(caller)));
124-
/**
125-
* The java agent classes are loaded by appClassloader, but their dependencies could be
126-
* loaded by NativeImageClassloader. So if the required method could not be resolved, we
127-
* look further into classes loaded by nativeImageClassloader.
128-
*/
129-
if (ret instanceof UnresolvedJavaMethod && universe.hostVM().isFromJavaAgent(OriginalClassProvider.getJavaClass(caller.getDeclaringClass()))) {
130-
UnresolvedJavaMethod unresolvedResult = (UnresolvedJavaMethod) ret;
131-
String className = unresolvedResult.format("%H");
132-
String methodNameWithSignature = unresolvedResult.format("%n(%P)");
133-
try {
134-
Class<?> loadedClass = ((AnalysisUniverse) universe).getConcurrentAnalysisAccess().findClassByName(className);
135-
ResolvedJavaType resolvedType = ((AnalysisUniverse) universe).getOriginalMetaAccess().lookupJavaType(loadedClass);
136-
ResolvedJavaMethod resolvedMethod = Arrays.stream(resolvedType.getDeclaredMethods(false)).filter(m -> m.format("%n(%P)").equals(methodNameWithSignature)).findFirst().get();
137-
return universe.lookupAllowUnresolved(resolvedMethod);
138-
} catch (Exception e) {
139-
// Could not get the resolved method, get to the unresolved path
140-
}
141-
}
142-
return ret;
123+
return universe.lookupAllowUnresolved(wrapped.lookupMethod(cpi, opcode, OriginalMethodProvider.getOriginalMethod(caller)));
143124
} catch (Throwable ex) {
144125
Throwable cause = ex;
145126
if (ex instanceof ExceptionInInitializerError && ex.getCause() != null) {

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/SVMHost.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,17 +1196,4 @@ public boolean allowConstantFolding(AnalysisMethod method) {
11961196
public SimulateClassInitializerSupport createSimulateClassInitializerSupport(AnalysisMetaAccess aMetaAccess) {
11971197
return new SimulateClassInitializerSupport(aMetaAccess, this);
11981198
}
1199-
1200-
@Override
1201-
public boolean isFromJavaAgent(Class<?> clazz) {
1202-
if (SubstrateOptions.JavaAgent.hasBeenSet()) {
1203-
try {
1204-
String classLocation = clazz.getProtectionDomain().getCodeSource().getLocation().getFile();
1205-
return SubstrateOptions.JavaAgent.getValue().values().stream().map(s -> s.split("=")[0]).anyMatch(s -> s.equals(classLocation));
1206-
} catch (Exception e) {
1207-
return false;
1208-
}
1209-
}
1210-
return false;
1211-
}
12121199
}

substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/javaagent/AgentTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ private static void testAgentOptions() {
4242
}
4343

4444
private static void testPremainSequence() {
45-
String first = AgentPremainHelper.getFirst();
46-
String second = AgentPremainHelper.getSecond();
45+
String first = System.getProperty("first.load.agent");
46+
String second = System.getProperty("second.load.agent");
4747
Assert.assertNotNull(first);
4848
if (second != null) {
4949
String agentName = TestJavaAgent1.class.getName();
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2025, 2025, Alibaba Group Holding Limited. All rights reserved.
4+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5+
*
6+
* This code is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License version 2 only, as
8+
* published by the Free Software Foundation. Oracle designates this
9+
* particular file as subject to the "Classpath" exception as provided
10+
* by Oracle in the LICENSE file that accompanied this code.
11+
*
12+
* This code is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15+
* version 2 for more details (a copy is included in the LICENSE file that
16+
* accompanied this code).
17+
*
18+
* You should have received a copy of the GNU General Public License version
19+
* 2 along with this work; if not, write to the Free Software Foundation,
20+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21+
*
22+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23+
* or visit www.oracle.com if you need additional information or have any
24+
* questions.
25+
*/
26+
27+
package com.oracle.svm.test.javaagent;
28+
29+
/**
30+
* Assertions used inside agent when not using JUNIT
31+
*/
32+
public class AssertInAgent {
33+
public static void assertNotNull(Object o) {
34+
if (o == null) {
35+
throw new RuntimeException("Object input is null, but expected to be non-null");
36+
}
37+
}
38+
39+
public static void assertEquals(boolean expected, boolean actual) {
40+
if (expected != actual) {
41+
throw new RuntimeException(String.format("Expected(%s) is not equal to actual(%s)", expected, actual));
42+
}
43+
}
44+
45+
public static void assertEquals(long expected, long actual) {
46+
if (expected != actual) {
47+
throw new RuntimeException(String.format("Expected(%s) is not equal to actual(%s)", expected, actual));
48+
}
49+
}
50+
51+
public static void assertEquals(Object expected, Object actual) {
52+
if (expected != actual) {
53+
if (expected != null) {
54+
assertNotNull(actual);
55+
if (!expected.equals(actual)) {
56+
throw new RuntimeException(String.format("Expected(%s) is not equal to actual(%s)", expected, actual));
57+
}
58+
} else {
59+
throw new RuntimeException(String.format("Expected(null) is not equal to actual(%s)", actual));
60+
}
61+
}
62+
}
63+
64+
65+
66+
public static void assertTrue(boolean actual) {
67+
assertEquals(true, actual);
68+
}
69+
}

substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/javaagent/agent1/TestJavaAgent1.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
package com.oracle.svm.test.javaagent.agent1;
2727

2828
import com.oracle.svm.test.javaagent.AgentPremainHelper;
29+
import com.oracle.svm.test.javaagent.AssertInAgent;
2930
import org.graalvm.nativeimage.ImageInfo;
30-
import org.junit.Assert;
3131

3232
import java.lang.classfile.ClassFile;
3333
import java.lang.classfile.ClassModel;
@@ -53,22 +53,22 @@ public static void premain(
5353
* Test {@code inst} is {@link NativeImageNoOpRuntimeInstrumentation} and behaves as
5454
* defined.
5555
*/
56-
Assert.assertNotNull(inst);
57-
Assert.assertEquals(false, inst.isRetransformClassesSupported());
58-
Assert.assertEquals(false, inst.removeTransformer(null));
59-
Assert.assertEquals(false, inst.isRedefineClassesSupported());
56+
AssertInAgent.assertNotNull(inst);
57+
AssertInAgent.assertEquals(false, inst.isRetransformClassesSupported());
58+
AssertInAgent.assertEquals(false, inst.removeTransformer(null));
59+
AssertInAgent.assertEquals(false, inst.isRedefineClassesSupported());
6060

61-
Assert.assertEquals(false, inst.isModifiableClass(null));
61+
AssertInAgent.assertEquals(false, inst.isModifiableClass(null));
6262

6363
Class<?>[] allClasses = inst.getAllLoadedClasses();
64-
Assert.assertTrue(allClasses.length > 0);
64+
AssertInAgent.assertTrue(allClasses.length > 0);
6565
Class<?> currentAgentClassFromAllLoaded = null;
6666
for (Class<?> c : allClasses) {
6767
if (c.equals(TestJavaAgent1.class)) {
6868
currentAgentClassFromAllLoaded = c;
6969
}
7070
}
71-
Assert.assertNotNull(currentAgentClassFromAllLoaded);
71+
AssertInAgent.assertNotNull(currentAgentClassFromAllLoaded);
7272

7373
// redefineClasses should throw UnsupportedOperationException
7474
Exception exception = null;
@@ -77,8 +77,8 @@ public static void premain(
7777
} catch (Exception e) {
7878
exception = e;
7979
}
80-
Assert.assertNotNull(exception);
81-
Assert.assertEquals(UnsupportedOperationException.class, exception.getClass());
80+
AssertInAgent.assertNotNull(exception);
81+
AssertInAgent.assertEquals(UnsupportedOperationException.class, exception.getClass());
8282

8383
// getInitiatedClasses should throw UnsupportedOperationException
8484
exception = null;
@@ -87,8 +87,8 @@ public static void premain(
8787
} catch (Exception e) {
8888
exception = e;
8989
}
90-
Assert.assertNotNull(exception);
91-
Assert.assertEquals(UnsupportedOperationException.class, exception.getClass());
90+
AssertInAgent.assertNotNull(exception);
91+
AssertInAgent.assertEquals(UnsupportedOperationException.class, exception.getClass());
9292

9393
// retransformClasses should throw UnsupportedOperationException
9494
exception = null;
@@ -97,8 +97,8 @@ public static void premain(
9797
} catch (Exception e) {
9898
exception = e;
9999
}
100-
Assert.assertNotNull(exception);
101-
Assert.assertEquals(UnsupportedOperationException.class, exception.getClass());
100+
AssertInAgent.assertNotNull(exception);
101+
AssertInAgent.assertEquals(UnsupportedOperationException.class, exception.getClass());
102102

103103
// appendToBootstrapClassLoaderSearch should throw UnsupportedOperationException
104104
exception = null;
@@ -107,8 +107,8 @@ public static void premain(
107107
} catch (Exception e) {
108108
exception = e;
109109
}
110-
Assert.assertNotNull(exception);
111-
Assert.assertEquals(UnsupportedOperationException.class, exception.getClass());
110+
AssertInAgent.assertNotNull(exception);
111+
AssertInAgent.assertEquals(UnsupportedOperationException.class, exception.getClass());
112112

113113
// appendToSystemClassLoaderSearch should throw UnsupportedOperationException
114114
exception = null;
@@ -117,14 +117,14 @@ public static void premain(
117117
} catch (Exception e) {
118118
exception = e;
119119
}
120-
Assert.assertNotNull(exception);
121-
Assert.assertEquals(UnsupportedOperationException.class, exception.getClass());
120+
AssertInAgent.assertNotNull(exception);
121+
AssertInAgent.assertEquals(UnsupportedOperationException.class, exception.getClass());
122122

123-
Assert.assertEquals(-1, inst.getObjectSize(null));
124-
Assert.assertEquals(false, inst.isNativeMethodPrefixSupported());
123+
AssertInAgent.assertEquals(-1, inst.getObjectSize(null));
124+
AssertInAgent.assertEquals(false, inst.isNativeMethodPrefixSupported());
125125

126126
Module currentModule = TestJavaAgent1.class.getModule();
127-
Assert.assertEquals(true, inst.isModifiableModule(currentModule));
127+
AssertInAgent.assertEquals(true, inst.isModifiableModule(currentModule));
128128

129129
// redefineModule only does checks, no actual actions.
130130
inst.redefineModule(currentModule, Set.of(Class.class.getModule()), Collections.emptyMap(), null, null, null);

0 commit comments

Comments
 (0)