Skip to content

Commit 134d317

Browse files
committed
Do not use first element in handle table
1 parent d286eab commit 134d317

File tree

1 file changed

+16
-8
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/transitions

1 file changed

+16
-8
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/transitions/CApiTransitions.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ public static final class HandleContext {
142142

143143
public HandleContext(boolean useShadowTable) {
144144
nativeStubLookupShadowTable = useShadowTable ? new HashMap<>() : null;
145+
nativeStubLookup = new PythonObjectReference[DEFAULT_CAPACITY];
146+
nativeStubLookupFreeStack = new HandleStack(DEFAULT_CAPACITY);
147+
// Never use 'handleTableIndex == 0' to avoid that zeroed memory
148+
// accidentally maps to some valid object.
149+
nativeStubLookupFreeStack.pushRange(1, DEFAULT_CAPACITY);
145150
}
146151

147152
public final NativeObjectReferenceArrayWrapper referencesToBeFreed = new NativeObjectReferenceArrayWrapper();
@@ -150,8 +155,8 @@ public HandleContext(boolean useShadowTable) {
150155
public final WeakHashMap<Object, WeakReference<Object>> managedNativeLookup = new WeakHashMap<>();
151156

152157
private final HashMap<Long, PythonObjectReference> nativeStubLookupShadowTable;
153-
public PythonObjectReference[] nativeStubLookup = new PythonObjectReference[DEFAULT_CAPACITY];
154-
public final HandleStack nativeStubLookupFreeStack = new HandleStack(DEFAULT_CAPACITY, true);
158+
public PythonObjectReference[] nativeStubLookup;
159+
public final HandleStack nativeStubLookupFreeStack;
155160

156161
public final Set<NativeStorageReference> nativeStorageReferences = new HashSet<>();
157162

@@ -515,14 +520,14 @@ public static IdReference<?> nativeLookupRemove(HandleContext context, long poin
515520
return context.nativeLookup.remove(pointer);
516521
}
517522

518-
public static PythonObjectReference nativeStubLookupGet(HandleContext context, long pointer, int id) {
519-
if (id <= 0) {
523+
public static PythonObjectReference nativeStubLookupGet(HandleContext context, long pointer, int idx) {
524+
if (idx <= 0) {
520525
if (PythonContext.DEBUG_CAPI && HandleContext.getShadowTable(context.nativeStubLookupShadowTable, pointer) != null) {
521526
throw CompilerDirectives.shouldNotReachHere();
522527
}
523528
return null;
524529
}
525-
PythonObjectReference result = context.nativeStubLookup[id - 1];
530+
PythonObjectReference result = context.nativeStubLookup[idx];
526531
if (PythonContext.DEBUG_CAPI && HandleContext.getShadowTable(context.nativeStubLookupShadowTable, pointer) != result) {
527532
throw CompilerDirectives.shouldNotReachHere();
528533
}
@@ -544,7 +549,7 @@ private static int nativeStubLookupReserve(HandleContext context) throws Overflo
544549
idx = resizeNativeStubLookupTable(context);
545550
}
546551
assert context.nativeStubLookup[idx] == null;
547-
return idx + 1;
552+
return idx;
548553
}
549554

550555
@TruffleBoundary
@@ -563,7 +568,7 @@ private static int resizeNativeStubLookupTable(HandleContext context) throws Ove
563568

564569
private static int nativeStubLookupPut(HandleContext context, PythonObjectReference value) {
565570
assert value.handleTableIndex > 0;
566-
final int idx = value.handleTableIndex - 1;
571+
final int idx = value.handleTableIndex;
567572
assert context.nativeStubLookup[idx] == null || context.nativeStubLookup[idx] == value;
568573
context.nativeStubLookup[idx] = value;
569574
if (PythonContext.DEBUG_CAPI) {
@@ -577,7 +582,7 @@ private static int nativeStubLookupPut(HandleContext context, PythonObjectRefere
577582

578583
private static PythonObjectReference nativeStubLookupRemove(HandleContext context, PythonObjectReference ref) {
579584
assert ref.handleTableIndex > 0;
580-
final int idx = ref.handleTableIndex - 1;
585+
final int idx = ref.handleTableIndex;
581586
PythonObjectReference result = context.nativeStubLookup[idx];
582587
context.nativeStubLookup[idx] = null;
583588
context.nativeStubLookupFreeStack.push(idx);
@@ -670,6 +675,9 @@ static long doGeneric(Node inliningTarget, PythonAbstractObjectNativeWrapper wra
670675
long stubPointer = coerceToLongNode.execute(inliningTarget, nativeObjectStub);
671676
long taggedPointer = HandlePointerConverter.stubToPointer(stubPointer);
672677
int idx = nativeStubLookupReserve(handleContext);
678+
// We don't allow 'handleTableIndex == 0' to avoid that zeroed memory
679+
// accidentally maps to some valid object.
680+
assert idx > 0;
673681
writeIntNode.write(stubPointer, CFields.GraalPyObject__handle_table_index, idx);
674682
PythonObjectReference ref = PythonObjectReference.create(handleContext, wrapper, immortal, taggedPointer, idx);
675683
nativeStubLookupPut(handleContext, ref);

0 commit comments

Comments
 (0)