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 llvm: pointer access with excess alignment, causing segfault #13232

Closed
topolarity opened this issue Oct 20, 2022 · 3 comments · Fixed by #13399
Closed

stage2 llvm: pointer access with excess alignment, causing segfault #13232

topolarity opened this issue Oct 20, 2022 · 3 comments · Fixed by #13399
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@topolarity
Copy link
Contributor

Zig Version

0.10.0-dev.4472+a4eb221b9

Steps to Reproduce

Build Zig and save the LLVM IR:

$ /build/zig2 build -p stage3_debug -Dstrip=true -Denable-llvm=true --verbose-llvm-ir 2>llvm_ir.txt

If you examine Autodocs.walkInstruction in the generated LLVM IR, you'll see this:

; Function Attrs: nounwind sspstrong uwtable
define internal fastcc void @Autodoc.walkInstruction(ptr noalias nonnull sret({ %Autodoc.DocData.WalkResult, i16, [14 x i8] }) %0, ptr nonnull align 8 %1, ptr nonnull align 8 %2, ptr nonnull align 8 %3, ptr nonnull readonly align 8 %4, i64 %5, i1 %6) unnamed_addr #0 {
Entry:
  ...
  %891 = alloca %Autodoc.DocData.Type, align 8
  ...
  %2849 = getelementptr inbounds { %Autodoc.DocData.Type.Type__struct_34832, i5, [15 x i8] }, ptr %891, i32 0, i32 0
  call void @llvm.memcpy.p0.p0.i64(ptr align 16 %2849, ptr align 16 %892, i64 592, i1 false)
  ...
}

Zig claims that %2849 has alignment 16, but it actually has alignment 8.

Expected Behavior

Alignment should be consistent between alloca and memcpy.

Actual Behavior

Alignments do not match.

@topolarity topolarity added bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness. labels Oct 20, 2022
@topolarity
Copy link
Contributor Author

This LLVM IR is generated from this part of Autodocs.walkInstruction, at line 1581:

const type_slot_index = self.types.items.len;
try self.types.append(self.arena, .{
    .Pointer = .{
        .size = ptr.size,
        .child = elem_type_ref.expr,
        .has_align = ptr.flags.has_align,
        .@"align" = @"align",
        .has_addrspace = ptr.flags.has_addrspace,
        .address_space = address_space,
        .has_sentinel = ptr.flags.has_sentinel,
        .sentinel = sentinel,
        .is_mutable = ptr.flags.is_mutable,
        .is_volatile = ptr.flags.is_volatile,
        .has_bit_range = ptr.flags.has_bit_range,
        .bit_start = bit_start,
        .host_size = host_size,
    },
});

@topolarity
Copy link
Contributor Author

In release modes, this memcpy sometimes gets lowered to a movaps instruction, which requires 16-byte alignment and causes a segfault otherwise.

That segfault is preventing #13074 from passing CI.

@topolarity topolarity changed the title stage2 llvm: Pointer access with excess alignment stage2 llvm: pointer access with excess alignment, causing segfault Oct 20, 2022
@Vexu Vexu added this to the 0.10.1 milestone Oct 20, 2022
@topolarity
Copy link
Contributor Author

Reduction:

const Type = union(enum) {
    Pointer: struct {
        child: u32 align(16),
    },
};

fn foo(x: Type) void {
    _ = x;
}

test {
    var child: u32 = 1;
    foo(.{ .Pointer = .{ .child = child } });
}

Generates an under-aligned alloca:

define internal fastcc i16 @test22.test_0(ptr nonnull %0) unnamed_addr #0 !dbg !4390 {
Entry:
  %1 = alloca %test22.Type, align 8
  ...

topolarity added a commit to topolarity/zig that referenced this issue Nov 1, 2022
Vexu pushed a commit that referenced this issue Nov 2, 2022
andrewrk pushed a commit that referenced this issue Nov 2, 2022
ryanschneider pushed a commit to ryanschneider/zig that referenced this issue Nov 4, 2022
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants