zig icon indicating copy to clipboard operation
zig copied to clipboard

assert(@alignof(i16) == 2) fails on 8-bit AVR MCU

Open Noxet opened this issue 1 year ago • 2 comments

Zig Version

0.12.0-dev.3630+215de3ee6

Steps to Reproduce and Observed Behavior

Trying to build a static lib for an AVR MCU, Atmega328p specifically. The following code

const std = @import("std");

export fn app_main() void {
    var buf: [20]u8 = undefined;
    var fba = std.heap.FixedBufferAllocator.init(&buf);
    const alloc = fba.allocator();

    const mem = alloc.alloc(u8, 10) catch |err| {
        err catch {};
        return;
    };
    _ = mem;
}

results in the following compiler error:

install
+- install app
   +- zig build-lib app Debug avr-freestanding-none 1 errors
/home/noxet/dev/tools/zig/zig-0.12.0/lib/std/debug.zig:403:14: error: reached unreachable code
/home/noxet/dev/tools/zig/zig-0.12.0/lib/std/os/wasi.zig:12:11: note: called from here
    if (!ok) unreachable; // assertion failure
             ^~~~~~~~~~~
/home/noxet/dev/tools/zig/zig-0.12.0/lib/std/os/wasi.zig:12:11: note: called from here
    assert(@alignOf(i16) == 2);
    ~~~~~~^~~~~~~~~~~~~~~~~~~~
error: the following command failed with 1 compilation errors:
/home/noxet/dev/tools/zig/zig-0.12.0/zig build-lib -ODebug -target avr-freestanding-none -mcpu atmega328p -Mroot=/home/noxet/dev/avr/app/src/main.zig --cache-dir /home/noxet/dev/avr/app/zig-cache --global-cache-dir /home/noxet/.cache/zig --name app -static --listen=- 
Build Summary: 0/3 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
+- install app transitive failure
   +- zig build-lib app Debug avr-freestanding-none 1 errors
error: the following build command failed with exit code 1:
/home/noxet/dev/avr/app/zig-cache/o/fa9f7ff03f1b36a46011aeeafddb06d5/build /home/noxet/dev/tools/zig/zig-0.12.0/zig /home/noxet/dev/avr/app /home/noxet/dev/avr/app/zig-cache /home/noxet/.cache/zig --seed 0x46dca3b3 -Z13d2886cbd8e8d28

The build configuration:

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = std.Target.Query{
        .abi = .none,
        .cpu_arch = .avr,
        .cpu_model = .{ .explicit = &std.Target.avr.cpu.atmega328p },
        .os_tag = .freestanding,
    };

    const optimize = b.standardOptimizeOption(.{});

    const lib = b.addStaticLibrary(.{
        .name = "app",
        .root_source_file = .{ .path = "src/main.zig" },
        .target = b.resolveTargetQuery(target),
        .optimize = optimize,
    });

    b.installArtifact(lib);

    const main_tests = b.addTest(.{
        .root_source_file = .{ .path = "src/main.zig" },
        .target = b.resolveTargetQuery(target),
        .optimize = optimize,
    });

    const run_main_tests = b.addRunArtifact(main_tests);
    const test_step = b.step("test", "Run library tests");
    test_step.dependOn(&run_main_tests.step);
}

Expected Behavior

The 8-bit AVR MCUs do not have any alignment requirements for words, since a word is accessed by loading/storing 2 bytes (8-bit) at a time.

Noxet avatar Apr 15 '24 19:04 Noxet

Note that the reference to @import("wasi.zig") was introduced by #18712 (where the lazily analyzed decl wasiCwd was moved to a less lazily analyzed field).

jacobly0 avatar Apr 15 '24 20:04 jacobly0

Those wasi asserts should only be run when targeting wasi.

Vexu avatar Apr 15 '24 20:04 Vexu