Skip to content

Commit 9122983

Browse files
teshullZeavee
authored andcommitted
Ensure only non null unresolved symbol are persisted in dispatch table
1 parent 398a5a0 commit 9122983

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/heap/SVMImageLayerWriter.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.HashMap;
3434
import java.util.List;
3535
import java.util.Map;
36+
import java.util.Objects;
3637
import java.util.Set;
3738
import java.util.function.IntFunction;
3839

@@ -389,10 +390,7 @@ private static void writeImageSingletonKeyStore(ImageSingletonObject.Builder obj
389390
case String[] strl -> {
390391
TextList.Builder strlb = b.getValue().initStrl(strl.length);
391392
for (int j = 0; j < strl.length; j++) {
392-
String s = strl[j];
393-
if (s != null) {
394-
strlb.set(j, new Text.Reader(s));
395-
}
393+
strlb.set(j, new Text.Reader(strl[j]));
396394
}
397395
}
398396
case boolean[] zl -> {
@@ -414,8 +412,13 @@ EconomicMap<String, Object> getKeyValueStore() {
414412
return keyValueStore;
415413
}
416414

415+
private static boolean nonNullEntries(List<?> list) {
416+
return list.stream().filter(Objects::isNull).findAny().isEmpty();
417+
}
418+
417419
@Override
418420
public void writeBoolList(String keyName, List<Boolean> value) {
421+
assert nonNullEntries(value);
419422
boolean[] b = new boolean[value.size()];
420423
for (int i = 0; i < value.size(); i++) {
421424
b[i] = value.get(i);
@@ -432,6 +435,7 @@ public void writeInt(String keyName, int value) {
432435

433436
@Override
434437
public void writeIntList(String keyName, List<Integer> value) {
438+
assert nonNullEntries(value);
435439
var previous = keyValueStore.put(keyName, value.stream().mapToInt(i -> i).toArray());
436440
assert previous == null : Assertions.errorMessage(keyName, previous);
437441
}
@@ -450,6 +454,7 @@ public void writeString(String keyName, String value) {
450454

451455
@Override
452456
public void writeStringList(String keyName, List<String> value) {
457+
assert nonNullEntries(value);
453458
var previous = keyValueStore.put(keyName, value.toArray(String[]::new));
454459
assert previous == null : Assertions.errorMessage(keyName, previous);
455460
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/imagelayer/LayeredDispatchTableSupport.java

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,16 @@ private void injectPriorLayerInfo(HostedType type, HostedDispatchTable dispatchT
229229
if (priorInfo != null) {
230230
compareTypeInfo(dispatchTable, priorInfo);
231231
dispatchTable.status = priorInfo.installed ? HubStatus.INSTALLED_PRIOR_LAYER : HubStatus.COMPUTED_PRIOR_LAYER;
232-
for (int i = 0; i < dispatchTable.slots.length; i++) {
233-
HostedDispatchSlot slot = dispatchTable.slots[i];
234-
PriorDispatchSlot priorSlot = priorInfo.slots[i];
235-
if (priorSlot.status.isCompiled()) {
236-
slot.status = SlotResolutionStatus.PRIOR_LAYER;
237-
slot.symbol = priorSlot.slotSymbolName;
232+
if (priorInfo.installed) {
233+
// record symbol info for installed hubs
234+
for (int i = 0; i < dispatchTable.slots.length; i++) {
235+
HostedDispatchSlot slot = dispatchTable.slots[i];
236+
PriorDispatchSlot priorSlot = priorInfo.slots[i];
237+
if (priorSlot.status.isCompiled()) {
238+
slot.status = SlotResolutionStatus.PRIOR_LAYER;
239+
assert !priorSlot.slotSymbolName.equals(PriorDispatchSlot.INVALID_SYMBOL_NAME);
240+
slot.symbol = priorSlot.slotSymbolName;
241+
}
238242
}
239243
}
240244
}
@@ -415,7 +419,7 @@ public void defineDispatchTableSlotSymbols(ObjectFile objectFile, ObjectFile.Sec
415419
* Next determine if any vtable symbols defined in prior layers now have a resolved target.
416420
*/
417421
for (HostedDispatchTable typeInfo : typeToDispatchTable.values()) {
418-
if (typeInfo.status == HubStatus.INSTALLED_PRIOR_LAYER || typeInfo.status == HubStatus.COMPUTED_PRIOR_LAYER) {
422+
if (typeInfo.status == HubStatus.INSTALLED_PRIOR_LAYER) {
419423
for (HostedDispatchSlot slotInfo : typeInfo.slots) {
420424
if (slotInfo.status == SlotResolutionStatus.COMPUTED && slotInfo.resolvedMethod.isCompiled()) {
421425
/*
@@ -449,9 +453,6 @@ public void defineDispatchTableSlotSymbols(ObjectFile objectFile, ObjectFile.Sec
449453
*/
450454
if (ImageLayerBuildingSupport.buildingApplicationLayer()) {
451455
priorUnresolvedSymbols.forEach(symbol -> {
452-
if (symbol.isEmpty()) {
453-
return;
454-
}
455456
if (!resolvedPriorVTableMap.containsKey(symbol)) {
456457
CompilationResult result = codeCache.compilationResultFor(invalidMethod);
457458

@@ -520,9 +521,9 @@ public PersistFlags preparePersist(ImageSingletonWriter writer) {
520521
* Write out dispatch tables for persisted types.
521522
*/
522523
List<Integer> dispatchTableInts = new ArrayList<>();
524+
List<Boolean> dispatchTableBooleans = new ArrayList<>();
523525
List<Integer> dispatchSlotInts = new ArrayList<>();
524526
List<String> dispatchSlotStrings = new ArrayList<>();
525-
List<Boolean> dispatchTableBooleans = new ArrayList<>();
526527
int nextSlotIdx = 0;
527528
for (HostedDispatchTable info : typeToDispatchTable.values()) {
528529
if (!layerWriter.isTypePersisted(info.type.getWrapped())) {
@@ -538,9 +539,11 @@ public PersistFlags preparePersist(ImageSingletonWriter writer) {
538539
localTargets.add(methodToOffsetMapper.apply(hMethod));
539540
}
540541

542+
boolean hubInstalled = info.status == HubStatus.INSTALLED_CURRENT_LAYER;
541543
if (info.slots != null) {
542544
for (var slotInfo : info.slots) {
543-
dispatchSlotStrings.add(slotInfo.symbol);
545+
var symbolName = hubInstalled ? slotInfo.symbol : PriorDispatchSlot.INVALID_SYMBOL_NAME;
546+
dispatchSlotStrings.add(symbolName);
544547
dispatchSlotInts.add(slotInfo.slotNum);
545548
dispatchSlotInts.add(slotInfo.status.ordinal());
546549
dispatchSlotInts.add(methodToOffsetMapper.apply(slotInfo.declaredMethod));
@@ -559,29 +562,29 @@ public PersistFlags preparePersist(ImageSingletonWriter writer) {
559562
dispatchTableInts.add(slotOffsets.size());
560563
dispatchTableInts.addAll(localTargets);
561564
dispatchTableInts.addAll(slotOffsets);
562-
dispatchTableBooleans.add(info.type.getWrapped().isReachable());
565+
dispatchTableBooleans.add(hubInstalled);
563566
}
564567

565568
writer.writeIntList("dispatchTableInts", dispatchTableInts);
569+
writer.writeBoolList("dispatchTableBooleans", dispatchTableBooleans);
566570
writer.writeIntList("dispatchSlotInts", dispatchSlotInts);
567571
writer.writeStringList("dispatchSlotStrings", dispatchSlotStrings);
568572
writer.writeBoolList("methodBooleans", methodBooleans);
569573
writer.writeIntList("methodInts", methodInts);
570574
writer.writeStringList("methodStrings", methodStrings);
571-
writer.writeBoolList("dispatchTableBooleans", dispatchTableBooleans);
572575

573576
return PersistFlags.CREATE;
574577
}
575578

576579
@SuppressWarnings("unused")
577580
public static Object createFromLoader(ImageSingletonLoader loader) {
578581
List<Integer> dispatchTableInts = loader.readIntList("dispatchTableInts");
582+
List<Boolean> dispatchTableBooleans = loader.readBoolList("dispatchTableBooleans");
579583
List<Integer> dispatchSlotInts = loader.readIntList("dispatchSlotInts");
580584
List<String> dispatchSlotStrings = loader.readStringList("dispatchSlotStrings");
581585
List<Boolean> methodBooleans = loader.readBoolList("methodBooleans");
582586
List<Integer> methodInts = loader.readIntList("methodInts");
583587
List<String> methodStrings = loader.readStringList("methodStrings");
584-
List<Boolean> dispatchTableBooleans = loader.readBoolList("dispatchTableBooleans");
585588

586589
Set<String> unresolvedSymbols = new HashSet<>();
587590
Map<Integer, PriorDispatchTable> priorTypes = new HashMap<>();
@@ -616,10 +619,6 @@ public static Object createFromLoader(ImageSingletonLoader loader) {
616619
resolvedMethod = priorMethods.get(index);
617620
}
618621

619-
if (status == SlotResolutionStatus.UNRESOLVED || status == SlotResolutionStatus.NOT_COMPILED) {
620-
unresolvedSymbols.add(slotSymbolName);
621-
}
622-
623622
var slotInfo = new PriorDispatchSlot(declaredMethod, resolvedMethod, slotNum, status, slotSymbolName);
624623
priorDispatchSlots.add(slotInfo);
625624
}
@@ -628,7 +627,7 @@ public static Object createFromLoader(ImageSingletonLoader loader) {
628627
boolIterator = dispatchTableBooleans.iterator();
629628
while (intIterator.hasNext()) {
630629
int typeId = intIterator.next();
631-
boolean installed = boolIterator.next();
630+
boolean hubInstalled = boolIterator.next();
632631
int locallyDeclaredMethodsSize = intIterator.next();
633632
int allSlotsSize = intIterator.next();
634633
PriorDispatchMethod[] locallyDeclaredSlots = new PriorDispatchMethod[locallyDeclaredMethodsSize];
@@ -638,9 +637,17 @@ public static Object createFromLoader(ImageSingletonLoader loader) {
638637
}
639638
for (int i = 0; i < allSlotsSize; i++) {
640639
dispatchTableSlots[i] = priorDispatchSlots.get(intIterator.next());
640+
if (hubInstalled) {
641+
var status = dispatchTableSlots[i].status;
642+
if (status == SlotResolutionStatus.UNRESOLVED || status == SlotResolutionStatus.NOT_COMPILED) {
643+
assert !dispatchTableSlots[i].slotSymbolName.equals(PriorDispatchSlot.INVALID_SYMBOL_NAME);
644+
unresolvedSymbols.add(dispatchTableSlots[i].slotSymbolName);
645+
}
646+
647+
}
641648
}
642649

643-
var priorDispatchTable = new PriorDispatchTable(typeId, installed, locallyDeclaredSlots, dispatchTableSlots);
650+
var priorDispatchTable = new PriorDispatchTable(typeId, hubInstalled, locallyDeclaredSlots, dispatchTableSlots);
644651
Object prev = priorTypes.put(typeId, priorDispatchTable);
645652
assert prev == null : prev;
646653

@@ -715,6 +722,7 @@ record PriorDispatchSlot(
715722
PriorDispatchMethod declaredMethod, PriorDispatchMethod resolvedMethod,
716723
int slotNum, SlotResolutionStatus status, String slotSymbolName) {
717724
static final int UNKNOWN_METHOD = -1;
725+
static final String INVALID_SYMBOL_NAME = "invalid";
718726
}
719727

720728
/**

0 commit comments

Comments
 (0)