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

Crash on invalid ptrCast #13034

Closed
gwenzek opened this issue Oct 1, 2022 · 2 comments
Closed

Crash on invalid ptrCast #13034

gwenzek opened this issue Oct 1, 2022 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@gwenzek
Copy link
Contributor

gwenzek commented Oct 1, 2022

Zig Version

0.10.0-dev+138623c8

Steps to Reproduce

Sorry cuda code again, but since cuModuleLoad is an extern symbol it should work with simpler code.
I'll try to reduce this further once I have a chance.

pub extern fn cuModuleLoad(module: [*c]CUmodule, fname: [*c]const u8) CUresult;

test "load_module" {
    const kernel_ptx_path: [:0]const u8 = "kernel.ptx";
    var m: cu.CUmodule = undefined;
    _ = cu.cuModuleLoad(&m, kernel_ptx_path.ptr);
}

test "load_module bad ptr cast" {
    const kernel_ptx_path: [:0]const u8 = "kernel.ptx";
    var m: cu.CUmodule = undefined;
    _ = cu.cuModuleLoad(&m, @ptrCast([*c]const u8, kernel_ptx_path));
}

Expected Behavior

I'd expect the first test to compile and run successfully.
I'd expect the second test to fail to compile with a proper error message

Actual Behavior

The first test compile and run succesfully
The second test triggers an assertion error in the backend.

thread 61164 panic: reached unreachable code
???:?:?: 0x709e51a in assert (/home/guw/github/zig-bootstrap/zig/src/zig_llvm.cpp)
???:?:?: 0x70fe044 in slicePtrFieldType (/home/guw/github/zig-bootstrap/zig/src/zig_llvm.cpp)
???:?:?: 0x73f75ef in lowerValue (/home/guw/github/zig-bootstrap/zig/src/zig_llvm.cpp)
???:?:?: 0x7a25b6d in resolveInst (/home/guw/github/zig-bootstrap/zig/src/zig_llvm.cpp)
???:?:?: 0x7a5ec1e in airCall (/home/guw/github/zig-bootstrap/zig/src/zig_llvm.cpp)
???:?:?: 0x77a445f in genBody (/home/guw/github/zig-bootstrap/zig/src/zig_llvm.cpp)
???:?:?: 0x779f1e0 in updateFunc (/home/guw/github/zig-bootstrap/zig/src/zig_llvm.cpp)
???:?:?: 0x77a7262 in updateFunc (/home/guw/github/zig-bootstrap/zig/src/zig_llvm.cpp)
???:?:?: 0x75dd87d in updateFunc (/home/guw/github/zig-bootstrap/zig/src/zig_llvm.cpp)
???:?:?: 0x73d0ca0 in ensureFuncBodyAnalyzed (/home/guw/github/zig-bootstrap/zig/src/zig_llvm.cpp)
???:?:?: 0x73cec53 in processOneJob (/home/guw/github/zig-bootstrap/zig/src/zig_llvm.cpp)
???:?:?: 0x72b53da in performAllTheWork (/home/guw/github/zig-bootstrap/zig/src/zig_llvm.cpp)
???:?:?: 0x72b1c1b in update (/home/guw/github/zig-bootstrap/zig/src/zig_llvm.cpp)
???:?:?: 0x72dd046 in updateModule (/home/guw/github/zig-bootstrap/zig/src/zig_llvm.cpp)
???:?:?: 0x7085f1c in buildOutputType (/home/guw/github/zig-bootstrap/zig/src/zig_llvm.cpp)
???:?:?: 0x7063f49 in mainArgs (/home/guw/github/zig-bootstrap/zig/src/zig_llvm.cpp)
???:?:?: 0x734ba87 in main (/home/guw/github/zig-bootstrap/zig/src/zig_llvm.cpp)
error: test...

I had this bug in my code for a long time and it was compiling fine on be94487

@gwenzek gwenzek added the bug Observed behavior contradicts documented or intended behavior label Oct 1, 2022
@Vexu
Copy link
Member

Vexu commented Oct 1, 2022

Reduction:

test {
    const s: [:0]const u8 = "foo";
    var p = @ptrCast([*c]const u8, s);
    _ = p;
}

should compile successfully like it does if you change the first const to var.

@Vexu Vexu added the frontend Tokenization, parsing, AstGen, Sema, and Liveness. label Oct 1, 2022
@Vexu Vexu added this to the 0.10.0 milestone Oct 1, 2022
@gwenzek
Copy link
Contributor Author

gwenzek commented Oct 1, 2022

Damn, you're fast. Thanks for the reduction. I tried w/o the function call, but it was getting optimized away

Vexu added a commit to Vexu/zig that referenced this issue Oct 4, 2022
Vexu added a commit to Vexu/zig that referenced this issue Oct 5, 2022
@Vexu Vexu closed this as completed in ba4aa12 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