Skip to content

Commit c538cd8

Browse files
author
Serguei Spitsyn
committed
8165276: Spec states to invoke the premain method in an agent class if it's public but implementation differs
Reviewed-by: mchung, dholmes, alanb
1 parent 9ea9323 commit c538cd8

22 files changed

+293
-242
lines changed

src/java.instrument/share/classes/sun/instrument/InstrumentationImpl.java

+14-39
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -27,6 +27,7 @@
2727

2828
import java.lang.instrument.UnmodifiableModuleException;
2929
import java.lang.reflect.Method;
30+
import java.lang.reflect.Modifier;
3031
import java.lang.reflect.AccessibleObject;
3132
import java.lang.instrument.ClassFileTransformer;
3233
import java.lang.instrument.ClassDefinition;
@@ -441,12 +442,6 @@ public Object run() {
441442
//
442443
// 1) declared with a signature of (String, Instrumentation)
443444
// 2) declared with a signature of (String)
444-
// 3) inherited with a signature of (String, Instrumentation)
445-
// 4) inherited with a signature of (String)
446-
//
447-
// So the declared version of either 1-arg or 2-arg always takes
448-
// primary precedence over an inherited version. After that, the
449-
// 2-arg version takes precedence over the 1-arg version.
450445
//
451446
// If no method is found then we throw the NoSuchMethodException
452447
// from the first attempt so that the exception text indicates
@@ -470,45 +465,25 @@ public Object run() {
470465
try {
471466
m = javaAgentClass.getDeclaredMethod(methodname,
472467
new Class<?>[] { String.class });
473-
} catch (NoSuchMethodException x) {
474-
// ignore this exception because we'll try
475-
// two arg inheritance next
476-
}
477-
}
478-
479-
if (m == null) {
480-
// now try the inherited 2-arg method
481-
try {
482-
m = javaAgentClass.getMethod( methodname,
483-
new Class<?>[] {
484-
String.class,
485-
java.lang.instrument.Instrumentation.class
486-
}
487-
);
488-
twoArgAgent = true;
489-
} catch (NoSuchMethodException x) {
490-
// ignore this exception because we'll try
491-
// one arg inheritance next
492-
}
493-
}
494-
495-
if (m == null) {
496-
// finally try the inherited 1-arg method
497-
try {
498-
m = javaAgentClass.getMethod(methodname,
499-
new Class<?>[] { String.class });
500468
} catch (NoSuchMethodException x) {
501469
// none of the methods exists so we throw the
502470
// first NoSuchMethodException as per 5.0
503471
throw firstExc;
504472
}
505473
}
506474

507-
// the premain method should not be required to be public,
508-
// make it accessible so we can call it
509-
// Note: The spec says the following:
510-
// The agent class must implement a public static premain method...
511-
setAccessible(m, true);
475+
// reject non-public premain or agentmain method
476+
if (!Modifier.isPublic(m.getModifiers())) {
477+
String msg = "method " + classname + "." + methodname + " must be declared public";
478+
throw new IllegalAccessException(msg);
479+
}
480+
481+
if (!Modifier.isPublic(javaAgentClass.getModifiers()) &&
482+
!javaAgentClass.getModule().isNamed()) {
483+
// If the java agent class is in an unnamed module, the java agent class can be non-public.
484+
// Suppress access check upon the invocation of the premain/agentmain method.
485+
setAccessible(m, true);
486+
}
512487

513488
// invoke the 1 or 2-arg method
514489
if (twoArgAgent) {

test/jdk/java/lang/instrument/PremainClass/NoPremainAgentTest.java test/jdk/java/lang/instrument/NegativeAgentRunner.java

+16-29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,38 +21,25 @@
2121
* questions.
2222
*/
2323

24-
import jdk.test.lib.process.OutputAnalyzer;
24+
package jdk.java.lang.instrument;
25+
26+
import java.lang.RuntimeException;
2527
import jdk.test.lib.process.ProcessTools;
26-
import jdk.test.lib.Utils;
28+
import jdk.test.lib.process.OutputAnalyzer;
2729

28-
/*
29-
* @test
30-
* @bug 6289149
31-
* @summary test when the agent's class is missing the premain() function.
32-
* @library /test/lib
33-
* @modules java.management
34-
* java.instrument
35-
* @run build DummyMain
36-
* @run shell ../MakeJAR3.sh NoPremainAgent
37-
* @run main/othervm -XX:-CreateCoredumpOnCrash NoPremainAgentTest
38-
*/
39-
public class NoPremainAgentTest {
40-
// Use a javaagent without the premain() function.
41-
// Verify that we get the correct exception.
42-
public static void main(String[] a) throws Exception {
43-
String testArgs = String.format(
44-
"-javaagent:NoPremainAgent.jar -classpath %s DummyMain",
45-
System.getProperty("test.classes", "."));
30+
public class NegativeAgentRunner {
4631

32+
public static void main(String argv[]) throws Exception {
33+
if (argv.length != 2) {
34+
throw new RuntimeException("Agent and exception class names are expected in arguments");
35+
}
36+
String agentClassName = argv[0];
37+
String excepClassName = argv[1];
4738
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
48-
Utils.addTestJavaOpts(testArgs.split("\\s+")));
49-
System.out.println("testjvm.cmd:" + Utils.getCommandLine(pb));
50-
51-
OutputAnalyzer output = ProcessTools.executeProcess(pb);
52-
System.out.println("testjvm.stdout:" + output.getStdout());
53-
System.out.println("testjvm.stderr:" + output.getStderr());
54-
55-
output.stderrShouldContain("java.lang.NoSuchMethodException");
39+
"-javaagent:" + agentClassName + ".jar",
40+
agentClassName);
41+
OutputAnalyzer output = new OutputAnalyzer(pb.start());
42+
output.shouldContain(excepClassName);
5643
if (0 == output.getExitValue()) {
5744
throw new RuntimeException("Expected error but got exit value 0");
5845
}

test/jdk/java/lang/instrument/PremainClass/InheritAgent0001.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,17 +23,20 @@
2323

2424
/**
2525
* @test
26-
* @bug 6289149
26+
* @bug 6289149 8165276
2727
* @summary test config (0,0,0,1): declared 1-arg in agent class
2828
* @author Daniel D. Daugherty, Sun Microsystems
2929
*
30-
* @run shell ../MakeJAR3.sh InheritAgent0001
30+
* @library /test/lib
31+
* @build jdk.java.lang.instrument.PremainClass.InheritAgent0001
32+
* @run driver jdk.test.lib.util.JavaAgentBuilder
33+
* InheritAgent0001 InheritAgent0001.jar
3134
* @run main/othervm -javaagent:InheritAgent0001.jar DummyMain
3235
*/
3336

3437
import java.lang.instrument.*;
3538

36-
class InheritAgent0001 extends InheritAgent0001Super {
39+
public class InheritAgent0001 extends InheritAgent0001Super {
3740

3841
//
3942
// This agent has a single argument premain() method which

test/jdk/java/lang/instrument/PremainClass/InheritAgent0010.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,17 +23,20 @@
2323

2424
/**
2525
* @test
26-
* @bug 6289149
26+
* @bug 6289149 8165276
2727
* @summary test config (0,0,1,0): declared 2-arg in agent class
2828
* @author Daniel D. Daugherty, Sun Microsystems
2929
*
30-
* @run shell ../MakeJAR3.sh InheritAgent0010
30+
* @library /test/lib
31+
* @build jdk.java.lang.instrument.PremainClass.InheritAgent0010
32+
* @run driver jdk.test.lib.util.JavaAgentBuilder
33+
* InheritAgent0010 InheritAgent0010.jar
3134
* @run main/othervm -javaagent:InheritAgent0010.jar DummyMain
3235
*/
3336

3437
import java.lang.instrument.*;
3538

36-
class InheritAgent0010 extends InheritAgent0010Super {
39+
public class InheritAgent0010 extends InheritAgent0010Super {
3740

3841
// This agent does NOT have a single argument premain() method.
3942

test/jdk/java/lang/instrument/PremainClass/InheritAgent0011.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,17 +23,20 @@
2323

2424
/**
2525
* @test
26-
* @bug 6289149
26+
* @bug 6289149 8165276
2727
* @summary test config (0,0,1,1): declared 2-arg and declared 1-arg in agent class
2828
* @author Daniel D. Daugherty, Sun Microsystems
2929
*
30-
* @run shell ../MakeJAR3.sh InheritAgent0011
30+
* @library /test/lib
31+
* @build jdk.java.lang.instrument.PremainClass.InheritAgent0011
32+
* @run driver jdk.test.lib.util.JavaAgentBuilder
33+
* InheritAgent0011 InheritAgent0011.jar
3134
* @run main/othervm -javaagent:InheritAgent0011.jar DummyMain
3235
*/
3336

3437
import java.lang.instrument.*;
3538

36-
class InheritAgent0011 extends InheritAgent0011Super {
39+
public class InheritAgent0011 extends InheritAgent0011Super {
3740

3841
//
3942
// This agent has a single argument premain() method which
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,34 +21,32 @@
2121
* questions.
2222
*/
2323

24-
/**
24+
/*
2525
* @test
26-
* @bug 6289149
27-
* @summary test config (0,1,0,0): inherited 1-arg in agent class
28-
* @author Daniel D. Daugherty, Sun Microsystems
29-
*
30-
* @run shell ../MakeJAR3.sh InheritAgent0100
31-
* @run main/othervm -javaagent:InheritAgent0100.jar DummyMain
26+
* @bug 6289149 8165276
27+
* @summary test config (0,1,0,0): 1-arg premain method in superclass of agent class must be rejected
28+
* @library /test/lib
29+
* @library /test
30+
* @modules java.instrument
31+
* @build jdk.java.lang.instrument.PremainClass.InheritAgent0100
32+
* @run driver jdk.test.lib.util.JavaAgentBuilder
33+
* InheritAgent0100 InheritAgent0100.jar
34+
* @run main/othervm jdk.java.lang.instrument.NegativeAgentRunner InheritAgent0100 NoSuchMethodException
3235
*/
3336

34-
import java.lang.instrument.*;
37+
public class InheritAgent0100 extends InheritAgent0100Super {
3538

36-
class InheritAgent0100 extends InheritAgent0100Super {
39+
// This agent does NOT have a single argument premain() method.
40+
// This agent does NOT have a double argument premain() method.
3741

38-
// This agent does NOT have a single argument premain() method.
39-
40-
// This agent does NOT have a double argument premain() method.
4142
}
4243

4344
class InheritAgent0100Super {
44-
45-
//
46-
// This agent has a single argument premain() method which
47-
// is the one that should be called.
48-
//
45+
// This agent class has a single argument premain() method which should NOT be called.
4946
public static void premain (String agentArgs) {
5047
System.out.println("Hello from Single-Arg InheritAgent0100Super!");
48+
throw new Error("ERROR: THIS AGENT SHOULD NOT HAVE BEEN CALLED.");
5149
}
5250

53-
// This agent does NOT have a double argument premain() method.
51+
// This agent class does NOT have a double argument premain() method.
5452
}

test/jdk/java/lang/instrument/PremainClass/InheritAgent0101.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,18 +23,21 @@
2323

2424
/**
2525
* @test
26-
* @bug 6289149
27-
* @summary test config (0,1,0,1): inherited 1-arg and declared 1-arg in agent class
26+
* @bug 6289149 8165276
27+
* @summary test config (0,1,0,1): 1-arg in superclass and declared 1-arg in agent class
2828
* @author Daniel D. Daugherty, Sun Microsystems
2929
*
3030
* @key intermittent
31-
* @run shell ../MakeJAR3.sh InheritAgent0101
31+
* @library /test/lib
32+
* @build jdk.java.lang.instrument.PremainClass.InheritAgent0101
33+
* @run driver jdk.test.lib.util.JavaAgentBuilder
34+
* InheritAgent0101 InheritAgent0101.jar
3235
* @run main/othervm -javaagent:InheritAgent0101.jar DummyMain
3336
*/
3437

3538
import java.lang.instrument.*;
3639

37-
class InheritAgent0101 extends InheritAgent0101Super {
40+
public class InheritAgent0101 extends InheritAgent0101Super {
3841

3942
//
4043
// This agent has a single argument premain() method which

test/jdk/java/lang/instrument/PremainClass/InheritAgent0110.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,17 +23,20 @@
2323

2424
/**
2525
* @test
26-
* @bug 6289149
27-
* @summary test config (0,1,1,0): inherited 1-arg and declared 2-arg in agent class
26+
* @bug 6289149 8165276
27+
* @summary test config (0,1,1,0): 1-arg in superclass and declared 2-arg in agent class
2828
* @author Daniel D. Daugherty, Sun Microsystems
2929
*
30-
* @run shell ../MakeJAR3.sh InheritAgent0110
30+
* @library /test/lib
31+
* @build jdk.java.lang.instrument.PremainClass.InheritAgent0110
32+
* @run driver jdk.test.lib.util.JavaAgentBuilder
33+
* InheritAgent0110 InheritAgent0110.jar
3134
* @run main/othervm -javaagent:InheritAgent0110.jar DummyMain
3235
*/
3336

3437
import java.lang.instrument.*;
3538

36-
class InheritAgent0110 extends InheritAgent0110Super {
39+
public class InheritAgent0110 extends InheritAgent0110Super {
3740

3841
// This agent does NOT have a one argument premain() method.
3942

test/jdk/java/lang/instrument/PremainClass/InheritAgent0111.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,17 +23,20 @@
2323

2424
/**
2525
* @test
26-
* @bug 6289149
27-
* @summary test config (0,1,1,1): inherited 1-arg, declared 2-arg and declared 1-arg in agent class
26+
* @bug 6289149 8165276
27+
* @summary test config (0,1,1,1): 1-arg in superclass, declared 2-arg and 1-arg in agent class
2828
* @author Daniel D. Daugherty, Sun Microsystems
2929
*
30-
* @run shell ../MakeJAR3.sh InheritAgent0111
30+
* @library /test/lib
31+
* @build jdk.java.lang.instrument.PremainClass.InheritAgent0111
32+
* @run driver jdk.test.lib.util.JavaAgentBuilder
33+
* InheritAgent0111 InheritAgent0111.jar
3134
* @run main/othervm -javaagent:InheritAgent0111.jar DummyMain
3235
*/
3336

3437
import java.lang.instrument.*;
3538

36-
class InheritAgent0111 extends InheritAgent0111Super {
39+
public class InheritAgent0111 extends InheritAgent0111Super {
3740

3841
//
3942
// This agent has a single argument premain() method which

0 commit comments

Comments
 (0)