Skip to content

[GR-74965] Respect class loader resource lookup delegation.#13475

Open
graalvmbot wants to merge 19 commits intomasterfrom
pwoegere/GR-74965-resource-lookup
Open

[GR-74965] Respect class loader resource lookup delegation.#13475
graalvmbot wants to merge 19 commits intomasterfrom
pwoegere/GR-74965-resource-lookup

Conversation

@graalvmbot
Copy link
Copy Markdown
Collaborator

This PR extends resource lookup for Native Image when -H:+ClassForNameRespectsClassLoader is enabled, especially for module/class-loader-sensitive runtime access.

Summary:

  • Preserves module-qualified resource metadata so embedded resources can be resolved through the class loader and module reader paths after module locations are redacted.
  • Adds module-reader substitutions that keep embedded resource lookup first, then rehydrate baked-in JarModuleReader state from a runtime-provided module path when needed.
  • Initializes jdk.internal.loader.ClassLoaders at run time for -H:+ClassForNameRespectsClassLoader, so runtime java.class.path values can be reflected in class-loader resource lookup.
  • Extends hellomodule coverage to define a runtime module layer with ModuleLayer#defineModulesWithOneLoader(...) under -H:+RuntimeClassLoading.
  • Stretches that runtime module test so runtime-loaded code calls into a package from moduletests.hello.lib that static analysis does not otherwise reach, and that code uses javax.xml.namespace.QName from java.xml loaded through the runtime JRT filesystem.

Follow-up:

  • GR-75374 tracks the temporary explicit --add-modules=java.xml test build argument. The requires java.xml in substratevm/src/native-image-module-tests/hello.lib/src/main/java/module-info.java should eventually be sufficient.

Verification:

  • mx hellomodule -H:+ClassForNameRespectsClassLoader -H:+RuntimeClassLoading
  • mx checkstyle
  • mx eclipseformat

olpaw added 13 commits May 4, 2026 09:42
For images built with classloader-respecting resource lookup, system-module resources that were baked into the native image must be served through the JDK module-reader path instead of falling through to the JRT-backed lookup machinery. This adds a RespectsClassLoader-only substitution for SystemModuleFinders.SystemModuleReader.read that first checks the embedded resource store, then executes a local copy of the original JDK fallback logic so misses still preserve normal JRT behavior and closed-reader semantics. The hellomodule test is extended to exercise resource access through both platform- and boot-loaded modules, and its resource configuration is updated to the modern module-aware glob form so java.base and jdk.dynalink module-info.class are embedded and the built-in resource path is covered explicitly.
The hello module test moved resource registration to the glob-based configuration, but we only registered resource-file.txt without module information. That works for unnamed resources, but it does not match the named test modules, so the file was never embedded into the image and the module resource lookup failed at runtime. This change registers resource-file.txt for moduletests.hello.app and moduletests.hello.lib explicitly, which restores the intended module resource checks.
File-backed module references are redacted during image building so that host build paths do not leak into the image heap. After that redaction, module readers for jar and exploded modules can no longer rely on the original build-time location when resolving resources at image run time. Preserve the module name in redacted file URIs for application modules and use that name to recover the runtime Module object when a substituted module reader needs to look up resources embedded in the image.

This extends the existing system-module resource fallback to the jar and exploded module-reader paths used when class-loader-sensitive lookup is enabled. JarModuleReader now tolerates redacted jar locations, preserves normal jar behavior when a real JarFile is available, and falls back to embedded resource data or resource: URIs for open/read/find. ExplodedModuleReader similarly tries the filesystem path first and falls back to embedded resources when the path is unavailable. The substitutions keep the original closed-state and normal-reader behavior where it matters while allowing built-in resources to remain visible after module locations have been sanitized.
SystemModuleReader.open and read could already resolve embedded resources because read checked the image resource store before falling back to the JRT image. The URL-based path was still incomplete: Class.getResource on a class from a system module asks the module reader to find a URI, and SystemModuleReader.find went directly to the JRT image. In images without JRT file system support this caused URL lookup for an otherwise embedded system-module resource to fail with the disabled-JRT error.

Teach SystemModuleReader.find to use the same embedded resource lookup before consulting the JRT image, while preserving the JDK closed-reader check and normal jrt: URI behavior when no embedded resource is present. Extend the hello module test to cover Class.getResource(...).openStream() for java.base, jdk.dynalink, and the application module-path jars, so both system-module and JarModuleReader URL lookup paths are exercised under -H:+ClassForNameRespectsClassLoader.
When ClassForNameRespectsClassLoader is enabled, class resource lookups can first probe system-module readers before falling back to classpath resources. If the image was built without JRT filesystem support, those system-module probes must still be able to miss cleanly after checking embedded resources instead of reaching deleted jimage state or throwing the disabled-JRT error. This change guards the JRT-backed SystemModuleReader find/read paths behind AllowJRTFileSystem while preserving embedded resource lookup, and adds a narrow BuiltinClassLoader classpath-resource fallback so normal classpath lookup is tried first and embedded resources are used only when the runtime classpath has no match.
Extend the hellomodule gate task so CI exercises the module smoke test both in the default configuration and with ClassForNameRespectsClassLoader enabled. The class-loader-sensitive mode uses different resource lookup paths, so running this variant in the gate keeps the embedded module resource fallback covered by the standard hellomodule CI job.
@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label May 5, 2026
@graalvmbot graalvmbot force-pushed the pwoegere/GR-74965-resource-lookup branch from 7036cd5 to cc3be28 Compare May 6, 2026 08:08
@graalvmbot graalvmbot force-pushed the pwoegere/GR-74965-resource-lookup branch from d64915d to 47f9ba8 Compare May 6, 2026 09:28
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.

2 participants