odin-lang / Odin

Odin Programming Language
https://odin-lang.org
BSD 3-Clause "New" or "Revised" License
6.55k stars 570 forks source link

Segfault while compiling when incorrectly passing a type while initializing a struct #3931

Open CobbCoding1 opened 1 month ago

CobbCoding1 commented 1 month ago

Context

The issue occurs with the following code:

package main

import "core:fmt"

Foo :: struct($T: typeid) {
    y: T,
}

Bar :: struct {
    x: Foo,
}

main :: proc() {
    x: Bar(int) = {1}
}

I accidentally made this mistake whilst writing an Odin program, and noticed it caused a segfault. This was the minimal reproducible example I could create. It is not very urgent as it only occurs when writing incorrect code, but it would be nice to have a proper error message.

Odin: dev-2024-07 OS: Arch Linux, Linux 6.9.8-arch1-1 CPU: Intel(R) Core(TM) i5-2400 CPU @ 3.10GHz RAM: 11920 MiB Backend: LLVM 18.1.8

Expected Behavior

If you remove the "Foo" struct, and use a builtin type such as int, it properly throws and error. I would expect it to give an error similar to this.

Current Behavior

Currently, it causes a segfault. This occurs during the compilation stage, not during runtime.

Failure Information (for bugs)

It will not output any text, it will simply crash with the generic segfault message: "Segmentation fault (core dumped)".

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. Copy the following code:
    
    package main

import "core:fmt"

Foo :: struct($T: typeid) { y: T, }

Bar :: struct { x: Foo, }

main :: proc() { x: Bar(int) = {1} }


2. Build the program using Odin.
3. It segfaults.

### Failure Logs

Segfaults as shown above, exit code is 139.
hbobenicio commented 1 month ago

Here is a backtrace against the master branch which may help finding the root cause.

gdb ./odin
r build main.odin -file
Thread 6 "odin" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffec5f9640 (LWP 2936634)]
0x000055555569f481 in check_polymorphic_record_type (c=0x7fffec5f78a0, operand=0x7fffec5f6e38, call=0x7fffe7d207d0) at src/check_expr.cpp:7286
7286        isize param_count = tuple->variables.count;
(gdb) bt
#0  0x000055555569f481 in check_polymorphic_record_type (c=0x7fffec5f78a0, operand=0x7fffec5f6e38, call=0x7fffe7d207d0) at src/check_expr.cpp:7286
#1  0x000055555562e582 in check_call_expr (c=0x7fffec5f78a0, operand=0x7fffec5f6e38, call=0x7fffe7d207d0, proc=0x7fffe7d206b0, args=..., inlining=ProcInlining_none, type_hint=0x0)
    at src/check_expr.cpp:7687
#2  0x00005555556193a2 in check_expr_base_internal (c=0x7fffec5f78a0, o=0x7fffec5f6e38, node=0x7fffe7d207d0, type_hint=0x0) at src/check_expr.cpp:10995
#3  0x0000555555616b4d in check_expr_base (c=0x7fffec5f78a0, o=0x7fffec5f6e38, node=0x7fffe7d207d0, type_hint=0x0) at src/check_expr.cpp:11126
#4  0x000055555562415d in check_expr_or_type (c=0x7fffec5f78a0, o=0x7fffec5f6e38, e=0x7fffe7d207d0, type_hint=0x0) at src/check_expr.cpp:11209
#5  0x000055555567746b in check_type_internal (ctx=0x7fffec5f78a0, e=0x7fffe7d207d0, type=0x7fffec5f7800, named_type=0x0) at src/check_type.cpp:3587
#6  0x0000555555674094 in check_type_expr (ctx=0x7fffec5f78a0, e=0x7fffe7d207d0, named_type=0x0) at src/check_type.cpp:3637
#7  0x000055555562fa76 in check_type (ctx=0x7fffec5f8760, e=0x7fffe7d207d0) at src/check_type.cpp:3632
#8  0x000055555565d7ed in check_value_decl_stmt (ctx=0x7fffec5f8760, node=0x7fffe7d20c90, mod_flags=32) at src/check_stmt.cpp:2028
#9  0x000055555565308d in check_stmt_internal (ctx=0x7fffec5f8760, node=0x7fffe7d20c90, flags=32) at src/check_stmt.cpp:2782
#10 0x0000555555651809 in check_stmt (ctx=0x7fffec5f8760, node=0x7fffe7d20c90, flags=32) at src/check_stmt.cpp:662
#11 0x000055555564f84d in check_stmt_list (ctx=0x7fffec5f8760, stmts=..., flags=32) at src/check_stmt.cpp:108
#12 0x000055555564dd56 in check_proc_body (ctx_=0x7fffec5f89a0, token=..., decl=0x7fffe59b4f00, type=0x7fffe80b85a0, body=0x7fffe7d20d30) at src/check_decl.cpp:1827
#13 0x000055555564cd83 in check_proc_info (c=0x7fffe7d1c6d0, pi=0x7fffe80b86b0, untyped=0x7fffe7f00d00) at src/checker.cpp:5794
#14 0x000055555564c6f6 in check_proc_info_worker_proc (data=0x7fffe80b86b0) at src/checker.cpp:5970
#15 0x00005555555c382e in thread_pool_thread_proc (thread=0x7fffee5ff578) at src/thread_pool.cpp:218
#16 0x00005555555c3398 in internal_thread_proc (arg=0x7fffee5ff578) at src/threading.cpp:561
#17 0x00007ffff0e94ac3 in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:442
#18 0x00007ffff0f26850 in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:81
(gdb) p tuple
$1 = (TypeTuple *) 0x0