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

Compile crash #21147

Closed
crat1985 opened this issue Aug 20, 2024 · 2 comments
Closed

Compile crash #21147

crat1985 opened this issue Aug 20, 2024 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@crat1985
Copy link

crat1985 commented Aug 20, 2024

Zig Version

0.14.0-dev.1200+104553714

Steps to Reproduce and Observed Behavior

Create a new Zig project directory, and replace the main file content with the following :

const std = @import("std");

pub const StackAllocator = struct {
    data: [40]u8 = undefined,
    offset: usize = 0,

    const Self = @This();

    fn alloc(ctx: *anyopaque, n: usize, ptr_align: u8, return_address: usize) ?[*]u8 {
        const self: *Self = @ptrCast(@alignCast(ctx));
        // _ = n;
        // _ = self;

        _ = ptr_align;
        _ = return_address;

        const data = self.data[self.offset .. self.offset + n];

        return data.ptr + n;
        // return null;
    }

    pub fn allocator(comptime self: *Self) std.mem.Allocator {
        return std.mem.Allocator{ .ptr = self, .vtable = &.{ .alloc = alloc, .free = undefined, .resize = undefined } };
    }
};

pub const HandlerFn =
    *const fn () void;

pub fn main() !void {
    comptime {
        var stack_allocator = StackAllocator{};

        const allocator = stack_allocator.allocator();

        const value =
            try allocator.create(HandlerFn);

        const handle_fn = struct {
            fn handle() void {}
        }.handle;

        value.* = handle_fn;
    }
}

Run zig build

Expected Behavior

The code to compile or at least to throw a real error (I think the issue comes from setting the std.mem.Allocator VTable's functions to undefined).
When compiling, I get the following error :

install
└─ install zig-tiny-http
   └─ zig build-exe zig-tiny-http Debug native failure
error: the following command terminated unexpectedly:
/usr/local/bin/zig build-exe -ODebug -Mroot=/home/riccardo/tiny-http-zig-reduced/src/main.zig --cache-dir /home/riccardo/tiny-http-zig-reduced/.zig-cache --global-cache-dir /home/riccardo/.cache/zig --name zig-tiny-http --zig-lib-dir /usr/local/bin/lib/ --listen=- 
Build Summary: 0/3 steps succeeded; 1 failed
install transitive failure
└─ install zig-tiny-http transitive failure
   └─ zig build-exe zig-tiny-http Debug native failure
error: the following build command failed with exit code 1:
/home/riccardo/tiny-http-zig-reduced/.zig-cache/o/1b6808dabce3e4d55c3311488c60b910/build /usr/local/bin/zig /usr/local/bin/lib /home/riccardo/tiny-http-zig-reduced /home/riccardo/tiny-http-zig-reduced/.zig-cache /home/riccardo/.cache/zig --seed 0x42d96380 -Z957a6ec646f3fd6e
@crat1985 crat1985 added the bug Observed behavior contradicts documented or intended behavior label Aug 20, 2024
@crat1985
Copy link
Author

When using this code :

const std = @import("std");

pub const StackAllocator = struct {
    data: [40]u8 = undefined,
    offset: usize = 0,

    const Self = @This();

    fn alloc(ctx: *anyopaque, n: usize, ptr_align: u8, return_address: usize) ?[*]u8 {
        const self: *Self = @ptrCast(@alignCast(ctx));
        _ = n;
        _ = self;

        _ = ptr_align;
        _ = return_address;

        // const data = self.data[self.offset .. self.offset + n];

        // return data.ptr + n;
        return null;
    }

    pub fn allocator(comptime self: *Self) std.mem.Allocator {
        return std.mem.Allocator{ .ptr = self, .vtable = &.{ .alloc = alloc, .free = undefined, .resize = undefined } };
    }
};

pub const HandlerFn =
    *const fn () void;

pub fn main() !void {
    comptime {
        var stack_allocator = StackAllocator{};

        const allocator = stack_allocator.allocator();

        const value =
            try allocator.create(HandlerFn);

        const handle_fn = struct {
            fn handle() void {}
        }.handle;

        value.* = handle_fn;
    }
}

The diff is :

     fn alloc(ctx: *anyopaque, n: usize, ptr_align: u8, return_address: usize) ?[*]u8 {
         const self: *Self = @ptrCast(@alignCast(ctx));
-        // _ = n;
-        // _ = self;
+        _ = n;
+        _ = self;
 
         _ = ptr_align;
         _ = return_address;
 
-        const data = self.data[self.offset .. self.offset + n];
+        // const data = self.data[self.offset .. self.offset + n];
 
-        return data.ptr + n;
-        // return null;
+        // return data.ptr + n;
+        return null;
     }
 
     pub fn allocator(comptime self: *Self) std.mem.Allocator {

There is a compiler error :

install
└─ install zig-tiny-http
   └─ zig build-exe zig-tiny-http Debug native 1 errors
src/main.zig:38:13: error: function called at runtime cannot return value at comptime
            try allocator.create(HandlerFn);
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    posixCallMainAndExit: /usr/local/bin/lib/std/start.zig:614:37
    _start: /usr/local/bin/lib/std/start.zig:421:40
    3 reference(s) hidden; use '-freference-trace=5' to see all references
error: the following command failed with 1 compilation errors:
/usr/local/bin/zig build-exe -ODebug -Mroot=/home/riccardo/tiny-http-zig-reduced/src/main.zig --cache-dir /home/riccardo/tiny-http-zig-reduced/.zig-cache --global-cache-dir /home/riccardo/.cache/zig --name zig-tiny-http --zig-lib-dir /usr/local/bin/lib/ --listen=- 
Build Summary: 0/3 steps succeeded; 1 failed
install transitive failure
└─ install zig-tiny-http transitive failure
   └─ zig build-exe zig-tiny-http Debug native 1 errors
error: the following build command failed with exit code 1:
/home/riccardo/tiny-http-zig-reduced/.zig-cache/o/1b6808dabce3e4d55c3311488c60b910/build /usr/local/bin/zig /usr/local/bin/lib /home/riccardo/tiny-http-zig-reduced /home/riccardo/tiny-http-zig-reduced/.zig-cache /home/riccardo/.cache/zig --seed 0x8c540e68 -Za55a3e4c4a160d32

@Vexu
Copy link
Member

Vexu commented Aug 26, 2024

Duplicate of #20765

@Vexu Vexu marked this as a duplicate of #20765 Aug 26, 2024
@Vexu Vexu closed this as not planned Won't fix, can't repro, duplicate, stale Aug 26, 2024
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
Projects
None yet
Development

No branches or pull requests

2 participants