Skip to content

undefined parameter not set to 0xaa bytes in the llvm backend #24100

Description

@andrewrk

Zig Version

0.15.0-dev.734+adc4418ba

Steps to Reproduce and Observed Behavior

const std = @import("std");
const expect = std.testing.expect;

fn myMemSet(bytes: []u8, value: u8) void {
    for (bytes) |*byte| byte.* = value;
}

test "memset" {
    var data: [100]u8 = undefined;
    try expect(data[50] == 0xaa);
    myMemSet(&data, 0);
    try expect(data[50] == 0);
    myMemSet(&data, undefined);
    try expect(data[50] == 0xaa);
}
$ zig test test.zig -fllvm
1/1 test.test.memset...FAIL (TestUnexpectedResult)
/home/andy/dev/zig/lib/std/testing.zig:586:14: 0x1048a7f in expect (test)
    if (!ok) return error.TestUnexpectedResult;
             ^
/home/andy/dev/zig/build-release/test.zig:14:5: 0x1048c47 in test.memset (test)
    try expect(data[50] == 0xaa);
    ^
0 passed; 0 skipped; 1 failed.
error: the following test command failed with exit code 1:
/home/andy/dev/zig/.zig-cache/o/82264e3fe302fd72325379939b8bf68f/test --seed=0xbc6acbdd

Expected Behavior

Same behavior as the x86 backend:

$ stage3/bin/zig test test.zig -fno-llvm
All 1 tests passed.

Activity

  1. added this to the 0.15.0 milestone on Jun 6, 2025
  2. added
    bugObserved behavior contradicts documented or intended behavior
    contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.
    backend-llvmThe LLVM backend outputs an LLVM IR Module.
    on Jun 6, 2025
  3. kmr-srbh commented on Aug 20, 2025

    @kmr-srbh

    @andrewrk may I please take up this issue? It might take me some time since I am starting out with zig, but I wish to contribute to the project.

  4. silversquirl commented on Aug 20, 2025

    @silversquirl
    Contributor

    @kmr-srbh you can just start working on it, you don't need to ask for permission! :)

  5. aznashwan commented on Nov 12, 2025

    @aznashwan
    Contributor

    Can confirm this is still an issue as of 0.16.0-dev.1301+cbfa87cbe.

    Would like to add there's some extra weird behavior when declaring single-byte variables:

    • just for consts (not var)
    • call me crazy but the order of statements seems to matter?!
    const std = @import("std");
    const print = std.debug.print;
    const expect = std.testing.expect;
    
    pub fn main() !void {}
    
    test "constant first" {           // Only failure.
        const c: u8 = undefined;
        try expect(c == 0xAA);
    
        var v: u8 = undefined;
        try expect(v == 0xAA);
    
        v = undefined;
        try expect(v == 0xAA);
    }
    
    test "constant middle" {
        var v: u8 = undefined;
        try expect(v == 0xAA);
    
        const c: u8 = undefined;
        try expect(c == 0xAA);
    
        v = undefined;
        try expect(v == 0xAA);
    }
    
    test "constant last" {
        var v: u8 = undefined;
        try expect(v == 0xAA);
    
        v = undefined;
        try expect(v == 0xAA);
    
        const c: u8 = undefined;
        try expect(c == 0xAA);
    }

    Output:

    $ file -h ../latest
    ../latest: symbolic link to /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/zig
    
    $ ../latest test src/main.zig -fllvm
    1/3 main.test.constant first...FAIL (TestUnexpectedResult)
    /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/lib/std/testing.zig:604:14: 0x1004887a3 in expect (test)
        if (!ok) return error.TestUnexpectedResult;
                 ^
    /Users/nashwan/Documents/Coding/Zig/compilers/playground/src/main.zig:9:5: 0x100488873 in test.constant first (test)
        try expect(c == 0xAA);
        ^
    2 passed; 0 skipped; 1 failed.
    error: the following test command failed with exit code 1:
    .zig-cache/o/2f45fb1504487078861b4e1bba8ecc8b/test --seed=0xf588a363
  6. aznashwan commented on Nov 12, 2025

    @aznashwan
    Contributor

    call me crazy but the order of statements seems to matter?!

    After a bit more finagling, it seems (somewhat arbitrarily) dependant on the size of the type of the const too (with 2, 6, 8, and 12 bytes seemingly working!?)

    Note: this is literally my first piece of comptime zig so please take it with a grain of salt...

    const std = @import("std");
    const print = std.debug.print;
    const warn = std.log.warn;
    const expect = std.testing.expect;
    
    pub fn main() !void {}
    
    fn default_val_for_type(comptime typ: type) comptime_int {
        var v = 0;
        var bits = 1;
        while (bits <= @bitSizeOf(typ)) : (bits += 1) {
            v = (v << 1) | (bits % 2);
        }
        return v;
    }
    
    comptime {
        const types = .{ u8, u12, u16, u24, u32, u48, u64, u96, u128, u192, u256, u384, u512 };
    
        // NOTE: putting `u8` to the back somehow makes it work for `u8` too?!
        // const types = .{ u12, u16, u24, u32, u48, u64, u96, u128, u192, u256, u384, u512, u8 };
    
        for (types) |typ| {
            _ = struct {
                test {
                    const expected = default_val_for_type(typ);
                    warn("############################### {s} -- 0x{x:}", .{ @typeName(typ), expected });
    
                    const c: typ = undefined;
                    try expect(c == expected);
    
                    // var v: typ = undefined;      // Uncomment => different results?!?!
                    // try expect(v == expected);
                    // v = undefined;
                    // try expect(v == expected);
    
                    warn("Passed for {s}!\n", .{@typeName(typ)});
                }
            };
        }
    }

    Output:

    [default] (warn): ############################### u8 -- 0xaa
    1/13 main.comptime__struct_149.test_0...FAIL (TestUnexpectedResult)
    /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/lib/std/testing.zig:604:14: 0x102a411b7 in expect (test)
        if (!ok) return error.TestUnexpectedResult;
                 ^
    /Users/nashwan/Documents/Coding/Zig/compilers/playground/src/main.zig:30:17: 0x102a4120b in test_0 (test)
                    try expect(c == expected);
                    ^
    [default] (warn): ############################### u12 -- 0xaaa
    2/13 main.comptime__struct_150.test_0...FAIL (TestUnexpectedResult)
    /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/lib/std/testing.zig:604:14: 0x102a411b7 in expect (test)
        if (!ok) return error.TestUnexpectedResult;
                 ^
    /Users/nashwan/Documents/Coding/Zig/compilers/playground/src/main.zig:30:17: 0x102a419eb in test_0 (test)
                    try expect(c == expected);
                    ^
    [default] (warn): ############################### u16 -- 0xaaaa
    [default] (warn): Passed for u16!
    
    [default] (warn): ############################### u24 -- 0xaaaaaa
    4/13 main.comptime__struct_154.test_0...FAIL (TestUnexpectedResult)
    /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/lib/std/testing.zig:604:14: 0x102a411b7 in expect (test)
        if (!ok) return error.TestUnexpectedResult;
                 ^
    /Users/nashwan/Documents/Coding/Zig/compilers/playground/src/main.zig:30:17: 0x102a428bb in test_0 (test)
                    try expect(c == expected);
                    ^
    [default] (warn): ############################### u32 -- 0xaaaaaaaa
    5/13 main.comptime__struct_156.test_0...FAIL (TestUnexpectedResult)
    /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/lib/std/testing.zig:604:14: 0x102a411b7 in expect (test)
        if (!ok) return error.TestUnexpectedResult;
                 ^
    /Users/nashwan/Documents/Coding/Zig/compilers/playground/src/main.zig:30:17: 0x102a42fa7 in test_0 (test)
                    try expect(c == expected);
                    ^
    [default] (warn): ############################### u48 -- 0xaaaaaaaaaaaa
    [default] (warn): Passed for u48!
    
    [default] (warn): ############################### u64 -- 0xaaaaaaaaaaaaaaaa
    [default] (warn): Passed for u64!
    
    [default] (warn): ############################### u96 -- 0xaaaaaaaaaaaaaaaaaaaaaaaa
    [default] (warn): Passed for u96!
    
    [default] (warn): ############################### u128 -- 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    9/13 main.comptime__struct_164.test_0...FAIL (TestUnexpectedResult)
    /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/lib/std/testing.zig:604:14: 0x102a411b7 in expect (test)
        if (!ok) return error.TestUnexpectedResult;
                 ^
    /Users/nashwan/Documents/Coding/Zig/compilers/playground/src/main.zig:30:17: 0x102a4526f in test_0 (test)
                    try expect(c == expected);
                    ^
    [default] (warn): ############################### u192 -- 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    10/13 main.comptime__struct_166.test_0...FAIL (TestUnexpectedResult)
    /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/lib/std/testing.zig:604:14: 0x102a411b7 in expect (test)
        if (!ok) return error.TestUnexpectedResult;
                 ^
    /Users/nashwan/Documents/Coding/Zig/compilers/playground/src/main.zig:30:17: 0x102a45a53 in test_0 (test)
                    try expect(c == expected);
                    ^
    [default] (warn): ############################### u256 -- 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    11/13 main.comptime__struct_168.test_0...FAIL (TestUnexpectedResult)
    /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/lib/std/testing.zig:604:14: 0x102a411b7 in expect (test)
        if (!ok) return error.TestUnexpectedResult;
                 ^
    /Users/nashwan/Documents/Coding/Zig/compilers/playground/src/main.zig:30:17: 0x102a47c8b in test_0 (test)
                    try expect(c == expected);
                    ^
    [default] (warn): ############################### u384 -- 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    12/13 main.comptime__struct_170.test_0...FAIL (TestUnexpectedResult)
    /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/lib/std/testing.zig:604:14: 0x102a411b7 in expect (test)
        if (!ok) return error.TestUnexpectedResult;
                 ^
    /Users/nashwan/Documents/Coding/Zig/compilers/playground/src/main.zig:30:17: 0x102a4a4df in test_0 (test)
                    try expect(c == expected);
                    ^
    [default] (warn): ############################### u512 -- 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    13/13 main.comptime__struct_172.test_0...FAIL (TestUnexpectedResult)
    /Users/nashwan/Documents/Coding/Zig/compilers/zig-aarch64-macos-0.16.0-dev.1301+cbfa87cbe/lib/std/testing.zig:604:14: 0x102a411b7 in expect (test)
        if (!ok) return error.TestUnexpectedResult;
                 ^
    /Users/nashwan/Documents/Coding/Zig/compilers/playground/src/main.zig:30:17: 0x102a4dddf in test_0 (test)
                    try expect(c == expected);
                    ^
    4 passed; 0 skipped; 9 failed.
    error: the following test command failed with exit code 1:
    .zig-cache/o/cd0e2620c51d4e7991d70f37a5320d2e/test --seed=0xa9e143ba
    

    PS: being able to dynamically name comptime tests like test @typeName(typ) would have been pretty slick.

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

    backend-llvmThe LLVM backend outputs an LLVM IR Module.bugObserved behavior contradicts documented or intended behaviorcontributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions