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

SIGSEGV when comparing optional type in an else branch #10315

Open
MageJohn opened this issue Dec 11, 2021 · 4 comments
Open

SIGSEGV when comparing optional type in an else branch #10315

MageJohn opened this issue Dec 11, 2021 · 4 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

@MageJohn
Copy link
Contributor

Zig Version

0.9.0-dev.1945+efdb94486

Steps to Reproduce

Run this test file:

var l: u8 = 1;
var r: ?u8 = 2;

test {
    const foo: u8 = 1;
    if (l == foo) {} else {
        _ = r == foo;
    }
}

E.g. $ zig test snippet.zig. I ran it on Linux; I'm not sure if there will be differences between OSes.

I whittled this down as much as I could from how I encountered it, but I'm not sure it's minimal.

Expected Behavior

The test should compile and run.

Actual Behavior

The compilation stops part-way through and terminates with SIGSEV. I use fish, and this is the output:

fish: Job 1, 'zig test bug_repr.zig' terminated by signal SIGSEGV (Address boundary
error)

Running the command through lldb gives this output:

$ lldb zig test bug_repr.zig
(lldb) target create "zig"
Current executable set to 'zig' (x86_64).
(lldb) settings set -- target.run-args  "test" "bug_repr.zig"
(lldb) run
Process 35455 launched: '/usr/bin/zig' (x86_64)
Semantic Analysis [1002/1405] Process 35455 stopped
* thread #1, name = 'zig', stop reason = signal SIGSEGV: invalid address (fault address: 0x0)
    frame #0: 0x0000000007348340 zig`llvm::BasicBlock::getContext() const
zig`llvm::BasicBlock::getContext:
->  0x7348340 <+0>: movq   (%rdi), %rax
    0x7348343 <+3>: movq   (%rax), %rax
    0x7348346 <+6>: retq   
    0x7348347:      int3   
(lldb) bt
* thread #1, name = 'zig', stop reason = signal SIGSEGV: invalid address (fault address: 0x0)
  * frame #0: 0x0000000007348340 zig`llvm::BasicBlock::getContext() const
    frame #1: 0x0000000007407309 zig`llvm::BranchInst::BranchInst(llvm::BasicBlock*, llvm::Instruction*) + 25
    frame #2: 0x0000000007380060 zig`LLVMBuildBr + 48
    frame #3: 0x00000000032d670f zig`ir_render(CodeGen*, ZigFn*) + 6239
    frame #4: 0x00000000032cab8b zig`do_code_gen(CodeGen*) + 2171
    frame #5: 0x00000000032c7eae zig`codegen_build_object(CodeGen*) + 3310
    frame #6: 0x00000000032bff28 zig`zig_stage1_build_object + 2392
    frame #7: 0x0000000002f96213 zig`Compilation.processOneJob + 78307
    frame #8: 0x0000000002f7907c zig`Compilation.update + 4268
    frame #9: 0x0000000002f39aaf zig`main.updateModule + 31
    frame #10: 0x0000000002f0f185 zig`main.buildOutputType + 78757
    frame #11: 0x0000000002ef0df4 zig`main + 2212
    frame #12: 0x000000000762fd3a zig`libc_start_main_stage2 + 41
    frame #13: 0x0000000002eef5b6 zig`_start + 22
@MageJohn MageJohn added the bug Observed behavior contradicts documented or intended behavior label Dec 11, 2021
@andrewrk andrewrk added the stage1 The process of building from source via WebAssembly and the C backend. label Dec 14, 2021
@andrewrk andrewrk added this to the 0.11.0 milestone Dec 14, 2021
@agathazeren
Copy link
Contributor

I just saw this issue, and was already working on this, though it's been slow.

It seems that the main issue is that some blocks are not making it into the executable to be built with build_all_basic_blocks. Adding calls to ir_set_cursor_at_end_and_append_block_gen for then_block and else_block in ir_analyze_instruction_cond_br solves this segfault, but leads to other issues. Firstly, ir_analyze_instruction_br can be fixed in the same way, but that leads to an assertion failure at codegen.cpp:1870, which is the next thing I'm going to look at.

@richiejp
Copy link
Sponsor

richiejp commented Feb 3, 2022

I think I'm hitting the same issue with the following block-assignment, inline for-loop and if combo. Using 0.10.0-dev.536+557a09752

test "SIGSEGV" {
    const ms = .{ "a", "b" };
    const a = set: {
        inline for (ms) |m| {
            if (mem.eql(u8, "a", m))
                break :set m;
        }
        break :set "c";
    };

    const a2: [:0]const u8 = "a";
    try testing.expectEqual(a2, a);
}
(No debugging symbols found in zig)
(gdb) r test src/main.zig
Starting program: /home/rich/bin/zig test src/main.zig
[New LWP 27870]
[New LWP 27871]
[New LWP 27872]
[New LWP 27873]
[New LWP 27874]
[New LWP 27875]
[New LWP 27876]
[New LWP 27877]

Thread 1 "zig" received signal SIGSEGV, Segmentation fault.
0x00000000073b15f0 in llvm::BasicBlock::getContext() const ()
(gdb) bt
#0  0x00000000073b15f0 in llvm::BasicBlock::getContext() const ()
#1  0x0000000007470662 in llvm::BranchInst::BranchInst(llvm::BasicBlock*, llvm::BasicBlock*, llvm::Value*, llvm::Instruction*) ()
#2  0x00000000073e93c0 in LLVMBuildCondBr ()
#3  0x000000000333b6fe in ?? ()
#4  0x000000000332fefb in ?? ()
#5  0x000000000332cfee in codegen_build_object(CodeGen*) ()
#6  0x0000000003325018 in zig_stage1_build_object ()
#7  0x0000000002fa995d in ?? ()
#8  0x0000000000000000 in ?? ()

@chrboesch
Copy link
Sponsor Contributor

With zig version 0.11.0-dev.1782+b52be973d everything works fine.

@richiejp
Copy link
Sponsor

No crash in 0.10.1 either!

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

No branches or pull requests

5 participants