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

[GR-57253] Add dynamic call usage detection #10176

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

graalvmbot
Copy link
Collaborator

@graalvmbot graalvmbot commented Nov 28, 2024

In this PR we add a new phase in analysis that detects calls that might require metadata and serializes them and their source location. The phase is enabled by adding the -H:TrackMethodsRequiringMetadata=<jar-path> option when running native-image. The phase detects calls only in the base project with the provided source jar path and not in its dependencies. Note that you can track metadata requiring call usage in multiple jars by providing multiple paths separated by commas (-H:TrackMethodsRequiringMetadata=<jar-path1,jar-path2,...>). If any calls are detected, their source locations are serialized in <jar-name>_method_usage.json.
When building a native-image layer of the library artifact org.slf4j:slf4j-api:2.0.16, the native-image output includes:

[2/8] Performing analysis...  [*************]                                                           (12.0s @ 0.66GB)
    5,925 reachable types   (60.7% of    9,760 total)
   17,569 reachable fields  (95.7% of   18,367 total)
   29,971 reachable methods (35.6% of   84,249 total)
    2,029 types,    17 fields, and   511 methods registered for reflection
       61 types,    61 fields, and    55 methods registered for JNI access
        4 native libraries: dl, pthread, rt, z
[3/8] Building universe...                                                                               (2.7s @ 1.13GB)
Dynamic method usage detected in /home/jovan/.m2/repository/org/slf4j/slf4j-api/2.0.16/slf4j-api-2.0.16.jar:
    Reflection calls detected:
        java.lang.Class#getConstructor:
            at org.slf4j.LoggerFactory.loadExplicitlySpecified(LoggerFactory.java:227)
        java.lang.Class#getMethod:
            at org.slf4j.helpers.SubstituteLogger.isDelegateEventAware(SubstituteLogger.java:467)
        java.lang.ClassLoader#loadClass:
            at org.slf4j.LoggerFactory.loadExplicitlySpecified(LoggerFactory.java:226)
        java.lang.reflect.Constructor#newInstance:
            at org.slf4j.LoggerFactory.loadExplicitlySpecified(LoggerFactory.java:228)
        java.lang.reflect.Method#invoke:
            at org.slf4j.helpers.SubstituteLogger.log(SubstituteLogger.java:478)
    Resource calls detected:
        java.lang.ClassLoader#getResources:
            at org.slf4j.LoggerFactory.findPossibleStaticLoggerBinderPathSet(LoggerFactory.java:269)
[4/8] Parsing methods...      [**]                                                                       (3.5s @ 1.45GB)

while the generated slf4j-api-2.0.16_method_usage.json looks like this:

{
  "reflection": {
    "java.lang.Class#getConstructor": [
      "org.slf4j.LoggerFactory.loadExplicitlySpecified(LoggerFactory.java:227)"
    ],
    "java.lang.Class#getMethod": [
      "org.slf4j.helpers.SubstituteLogger.isDelegateEventAware(SubstituteLogger.java:467)"
    ],
    "java.lang.ClassLoader#loadClass": [
      "org.slf4j.LoggerFactory.loadExplicitlySpecified(LoggerFactory.java:226)"
    ],
    "java.lang.reflect.Constructor#newInstance": [
      "org.slf4j.LoggerFactory.loadExplicitlySpecified(LoggerFactory.java:228)"
    ],
    "java.lang.reflect.Method#invoke": [
      "org.slf4j.helpers.SubstituteLogger.log(SubstituteLogger.java:478)"
    ]
  },
  "resource": {
    "java.lang.ClassLoader#getResources": [
      "org.slf4j.LoggerFactory.findPossibleStaticLoggerBinderPathSet(LoggerFactory.java:269)"
    ]
  }
}

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Nov 28, 2024
@zakkak
Copy link
Collaborator

zakkak commented Dec 9, 2024

Hello, this is an interesting addition. Could you please mirror the internal description as well? Thank you.

@graalvmbot graalvmbot force-pushed the jvukicev/GR-57253 branch 2 times, most recently from 5453059 to 3eaf0db Compare December 17, 2024 08:45
@graalvmbot graalvmbot changed the title [GR-57253] Add reflection call detection [GR-57253] Add dynamic call usage detection Dec 23, 2024
@zakkak
Copy link
Collaborator

zakkak commented Jan 7, 2025

Hello, this is an interesting addition. Could you please mirror the internal description as well? Thank you.

@jormundur00 ^^

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.

3 participants