Skip to content

Commit df0db0d

Browse files
committed
cbe: fix some miscompilations
The change to `codegen.c` is a blatant hack, but the thing it's working around isn't a regression: it's an existing miscompilation. This branch happened to *expose* that miscompilation in a behavior test by changing how an incorrect result is *used*.
1 parent 249d893 commit df0db0d

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

src/Type.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3556,7 +3556,7 @@ pub fn packedStructFieldPtrInfo(
35563556
} else .{
35573557
switch (zcu.comp.getZigBackend()) {
35583558
else => (running_bits + 7) / 8,
3559-
.stage2_x86_64 => @intCast(struct_ty.abiSize(zcu)),
3559+
.stage2_x86_64, .stage2_c => @intCast(struct_ty.abiSize(zcu)),
35603560
},
35613561
bit_offset,
35623562
};

src/codegen/c.zig

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4098,9 +4098,24 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
40984098
if (val_is_undef) {
40994099
try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs });
41004100
if (safety and ptr_info.packed_offset.host_size == 0) {
4101+
// If the thing we're initializing is a packed struct/union, we set to 0 instead of
4102+
// 0xAA. This is a hack to work around a problem with partially-undefined packed
4103+
// aggregates. If we used 0xAA here, then a later initialization through RLS would
4104+
// not zero the high padding bits (for a packed type which is not 8/16/32/64/etc bits),
4105+
// so we would get a miscompilation. Using 0x00 here avoids this bug in some cases. It
4106+
// is *not* a correct fix; for instance it misses any case where packed structs are
4107+
// nested in other aggregates. A proper fix for this will involve changing the language,
4108+
// such as to remove RLS. This just prevents miscompilations in *some* common cases.
4109+
const byte_str: []const u8 = switch (src_ty.zigTypeTag(zcu)) {
4110+
else => "0xaa",
4111+
.@"struct", .@"union" => switch (src_ty.containerLayout(zcu)) {
4112+
.auto, .@"extern" => "0xaa",
4113+
.@"packed" => "0x00",
4114+
},
4115+
};
41014116
try w.writeAll("memset(");
41024117
try f.writeCValue(w, ptr_val, .FunctionArgument);
4103-
try w.writeAll(", 0xaa, sizeof(");
4118+
try w.print(", {s}, sizeof(", .{byte_str});
41044119
try f.renderType(w, .fromInterned(ptr_info.child));
41054120
try w.writeAll("));");
41064121
try f.object.newline();

test/behavior/union.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1547,7 +1547,7 @@ test "packed union field pointer has correct alignment" {
15471547

15481548
const host_size = switch (builtin.zig_backend) {
15491549
else => comptime std.math.divCeil(comptime_int, @bitSizeOf(S), 8) catch unreachable,
1550-
.stage2_x86_64 => @sizeOf(S),
1550+
.stage2_x86_64, .stage2_c => @sizeOf(S),
15511551
};
15521552
comptime assert(@TypeOf(ap) == *align(4:2:host_size) u20);
15531553
comptime assert(@TypeOf(bp) == *align(1:2:host_size) u20);

0 commit comments

Comments
 (0)