Skip to content

Commit 0760415

Browse files
Check stack bounds in JIT optimizer
1 parent 8801c6d commit 0760415

File tree

5 files changed

+213
-191
lines changed

5 files changed

+213
-191
lines changed

Lib/test/test_generated_cases.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,7 +2116,7 @@ def test_validate_uop_unused_input(self):
21162116
output = """
21172117
case OP: {
21182118
stack_pointer += -1;
2119-
assert(WITHIN_STACK_BOUNDS());
2119+
CHECK_STACK_BOUNDS();
21202120
break;
21212121
}
21222122
"""
@@ -2133,7 +2133,7 @@ def test_validate_uop_unused_input(self):
21332133
output = """
21342134
case OP: {
21352135
stack_pointer += -1;
2136-
assert(WITHIN_STACK_BOUNDS());
2136+
CHECK_STACK_BOUNDS();
21372137
break;
21382138
}
21392139
"""
@@ -2155,7 +2155,7 @@ def test_validate_uop_unused_output(self):
21552155
foo = NULL;
21562156
stack_pointer[0] = foo;
21572157
stack_pointer += 1;
2158-
assert(WITHIN_STACK_BOUNDS());
2158+
CHECK_STACK_BOUNDS();
21592159
break;
21602160
}
21612161
"""
@@ -2173,7 +2173,7 @@ def test_validate_uop_unused_output(self):
21732173
output = """
21742174
case OP: {
21752175
stack_pointer += 1;
2176-
assert(WITHIN_STACK_BOUNDS());
2176+
CHECK_STACK_BOUNDS();
21772177
break;
21782178
}
21792179
"""

Python/optimizer_analysis.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,6 @@ incorrect_keys(PyObject *obj, uint32_t version)
144144

145145
#define CURRENT_FRAME_IS_INIT_SHIM() (ctx->frame->code == ((PyCodeObject *)&_Py_InitCleanup))
146146

147-
#define WITHIN_STACK_BOUNDS() \
148-
(CURRENT_FRAME_IS_INIT_SHIM() || (STACK_LEVEL() >= 0 && STACK_LEVEL() <= STACK_SIZE()))
149-
150-
151147
#define GETLOCAL(idx) ((ctx->frame->locals[idx]))
152148

153149
#define REPLACE_OP(INST, OP, ARG, OPERAND) \
@@ -191,6 +187,13 @@ incorrect_keys(PyObject *obj, uint32_t version)
191187
#define sym_new_truthiness _Py_uop_sym_new_truthiness
192188

193189
#define JUMP_TO_LABEL(label) goto label;
190+
#define CHECK_STACK_BOUNDS() do { \
191+
if (!CURRENT_FRAME_IS_INIT_SHIM() && (STACK_LEVEL() < 0 || STACK_LEVEL() > STACK_SIZE())) { \
192+
ctx->contradiction = true; \
193+
ctx->done = true; \
194+
break; \
195+
} \
196+
} while (0);
194197

195198
static int
196199
optimize_to_bool(

0 commit comments

Comments
 (0)