Skip to content

Commit 9dc2959

Browse files
committed
Phase B5 prep: Add static_assert guards for struct size invariants
Add compile-time checks before OperandBase devirtualization: - sizeof(OperandBase) == sizeof(Operand) == sizeof(LinkedOperand) (prerequisite for safe deletion through base pointer) - sizeof(LirBasicBlock) == sizeof(BasicBlock) - sizeof(LirFunction) == sizeof(Function) (cross-validate C and C++ struct layouts per Pythia python#8)
1 parent 2242ebc commit 9dc2959

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Python/jit/lir/lir_c_api.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,22 @@
1919
#include "cinderx/Jit/lir/instruction.h"
2020
#include "cinderx/Jit/lir/operand.h"
2121

22+
// Phase B5: Include lir_types_c.h for cross-struct size validation.
23+
#include "cinderx/Jit/lir/lir_types_c.h"
24+
2225
using jit::codegen::CodeSection;
2326
using jit::lir::BasicBlock;
2427
using jit::lir::Function;
2528
using jit::lir::Instruction;
2629
using jit::lir::OperandBase;
2730

31+
// Phase B5: Cross-validate C struct sizes against C++ struct sizes.
32+
// These fire at compile time if the layouts diverge.
33+
static_assert(sizeof(LirBasicBlock) == sizeof(BasicBlock),
34+
"LirBasicBlock and BasicBlock size mismatch");
35+
static_assert(sizeof(LirFunction) == sizeof(Function),
36+
"LirFunction and Function size mismatch");
37+
2838
/* ---- Function accessors ---- */
2939

3040
extern "C" size_t

Python/jit/lir/operand.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
namespace jit::lir {
99

10+
// Phase B5: Verify that Operand/LinkedOperand add no data members beyond
11+
// OperandBase. This is a prerequisite for safe deletion through OperandBase*
12+
// after vtable removal.
13+
static_assert(sizeof(OperandBase) == sizeof(Operand),
14+
"Operand must not add data members beyond OperandBase");
15+
static_assert(sizeof(OperandBase) == sizeof(LinkedOperand),
16+
"LinkedOperand must not add data members beyond OperandBase");
17+
1018
OperandBase::OperandBase(Instruction* parent) : parent_instr_{parent} {}
1119

1220
OperandBase::OperandBase(const OperandBase& ob)

0 commit comments

Comments
 (0)