Skip to content
Merged
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
Prev Previous commit
Next Next commit
add sanity check for opcode metadata
  • Loading branch information
iritkatriel committed Jan 4, 2023
commit 404053b44578b10471e67093760cb056884e4d88
22 changes: 22 additions & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -8670,6 +8670,27 @@ no_redundant_jumps(cfg_builder *g) {
return true;
}

static bool
opcode_metadata_is_sane(cfg_builder *g) {
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
for (int i = 0; i < b->b_iused; i++) {
struct instr *instr = &b->b_instr[i];
int opcode = instr->i_opcode;
assert(opcode <= MAX_REAL_OPCODE);
int pushed = _PyOpcode_opcode_metadata[opcode].n_pushed;
int popped = _PyOpcode_opcode_metadata[opcode].n_popped;
assert((pushed < 0) == (popped < 0));
if (pushed >= 0) {
int effect = stack_effect(opcode, instr->i_oparg, -1);
if (effect != pushed - popped) {
return false;
}
}
}
}
return true;
}

static bool
no_empty_basic_blocks(cfg_builder *g) {
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
Expand Down Expand Up @@ -8853,6 +8874,7 @@ assemble(struct compiler *c, int addNone)
}

assert(no_redundant_jumps(g));
assert(opcode_metadata_is_sane(g));

/* Can't modify the bytecode after computing jump offsets. */
assemble_jump_offsets(g->g_entryblock);
Expand Down