LLVM: fix missing alignment on wrapping instructions

Previously, when lowering AIR instructions `wrap_errunion_payload`,
`wrap_errunion_err`, and `wrap_optional`, the LLVM backend would create
an alloca instruction to store the result, but did not set the alignment
on it. This caused UB which went undetected for a long time until we
started enabling the stack protector.

Closes #12594
Unblocks #12508
Inspires #12634

Tests passed locally:
 * test-behavior
 * test-cases
This commit is contained in:
Andrew Kelley
2022-08-25 16:10:36 -07:00
parent 9d231c4991
commit d2ad8afff4
3 changed files with 39 additions and 3 deletions

View File

@@ -412,3 +412,19 @@ test "orelse on C pointer" {
const d = foo orelse @compileError("bad");
try expectEqual([*c]const u8, @TypeOf(d));
}
test "alignment of wrapping an optional payload" {
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
const S = struct {
const I = extern struct { x: i128 };
fn foo() ?I {
var i: I = .{ .x = 1234 };
return i;
}
};
try expect(S.foo().?.x == 1234);
}