@@ -142,6 +142,11 @@ public static final class HandleContext {
142
142
143
143
public HandleContext (boolean useShadowTable ) {
144
144
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 );
145
150
}
146
151
147
152
public final NativeObjectReferenceArrayWrapper referencesToBeFreed = new NativeObjectReferenceArrayWrapper ();
@@ -150,8 +155,8 @@ public HandleContext(boolean useShadowTable) {
150
155
public final WeakHashMap <Object , WeakReference <Object >> managedNativeLookup = new WeakHashMap <>();
151
156
152
157
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 ;
155
160
156
161
public final Set <NativeStorageReference > nativeStorageReferences = new HashSet <>();
157
162
@@ -515,14 +520,14 @@ public static IdReference<?> nativeLookupRemove(HandleContext context, long poin
515
520
return context .nativeLookup .remove (pointer );
516
521
}
517
522
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 ) {
520
525
if (PythonContext .DEBUG_CAPI && HandleContext .getShadowTable (context .nativeStubLookupShadowTable , pointer ) != null ) {
521
526
throw CompilerDirectives .shouldNotReachHere ();
522
527
}
523
528
return null ;
524
529
}
525
- PythonObjectReference result = context .nativeStubLookup [id - 1 ];
530
+ PythonObjectReference result = context .nativeStubLookup [idx ];
526
531
if (PythonContext .DEBUG_CAPI && HandleContext .getShadowTable (context .nativeStubLookupShadowTable , pointer ) != result ) {
527
532
throw CompilerDirectives .shouldNotReachHere ();
528
533
}
@@ -544,7 +549,7 @@ private static int nativeStubLookupReserve(HandleContext context) throws Overflo
544
549
idx = resizeNativeStubLookupTable (context );
545
550
}
546
551
assert context .nativeStubLookup [idx ] == null ;
547
- return idx + 1 ;
552
+ return idx ;
548
553
}
549
554
550
555
@ TruffleBoundary
@@ -563,7 +568,7 @@ private static int resizeNativeStubLookupTable(HandleContext context) throws Ove
563
568
564
569
private static int nativeStubLookupPut (HandleContext context , PythonObjectReference value ) {
565
570
assert value .handleTableIndex > 0 ;
566
- final int idx = value .handleTableIndex - 1 ;
571
+ final int idx = value .handleTableIndex ;
567
572
assert context .nativeStubLookup [idx ] == null || context .nativeStubLookup [idx ] == value ;
568
573
context .nativeStubLookup [idx ] = value ;
569
574
if (PythonContext .DEBUG_CAPI ) {
@@ -577,7 +582,7 @@ private static int nativeStubLookupPut(HandleContext context, PythonObjectRefere
577
582
578
583
private static PythonObjectReference nativeStubLookupRemove (HandleContext context , PythonObjectReference ref ) {
579
584
assert ref .handleTableIndex > 0 ;
580
- final int idx = ref .handleTableIndex - 1 ;
585
+ final int idx = ref .handleTableIndex ;
581
586
PythonObjectReference result = context .nativeStubLookup [idx ];
582
587
context .nativeStubLookup [idx ] = null ;
583
588
context .nativeStubLookupFreeStack .push (idx );
@@ -670,6 +675,9 @@ static long doGeneric(Node inliningTarget, PythonAbstractObjectNativeWrapper wra
670
675
long stubPointer = coerceToLongNode .execute (inliningTarget , nativeObjectStub );
671
676
long taggedPointer = HandlePointerConverter .stubToPointer (stubPointer );
672
677
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 ;
673
681
writeIntNode .write (stubPointer , CFields .GraalPyObject__handle_table_index , idx );
674
682
PythonObjectReference ref = PythonObjectReference .create (handleContext , wrapper , immortal , taggedPointer , idx );
675
683
nativeStubLookupPut (handleContext , ref );
0 commit comments