From c62fb118e70faf531162b897d7e57df843229b72 Mon Sep 17 00:00:00 2001 From: Bingwu Zhang Date: Sun, 16 Mar 2025 08:51:53 +0800 Subject: [PATCH] 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 --- src/arch/x86_64/CodeGen.zig | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/arch/x86_64/CodeGen.zig b/src/arch/x86_64/CodeGen.zig index 2a9e25500c..0790082985 100644 --- a/src/arch/x86_64/CodeGen.zig +++ b/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 });