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

Segfault when creating dynamically allocated union(enum) #723

Closed
tiehuis opened this Issue Jan 26, 2018 · 2 comments

Comments

Projects
None yet
3 participants
@tiehuis
Member

tiehuis commented Jan 26, 2018

const Expr = union(enum) {
    Literal: u8,
    Question: &Expr,
};

test "parse" {
    const alloc = @import("std").debug.global_allocator;
    try alloc.create(Expr);
}
zig test testcase.zig
zsh: segmentation fault (core dumped)

backtrace

#0  0x00007ffff34e2e6d in vfprintf () from /usr/lib/libc.so.6
#1  0x00007ffff350c632 in vsnprintf () from /usr/lib/libc.so.6
#2  0x0000555555d7eb1d in buf_appendf (buf=0x555558152d88, format=0x555556ec6490 "[]%s") at /home/me/src/zig/src/buffer.cpp:46
#3  0x0000555555d600ca in get_slice_type (g=0x5555576d8dc0, ptr_type=0x5555578e3590) at /home/me/src/zig/src/analyze.cpp:669
#4  0x0000555555d60177 in get_slice_type (g=0x5555576d8dc0, ptr_type=0x5555578e3590) at /home/me/src/zig/src/analyze.cpp:677
#5  0x0000555555d60177 in get_slice_type (g=0x5555576d8dc0, ptr_type=0x5555578e3590) at /home/me/src/zig/src/analyze.cpp:677
#6  0x0000555555d60177 in get_slice_type (g=0x5555576d8dc0, ptr_type=0x5555578e3590) at /home/me/src/zig/src/analyze.cpp:677
#7  0x0000555555d60177 in get_slice_type (g=0x5555576d8dc0, ptr_type=0x5555578e3590) at /home/me/src/zig/src/analyze.cpp:677
#8  0x0000555555d60177 in get_slice_type (g=0x5555576d8dc0, ptr_type=0x5555578e3590) at /home/me/src/zig/src/analyze.cpp:677
#9  0x0000555555d60177 in get_slice_type (g=0x5555576d8dc0, ptr_type=0x5555578e3590) at /home/me/src/zig/src/analyze.cpp:677

@tiehuis tiehuis added the bug label Jan 26, 2018

@bnoordhuis

This comment has been minimized.

Member

bnoordhuis commented Jan 27, 2018

It looks to be infinite recursion. A random backtrace sampling:

#16190 0x0000555555d662e7 in get_slice_type (g=0x55555762fdc0, ptr_type=0x55555783a7c0) at /home/bnoordhuis/src/zig/src/analyze.cpp:677
#16191 0x0000555555d662e7 in get_slice_type (g=0x55555762fdc0, ptr_type=0x55555783a7c0) at /home/bnoordhuis/src/zig/src/analyze.cpp:677
#16192 0x0000555555d662e7 in get_slice_type (g=0x55555762fdc0, ptr_type=0x55555783a7c0) at /home/bnoordhuis/src/zig/src/analyze.cpp:677
#16193 0x0000555555d662e7 in get_slice_type (g=0x55555762fdc0, ptr_type=0x55555783a7c0) at /home/bnoordhuis/src/zig/src/analyze.cpp:677
#16194 0x0000555555d662e7 in get_slice_type (g=0x55555762fdc0, ptr_type=0x55555783a7c0) at /home/bnoordhuis/src/zig/src/analyze.cpp:677
#16195 0x0000555555d662e7 in get_slice_type (g=0x55555762fdc0, ptr_type=0x55555783a7c0) at /home/bnoordhuis/src/zig/src/analyze.cpp:677
#16196 0x0000555555d662e7 in get_slice_type (g=0x55555762fdc0, ptr_type=0x55555783a7c0) at /home/bnoordhuis/src/zig/src/analyze.cpp:677
#16197 0x0000555555d662e7 in get_slice_type (g=0x55555762fdc0, ptr_type=0x55555783a7c0) at /home/bnoordhuis/src/zig/src/analyze.cpp:677
#16198 0x0000555555d662e7 in get_slice_type (g=0x55555762fdc0, ptr_type=0x55555783a7c0) at /home/bnoordhuis/src/zig/src/analyze.cpp:677
#16199 0x0000555555d662e7 in get_slice_type (g=0x55555762fdc0, ptr_type=0x55555783a7c0) at /home/bnoordhuis/src/zig/src/analyze.cpp:677
#16200 0x0000555555d662e7 in get_slice_type (g=0x55555762fdc0, ptr_type=0x55555783a7c0) at /home/bnoordhuis/src/zig/src/analyze.cpp:677

edit: and the bottom:

#23802 0x0000555555d662e7 in get_slice_type (g=0x55555762fdc0, ptr_type=0x55555783a7c0) at /home/bnoordhuis/src/zig/src/analyze.cpp:677
#23803 0x0000555555d662e7 in get_slice_type (g=0x55555762fdc0, ptr_type=0x55555783a7c0) at /home/bnoordhuis/src/zig/src/analyze.cpp:677
#23804 0x0000555555d662e7 in get_slice_type (g=0x55555762fdc0, ptr_type=0x55555783a7c0) at /home/bnoordhuis/src/zig/src/analyze.cpp:677
#23805 0x0000555555dcaf4b in ir_analyze_instruction_slice_type (ira=0x555557ada370, slice_type_instruction=0x555557ada050) at /home/bnoordhuis/src/zig/src/ir.cpp:11785
#23806 0x0000555555dd7672 in ir_analyze_instruction_nocast (ira=0x555557ada370, instruction=0x555557ada050) at /home/bnoordhuis/src/zig/src/ir.cpp:15282
#23807 0x0000555555dd7d1f in ir_analyze_instruction (ira=0x555557ada370, instruction=0x555557ada050) at /home/bnoordhuis/src/zig/src/ir.cpp:15428
#23808 0x0000555555dd7fc2 in ir_analyze (codegen=0x55555762fdc0, old_exec=0x555557ad9d00, new_exec=0x555557ada2d0, expected_type=0x55555763dfe0, expected_type_source_node=0x5555577bc3a0) at /home/bnoordhuis/src/zig/src/ir.cpp:15479
#23809 0x0000555555dba393 in ir_eval_const_value (codegen=0x55555762fdc0, scope=0x555557ad9cd0, node=0x5555577bc3a0, expected_type=0x55555763dfe0, backward_branch_count=0x7fffffffd0c0, backward_branch_quota=1000, fn_entry=0x0, c_import_buf=0x0, source_node=0x5555577bc3a0, exec_name=0x0, parent_exec=0x0) at /home/bnoordhuis/src/zig/src/ir.cpp:7118
#23810 0x0000555555d67a43 in analyze_const_value (g=0x55555762fdc0, scope=0x555557ad9cd0, node=0x5555577bc3a0, type_entry=0x55555763dfe0, type_name=0x0) at /home/bnoordhuis/src/zig/src/analyze.cpp:1057
#23811 0x0000555555d67a96 in analyze_type_expr (g=0x55555762fdc0, scope=0x555557ad9cd0, node=0x5555577bc3a0) at /home/bnoordhuis/src/zig/src/analyze.cpp:1061
#23812 0x0000555555dc4b0d in ir_analyze_fn_call (ira=0x555557ad6650, call_instruction=0x555557ad57b0, fn_entry=0x555557ad69c0, fn_type=0x555557ad7490, fn_ref=0x0, first_arg_ptr=0x555557ad68f0, comptime_fn_call=false, fn_inline=FnInlineAuto) at /home/bnoordhuis/src/zig/src/ir.cpp:10072
#23813 0x0000555555dc5552 in ir_analyze_instruction_call (ira=0x555557ad6650, call_instruction=0x555557ad57b0) at /home/bnoordhuis/src/zig/src/ir.cpp:10219
#23814 0x0000555555dd756a in ir_analyze_instruction_nocast (ira=0x555557ad6650, instruction=0x555557ad57b0) at /home/bnoordhuis/src/zig/src/ir.cpp:15260
#23815 0x0000555555dd7d1f in ir_analyze_instruction (ira=0x555557ad6650, instruction=0x555557ad57b0) at /home/bnoordhuis/src/zig/src/ir.cpp:15428
#23816 0x0000555555dd7fc2 in ir_analyze (codegen=0x55555762fdc0, old_exec=0x55555783ae30, new_exec=0x55555783aec8, expected_type=0x55555783cb10, expected_type_source_node=0x5555577ba7d0) at /home/bnoordhuis/src/zig/src/ir.cpp:15479
#23817 0x0000555555d704b1 in analyze_fn_ir (g=0x55555762fdc0, fn_table_entry=0x55555783adc0, return_type_node=0x5555577ba7d0) at /home/bnoordhuis/src/zig/src/analyze.cpp:3721
#23818 0x0000555555d707a0 in analyze_fn_body (g=0x55555762fdc0, fn_table_entry=0x55555783adc0) at /home/bnoordhuis/src/zig/src/analyze.cpp:3773
#23819 0x0000555555d713b5 in semantic_analyze (g=0x55555762fdc0) at /home/bnoordhuis/src/zig/src/analyze.cpp:3977
#23820 0x0000555555d9d5bc in gen_root_source (g=0x55555762fdc0) at /home/bnoordhuis/src/zig/src/codegen.cpp:5734
#23821 0x0000555555d9f2af in codegen_build (g=0x55555762fdc0) at /home/bnoordhuis/src/zig/src/codegen.cpp:6253
#23822 0x0000555555dedb8a in main (argc=3, argv=0x7fffffffdb08) at /home/bnoordhuis/src/zig/src/main.cpp:879

@andrewrk andrewrk added this to the 0.2.0 milestone Jan 27, 2018

@andrewrk

This comment has been minimized.

Member

andrewrk commented Jan 27, 2018

I've narrowed the problem down slightly:

--- a/src/analyze.cpp
+++ b/src/analyze.cpp
@@ -362,8 +362,10 @@ TypeTableEntry *get_pointer_to_type_extra(CodeGen *g, TypeTableEntry *child_type
     } else {
         assert(bit_offset == 0);
         parent_pointer = &child_type->pointer_parent[(is_const ? 1 : 0)];
-        if (*parent_pointer)
+        if (*parent_pointer) {
+            assert((*parent_pointer)->data.pointer.alignment == byte_alignment);
             return *parent_pointer;
+        }
     }
 
     type_ensure_zero_bits_known(g, child_type);

still investigating.

@andrewrk andrewrk closed this in f66ac9a Jan 27, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment