Skip to content

Commit 493a36f

Browse files
committed
Fix: sys._getframe must never return Java null.
1 parent 2d367a0 commit 493a36f

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
import com.oracle.truffle.api.dsl.TypeSystemReference;
9595
import com.oracle.truffle.api.frame.VirtualFrame;
9696
import com.oracle.truffle.api.nodes.LanguageInfo;
97+
import com.oracle.truffle.api.profiles.ConditionProfile;
9798
import com.oracle.truffle.llvm.api.Toolchain;
9899

99100
@CoreFunctions(defineModule = "sys")
@@ -350,42 +351,54 @@ public abstract static class GetFrameNode extends PythonBuiltinNode {
350351
@Specialization
351352
PFrame first(VirtualFrame frame, @SuppressWarnings("unused") PNone arg,
352353
@Shared("caller") @Cached ReadCallerFrameNode readCallerNode) {
353-
return escapeFrame(frame, 0, readCallerNode);
354+
PFrame requested = escapeFrame(frame, 0, readCallerNode);
355+
// there must always be *the current frame*
356+
assert requested != null : "frame must not be null";
357+
return requested;
354358
}
355359

356360
@Specialization
357361
PFrame counted(VirtualFrame frame, int num,
358-
@Shared("caller") @Cached ReadCallerFrameNode readCallerNode) {
359-
return escapeFrame(frame, num, readCallerNode);
362+
@Shared("caller") @Cached ReadCallerFrameNode readCallerNode,
363+
@Shared("callStackDepthProfile") @Cached("createBinaryProfile()") ConditionProfile callStackDepthProfile) {
364+
PFrame requested = escapeFrame(frame, num, readCallerNode);
365+
if (callStackDepthProfile.profile(requested == null)) {
366+
throw raiseCallStackDepth();
367+
}
368+
return requested;
360369
}
361370

362371
@Specialization(rewriteOn = ArithmeticException.class)
363372
PFrame countedLong(VirtualFrame frame, long num,
364-
@Shared("caller") @Cached ReadCallerFrameNode readCallerNode) {
365-
return counted(frame, PInt.intValueExact(num), readCallerNode);
373+
@Shared("caller") @Cached ReadCallerFrameNode readCallerNode,
374+
@Shared("callStackDepthProfile") @Cached("createBinaryProfile()") ConditionProfile callStackDepthProfile) {
375+
return counted(frame, PInt.intValueExact(num), readCallerNode, callStackDepthProfile);
366376
}
367377

368378
@Specialization
369379
PFrame countedLongOvf(VirtualFrame frame, long num,
370-
@Shared("caller") @Cached ReadCallerFrameNode readCallerNode) {
380+
@Shared("caller") @Cached ReadCallerFrameNode readCallerNode,
381+
@Shared("callStackDepthProfile") @Cached("createBinaryProfile()") ConditionProfile callStackDepthProfile) {
371382
try {
372-
return counted(frame, PInt.intValueExact(num), readCallerNode);
383+
return counted(frame, PInt.intValueExact(num), readCallerNode, callStackDepthProfile);
373384
} catch (ArithmeticException e) {
374385
throw raiseCallStackDepth();
375386
}
376387
}
377388

378389
@Specialization(rewriteOn = ArithmeticException.class)
379390
PFrame countedPInt(VirtualFrame frame, PInt num,
380-
@Shared("caller") @Cached ReadCallerFrameNode readCallerNode) {
381-
return counted(frame, num.intValueExact(), readCallerNode);
391+
@Shared("caller") @Cached ReadCallerFrameNode readCallerNode,
392+
@Shared("callStackDepthProfile") @Cached("createBinaryProfile()") ConditionProfile callStackDepthProfile) {
393+
return counted(frame, num.intValueExact(), readCallerNode, callStackDepthProfile);
382394
}
383395

384396
@Specialization
385397
PFrame countedPIntOvf(VirtualFrame frame, PInt num,
386-
@Shared("caller") @Cached ReadCallerFrameNode readCallerNode) {
398+
@Shared("caller") @Cached ReadCallerFrameNode readCallerNode,
399+
@Shared("callStackDepthProfile") @Cached("createBinaryProfile()") ConditionProfile callStackDepthProfile) {
387400
try {
388-
return counted(frame, num.intValueExact(), readCallerNode);
401+
return counted(frame, num.intValueExact(), readCallerNode, callStackDepthProfile);
389402
} catch (ArithmeticException e) {
390403
throw raiseCallStackDepth();
391404
}

0 commit comments

Comments
 (0)