Skip to content

Commit 5ddc82d

Browse files
committed
Avoid throwing new instances of ArithmeticException.
1 parent 9476d94 commit 5ddc82d

21 files changed

+229
-115
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/LZMAModuleBuiltins.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import com.oracle.graal.python.runtime.PythonContext;
7373
import com.oracle.graal.python.runtime.PythonCore;
7474
import com.oracle.graal.python.runtime.exception.PException;
75+
import com.oracle.graal.python.util.OverflowException;
7576
import com.oracle.truffle.api.CompilerDirectives;
7677
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
7778
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -461,7 +462,7 @@ abstract static class IsCheckSupportedNode extends PythonUnaryBuiltinNode {
461462

462463
@Specialization
463464
@TruffleBoundary
464-
boolean doInt(int checkID) {
465+
static boolean doInt(int checkID) {
465466
try {
466467
return Check.getInstance(checkID) != null;
467468
} catch (UnsupportedOptionsException e) {
@@ -470,16 +471,16 @@ boolean doInt(int checkID) {
470471
}
471472

472473
@Specialization
473-
boolean doLong(long checkID) {
474+
static boolean doLong(long checkID) {
474475
try {
475476
return doInt(PInt.intValueExact(checkID));
476-
} catch (ArithmeticException e) {
477+
} catch (OverflowException e) {
477478
return false;
478479
}
479480
}
480481

481482
@Specialization
482-
boolean doLong(PInt checkID) {
483+
static boolean doLong(PInt checkID) {
483484
try {
484485
return doInt(checkID.intValueExact());
485486
} catch (ArithmeticException e) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MMapModuleBuiltins.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6464
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
6565
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
66+
import com.oracle.graal.python.util.OverflowException;
6667
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6768
import com.oracle.truffle.api.TruffleFile;
6869
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
@@ -121,7 +122,7 @@ PMMap doFile(LazyPythonClass clazz, long fd, long length, @SuppressWarnings("unu
121122
int ifd;
122123
try {
123124
ifd = PInt.intValueExact(fd);
124-
} catch (ArithmeticException e) {
125+
} catch (OverflowException e) {
125126
throw raise(ValueError, ErrorMessages.INVALID_FILE_DESCRIPTOR);
126127
}
127128

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/PythonCextBuiltins.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@
7171
import com.oracle.graal.python.builtins.modules.BuiltinConstructors.IntNode;
7272
import com.oracle.graal.python.builtins.modules.BuiltinConstructorsFactory.IntNodeFactory;
7373
import com.oracle.graal.python.builtins.modules.ExternalFunctionNodes.AllocFuncRootNode;
74-
import com.oracle.graal.python.builtins.modules.ExternalFunctionNodes.MethDirectRoot;
7574
import com.oracle.graal.python.builtins.modules.ExternalFunctionNodes.GetAttrFuncRootNode;
75+
import com.oracle.graal.python.builtins.modules.ExternalFunctionNodes.MethDirectRoot;
7676
import com.oracle.graal.python.builtins.modules.ExternalFunctionNodes.MethFastcallRoot;
7777
import com.oracle.graal.python.builtins.modules.ExternalFunctionNodes.MethFastcallWithKeywordsRoot;
7878
import com.oracle.graal.python.builtins.modules.ExternalFunctionNodes.MethKeywordsRoot;
@@ -243,6 +243,7 @@
243243
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
244244
import com.oracle.graal.python.runtime.sequence.storage.MroSequenceStorage;
245245
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
246+
import com.oracle.graal.python.util.OverflowException;
246247
import com.oracle.graal.python.util.Supplier;
247248
import com.oracle.truffle.api.CompilerAsserts;
248249
import com.oracle.truffle.api.CompilerDirectives;
@@ -1185,12 +1186,11 @@ long doPInt4(VirtualFrame frame, PInt obj, int signed, @SuppressWarnings("unused
11851186
return obj.intValueExact();
11861187
} else if (obj.bitCount() <= 32) {
11871188
return obj.intValue();
1188-
} else {
1189-
throw new ArithmeticException();
11901189
}
11911190
} catch (ArithmeticException e) {
1192-
return raiseTooLarge(frame, targetTypeSize);
1191+
// fall through
11931192
}
1193+
return raiseTooLarge(frame, targetTypeSize);
11941194
}
11951195

11961196
@Specialization(guards = "targetTypeSize == 8")
@@ -1200,12 +1200,11 @@ long doPInt8(VirtualFrame frame, PInt obj, int signed, @SuppressWarnings("unused
12001200
return obj.longValueExact();
12011201
} else if (obj.bitCount() <= 64) {
12021202
return obj.longValue();
1203-
} else {
1204-
throw new ArithmeticException();
12051203
}
12061204
} catch (ArithmeticException e) {
1207-
return raiseTooLarge(frame, targetTypeSize);
1205+
// fall through
12081206
}
1207+
return raiseTooLarge(frame, targetTypeSize);
12091208
}
12101209

12111210
@Specialization(guards = {"targetTypeSize != 4", "targetTypeSize != 8"})
@@ -2098,8 +2097,8 @@ PBytes doInt(int size) {
20982097
return factory().createBytes(new byte[size]);
20992098
}
21002099

2101-
@Specialization(rewriteOn = ArithmeticException.class)
2102-
PBytes doLong(long size) {
2100+
@Specialization(rewriteOn = OverflowException.class)
2101+
PBytes doLong(long size) throws OverflowException {
21032102
return doInt(PInt.intValueExact(size));
21042103
}
21052104

@@ -2108,7 +2107,7 @@ PBytes doLongOvf(long size,
21082107
@Shared("raiseNode") @Cached PRaiseNode raiseNode) {
21092108
try {
21102109
return doInt(PInt.intValueExact(size));
2111-
} catch (ArithmeticException e) {
2110+
} catch (OverflowException e) {
21122111
throw raiseNode.raiseNumberTooLarge(IndexError, size);
21132112
}
21142113
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SysModuleBuiltins.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import java.util.Date;
5252
import java.util.List;
5353

54+
import com.oracle.graal.python.util.OverflowException;
5455
import org.graalvm.nativeimage.ImageInfo;
5556

5657
import com.oracle.graal.python.PythonLanguage;
@@ -357,10 +358,10 @@ PFrame counted(VirtualFrame frame, int num,
357358
return requested;
358359
}
359360

360-
@Specialization(rewriteOn = ArithmeticException.class)
361+
@Specialization(rewriteOn = OverflowException.class)
361362
PFrame countedLong(VirtualFrame frame, long num,
362363
@Shared("caller") @Cached ReadCallerFrameNode readCallerNode,
363-
@Shared("callStackDepthProfile") @Cached("createBinaryProfile()") ConditionProfile callStackDepthProfile) {
364+
@Shared("callStackDepthProfile") @Cached("createBinaryProfile()") ConditionProfile callStackDepthProfile) throws OverflowException {
364365
return counted(frame, PInt.intValueExact(num), readCallerNode, callStackDepthProfile);
365366
}
366367

@@ -370,7 +371,7 @@ PFrame countedLongOvf(VirtualFrame frame, long num,
370371
@Shared("callStackDepthProfile") @Cached("createBinaryProfile()") ConditionProfile callStackDepthProfile) {
371372
try {
372373
return counted(frame, PInt.intValueExact(num), readCallerNode, callStackDepthProfile);
373-
} catch (ArithmeticException e) {
374+
} catch (OverflowException e) {
374375
throw raiseCallStackDepth();
375376
}
376377
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/PythonAbstractObject.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects;
4242

43+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.AttributeError;
44+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
45+
import static com.oracle.graal.python.nodes.SpecialMethodNames.FILENO;
4346
import static com.oracle.graal.python.nodes.SpecialMethodNames.__BOOL__;
4447
import static com.oracle.graal.python.nodes.SpecialMethodNames.__CALL__;
4548
import static com.oracle.graal.python.nodes.SpecialMethodNames.__DELETE__;
@@ -48,17 +51,20 @@
4851
import static com.oracle.graal.python.nodes.SpecialMethodNames.__EQ__;
4952
import static com.oracle.graal.python.nodes.SpecialMethodNames.__EXIT__;
5053
import static com.oracle.graal.python.nodes.SpecialMethodNames.__FLOAT__;
54+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__FSPATH__;
55+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTRIBUTE__;
56+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTR__;
5157
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETITEM__;
5258
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GET__;
5359
import static com.oracle.graal.python.nodes.SpecialMethodNames.__HASH__;
5460
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INDEX__;
61+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INT__;
5562
import static com.oracle.graal.python.nodes.SpecialMethodNames.__ITER__;
5663
import static com.oracle.graal.python.nodes.SpecialMethodNames.__LEN__;
5764
import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEXT__;
5865
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SETITEM__;
5966
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SET__;
6067
import static com.oracle.graal.python.nodes.SpecialMethodNames.__STR__;
61-
import static com.oracle.graal.python.nodes.SpecialMethodNames.FILENO;
6268

6369
import java.time.LocalDate;
6470
import java.time.LocalTime;
@@ -68,8 +74,6 @@
6874

6975
import com.oracle.graal.python.PythonLanguage;
7076
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
71-
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.AttributeError;
72-
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
7377
import com.oracle.graal.python.builtins.modules.MathGuards;
7478
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
7579
import com.oracle.graal.python.builtins.objects.cext.CApiGuards;
@@ -102,10 +106,6 @@
102106
import com.oracle.graal.python.nodes.PGuards;
103107
import com.oracle.graal.python.nodes.PRaiseNode;
104108
import com.oracle.graal.python.nodes.SpecialMethodNames;
105-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__FSPATH__;
106-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTRIBUTE__;
107-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTR__;
108-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INT__;
109109
import com.oracle.graal.python.nodes.attributes.HasInheritedAttributeNode;
110110
import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode;
111111
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
@@ -133,6 +133,7 @@
133133
import com.oracle.graal.python.runtime.PythonContext;
134134
import com.oracle.graal.python.runtime.PythonOptions;
135135
import com.oracle.graal.python.runtime.exception.PException;
136+
import com.oracle.graal.python.util.OverflowException;
136137
import com.oracle.truffle.api.CompilerDirectives;
137138
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
138139
import com.oracle.truffle.api.TruffleLanguage;
@@ -1065,7 +1066,7 @@ public int asSizeWithState(LazyPythonClass type, ThreadState state,
10651066
}
10661067
try {
10671068
return PInt.intValueExact(longResult);
1068-
} catch (ArithmeticException e) {
1069+
} catch (OverflowException e) {
10691070
overflow.enter();
10701071
if (ignoreOverflow.profile(type != null)) {
10711072
throw raise.raiseNumberTooLarge(type, result);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/CArrayWrappers.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444

4545
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.PCallCapiFunction;
4646
import com.oracle.graal.python.builtins.objects.ints.PInt;
47+
import com.oracle.graal.python.util.OverflowException;
48+
import com.oracle.truffle.api.CompilerDirectives;
4749
import com.oracle.truffle.api.dsl.Cached;
4850
import com.oracle.truffle.api.dsl.Cached.Exclusive;
4951
import com.oracle.truffle.api.interop.InteropLibrary;
@@ -134,9 +136,10 @@ final Object readArrayElement(long index,
134136
} else if (idx == s.length()) {
135137
return '\0';
136138
}
137-
} catch (ArithmeticException e) {
139+
} catch (OverflowException e) {
138140
// fall through
139141
}
142+
CompilerDirectives.transferToInterpreterAndInvalidate();
140143
throw InvalidArrayIndexException.create(index);
141144
}
142145

@@ -198,9 +201,10 @@ Object readArrayElement(long index,
198201
} else if (idx == arr.length) {
199202
return (byte) 0;
200203
}
201-
} catch (ArithmeticException e) {
204+
} catch (OverflowException e) {
202205
// fall through
203206
}
207+
CompilerDirectives.transferToInterpreterAndInvalidate();
204208
throw InvalidArrayIndexException.create(index);
205209
}
206210

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/common/CExtCommonNodes.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import com.oracle.graal.python.nodes.util.CastToJavaLongLossyNode;
7070
import com.oracle.graal.python.runtime.PythonOptions;
7171
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
72+
import com.oracle.graal.python.util.OverflowException;
7273
import com.oracle.truffle.api.CompilerAsserts;
7374
import com.oracle.truffle.api.CompilerDirectives;
7475
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -269,7 +270,7 @@ static String doBytes(Object arr, @SuppressWarnings("unused") long elementSize,
269270
throw raiseNode.raise(ValueError, ErrorMessages.UNSUPPORTED_SIZE_WAS, "wchar_t", cachedElementSize);
270271
}
271272
return decode(bytes);
272-
} catch (ArithmeticException e) {
273+
} catch (OverflowException e) {
273274
throw raiseNode.raise(ValueError, ErrorMessages.ARRAY_SIZE_TOO_LARGE);
274275
} catch (CharacterCodingException e) {
275276
throw raiseNode.raise(UnicodeError, "%m", e);
@@ -324,7 +325,7 @@ private static int readElement(InteropLibrary arrLib, InteropLibrary elemLib, Ob
324325
if (elemLib.fitsInLong(elem)) {
325326
barr[j] = (byte) elemLib.asLong(elem);
326327
} else {
327-
CompilerDirectives.transferToInterpreter();
328+
CompilerDirectives.transferToInterpreterAndInvalidate();
328329
throw new IllegalElementTypeException(elem);
329330
}
330331
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/IndexNodes.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.oracle.graal.python.builtins.objects.common.IndexNodesFactory.NormalizeIndexWithoutBoundsCheckNodeGen;
4747
import com.oracle.graal.python.builtins.objects.ints.PInt;
4848
import com.oracle.graal.python.nodes.PRaiseNode;
49+
import com.oracle.graal.python.util.OverflowException;
4950
import com.oracle.truffle.api.dsl.Cached;
5051
import com.oracle.truffle.api.dsl.Cached.Shared;
5152
import com.oracle.truffle.api.dsl.GenerateUncached;
@@ -197,10 +198,10 @@ int doBool(boolean bIndex, int length, String errorMessage,
197198
return index;
198199
}
199200

200-
@Specialization(rewriteOn = ArithmeticException.class)
201+
@Specialization(rewriteOn = OverflowException.class)
201202
int doLong(long lIndex, int length, String errorMessage,
202203
@Shared("negativeIndexProfile") @Cached("createBinaryProfile()") ConditionProfile negativeIndexProfile,
203-
@Shared("boundsCheckNode") @Cached BoundsCheckNode boundsCheckNode) {
204+
@Shared("boundsCheckNode") @Cached BoundsCheckNode boundsCheckNode) throws OverflowException {
204205
int index = PInt.intValueExact(lIndex);
205206
return doInt(index, length, errorMessage, negativeIndexProfile, boundsCheckNode);
206207
}
@@ -212,7 +213,7 @@ int doLongOvf(long index, int length, String errorMessage,
212213
@Shared("raiseNode") @Cached PRaiseNode raiseNode) {
213214
try {
214215
return doLong(index, length, errorMessage, negativeIndexProfile, boundsCheckNode);
215-
} catch (ArithmeticException e) {
216+
} catch (OverflowException e) {
216217
throw raiseNode.raiseNumberTooLarge(PythonBuiltinClassType.IndexError, index);
217218
}
218219
}
@@ -243,7 +244,7 @@ int doPIntOvf(PInt index, int length, String errorMessage,
243244
abstract static class NormalizeIndexWithoutBoundsCheckNode extends NormalizeIndexCustomMessageNode {
244245

245246
@Specialization
246-
int doInt(int index, int length, @SuppressWarnings("unused") String errorMessage,
247+
static int doInt(int index, int length, @SuppressWarnings("unused") String errorMessage,
247248
@Shared("negativeIndexProfile") @Cached("createBinaryProfile()") ConditionProfile negativeIndexProfile) {
248249
int idx = index;
249250
if (negativeIndexProfile.profile(idx < 0)) {
@@ -253,38 +254,37 @@ int doInt(int index, int length, @SuppressWarnings("unused") String errorMessage
253254
}
254255

255256
@Specialization
256-
int doBool(boolean index, @SuppressWarnings("unused") int length, @SuppressWarnings("unused") String errorMessage) {
257-
int idx = PInt.intValue(index);
258-
return idx;
257+
static int doBool(boolean index, @SuppressWarnings("unused") int length, @SuppressWarnings("unused") String errorMessage) {
258+
return PInt.intValue(index);
259259
}
260260

261-
@Specialization(rewriteOn = ArithmeticException.class)
262-
int doLong(long index, int length, String errorMessage,
263-
@Shared("negativeIndexProfile") @Cached("createBinaryProfile()") ConditionProfile negativeIndexProfile) {
261+
@Specialization(rewriteOn = OverflowException.class)
262+
static int doLong(long index, int length, String errorMessage,
263+
@Shared("negativeIndexProfile") @Cached("createBinaryProfile()") ConditionProfile negativeIndexProfile) throws OverflowException {
264264
int idx = PInt.intValueExact(index);
265265
return doInt(idx, length, errorMessage, negativeIndexProfile);
266266
}
267267

268268
@Specialization(replaces = "doLong")
269-
int doLongOvf(long index, int length, String errorMessage,
269+
static int doLongOvf(long index, int length, String errorMessage,
270270
@Shared("negativeIndexProfile") @Cached("createBinaryProfile()") ConditionProfile negativeIndexProfile,
271271
@Shared("raiseNode") @Cached PRaiseNode raiseNode) {
272272
try {
273273
return doLong(index, length, errorMessage, negativeIndexProfile);
274-
} catch (ArithmeticException e) {
274+
} catch (OverflowException e) {
275275
throw raiseNode.raiseNumberTooLarge(PythonBuiltinClassType.IndexError, index);
276276
}
277277
}
278278

279279
@Specialization(rewriteOn = ArithmeticException.class)
280-
int doPInt(PInt index, int length, String errorMessage,
280+
static int doPInt(PInt index, int length, String errorMessage,
281281
@Shared("negativeIndexProfile") @Cached("createBinaryProfile()") ConditionProfile negativeIndexProfile) {
282282
int idx = index.intValueExact();
283283
return doInt(idx, length, errorMessage, negativeIndexProfile);
284284
}
285285

286286
@Specialization(replaces = "doPInt")
287-
int doPIntOvf(PInt index, int length, String errorMessage,
287+
static int doPIntOvf(PInt index, int length, String errorMessage,
288288
@Shared("negativeIndexProfile") @Cached("createBinaryProfile()") ConditionProfile negativeIndexProfile,
289289
@Shared("raiseNode") @Cached PRaiseNode raiseNode) {
290290
try {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/SequenceStorageNodes.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
import com.oracle.graal.python.runtime.sequence.storage.TupleSequenceStorage;
154154
import com.oracle.graal.python.runtime.sequence.storage.TypedSequenceStorage;
155155
import com.oracle.graal.python.util.BiFunction;
156+
import com.oracle.graal.python.util.OverflowException;
156157
import com.oracle.graal.python.util.PythonUtils;
157158
import com.oracle.graal.python.util.Supplier;
158159
import com.oracle.truffle.api.CompilerDirectives;
@@ -3759,7 +3760,7 @@ protected SequenceStorage doIt(VirtualFrame frame, Object iterator, ListStorageT
37593760
array = elements;
37603761
}
37613762
elements[i++] = bvalue;
3762-
} catch (ArithmeticException e) {
3763+
} catch (OverflowException e) {
37633764
throw new UnexpectedResultException(value);
37643765
}
37653766
} catch (PException e) {

0 commit comments

Comments
 (0)