Skip to content

Commit

Permalink
add behavior test for resolved alignment issue
Browse files Browse the repository at this point in the history
std.zig.Server: remove workaround for fixed issue

closes #14904

Note the C backend is not passing the new test.
  • Loading branch information
andrewrk committed Aug 1, 2023
1 parent e84cda0 commit 88e3a26
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
17 changes: 3 additions & 14 deletions lib/std/zig/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ pub fn receiveMessage(s: *Server) !InMessage.Header {
const buf = fifo.readableSlice(0);
assert(fifo.readableLength() == buf.len);
if (buf.len >= @sizeOf(Header)) {
// workaround for https://github.com/ziglang/zig/issues/14904
const bytes_len = bswap_and_workaround_u32(buf[4..][0..4]);
const tag = bswap_and_workaround_tag(buf[0..][0..4]);
const header: *align(1) const Header = @ptrCast(buf[0..@sizeOf(Header)]);
const bytes_len = bswap(header.bytes_len);
const tag = bswap(header.tag);

if (buf.len - @sizeOf(Header) >= bytes_len) {
fifo.discard(@sizeOf(Header));
Expand Down Expand Up @@ -278,17 +278,6 @@ fn bswap_u32_array(slice: []u32) void {
for (slice) |*elem| elem.* = @byteSwap(elem.*);
}

/// workaround for https://github.com/ziglang/zig/issues/14904
fn bswap_and_workaround_u32(bytes_ptr: *const [4]u8) u32 {
return std.mem.readIntLittle(u32, bytes_ptr);
}

/// workaround for https://github.com/ziglang/zig/issues/14904
fn bswap_and_workaround_tag(bytes_ptr: *const [4]u8) InMessage.Tag {
const int = std.mem.readIntLittle(u32, bytes_ptr);
return @as(InMessage.Tag, @enumFromInt(int));
}

const OutMessage = std.zig.Server.Message;
const InMessage = std.zig.Client.Message;

Expand Down
22 changes: 22 additions & 0 deletions test/behavior/align.zig
Original file line number Diff line number Diff line change
Expand Up @@ -593,3 +593,25 @@ test "alignment of slice element" {
const a: []align(1024) const u8 = undefined;
try expect(@TypeOf(&a[0]) == *align(1024) const u8);
}

test "sub-aligned pointer field access" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;

// Originally reported at https://github.com/ziglang/zig/issues/14904

const Header = extern struct {
tag: u32,
bytes_len: u32,
};
var buf: [9]u8 align(4) = .{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
const ptr: *align(1) Header = @ptrCast(buf[1..][0..8]);
const x = ptr.bytes_len;
switch (builtin.cpu.arch.endian()) {
.Big => try expect(x == 0x06070809),
.Little => try expect(x == 0x09080706),
}
}

0 comments on commit 88e3a26

Please sign in to comment.