Skip to content

Conversation

graalvmbot
Copy link
Collaborator

@graalvmbot graalvmbot commented Nov 18, 2024

Fixes #10080.

The underlying issue is that we miss registering a field's declared type as reachable during reflection metadata processing.

First, the com.oracle.svm.hosted.reflect.ReflectionDataBuilder detects a field as being a hiding field, i.e., a field that shadows a superclass element registered for reflection, and it registers it to be excluded from reflection queries. This field, including its declared type, is then included in the image code info by com.oracle.svm.hosted.image.NativeImageCodeCache#buildRuntimeMetadata, however its type was never properly registered as reachable.

Then, during shadow heap verification when we scan the com.oracle.svm.core.code.ImageCodeInfo#classes we detect the corresponding class constant and we wrongly try to register the corresponding AnalysisType as reachable, but at this stage it is too late, as the analysis has finished and no new types can be registered. This leads to the NPE. Catching the late registration attempt and reporting leads to an improved error message:

com.oracle.graal.pointsto.constraints.UnsupportedFeatureException: Unsupported features in 5 methods
Detailed message:
Error: Type not found during analysis: AnalysisType<PsiDeconstructionPattern[] -> HotSpotType<[Lcom/intellij/psi/PsiDeconstructionPattern;, resolved>, instantiated: false, reachable: false>
Trace: Object was reached by
  indexing into array java.lang.Class[]@782d3f7c: [Ljava.lang.Class;@782d3f7c
  reading field com.oracle.svm.core.code.ImageCodeInfo.classes of constant 
    com.oracle.svm.core.code.ImageCodeInfo@40c10399: com.oracle.svm.core.code.ImageCodeInfo@40c10399
  indexing into array com.oracle.svm.core.code.ImageCodeInfo[]@5a2ec7c6: [Lcom.oracle.svm.core.code.ImageCodeInfo;@5a2ec7c6
  scanning root com.oracle.svm.core.code.ImageCodeInfo[]@5a2ec7c6: [Lcom.oracle.svm.core.code.ImageCodeInfo;@5a2ec7c6 embedded in
    com.oracle.svm.core.layeredimagesingleton.MultiLayeredImageSingleton.getAllLayers(MultiLayeredImageSingleton.java)
  parsing method com.oracle.svm.core.layeredimagesingleton.MultiLayeredImageSingleton.getAllLayers(MultiLayeredImageSingleton.java:40) reachable via the parsing context <no parsing context available> 

But the original issue was trying to include an unreachable type in the native image code cache. Fixing the detection condition there leads to an error message that takes us closer to the actual problem:

com.oracle.svm.core.util.VMError$HostedError: Type added to the runtime metadata was seen by the analysis, but not marked as reachable: class [Lcom.intellij.psi.PsiIdentifier;
	at org.graalvm.nativeimage.builder/com.oracle.svm.core.util.VMError.shouldNotReachHere(VMError.java:78)
	at org.graalvm.nativeimage.builder/com.oracle.svm.core.util.VMError.shouldNotReachHere(VMError.java:137)
	at org.graalvm.nativeimage.builder/com.oracle.svm.core.util.VMError.guarantee(VMError.java:146)
	at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.image.NativeImageCodeCache.lambda$buildRuntimeMetadata$0(NativeImageCodeCache.java:329)
	at jdk.graal.compiler/jdk.graal.compiler.core.common.util.FrequencyEncoder$VerifyingFrequencyEncoder.addObject(FrequencyEncoder.java:182)
	at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.code.RuntimeMetadataEncoderImpl.addHidingFieldMetadata(RuntimeMetadataEncoderImpl.java:584)
	at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.image.NativeImageCodeCache.buildRuntimeMetadata(NativeImageCodeCache.java:420)
	at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.image.NativeImageCodeCache.buildRuntimeMetadata(NativeImageCodeCache.java:293)
	at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:675)
	at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.run(NativeImageGenerator.java:533)
	at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.buildImage(NativeImageGeneratorRunner.java:545)
	at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.build(NativeImageGeneratorRunner.java:732)
	at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.start(NativeImageGeneratorRunner.java:151)
	at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.main(NativeImageGeneratorRunner.java:99)

Finally, the actual fix is to automatically mark the hiding field's declared type as reachable as soon as the field is discovered.

Improve error reporting when registering unreachable types.
@nreid260
Copy link

This aligns with issues we were seeing with Graal 22 as well. We had to manually register [Lcom.intellij.psi.PsiIdentifier; for reflection to get kotlinc working.

@graalvmbot graalvmbot closed this Nov 19, 2024
@graalvmbot graalvmbot merged commit d6da064 into master Nov 19, 2024
13 checks passed
@graalvmbot graalvmbot deleted the cs/GR-59858-analysis-npe branch November 19, 2024 00:40
@nreid260
Copy link

Is there a way to track the numbered release that will include this?

@cstancu
Copy link
Member

cstancu commented Nov 19, 2024

Is there a way to track the numbered release that will include this?

Since it is merged on master it is automatically in next feature release, 24, coming up on March 18, 2025. Will also be back-ported to 23.0.2 coming up January 21, 2025. (https://www.graalvm.org/release-calendar/)

@cstancu
Copy link
Member

cstancu commented Nov 19, 2024

This aligns with issues we were seeing with Graal 22 as well. We had to manually register [Lcom.intellij.psi.PsiIdentifier; for reflection to get kotlinc working.

While working on the issue these were all the extra types that I found were required in the config without this fix:

{
  "name":"[Lcom.intellij.psi.PsiDeconstructionPattern;"
},
{
  "name":"[Lcom.intellij.psi.PsiFunctionalExpression;"
},  
{
  "name":"[Lcom.intellij.psi.PsiIdentifier;"
},
{
  "name":"[Lcom.intellij.psi.PsiReferenceList;"
},
{
  "name":"[Lcom.intellij.psi.javadoc.PsiSnippetAttribute;"
},

@nreid260
Copy link

Yep that looks like our list. Maybe even some we never found.

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

3 participants