-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add premain for static agent support #8988
base: master
Are you sure you want to change the base?
Conversation
|
||
private static final String PREMAIN_OPTION_PREFIX = "-XX-premain:"; | ||
|
||
class PremainMethod { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be a record, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be static
if it is a class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be a record, right?
Yes, in my current implementation it's recorded by native-image-agent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be a record, right?
Yes, in my current implementation it's recorded by native-image-agent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I say a record
I mean a java record.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Yes, this is a java record. I have fixed it.
// premain method must be static | ||
premainMethod.method.invoke(null, args); | ||
} catch (Throwable t) { | ||
VMError.shouldNotReachHere("Fail to execute " premainMethod.className ".premain", t); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a user-facing error, right? User code can throw, so I don't see this as something that is a VMError
. What does the JVM do in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In JVM, if the premain
method fails, the JVM starting process shall be terminated. See https://github.com/openjdk/jdk21u/blob/2971cb5769121b47ac8c8db1078d67680a19341f/src/java.instrument/share/native/libinstrument/InvocationAdapter.c#L623-L629
I use VMError
because it's a fatal error during VM starts up. But the cause is indeed an user error.
* registered premain method's second parameter. At native image runtime, no actual | ||
* instrumentation work can do. So all the methods here are empty. | ||
*/ | ||
public static class SVMRuntimeInstrumentImpl implements Instrumentation { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe NativeImageNoOpRuntimeInstrumentation
? SVM is an internal name which we should not use. I also feel we should mention it is a noop and that it happens at runtime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
} | ||
preMainSupport.registerPremainMethod(premainClass, premain, args.toArray(new Object[0])); | ||
} catch (ClassNotFoundException e) { | ||
VMError.shouldNotReachHere("Can't register agent premain method, because the declaring class " premainClass " is not found", e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a UserError
as the user can make a mistake. We should also add a sentence on how to recover from that mistake to the error message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
* <ul> | ||
* <li>Isolate code by checking current runtime. For example: <code> | ||
* <pre> | ||
* String vm = System.getProperty("java.vm.name"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should guide the users to use the properties from ImageInfo. Then they can see if the agent is applied at build time or at runtime. We also need to make sure that this property is set before premain is called for the Native Image builder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. This property can be obtained in premain
.
* Keep premain options and return the rest args as main args. | ||
* The premain options format: | ||
* <br> | ||
* -XX-premain:[full.qualified.premain.class]:[premain options] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if this should be our final format for the flag. I think we can use it until we complete the feature and decide what is the user interface in the end.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. I added a //todo
for future changing.
|
||
public static class Options { | ||
@Option(help = "Specify premain-class list. Multiple classes are separated by comma, and order matters.")// | ||
public static final HostedOptionKey<LocatableMultiOptionValue.Strings> PremainClasses = new HostedOptionKey<>(null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is also an internal option. In the end we should probably pick the premain
functions from the agent JARs.
@Override | ||
public void afterRegistration(AfterRegistrationAccess access) { | ||
FeatureImpl.AfterRegistrationAccessImpl a = (FeatureImpl.AfterRegistrationAccessImpl) access; | ||
cl = a.getImageClassLoader().getClassLoader(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this is still required, but I had to provide an agent JAR twice as -J-javaagent
and -cp
for NI build configurations in agent JARs to be picked up by the NI builder. So just checking, users can provide an agent JAR only once as -J-javaagent
for their premain classes to be registered?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this PR, we are not automatically picking agent from -cp
or -J-javaagent
. It will be handled in the following PRs. But I'm sure user doesn't need to provide agent jar multiple times.
8e2bd2f
to
c9d5652
Compare
} | ||
preMainSupport.registerPremainMethod(premainClass, premain, args.toArray(new Object[0])); | ||
} catch (ClassNotFoundException e) { | ||
UserError.abort(e,"Can't register agent premain method, because the given class %s is not found. Please check your -H:%s setting.", premainClass, Options.PremainClasses.getName()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is better to use SubstrateOptionsParser.commandArgument
to print the option name. This keeps the correct behavior when options become public or change the type.
To keep this feature working as expected we need to add a few tests into: The tests can be executed from similarly to the tests for class initialization. |
c9d5652
to
538ad3f
Compare
I have added a test task agenttest. It now only checks the |
510b031
to
bc9e8ad
Compare
@@ -1717,6 1717,66 @@ def cinterfacetutorial(args): | |||
native_image_context_run(_cinterfacetutorial, args) | |||
|
|||
|
|||
@mx.command(suite.name, 'agenttest', 'Runs the ') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel it would be more accurate to call it javaagenttest
.
Also, we should complete the usage message here. Something like:
usage_msg='Runs tests for java agents with native image'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
We need to add the |
} | ||
} | ||
} | ||
System.setProperty("instrument.enable", "true"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add a set of assertions that checks that the values for Instrumentation
are correct at image run time. This way we assure nobody changes those values and surprises the users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check of Instrumentation
class is put in the com.oracle.svm.test.agent.Agent
.
I removed the Instrumentation
parameter from Agent2 so it can test the case of premain
without Instrumention
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would still add a check for all the relevant methods that can be invoked on the Instrumentation
good examples are: Instrumentation#isModifiableClass
, Instrumentation#isRetransformClassesSupported
, Instrumentation#isModifiableModule
, Instrumentation#getAllLoadedClasses
, etc.
Not sure which version and the internal code, I added the entry at the top of the CHANGELOG.md. |
f69fbd2
to
b05de40
Compare
|
||
@Override | ||
public Class<?>[] getAllLoadedClasses() { | ||
return new Class<?>[0]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should discuss this. How is this method typically used?
Should we maybe return all the classes that were reached? I think that we need an explanation for the value we return.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In observability scenario, user can call this method to collect all jars loaded by the application.
I think return the reached classes in native image here is reasonable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we maybe return all the classes that were reached?
this sounds good if it's possible (and if there's a desire to go beyond no-op implementation)
|
||
@Override | ||
public long getObjectSize(Object objectToSize) { | ||
return 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should either return the actual value by looking into Native Image internals, or something completely unusable such as Long.MIN_VALUE
. Is this information used to transform classes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is rarely used.
It is not used by class transformation.
I've searched the source of opentelemetery java instrumentation and didn't find any usage as well.
In OpenJDK, it returns oop->size() * wordSize
. What is the similar function in substratevm? We can do the similar calculation so it will be perfectly compatible with JVM. But considering the rarely usage, Long.MIN_VALUE
may be also acceptable for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest 0
in case anyone is summing over these values, otherwise I think they'll get negative overflow(?) by using MIN_VALUE
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I set it to -1 as it's both invalid and won't get overflowed easily.
} | ||
|
||
@Override | ||
public void redefineModule(Module module, Set<Module> extraReads, Map<String, Set<Module>> extraExports, Map<String, Set<Module>> extraOpens, Set<Class<?>> extraUses, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel this should be a hard failure that will explain to the users what they should do (preferable).
Now, the question is how many agents will call this even when transformation is not allowed. I suspect not many.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is called to declare extra exports/opens/uses/reads packages from module.
It is not rarely used. The agent may need extra access to certain modules.
We can call APIs from ModuleNative to make checks only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest throw new UnsupportedOperationException()
based on the javadoc for the method https://docs.oracle.com/javase/8/docs/api/java/lang/instrument/Instrumentation.html#redefineClasses-java.lang.instrument.ClassDefinition...-
UnsupportedOperationException - if the current configuration of the JVM does not allow redefinition (isRedefineClassesSupported() is false) or the redefinition attempted to make unsupported changes
} | ||
|
||
/** | ||
* This class is a dummy implementation of {@link Instrumentation} interface. It serves as the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to mention in the release notes that this is experimental and that the behavior of some methods might change. We should test only the methods for which we are completely certain they will stay the same.
* <ul> | ||
* <li>Isolate code by checking current runtime. For example: <code> | ||
* <pre> | ||
* if (ImageInfo.inImageRuntimeCode()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to use the system property directly here, or at least mention it. This will not require extra dependencies to use this feature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
@@ -3,6 3,7 @@ | |||
This changelog summarizes major changes to GraalVM Native Image. | |||
|
|||
## GraalVM for JDK 24 (Internal Version 24.2.0) | |||
* Together with Alibaba, we added java agent support for native image as experimental feature. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will polish this line when the PR is done.
* registered premain method's second parameter. At native image runtime, no actual | ||
* instrumentation work can do. So all the methods here are empty. | ||
*/ | ||
public static class NativeImageNoOpRuntimeInstrumentation implements Instrumentation { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think no-op is ok for everything, under the assumption that agents can't do "runtime instrumentation" things in native images
246d942
to
1553530
Compare
@Override | ||
public Class<?>[] getAllLoadedClasses() { | ||
synchronized (this) { | ||
if (allLoadedClasses == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not cache here as it takes up RSS.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
@@ -213,4 215,15 @@ public RuntimeConditionSet getConditionFor(Class<?> jClass) { | |||
return conditionalClass.getConditions(); | |||
} | |||
} | |||
|
|||
public Class<?>[] getAllLoadedClasses() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would implement this as follows:
ArrayList<Class<?>> userClasses = new ArrayList<>();
CodeInfo imageCodeInfo = CodeInfoTable.getFirstImageCodeInfo();
while (imageCodeInfo.isNonNull()) {
Class<?>[] classes = NonmovableArrays.heapCopyOfObjectArray(CodeInfoAccess.getClasses(imageCodeInfo));
if (classes != null) {
for (Class<?> clazz : classes) {
if (clazz != null) {
Module module = clazz.getModule();
if (module == null ||
module.getName() == null ||
!isSystemClass(module)) {
userClasses.add(clazz);
}
}
}
}
imageCodeInfo = CodeInfoAccess.getNextImageCodeInfo(imageCodeInfo);
}
userClasses.trimToSize();
return userClasses.toArray(new Class[0]);
where we have
private static final Set<String> systemModules = Set.of("org.graalvm.nativeimage.builder", "org.graalvm.nativeimage", "org.graalvm.nativeimage.base", "com.oracle.svm.svm_enterprise",
"org.graalvm.word", "jdk.internal.vm.ci", "jdk.graal.compiler", "com.oracle.graal.graal_enterprise");
private static boolean isSystemClass(Module module) {
return systemModules.contains(module.getName());
}
Please re-use systemModules
in com.oracle.svm.hosted.classinitialization.ClassInitializationSupport#isAlwaysReached
. @loicottet we can use that list later for the list of types where reflection is prohibited.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. I put the getAllLoadedClasses
implementation to PremainSupport
, because it doesn't need anything from ClassForNameSupport
any more.
} | ||
|
||
@Override | ||
public boolean isModifiableModule(Module module) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ivan-ristovic can you please look at this implementation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. This is the same implementation as in java.instrument
API, and our run-time module system has no specific quirks that require divergence from the JDK for this method.
} | ||
|
||
@Override | ||
public void appendToBootstrapClassLoaderSearch(JarFile jarfile) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would throw here as it is not really supported as well as to appendToSystemClassLoaderSearch
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
* defined. | ||
*/ | ||
Assert.assertNotNull(inst); | ||
Assert.assertEquals("com.oracle.svm.core.PreMainSupport$NativeImageNoOpRuntimeInstrumentation", inst.getClass().getName()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementation details, I would avoid this.
public class PreMainSupport { | ||
|
||
// todo: Not finally decided, could be changed in the future | ||
private static final String PREMAIN_OPTION_PREFIX = "-XX-premain:"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would go with -XXpremain:
to be consistent with other arguments.
Once we make a flag for build-time transformation this should be enabled automatically for enabled agents. Option will be there only for passing the arguments.
public static class Options { | ||
// todo: Not finally decided, could be changed in the future | ||
@Option(help = "Specify premain-class list. Multiple classes are separated by comma, and order matters.")// | ||
public static final HostedOptionKey<AccumulatingLocatableMultiOptionValue.Strings> PremainClasses = new HostedOptionKey<>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be an experimental option until we add an API option for this.
private PreMainSupport preMainSupport; | ||
|
||
public static class Options { | ||
// todo: Not finally decided, could be changed in the future |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove all todos.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
78461d0
to
afca03a
Compare
|
||
@Override | ||
public void appendToBootstrapClassLoaderSearch(JarFile jarfile) { | ||
throw new UnsupportedOperationException("Native image doesn't support modify classloader search path at runtime."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe Native image doesn't support modification of the classloader search path at run time. Please avoid calling this method in Native Image by checking "runtime".equals(System.getProperty("org.graalvm.nativeimage.imagecode"))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
|
||
@Override | ||
public void appendToSystemClassLoaderSearch(JarFile jarfile) { | ||
throw new UnsupportedOperationException("Native image doesn't support modify classloader search path at runtime."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar here, as a matter of fact I would add the action sentence to all UnsupportedOperationException
s
} | ||
imageCodeInfo = CodeInfoAccess.getNextImageCodeInfo(imageCodeInfo); | ||
} | ||
userClasses.trimToSize(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I see we don't need the trimToSize
as the list is thrown away.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
This PR makes a good foundation for the agent support. After the final changes I will run all the tests, polish the |
afca03a
to
e3dc8e7
Compare
Java agent always has a premain method to initialize the agent. SVM needs the premain as well to support agent in native image. At compile time, -H:PremainClasses= option is used to set the premain classes. At runtime, premain runtime options are set along with main class' arguments in the format of -XXpremain:[class]:[options]
e3dc8e7
to
cd5430e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Any last comments from other reviewers?
Java agent always has a premain method to initialize the agent. SVM needs the premain as well to support agent in native image.
At compile time, -H:PremainClasses= option is used to set the premain classes.
At runtime, premain runtime options are set along with main class' arguments in the format of -XX-premain:[class]:[options]
This PR is part of #8077