Skip to content

Packed structs inside packed struct not initialized properly #16609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jacobly0 opened this issue Jul 29, 2023 · 1 comment · Fixed by #16611
Closed

Packed structs inside packed struct not initialized properly #16609

jacobly0 opened this issue Jul 29, 2023 · 1 comment · Fixed by #16611
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness. miscompilation The compiler reports success but produces semantically incorrect code.
Milestone

Comments

@jacobly0
Copy link
Member

Extracted from #13480 (comment)

Not sure if this is the same issue @Vexu but I just ran into this:

const std = @import("std");

const Node = packed struct {
    const @"c/d" = packed struct(u40) {
        c: u32 = 0,
        d: u8 = 0,
    };

    const @"a/b" = packed struct(u40) {
        a: u32 = 0,
        b: u8 = 0,
    };

    @"a/b": @"a/b" = .{},
    @"c/d": @"c/d" = .{},

    pub fn init(d: u8) Node {
        return Node{
            // These fields work okay
            .@"a/b" = .{ .a = d, .b = d + 1 },
            // Writing to either of these fields will not work:
            .@"c/d" = .{ .c = d, .d = d + 1 },
        };
    }
};

pub fn main() !void {
    const node = Node.init(9);
    std.debug.print("{any}\n", .{node});
}
; Function Attrs: nounwind uwtable
define internal fastcc i80 @repro.Node.init(i8 %0) unnamed_addr #0 {
  %2 = alloca i80, align 8
  %3 = zext i8 %0 to i32
  store i32 %3, ptr %2, align 8
  %4 = getelementptr inbounds i8, ptr %2, i64 4
  %5 = call fastcc { i8, i1 } @llvm.uadd.with.overflow.i8(i8 %0, i8 1)
  %6 = extractvalue { i8, i1 } %5, 1
  br i1 %6, label %7, label %8

7:                                                ; preds = %1
  call fastcc void @repro.panic(ptr @builtin.panic_messages.integer_overflow__anon_1020, i64 16, ptr null, ptr @0)
  unreachable

8:                                                ; preds = %1
  %9 = extractvalue { i8, i1 } %5, 0
  store i8 %9, ptr %4, align 4
  %10 = zext i8 %0 to i32
  store i32 %10, ptr %2, align 1
  %11 = getelementptr inbounds i8, ptr %2, i64 4
  %12 = call fastcc { i8, i1 } @llvm.uadd.with.overflow.i8(i8 %0, i8 1)
  %13 = extractvalue { i8, i1 } %12, 1
  br i1 %13, label %14, label %15

14:                                               ; preds = %8
  call fastcc void @repro.panic(ptr @builtin.panic_messages.integer_overflow__anon_1020, i64 16, ptr null, ptr @0)
  unreachable

15:                                               ; preds = %8
  %16 = extractvalue { i8, i1 } %12, 0
  store i8 %16, ptr %11, align 1
  %17 = load i80, ptr %2, align 8
  ret i80 %17
}

Note that all stores to c/d occur to a/b instead.

@jacobly0 jacobly0 added bug Observed behavior contradicts documented or intended behavior miscompilation The compiler reports success but produces semantically incorrect code. labels Jul 29, 2023
@jacobly0 jacobly0 added the frontend Tokenization, parsing, AstGen, Sema, and Liveness. label Jul 29, 2023
@jacobly0 jacobly0 added this to the 0.11.0 milestone Jul 29, 2023
xxxbxxx added a commit to xxxbxxx/zig that referenced this issue Jul 29, 2023
When acessing a packed struct member via a byte aligned ptr (from the optimisation in Sema.structFieldPtrByIndex())
the codegen must apply the parent ptr packed_offset.

resolves ziglang#16609
xxxbxxx added a commit to xxxbxxx/zig that referenced this issue Jul 29, 2023
When acessing a packed struct member via a byte aligned ptr (from the optimisation in Sema.structFieldPtrByIndex())
the codegen must apply the parent ptr packed_offset in addition to the field offset itself.

resolves ziglang#16609
@Pyrolistical
Copy link
Contributor

Seem related to #16581

jacobly0 added a commit that referenced this issue Jul 30, 2023
codegen: fix various packed struct issues

Closes #16609
Closes #15337
QusaiHroub pushed a commit to QusaiHroub/zig that referenced this issue Aug 3, 2023
When acessing a packed struct member via a byte aligned ptr (from the optimisation in Sema.structFieldPtrByIndex())
the codegen must apply the parent ptr packed_offset in addition to the field offset itself.

resolves ziglang#16609
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness. miscompilation The compiler reports success but produces semantically incorrect code.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants