Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add stack check assertions to generated interpreter code
  • Loading branch information
markshannon committed Jun 25, 2024
commit 4d9357d2fa3522bf8bff978e150b1da9bf8db7e8
14 changes: 14 additions & 0 deletions Lib/test/test_generated_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def test_inst_one_pop(self):
value = stack_pointer[-1];
spam();
stack_pointer += -1;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}
"""
Expand All @@ -171,6 +172,7 @@ def test_inst_one_push(self):
spam();
stack_pointer[0] = res;
stack_pointer += 1;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}
"""
Expand Down Expand Up @@ -216,6 +218,7 @@ def test_binary_op(self):
spam();
stack_pointer[-2] = res;
stack_pointer += -1;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}
"""
Expand Down Expand Up @@ -337,6 +340,7 @@ def test_error_if_pop(self):
if (cond) goto pop_2_label;
stack_pointer[-2] = res;
stack_pointer += -1;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}
"""
Expand All @@ -360,6 +364,7 @@ def test_cache_effect(self):
uint32_t extra = read_u32(&this_instr[2].cache);
(void)extra;
stack_pointer += -1;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}
"""
Expand Down Expand Up @@ -425,6 +430,7 @@ def test_macro_instruction(self):
}
stack_pointer[-3] = res;
stack_pointer += -2;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}

Expand Down Expand Up @@ -459,6 +465,7 @@ def test_macro_instruction(self):
res = op3(arg2, left, right);
stack_pointer[-3] = res;
stack_pointer += -2;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}
"""
Expand Down Expand Up @@ -540,6 +547,7 @@ def test_array_input(self):
below = stack_pointer[-2 - oparg*2];
spam();
stack_pointer += -2 - oparg*2;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}
"""
Expand All @@ -564,6 +572,7 @@ def test_array_output(self):
stack_pointer[-2] = below;
stack_pointer[-1 + oparg*3] = above;
stack_pointer += oparg*3;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}
"""
Expand All @@ -586,6 +595,7 @@ def test_array_input_output(self):
spam(values, oparg);
stack_pointer[0] = above;
stack_pointer += 1;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}
"""
Expand All @@ -608,6 +618,7 @@ def test_array_error_if(self):
extra = stack_pointer[-1 - oparg];
if (oparg == 0) { stack_pointer += -1 - oparg; goto somewhere; }
stack_pointer += -1 - oparg;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}
"""
Expand Down Expand Up @@ -638,6 +649,7 @@ def test_cond_effect(self):
if (oparg & 2) stack_pointer[-1 - (((oparg & 1) == 1) ? 1 : 0)] = output;
stack_pointer[-1 - (((oparg & 1) == 1) ? 1 : 0) + ((oparg & 2) ? 1 : 0)] = zz;
stack_pointer += -(((oparg & 1) == 1) ? 1 : 0) + ((oparg & 2) ? 1 : 0);
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}
"""
Expand Down Expand Up @@ -679,6 +691,7 @@ def test_macro_cond_effect(self):
if (oparg) stack_pointer[-2] = extra;
stack_pointer[-2 + ((oparg) ? 1 : 0)] = res;
stack_pointer += -1 + ((oparg) ? 1 : 0);
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}
"""
Expand Down Expand Up @@ -712,6 +725,7 @@ def test_macro_push_push(self):
stack_pointer[0] = val1;
stack_pointer[1] = val2;
stack_pointer += 2;
assert(WITHIN_STACK_BOUNDS());
DISPATCH();
}
"""
Expand Down
2 changes: 2 additions & 0 deletions Python/ceval_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ GETITEM(PyObject *v, Py_ssize_t i) {
#define STACK_SHRINK(n) BASIC_STACKADJ(-(n))
#endif

#define WITHIN_STACK_BOUNDS() \
Comment thread
markshannon marked this conversation as resolved.
Outdated
(frame == &entry_frame || (STACK_LEVEL() >= 0 && STACK_LEVEL() <= STACK_SIZE()))

/* Data access macros */
#define FRAME_CO_CONSTS (_PyFrame_GetCode(frame)->co_consts)
Expand Down
Loading