Skip to content

Commit 8f5a017

Browse files
authored
GH-120982: Add stack check assertions to generated interpreter code (GH-120992)
1 parent 42b2c9d commit 8f5a017

File tree

8 files changed

+448
-0
lines changed

8 files changed

+448
-0
lines changed

Lib/test/test_generated_cases.py

+14
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ def test_inst_one_pop(self):
151151
value = stack_pointer[-1];
152152
spam();
153153
stack_pointer += -1;
154+
assert(WITHIN_STACK_BOUNDS());
154155
DISPATCH();
155156
}
156157
"""
@@ -171,6 +172,7 @@ def test_inst_one_push(self):
171172
spam();
172173
stack_pointer[0] = res;
173174
stack_pointer += 1;
175+
assert(WITHIN_STACK_BOUNDS());
174176
DISPATCH();
175177
}
176178
"""
@@ -216,6 +218,7 @@ def test_binary_op(self):
216218
spam();
217219
stack_pointer[-2] = res;
218220
stack_pointer += -1;
221+
assert(WITHIN_STACK_BOUNDS());
219222
DISPATCH();
220223
}
221224
"""
@@ -337,6 +340,7 @@ def test_error_if_pop(self):
337340
if (cond) goto pop_2_label;
338341
stack_pointer[-2] = res;
339342
stack_pointer += -1;
343+
assert(WITHIN_STACK_BOUNDS());
340344
DISPATCH();
341345
}
342346
"""
@@ -360,6 +364,7 @@ def test_cache_effect(self):
360364
uint32_t extra = read_u32(&this_instr[2].cache);
361365
(void)extra;
362366
stack_pointer += -1;
367+
assert(WITHIN_STACK_BOUNDS());
363368
DISPATCH();
364369
}
365370
"""
@@ -425,6 +430,7 @@ def test_macro_instruction(self):
425430
}
426431
stack_pointer[-3] = res;
427432
stack_pointer += -2;
433+
assert(WITHIN_STACK_BOUNDS());
428434
DISPATCH();
429435
}
430436
@@ -459,6 +465,7 @@ def test_macro_instruction(self):
459465
res = op3(arg2, left, right);
460466
stack_pointer[-3] = res;
461467
stack_pointer += -2;
468+
assert(WITHIN_STACK_BOUNDS());
462469
DISPATCH();
463470
}
464471
"""
@@ -540,6 +547,7 @@ def test_array_input(self):
540547
below = stack_pointer[-2 - oparg*2];
541548
spam();
542549
stack_pointer += -2 - oparg*2;
550+
assert(WITHIN_STACK_BOUNDS());
543551
DISPATCH();
544552
}
545553
"""
@@ -564,6 +572,7 @@ def test_array_output(self):
564572
stack_pointer[-2] = below;
565573
stack_pointer[-1 + oparg*3] = above;
566574
stack_pointer += oparg*3;
575+
assert(WITHIN_STACK_BOUNDS());
567576
DISPATCH();
568577
}
569578
"""
@@ -586,6 +595,7 @@ def test_array_input_output(self):
586595
spam(values, oparg);
587596
stack_pointer[0] = above;
588597
stack_pointer += 1;
598+
assert(WITHIN_STACK_BOUNDS());
589599
DISPATCH();
590600
}
591601
"""
@@ -608,6 +618,7 @@ def test_array_error_if(self):
608618
extra = stack_pointer[-1 - oparg];
609619
if (oparg == 0) { stack_pointer += -1 - oparg; goto somewhere; }
610620
stack_pointer += -1 - oparg;
621+
assert(WITHIN_STACK_BOUNDS());
611622
DISPATCH();
612623
}
613624
"""
@@ -638,6 +649,7 @@ def test_cond_effect(self):
638649
if (oparg & 2) stack_pointer[-1 - (((oparg & 1) == 1) ? 1 : 0)] = output;
639650
stack_pointer[-1 - (((oparg & 1) == 1) ? 1 : 0) + ((oparg & 2) ? 1 : 0)] = zz;
640651
stack_pointer += -(((oparg & 1) == 1) ? 1 : 0) + ((oparg & 2) ? 1 : 0);
652+
assert(WITHIN_STACK_BOUNDS());
641653
DISPATCH();
642654
}
643655
"""
@@ -679,6 +691,7 @@ def test_macro_cond_effect(self):
679691
if (oparg) stack_pointer[-2] = extra;
680692
stack_pointer[-2 + ((oparg) ? 1 : 0)] = res;
681693
stack_pointer += -1 + ((oparg) ? 1 : 0);
694+
assert(WITHIN_STACK_BOUNDS());
682695
DISPATCH();
683696
}
684697
"""
@@ -712,6 +725,7 @@ def test_macro_push_push(self):
712725
stack_pointer[0] = val1;
713726
stack_pointer[1] = val2;
714727
stack_pointer += 2;
728+
assert(WITHIN_STACK_BOUNDS());
715729
DISPATCH();
716730
}
717731
"""

Python/ceval_macros.h

+2
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ GETITEM(PyObject *v, Py_ssize_t i) {
246246
#define STACK_SHRINK(n) BASIC_STACKADJ(-(n))
247247
#endif
248248

249+
#define WITHIN_STACK_BOUNDS() \
250+
(frame == &entry_frame || (STACK_LEVEL() >= 0 && STACK_LEVEL() <= STACK_SIZE()))
249251

250252
/* Data access macros */
251253
#define FRAME_CO_CONSTS (_PyFrame_GetCode(frame)->co_consts)

0 commit comments

Comments
 (0)