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

Compiler segfaults when invoking a function stored in a comptime field #12801

Closed
tauoverpi opened this issue Sep 9, 2022 · 0 comments
Closed
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@tauoverpi
Copy link
Sponsor Contributor

Zig Version

0.10.0-dev.3952+9e070b653

Steps to Reproduce

This issue was discovered by a user on telegram (posting on their behalf as they don't have a github account).

build the following

comptime capacity: fn () u64 = capacity_,
// comptime capacity: *const fn () u64 = capacity_,
fn capacity_() u64 {
    return 64;
}

pub export fn _start() void {
    _ = (@This(){}).capacity();
}

and for the second case, the given:

const print = @import("std").debug.print;
const Auto = struct {
    auto: [max_len]u8 = undefined,
    offset: u64 = 0,

    comptime capacity: *const fn () u64 = capacity,

    const max_len: u64 = 32;

    fn length(heap: Auto) u64 {
        return heap.offset;
    }
    fn capacity() u64 {
        return max_len;
    }
};
const Heap = struct {
    start: u64,
    next: u64,
    finish: u64,

    fn length(heap: Heap) u64 {
        return heap.next - heap.start;
    }
    fn capacity(heap: Heap) u64 {
        return heap.finish - heap.start;
    }
};
fn doPrint(any: anytype) void {
    print("offset: {d}, capacity: {d}\n", .{ any.length(), any.capacity() });
}
pub fn main() void {
    const a: Auto = .{ .offset = 16, .capacity = Auto.capacity };
    const b: Heap = .{ .start = 0x4000000, .next = 0x4000010, .finish = 0x4000020 };
    doPrint(a);
    doPrint(b);
}

Expected Behavior

Compile as this works with stage1 (first example) and not generate invalid IR (second example).

Actual Behavior

Segfault

> zig build-exe c.zig
fish: Job 1, 'zig build-exe c.zig' terminated by signal SIGSEGV (Address boundary error)

For the second one, invalid IR

><> zig run c.zig
LLVM Emit Object... LLVM ERROR: Cannot select: 0x7fa0816eeaf0: ch,glue = X86ISD::CALL 0x7fa0816eeb58, 0x7fa0816d6d90, RegisterMask:Untyped, c.zig:30:72
  0x7fa0816d6d90: i8,ch = CopyFromReg 0x7fa0818ad3e8, Register:i8 %4, c.zig:30:72
    0x7fa0816ef2a8: i8 = Register %4
  0x7fa0816d6ec8: Untyped = RegisterMask
In function: c.doPrint__anon_3733
@tauoverpi tauoverpi added the bug Observed behavior contradicts documented or intended behavior label Sep 9, 2022
@Vexu Vexu added the frontend Tokenization, parsing, AstGen, Sema, and Liveness. label Sep 9, 2022
@Vexu Vexu added this to the 0.10.0 milestone Sep 9, 2022
Vexu added a commit to Vexu/zig that referenced this issue Sep 9, 2022
@Vexu Vexu closed this as completed in 5e4483f Sep 16, 2022
Vexu added a commit to Vexu/zig that referenced this issue Oct 10, 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

No branches or pull requests

2 participants