52
52
import java .util .concurrent .ConcurrentHashMap ;
53
53
import java .util .logging .Level ;
54
54
55
+ import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
55
56
import com .oracle .graal .python .builtins .objects .PNone ;
56
57
import com .oracle .graal .python .builtins .objects .PythonAbstractObject ;
57
58
import com .oracle .graal .python .builtins .objects .cext .PythonAbstractNativeObject ;
85
86
import com .oracle .graal .python .builtins .objects .tuple .PTuple ;
86
87
import com .oracle .graal .python .nodes .PGuards ;
87
88
import com .oracle .graal .python .nodes .PNodeWithContext ;
89
+ import com .oracle .graal .python .nodes .PRaiseNode ;
88
90
import com .oracle .graal .python .nodes .object .GetClassNode ;
89
91
import com .oracle .graal .python .runtime .GilNode ;
90
92
import com .oracle .graal .python .runtime .PythonContext ;
91
93
import com .oracle .graal .python .runtime .sequence .storage .NativeSequenceStorage ;
92
94
import com .oracle .graal .python .runtime .sequence .storage .SequenceStorage ;
93
95
import com .oracle .graal .python .runtime .sequence .storage .SequenceStorage .ListStorageType ;
96
+ import com .oracle .graal .python .util .OverflowException ;
94
97
import com .oracle .graal .python .util .PythonUtils ;
95
98
import com .oracle .truffle .api .CompilerAsserts ;
96
99
import com .oracle .truffle .api .CompilerDirectives ;
@@ -526,7 +529,16 @@ public static PythonObjectReference nativeStubLookupGet(HandleContext context, l
526
529
return result ;
527
530
}
528
531
529
- private static int nativeStubLookupReserve (HandleContext context ) {
532
+ /**
533
+ * Reserves a free slot in the handle table that can later be used to store a
534
+ * {@link PythonObjectReference} using
535
+ * {@link #nativeStubLookupPut(HandleContext, PythonObjectReference)}. If the handle table is
536
+ * currently too small, it will be enlarged.
537
+ *
538
+ * @throws OverflowException Indicates that we cannot resize the handle table anymore. This
539
+ * essentially indicates a Python-level MemoryError.
540
+ */
541
+ private static int nativeStubLookupReserve (HandleContext context ) throws OverflowException {
530
542
int idx = context .nativeStubLookupFreeStack .pop ();
531
543
if (idx == -1 ) {
532
544
idx = resizeNativeStubLookupTable (context );
@@ -536,10 +548,10 @@ private static int nativeStubLookupReserve(HandleContext context) {
536
548
}
537
549
538
550
@ TruffleBoundary
539
- private static int resizeNativeStubLookupTable (HandleContext context ) {
551
+ private static int resizeNativeStubLookupTable (HandleContext context ) throws OverflowException {
540
552
int oldSize = context .nativeStubLookup .length ;
541
553
// exponentially grow until 1 MB; then linearly grow by 1 MB
542
- int newSize = oldSize >= HandleContext .LINEAR_THRESHOLD ? oldSize + HandleContext .LINEAR_THRESHOLD : oldSize * 2 ;
554
+ int newSize = oldSize >= HandleContext .LINEAR_THRESHOLD ? PythonUtils . addExact ( oldSize , HandleContext .LINEAR_THRESHOLD ) : PythonUtils . multiplyExact ( oldSize , 2 ) ;
543
555
assert newSize != oldSize ;
544
556
if (LOGGER .isLoggable (Level .FINE )) {
545
557
LOGGER .fine (String .format ("Resizing native stub lookup table: %d -> %d" , oldSize , newSize ));
@@ -663,6 +675,14 @@ static long doGeneric(Node inliningTarget, PythonAbstractObjectNativeWrapper wra
663
675
nativeStubLookupPut (handleContext , ref );
664
676
665
677
return logResult (taggedPointer );
678
+ } catch (OverflowException e ) {
679
+ /*
680
+ * The OverflowException may be thrown by 'nativeStubLookupReserve' and indicates
681
+ * that we cannot resize the handle table anymore. This essentially indicates a
682
+ * Python-level MemoryError.
683
+ */
684
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
685
+ throw PRaiseNode .raiseUncached (inliningTarget , PythonBuiltinClassType .MemoryError );
666
686
} finally {
667
687
gil .release (acquired );
668
688
}
0 commit comments