Skip to content

Coercion to pointer to vector segfaults at runtime rather than catching the alignment issue at comptime #16997

Description

@hmusgrave

Zig Version

0.12.0-dev.86+197d9a9eb

Steps to Reproduce and Observed Behavior

It looks like coercion involving pointer-of-vector types doesn't have the correct alignment in its calculations. Zig normally catches potentially unsafe coercion at comptime, so it's weird that this sort of code could segfault.

test "segfaults more often than not" {
    var x: [16]f32 = undefined;
    var y: *[1]@Vector(16, f32) = &x;
    for (y) |*v|
        v.* = @splat(0);
}

test "fails at comptime, as it should" {
    var x: [16]f32 = undefined;
    // cast increases pointer alignment
    var y: *[1]@Vector(16, f32) = @ptrCast(&x);
    _ = y;
}

test "also fails at comptime since the coercion doesn't involve vectors" {
    var x: u128 = 0;
    var y: *align(64) u128 = &x;
    _ = y;
}

Expected Behavior

All three of those tests should fail to compile because there's a potentially unsafe coercion from a less aligned type to a more aligned type.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions