Skip to content
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

Stage2 specific union segfaults test runner when used in error union or optional #11857

Closed
Tracked by #11899 ...
Vexu opened this issue Jun 13, 2022 · 2 comments · Fixed by #11966
Closed
Tracked by #11899 ...

Stage2 specific union segfaults test runner when used in error union or optional #11857

Vexu opened this issue Jun 13, 2022 · 2 comments · Fixed by #11966
Assignees
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. 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

@Vexu
Copy link
Member

Vexu commented Jun 13, 2022

test {
    const U = extern union {
        in6: extern struct {
            family: u16,
            port: u16,
            flowinfo: u32,
            addr: [20]u8,
        },
        un: [110]u8,

        fn foo() !@This() {
            return undefined;
        }
    };
    var a: ?U = .{ .un = [_]u8{0} ** 110 };
    _ = a.?;
}
@Vexu Vexu added frontend Tokenization, parsing, AstGen, Sema, and Liveness. miscompilation The compiler reports success but produces semantically incorrect code. backend-llvm The LLVM backend outputs an LLVM IR Module. labels Jun 13, 2022
@Vexu Vexu added this to the 0.10.0 milestone Jun 13, 2022
@Vexu Vexu added the bug Observed behavior contradicts documented or intended behavior label Jun 20, 2022
@Vexu Vexu mentioned this issue Jun 20, 2022
7 tasks
@andrewrk andrewrk self-assigned this Jun 30, 2022
@andrewrk
Copy link
Member

andrewrk commented Jun 30, 2022

The AIR looks good:

  %10 = optional_payload_ptr_set(*test2.doTheTest.U, %8)
  %28!= store(%10!, %26!)

The LLVM IR reveals the store being lowered to a memcpy with 112 bytes where 110 is expected:

  call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 4 %5, i8* align 4 getelementptr inbounds ({ [110 x i8] }, { [110 x i8] }* @0, i32 0, i32 0, i32 0), i64 112, i1 false), !dbg !86

I think the next step here is figuring out why the memcpy is copying 112 bytes (overwriting the non-null-byte) instead of the expected 110 (size of the union).

@andrewrk
Copy link
Member

112 is computed based on taking 110 and then aligning it to 4 bytes (because of the u32). This is correct. It's actually the layout of the union llvm type that is missing 2 bytes of padding:

%test2.doTheTest.U = type { <{ %test2.doTheTest.U.doTheTest.U__struct_222, [82 x i8] }> }

This comes out to 110 but it should be 84 x i8, making it 112.

andrewrk added a commit that referenced this issue Jun 30, 2022
The LLVM backend was calculating the amount of padding solely based
on the payload size. However, in the case where there is no union
tag, this fails to take into account alignment.

Closes #11857
andrewrk added a commit that referenced this issue Jun 30, 2022
The LLVM backend was calculating the amount of padding solely based
on the payload size. However, in the case where there is no union
tag, this fails to take into account alignment.

Closes #11857
andrewrk added a commit that referenced this issue Jul 19, 2022
The LLVM backend was calculating the amount of padding solely based
on the payload size. However, in the case where there is no union
tag, this fails to take into account alignment.

Closes #11857
wooster0 pushed a commit to wooster0/zig that referenced this issue Jul 24, 2022
The LLVM backend was calculating the amount of padding solely based
on the payload size. However, in the case where there is no union
tag, this fails to take into account alignment.

Closes ziglang#11857
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. 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