ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
32.87k stars 2.4k forks source link

Segfault at runtime when calling a function from an `@export`'d function pointer #20472

Closed Jarred-Sumner closed 2 weeks ago

Jarred-Sumner commented 3 weeks ago

Zig Version

0.13.0

Steps to Reproduce and Observed Behavior

To reproduce:

  1. Run zig test on this file:
    
    fn function() callconv(.C) void {}
    const exported = &function;
    comptime {
    @export(exported, .{ .name = "exported2" });
    }

extern fn exported2() void;

test { exported2(); }

2. Output:
```zig
Bus error at address 0x1029a4150
Panicked during a panic. Aborting.
error: the following test command crashed:
/Users/jarred/Code/bun/.zig-cache/o/3c26355fcac2b8e03bcdb703800c6976/test

This reproduces on macOS arm64 and Windows x64. It caused the "fuzzy-wuzzy.test.ts" test to fail in this github action run (and locally in a build with optimizations enabled).

Expected Behavior

The test should pass without crashing

llogick commented 3 weeks ago

The technical aspect of this is that the code is exporting the equivalent of *const *const fn () callconv(.C) void while the extern fn exported2() void; is expecting a *const fn () callconv(.C) void.

To handle that

const exported2 = @extern(*const *const fn () callconv(.C) void, .{ .name = "exported2" });
...
    exported2.*();
...
Vexu commented 2 weeks ago

This footgun should be addressed by this accepted proposal #14911