mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 22:48:50 +00:00
test(semantic): add negative controls, syntax rejection, and mutation detection for ds_add_rtn_u32
This commit is contained in:
@ -1100,6 +1100,219 @@ class TestIEEE754Boundaries(unittest.TestCase):
|
||||
self.assertEqual(state.lds.read_dword(addr), MIN_SUBNORMAL_F32 + 2)
|
||||
|
||||
|
||||
class TestNegativeControlsAndMutations(unittest.TestCase):
|
||||
"""Phase 8: Unit tests for negative controls, syntax rejection, and mutation detection."""
|
||||
|
||||
def test_syntax_rejection_registers(self) -> None:
|
||||
with self.assertRaises(ValueError):
|
||||
RegisterOperand(-1, RegType.VGPR)
|
||||
with self.assertRaises(ValueError):
|
||||
RegisterOperand(256, RegType.VGPR)
|
||||
with self.assertRaises(ValueError):
|
||||
RegisterOperand(106, RegType.SGPR)
|
||||
|
||||
def test_syntax_rejection_instruction_operands(self) -> None:
|
||||
v0 = RegisterOperand(0, RegType.VGPR)
|
||||
s0 = RegisterOperand(0, RegType.SGPR)
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
DSAddRtnInstruction(vdst=s0, vaddr=v0, vdata0=v0)
|
||||
with self.assertRaises(ValueError):
|
||||
DSAddRtnInstruction(vdst=v0, vaddr=s0, vdata0=v0)
|
||||
with self.assertRaises(ValueError):
|
||||
DSAddRtnInstruction(vdst=v0, vaddr=v0, vdata0=v0, offset=-1)
|
||||
with self.assertRaises(ValueError):
|
||||
DSAddRtnInstruction(vdst=v0, vaddr=v0, vdata0=v0, offset=65536)
|
||||
|
||||
def test_syntax_rejection_memory_and_simd_state(self) -> None:
|
||||
with self.assertRaises(ValueError):
|
||||
LDSMemory(size_dwords=0)
|
||||
with self.assertRaises(ValueError):
|
||||
LDSMemory(size_dwords=-16)
|
||||
with self.assertRaises(ValueError):
|
||||
LDSMemory(size_dwords=1000) # Non-power-of-2
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
SIMDState(num_lanes=16)
|
||||
with self.assertRaises(ValueError):
|
||||
SIMDState(num_lanes=128)
|
||||
|
||||
def test_negative_control_bit_perturbation(self) -> None:
|
||||
old_val, new_mem = ref_ds_add_rtn_u32(100, 200)
|
||||
self.assertEqual(old_val, 100)
|
||||
self.assertEqual(new_mem, 300)
|
||||
|
||||
# Perturbed bits must be rejected
|
||||
perturbed_old = old_val ^ 1
|
||||
perturbed_new = new_mem ^ 1
|
||||
self.assertNotEqual(old_val, perturbed_old)
|
||||
self.assertNotEqual(new_mem, perturbed_new)
|
||||
|
||||
def test_mutation_detection_add_to_sub(self) -> None:
|
||||
def mutant_add(mem: int, val: int) -> Tuple[int, int]:
|
||||
# Mutant: subtraction instead of addition
|
||||
return u32(mem), u32(u32(mem) - u32(val))
|
||||
|
||||
correct_old, correct_new = ref_ds_add_rtn_u32(100, 40)
|
||||
mutant_old, mutant_new = mutant_add(100, 40)
|
||||
|
||||
self.assertEqual(correct_new, 140)
|
||||
self.assertEqual(mutant_new, 60)
|
||||
self.assertNotEqual(correct_new, mutant_new)
|
||||
|
||||
def test_mutation_detection_missing_32bit_wrap(self) -> None:
|
||||
def mutant_no_wrap(mem: int, val: int) -> Tuple[int, int]:
|
||||
# Mutant: does not wrap modulo 2^32
|
||||
return mem, mem + val
|
||||
|
||||
correct_old, correct_new = ref_ds_add_rtn_u32(MASK32, 1)
|
||||
mutant_old, mutant_new = mutant_no_wrap(MASK32, 1)
|
||||
|
||||
self.assertEqual(correct_new, 0)
|
||||
self.assertEqual(mutant_new, 0x100000000)
|
||||
self.assertNotEqual(correct_new, mutant_new)
|
||||
|
||||
def test_mutation_detection_return_new_instead_of_old(self) -> None:
|
||||
def mutant_return_post_val(mem: int, val: int) -> Tuple[int, int]:
|
||||
# Mutant: returns new_mem instead of old_val
|
||||
new_val = u32(u32(mem) + u32(val))
|
||||
return new_val, new_val
|
||||
|
||||
correct_old, correct_new = ref_ds_add_rtn_u32(50, 25)
|
||||
mutant_old, mutant_new = mutant_return_post_val(50, 25)
|
||||
|
||||
self.assertEqual(correct_old, 50)
|
||||
self.assertEqual(mutant_old, 75)
|
||||
self.assertNotEqual(correct_old, mutant_old)
|
||||
|
||||
def test_mutation_detection_dropped_immediate_offset(self) -> None:
|
||||
def mutant_eff_addr(vaddr: int, offset: int = 0) -> int:
|
||||
# Mutant: drops immediate offset
|
||||
return vaddr & MASK32
|
||||
|
||||
base = 0x200
|
||||
offset = 0x18
|
||||
correct = calc_lds_effective_address(base, offset)
|
||||
mutant = mutant_eff_addr(base, offset)
|
||||
|
||||
self.assertEqual(correct, 0x218)
|
||||
self.assertEqual(mutant, 0x200)
|
||||
self.assertNotEqual(correct, mutant)
|
||||
|
||||
def test_mutation_detection_simd_exec_leak(self) -> None:
|
||||
# Mutant: SIMD interpreter executes inactive lanes
|
||||
class MutantSIMDInterpreter(SIMDInterpreter):
|
||||
def execute(self, instr: DSAddRtnInstruction, state: SIMDState) -> List[Optional[int]]:
|
||||
results: List[Optional[int]] = [None] * state.num_lanes
|
||||
for lane in range(state.num_lanes):
|
||||
# Bug: ignores is_lane_active
|
||||
vaddr = state.get_lane_vgpr(lane, instr.vaddr.index)
|
||||
addend = state.resolve_lane_operand(instr.vdata0, lane)
|
||||
eff_addr = calc_lds_effective_address(vaddr, instr.offset)
|
||||
dword_idx = calc_lds_dword_index(eff_addr, state.lds.size_dwords)
|
||||
curr_val = state.lds.memory[dword_idx]
|
||||
old_val, new_val = ref_ds_add_rtn_u32(curr_val, addend)
|
||||
state.lds.memory[dword_idx] = new_val
|
||||
results[lane] = old_val
|
||||
state.set_lane_vgpr(lane, instr.vdst.index, old_val)
|
||||
return results
|
||||
|
||||
state = SIMDState(num_lanes=32, exec_mask=0, lds_size_dwords=1024) # All inactive
|
||||
sentinel = 0x5555
|
||||
for lane in range(32):
|
||||
state.set_lane_vgpr(lane, 0, sentinel)
|
||||
state.set_lane_vgpr(lane, 1, 0x40)
|
||||
state.set_lane_vgpr(lane, 2, 1)
|
||||
|
||||
state.lds.write_dword(0x40, 100)
|
||||
instr = DSAddRtnInstruction(
|
||||
vdst=RegisterOperand(0, RegType.VGPR),
|
||||
vaddr=RegisterOperand(1, RegType.VGPR),
|
||||
vdata0=RegisterOperand(2, RegType.VGPR),
|
||||
)
|
||||
|
||||
mutant = MutantSIMDInterpreter()
|
||||
mutant.execute(instr, state)
|
||||
|
||||
# Mutation caught: inactive lanes were modified
|
||||
corrupted = [l for l in range(32) if state.get_lane_vgpr(l, 0) != sentinel]
|
||||
self.assertEqual(len(corrupted), 32)
|
||||
self.assertNotEqual(state.lds.read_dword(0x40), 100)
|
||||
|
||||
def test_mutation_detection_premature_writeback_aliasing(self) -> None:
|
||||
# Mutant: overwrites destination register before reading address/operands
|
||||
class PrematureWritebackInterpreter(ScalarInterpreter):
|
||||
def execute(self, instr: DSAddRtnInstruction, state: ScalarState) -> Tuple[int, int]:
|
||||
# Bug: sets vdst before reading vaddr
|
||||
state.set_reg(instr.vdst, 0xDEADBEEF)
|
||||
vaddr = state.get_reg(instr.vaddr) # Reads corrupted address 0xDEADBEEF!
|
||||
addend = state.resolve_operand(instr.vdata0)
|
||||
old_val, new_mem = state.lds.atomic_add_rtn_u32(vaddr=vaddr, vdata0=addend, offset=instr.offset)
|
||||
return old_val, new_mem
|
||||
|
||||
state = ScalarState(lds_size_dwords=1024)
|
||||
addr = 0x50
|
||||
state.set_vgpr(0, addr) # v0 is both vdst and vaddr
|
||||
state.set_vgpr(1, 10)
|
||||
state.lds.write_dword(addr, 500)
|
||||
|
||||
instr = DSAddRtnInstruction(
|
||||
vdst=RegisterOperand(0, RegType.VGPR),
|
||||
vaddr=RegisterOperand(0, RegType.VGPR),
|
||||
vdata0=RegisterOperand(1, RegType.VGPR),
|
||||
)
|
||||
|
||||
buggy = PrematureWritebackInterpreter()
|
||||
old_val, _ = buggy.execute(instr, state)
|
||||
# Buggy interpreter read corrupted address, returning 0 instead of 500
|
||||
self.assertNotEqual(old_val, 500)
|
||||
|
||||
def test_mutation_detection_reversed_collision_order(self) -> None:
|
||||
# Mutant: serializes colliding lanes in reverse order
|
||||
class ReversedCollisionSIMDInterpreter(SIMDInterpreter):
|
||||
def execute(self, instr: DSAddRtnInstruction, state: SIMDState) -> List[Optional[int]]:
|
||||
results: List[Optional[int]] = [None] * state.num_lanes
|
||||
# Bug: iterates in reversed lane order
|
||||
for lane in reversed(range(state.num_lanes)):
|
||||
if not state.is_lane_active(lane):
|
||||
continue
|
||||
vaddr = state.get_lane_vgpr(lane, instr.vaddr.index)
|
||||
addend = state.resolve_lane_operand(instr.vdata0, lane)
|
||||
eff_addr = calc_lds_effective_address(vaddr, instr.offset)
|
||||
dword_idx = calc_lds_dword_index(eff_addr, state.lds.size_dwords)
|
||||
curr_val = state.lds.memory[dword_idx]
|
||||
old_val, new_val = ref_ds_add_rtn_u32(curr_val, addend)
|
||||
state.lds.memory[dword_idx] = new_val
|
||||
results[lane] = old_val
|
||||
state.set_lane_vgpr(lane, instr.vdst.index, old_val)
|
||||
return results
|
||||
|
||||
state = SIMDState(num_lanes=32, exec_mask=0x3, lds_size_dwords=1024) # Lanes 0 and 1 active
|
||||
common_addr = 0x100
|
||||
state.lds.write_dword(common_addr, 0)
|
||||
|
||||
# Lane 0 adds 10, Lane 1 adds 20
|
||||
state.set_lane_vgpr(0, 1, common_addr)
|
||||
state.set_lane_vgpr(0, 2, 10)
|
||||
state.set_lane_vgpr(1, 1, common_addr)
|
||||
state.set_lane_vgpr(1, 2, 20)
|
||||
|
||||
instr = DSAddRtnInstruction(
|
||||
vdst=RegisterOperand(0, RegType.VGPR),
|
||||
vaddr=RegisterOperand(1, RegType.VGPR),
|
||||
vdata0=RegisterOperand(2, RegType.VGPR),
|
||||
)
|
||||
|
||||
buggy = ReversedCollisionSIMDInterpreter()
|
||||
results = buggy.execute(instr, state)
|
||||
|
||||
# In reverse order, Lane 1 executes first and sees 0, Lane 0 executes second and sees 20
|
||||
# In correct ascending order: Lane 0 sees 0, Lane 1 sees 10!
|
||||
self.assertEqual(results[1], 0) # Mutation: Lane 1 got 0
|
||||
self.assertEqual(results[0], 20) # Mutation: Lane 0 got 20
|
||||
self.assertNotEqual(results[0], 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -1107,3 +1320,4 @@ if __name__ == "__main__":
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user