Skip to content

Commit c2ea8b8

Browse files
committed
Track 'unsafe allocated' preserved state in ClassForNameSupport
1 parent 7450b85 commit c2ea8b8

File tree

5 files changed

+20
-8
lines changed

5 files changed

+20
-8
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/ClassForNameSupport.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,14 @@ private void registerKnownClassName(AccessCondition condition, String className,
296296
}
297297

298298
@Platforms(Platform.HOSTED_ONLY.class)
299-
public void registerUnsafeAllocated(AccessCondition condition, Class<?> clazz) {
299+
public void registerUnsafeAllocated(AccessCondition condition, Class<?> clazz, boolean preserved) {
300300
if (!clazz.isArray() && !clazz.isInterface() && !Modifier.isAbstract(clazz.getModifiers())) {
301301
/* Otherwise, UNSAFE.allocateInstance results in InstantiationException */
302302
if (!previousLayerUnsafe.contains(clazz.getName())) {
303-
var conditionSet = unsafeInstantiatedClasses.putIfAbsent(clazz, RuntimeConditionSet.createHosted(condition, false));
303+
var conditionSet = unsafeInstantiatedClasses.putIfAbsent(clazz, RuntimeConditionSet.createHosted(condition, preserved));
304304
if (conditionSet != null) {
305305
conditionSet.addCondition(condition);
306+
conditionSet.reportReregistered(preserved);
306307
}
307308
}
308309
}
@@ -463,6 +464,17 @@ public static boolean isPreserved(Class<?> jClass) {
463464
return false;
464465
}
465466

467+
public static boolean isUnsafeAllocatedPreserved(Class<?> jClass) {
468+
Objects.requireNonNull(jClass);
469+
for (var singleton : layeredSingletons()) {
470+
RuntimeConditionSet conditionSet = singleton.unsafeInstantiatedClasses.get(jClass);
471+
if (conditionSet != null) {
472+
return conditionSet.preserved();
473+
}
474+
}
475+
return false;
476+
}
477+
466478
public static boolean isRegisteredClass(String className) {
467479
if (respectClassLoader()) {
468480
RuntimeConditionSet conditionSet = getConditionForName(className);

substratevm/src/com.oracle.svm.graal/src/com/oracle/svm/graal/hosted/FieldsOffsetsFeature.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ private static void registerFields(FieldIntrospection<?> introspection, BeforeAn
167167

168168
/* The partial evaluator allocates Node classes via Unsafe. */
169169
AnalysisType nodeType = config.getMetaAccess().lookupJavaType(nodeClass.getJavaClass());
170-
nodeType.registerInstantiatedCallback(_ -> config.registerAsUnsafeAllocated(nodeType));
170+
nodeType.registerInstantiatedCallback(_ -> config.registerAsUnsafeAllocated(nodeType, false));
171171

172172
Fields dataFields = nodeClass.getData();
173173
registerFields(dataFields, config, "Graal node data field");

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/FeatureImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,15 +415,15 @@ public void registerAsInHeap(AnalysisType aType, Object reason) {
415415

416416
@Override
417417
public void registerAsUnsafeAllocated(Class<?> clazz) {
418-
registerAsUnsafeAllocated(getMetaAccess().lookupJavaType(clazz));
418+
registerAsUnsafeAllocated(getMetaAccess().lookupJavaType(clazz), false);
419419
}
420420

421-
public void registerAsUnsafeAllocated(AnalysisType aType) {
421+
public void registerAsUnsafeAllocated(AnalysisType aType, boolean preserved) {
422422
if (aType.isAbstract()) {
423423
throw UserError.abort("Cannot register an abstract class as instantiated: " + aType.toJavaName(true));
424424
}
425425
aType.registerAsUnsafeAllocated("From feature");
426-
classForNameSupport.registerUnsafeAllocated(AccessCondition.unconditional(), aType.getJavaClass());
426+
classForNameSupport.registerUnsafeAllocated(AccessCondition.unconditional(), aType.getJavaClass(), preserved);
427427
}
428428

429429
@Override

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jni/JNIAccessFeature.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ public void addMethod(Executable method, boolean preserved, DuringAnalysisAccess
461461
* Constructors can be invoked on objects allocated separately via AllocObject,
462462
* which we implement via Unsafe.
463463
*/
464-
access.registerAsUnsafeAllocated(aTargetMethod.getDeclaringClass());
464+
access.registerAsUnsafeAllocated(aTargetMethod.getDeclaringClass(), preserved);
465465
newObjectMethod = aFactoryMethod.getWrapped();
466466
}
467467

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/reflect/ReflectionDataBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ private void registerClass(AccessCondition condition, Class<?> clazz, boolean un
299299
type.registerAsReachable("Is registered for reflection.");
300300
if (unsafeInstantiated) {
301301
type.registerAsUnsafeAllocated("Is registered via reflection metadata.");
302-
classForNameSupport.registerUnsafeAllocated(condition, clazz);
302+
classForNameSupport.registerUnsafeAllocated(condition, clazz, preserved);
303303
}
304304

305305
if (allowForName) {

0 commit comments

Comments
 (0)