@@ -587,15 +587,25 @@ private ImageHeapConstant markReachable(ImageHeapConstant imageHeapConstant, Sca
587
587
}
588
588
589
589
protected void onObjectReachable (ImageHeapConstant imageHeapConstant , ScanReason reason , Consumer <ScanReason > onAnalysisModified ) {
590
- AnalysisType objectType = metaAccess .lookupJavaType (imageHeapConstant );
591
- imageHeap .addReachableObject (objectType , imageHeapConstant );
592
590
593
- AnalysisType type = imageHeapConstant .getType ();
594
- Object object = bb .getSnippetReflectionProvider ().asObject (Object .class , imageHeapConstant );
595
- /* Simulated constants don't have a backing object and don't need to be processed. */
596
- if (object != null ) {
591
+ AnalysisType objectType = imageHeapConstant .getType ();
592
+ if (imageHeapConstant .isBackedByHostedObject ()) {
593
+ /* Simulated constants don't have a backing object and don't need to be processed. */
597
594
try {
598
- type .notifyObjectReachable (universe .getConcurrentAnalysisAccess (), object , reason );
595
+ Object object = bb .getSnippetReflectionProvider ().asObject (Object .class , imageHeapConstant );
596
+ /*
597
+ * Before adding the object to ImageHeap.reachableObjects, where it could be read by
598
+ * other threads, run validation checks, e.g., verify that the object's type can be
599
+ * initialized at build time. Also run the validation before exposing the object to
600
+ * other reachability hooks to avoid propagating an invalid object.
601
+ */
602
+ hostVM .validateReachableObject (object );
603
+ /*
604
+ * Note that reachability hooks can also reject objects based on specific validation
605
+ * conditions, e.g., a started Thread should never be added to the image heap, but
606
+ * the structure of the object is valid, as ensured by the validity check above.
607
+ */
608
+ objectType .notifyObjectReachable (universe .getConcurrentAnalysisAccess (), object , reason );
599
609
} catch (UnsupportedFeatureException e ) {
600
610
/* Enhance the unsupported feature message with the object trace and rethrow. */
601
611
StringBuilder backtrace = new StringBuilder ();
@@ -604,6 +614,8 @@ protected void onObjectReachable(ImageHeapConstant imageHeapConstant, ScanReason
604
614
}
605
615
}
606
616
617
+ imageHeap .addReachableObject (objectType , imageHeapConstant );
618
+
607
619
markTypeInstantiated (objectType , reason );
608
620
if (imageHeapConstant instanceof ImageHeapObjectArray imageHeapArray ) {
609
621
AnalysisType arrayType = imageHeapArray .getType ();
0 commit comments