Search code examples
javajvminstrumentationjvmti

Is there a way to enable "native method prefix" capabilities in a java language (non-native) JVM agent?


background: java has a mechanism for instrumenting native methods by basically allowing you to rename the native method with a set prefix and then create a method that delegates to it, in bytecode.

It appears by default this is disallowed. Calling Instrumentation.setNativePrefix() performs a check on InstrumentationImpl.mEnvironmentSupportsNativeMethodPrefix, which I see in the jvm C code defaults to "false".

I see there is possibly a way to enable it with a native agent via JVMTI, but I can't find anything about enabling it with a java-language agent.


Solution

  • The required capabilities for a Java Agent are determined by the Manifest Attributes of its jar file, as specified at the bottom of the package documentation:

    Manifest Attributes

    The following manifest attributes are defined for an agent JAR file:

    • Can-Set-Native-Method-Prefix
      Boolean (true or false, case irrelevant). Is the ability to set native method prefix needed by this agent. Values other than true are considered false. This attribute is optional, the default is false.

    I didn’t try it specifically with the native method prefix, but I know from the other capabilities (redefinition and retransformation) that they are only reported as supported by the Instrumentation interface when previously requested via the manifest attributes. The absence of any startup-time agent requesting the feature may even make it impossible to request the feature later-on by an agent loaded via the Attach API.