Commit c62fb118e7

Bingwu Zhang <xtex@aosc.io>
2025-03-16 01:51:53
x86_64: fix packedStore miscomp by spilling EFLAGS
Fixes #20113 and #20581. AND instructions in packedStore clobbers EFLAGS. Bug: https://github.com/ziglang/zig/issues/20113 Bug: https://github.com/ziglang/zig/issues/20581 Signed-off-by: Bingwu Zhang <xtex@aosc.io>
1 parent 9c9d393
Changed files (1)
src
arch
src/arch/x86_64/CodeGen.zig
@@ -88178,12 +88178,15 @@ fn airStore(self: *CodeGen, inst: Air.Inst.Index, safety: bool) !void {
         const reg_locks = self.register_manager.lockRegsAssumeUnused(3, .{ .rdi, .rsi, .rcx });
         defer for (reg_locks) |lock| self.register_manager.unlockReg(lock);
 
+        const ptr_ty = self.typeOf(bin_op.lhs);
+        const ptr_info = ptr_ty.ptrInfo(zcu);
+        const is_packed = ptr_info.flags.vector_index != .none or ptr_info.packed_offset.host_size > 0;
+        if (is_packed) try self.spillEflagsIfOccupied();
+
         const src_mcv = try self.resolveInst(bin_op.rhs);
         const ptr_mcv = try self.resolveInst(bin_op.lhs);
-        const ptr_ty = self.typeOf(bin_op.lhs);
 
-        const ptr_info = ptr_ty.ptrInfo(zcu);
-        if (ptr_info.flags.vector_index != .none or ptr_info.packed_offset.host_size > 0) {
+        if (is_packed) {
             try self.packedStore(ptr_ty, ptr_mcv, src_mcv);
         } else {
             try self.store(ptr_ty, ptr_mcv, src_mcv, .{ .safety = safety });