Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ziyilin
Copy link
Contributor

@ziyilin ziyilin commented May 29, 2024

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

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label May 29, 2024

private static final String PREMAIN_OPTION_PREFIX = "-XX-premain:";

class PremainMethod {
Copy link
Member

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?

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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);
Copy link
Member

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?

Copy link
Contributor Author

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 {
Copy link
Member

@vjovanov vjovanov Jun 12, 2024

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.

Copy link
Contributor Author

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);
Copy link
Member

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.

Copy link
Contributor Author

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");
Copy link
Member

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.

Copy link
Contributor Author

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]
Copy link
Member

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.

Copy link
Contributor Author

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);
Copy link
Member

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();
Copy link

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?

Copy link
Contributor Author

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.

@ziyilin ziyilin force-pushed the staticInstrument-premain branch 2 times, most recently from 8e2bd2f to c9d5652 Compare June 14, 2024 09:39
}
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());
Copy link
Member

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.

@vjovanov
Copy link
Member

vjovanov commented Jun 17, 2024

To keep this feature working as expected we need to add a few tests into: com.oracle.svm.test

The tests can be executed from similarly to the tests for class initialization.

@ziyilin
Copy link
Contributor Author

ziyilin commented Jun 19, 2024

To keep this feature working as expected we need to add a few tests into: com.oracle.svm.test

The tests can be executed from similarly to the tests for class initialization.

I have added a test task agenttest. It now only checks the premain method, doesn't do any class transformation yet.

@ziyilin ziyilin force-pushed the staticInstrument-premain branch 3 times, most recently from 510b031 to bc9e8ad Compare June 26, 2024 08:22
@@ -1717,6 1717,66 @@ def cinterfacetutorial(args):
native_image_context_run(_cinterfacetutorial, args)


@mx.command(suite.name, 'agenttest', 'Runs the ')
Copy link
Member

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'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@vjovanov
Copy link
Member

We need to add the CHANGELOG.md entry that claims that this is an experimental feature for now.

}
}
}
System.setProperty("instrument.enable", "true");
Copy link
Member

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.

Copy link
Contributor Author

@ziyilin ziyilin Jul 23, 2024

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.

Copy link
Member

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.

@ziyilin
Copy link
Contributor Author

ziyilin commented Jul 23, 2024

We need to add the CHANGELOG.md entry that claims that this is an experimental feature for now.

Not sure which version and the internal code, I added the entry at the top of the CHANGELOG.md.

@ziyilin ziyilin force-pushed the staticInstrument-premain branch 2 times, most recently from f69fbd2 to b05de40 Compare July 23, 2024 07:10

@Override
public Class<?>[] getAllLoadedClasses() {
return new Class<?>[0];
Copy link
Member

@vjovanov vjovanov Jul 23, 2024

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.

Copy link
Contributor Author

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.

Copy link

@trask trask Jul 25, 2024

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;
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link

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

Copy link
Contributor Author

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,
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link

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
Copy link
Member

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()) {
Copy link
Member

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.

Copy link
Contributor Author

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.
Copy link
Member

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 {
Copy link

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

@ziyilin ziyilin force-pushed the staticInstrument-premain branch 3 times, most recently from 246d942 to 1553530 Compare July 26, 2024 09:57
@Override
public Class<?>[] getAllLoadedClasses() {
synchronized (this) {
if (allLoadedClasses == null) {
Copy link
Member

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.

Copy link
Contributor Author

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() {
Copy link
Member

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.

CC @christianwimmer

Copy link
Contributor Author

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) {
Copy link
Member

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?

Copy link
Contributor

@ivan-ristovic ivan-ristovic Jul 30, 2024

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) {
Copy link
Member

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.

Copy link
Contributor Author

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());
Copy link
Member

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:";
Copy link
Member

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<>(
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove all todos.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@ziyilin ziyilin force-pushed the staticInstrument-premain branch 2 times, most recently from 78461d0 to afca03a Compare July 31, 2024 08:24

@Override
public void appendToBootstrapClassLoaderSearch(JarFile jarfile) {
throw new UnsupportedOperationException("Native image doesn't support modify classloader search path at runtime.");
Copy link
Member

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"))

Copy link
Contributor Author

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.");
Copy link
Member

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 UnsupportedOperationExceptions

}
imageCodeInfo = CodeInfoAccess.getNextImageCodeInfo(imageCodeInfo);
}
userClasses.trimToSize();
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

@vjovanov
Copy link
Member

vjovanov commented Aug 2, 2024

This PR makes a good foundation for the agent support. After the final changes I will run all the tests, polish the CHANGELOG.md entry, and start with merging.

vjovanov
vjovanov previously approved these changes Aug 2, 2024
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]
Copy link
Member

@vjovanov vjovanov left a 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OCA Verified All contributors have signed the Oracle Contributor Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants