Skip to content

Segfault during code generation #4749

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

Closed
joachimschmidt557 opened this issue Mar 16, 2020 · 0 comments
Closed

Segfault during code generation #4749

joachimschmidt557 opened this issue Mar 16, 2020 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@joachimschmidt557
Copy link
Contributor

I don't know if this is related to another issue, but when compiling this code with zig from a2432b6, a segmentation fault occurs:

const std = @import("std");
const fixedBufferStream = std.io.fixedBufferStream;

fn exec(input_stream: var) !void {
    var buf: [1024]u8 = undefined;
    const line = (try input_stream.readUntilDelimiterOrEof(&buf, '\n')) orelse "";
}

pub fn main() !void {
    var in_stream = fixedBufferStream("").inStream();
    try exec(in_stream);
}

Here's the valgrind output:

==8618== Memcheck, a memory error detector
==8618== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==8618== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==8618== Command: zig build-exe asdf.zig
==8618== 
==8618== Invalid read of size 8
==8618==    at 0x541F22C: LLVMBuildInBoundsGEP (in /usr/lib/libLLVM-9.so)
==8618==    by 0xBBC3AC: ir_render_slice(CodeGen*, IrExecutableGen*, IrInstGenSlice*) (codegen.cpp:5455)
==8618==    by 0xBC07E5: ir_render_instruction(CodeGen*, IrExecutableGen*, IrInstGen*) (codegen.cpp:6456)
==8618==    by 0xBC0EE6: ir_render(CodeGen*, ZigFn*) (codegen.cpp:6576)
==8618==    by 0xBC7211: do_code_gen(CodeGen*) (codegen.cpp:7935)
==8618==    by 0xBD2379: codegen_build_and_link(CodeGen*) (codegen.cpp:10689)
==8618==    by 0xBA3130: main0(int, char**) (main.cpp:1256)
==8618==    by 0xBA3A2C: main (main.cpp:1407)
==8618==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==8618== 
Segmentation fault at address 0x0
???:?:?: 0x541f22c in ??? (???)
==8618== Warning: set address range perms: large range [0x59c83000, 0x7048c000) (defined)
==8618== Warning: set address range perms: large range [0x59c83000, 0x7048c000) (undefined)
==8618== Warning: set address range perms: large range [0x7048c000, 0x9209a000) (defined)
==8618== Warning: set address range perms: large range [0x7048c000, 0x9209a000) (undefined)
==8618== Warning: set address range perms: large range [0x9209a000, 0xc4aaf000) (defined)
==8618== Warning: set address range perms: large range [0x9209a000, 0xc4aaf000) (undefined)
???:?:?: 0xbc07e5 in ir_render_instruction (/home/joachim/src/zig/src/codegen.cpp)
???:?:?: 0xbc0ee6 in ir_render (/home/joachim/src/zig/src/codegen.cpp)
???:?:?: 0xbc7211 in do_code_gen (/home/joachim/src/zig/src/codegen.cpp)
???:?:?: 0xbd2379 in codegen_build_and_link (/home/joachim/src/zig/src/codegen.cpp)
???:?:?: 0xba3130 in main0 (/home/joachim/src/zig/src/main.cpp)
???:?:?: 0xba3a2c in main (/home/joachim/src/zig/src/main.cpp)
==8618== 
==8618== Process terminating with default action of signal 6 (SIGABRT)
==8618==    at 0x9A33931: raise (raise.c:51)
==8618==    by 0x9A1D55A: abort (abort.c:79)
==8618==    by 0xCDF098: std.os.abort (os.zig:213)
==8618==    by 0xCFB2AA: std.debug.handleSegfaultLinux (debug.zig:1734)
==8618==    by 0x961411F: ??? (in /usr/lib/libpthread-2.30.so)
==8618==    by 0x541F22B: LLVMBuildInBoundsGEP (in /usr/lib/libLLVM-9.so)
==8618==    by 0xBBC3AC: ir_render_slice(CodeGen*, IrExecutableGen*, IrInstGenSlice*) (codegen.cpp:5455)
==8618==    by 0xBC07E5: ir_render_instruction(CodeGen*, IrExecutableGen*, IrInstGen*) (codegen.cpp:6456)
==8618==    by 0xBC0EE6: ir_render(CodeGen*, ZigFn*) (codegen.cpp:6576)
==8618==    by 0xBC7211: do_code_gen(CodeGen*) (codegen.cpp:7935)
==8618==    by 0xBD2379: codegen_build_and_link(CodeGen*) (codegen.cpp:10689)
==8618==    by 0xBA3130: main0(int, char**) (main.cpp:1256)
==8618== 
==8618== HEAP SUMMARY:
==8618==     in use at exit: 117,146,740 bytes in 900,504 blocks
==8618==   total heap usage: 1,288,643 allocs, 388,139 frees, 167,215,472 bytes allocated
==8618== 
==8618== LEAK SUMMARY:
==8618==    definitely lost: 9,063,466 bytes in 136,116 blocks
==8618==    indirectly lost: 5,000,198 bytes in 76,969 blocks
==8618==      possibly lost: 34,342,479 bytes in 146,765 blocks
==8618==    still reachable: 68,740,597 bytes in 540,654 blocks
==8618==         suppressed: 0 bytes in 0 blocks
==8618== Rerun with --leak-check=full to see details of leaked memory
==8618== 
==8618== For lists of detected and suppressed errors, rerun with: -s
==8618== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
LemonBoy added a commit to LemonBoy/zig that referenced this issue Mar 16, 2020
Zero-length array with a sentinel may not have zero size.

Closes ziglang#4749
@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Mar 17, 2020
@andrewrk andrewrk added this to the 0.6.0 milestone Mar 17, 2020
andrewrk added a commit that referenced this issue Mar 18, 2020
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 stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants