crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.34k stars 1.61k forks source link

Compiler crash printing match data via constants #5211

Open maiha opened 6 years ago

maiha commented 6 years ago

I am getting a compiler crash with printing match data via constants.

minimum code

X = "#{$1}"
p X
% crystal test.cr
Using compiled compiler at `.build/crystal'
Invalid memory access (signal 11) at address 0x0
[0x936cc5] *CallStack::print_backtrace:Int32 +117
[0x91b616] __crystal_sigfault_handler +70
[0x257c627] sigfault_handler +40

FYI

It just raises an expected error without constants.

p "#{$1}"  # raises "Nil assertion failed (Exception)"

version

Got it in crystal-0.24.0(latest master). It maybe occur in all versions.

% crystal -v
Using compiled compiler at `.build/crystal'
Crystal 0.24.0 [ea4187c] (2017-10-29)

LLVM: 3.8.0
Default target: x86_64-pc-linux-gnu

for 0.24.0

We've removed special syntax $0 in 0.24.0. But some users still use it as PROGRAM_NAME. :smile: https://github.com/maiha/opts.cr/commit/5d7e3806ca5a2fc5e132f4df12ba1ff8f7f81244#diff-1953d238d9a0113d71840d37088c52cfL2

I hope that it will be announced as breaking-change.

Thank you,

bew commented 6 years ago

Well, if you don't have a regex match before, it will fail, this works:

"abc".match(/a(b)/);
puts $1 # => "b"
jhass commented 5 years ago

Produces a module validation error these days:

Module validation failed: loading unsized types is not allowed
  load void, void* %73
Call parameter type does not match function signature!
  load void, void* %73
 %String*  %74 = call %String* @"*p<String>:String"(void <badref>), !dbg !77
 (Exception)
straight-shoota commented 3 years ago

Now we're back to a compiler segfault (for the original code snipped).

HertzDevil commented 1 year ago

This triggers an assertion failure on a debug build of LLVM:

crystal: /home/quinton/llvm-16.0.0.src/lib/IR/Type.cpp:731: static llvm::PointerType* llvm::PointerType::get(llvm::Type*, unsigned int): Assertion `isValidElementType(EltTy) && "Invalid type for pointer element!"' failed.
Process 1006 stopped
* thread #1, name = 'crystal', stop reason = signal SIGABRT
    frame #0: 0x00007ffff79f9ccc libc.so.6`___lldb_unnamed_symbol3516 + 268
libc.so.6`___lldb_unnamed_symbol3516:
->  0x7ffff79f9ccc <+268>: movl   %eax, %ebp
    0x7ffff79f9cce <+270>: negl   %ebp
    0x7ffff79f9cd0 <+272>: cmpl   $0xfffff000, %eax         ; imm = 0xFFFFF000
    0x7ffff79f9cd5 <+277>: movl   $0x0, %eax
(lldb) bt
* thread #1, name = 'crystal', stop reason = signal SIGABRT
  * frame #0: 0x00007ffff79f9ccc libc.so.6`___lldb_unnamed_symbol3516 + 268
    frame #1: 0x00007ffff79aaef2 libc.so.6`raise + 18
    frame #2: 0x00007ffff7995472 libc.so.6`abort + 211
    frame #3: 0x00007ffff7995395 libc.so.6`___lldb_unnamed_symbol3060 + 15
    frame #4: 0x00007ffff79a3df2 libc.so.6`__assert_fail + 66
    frame #5: 0x000055555d542271 crystal`llvm::PointerType::get(EltTy=0x000055555dc23388, AddressSpace=0) at Type.cpp:731:3
    frame #6: 0x000055555d44e3a7 crystal`llvm::GlobalValue::GlobalValue(this=0x000055555f68d130, Ty=0x000055555dc23388, VTy=GlobalVariableVal, Ops=0x000055555f68d110, NumOps=0, Linkage=ExternalLinkage, Name=0x00007fffffffca00, AddressSpace=0) at GlobalValue.h:78:34
    frame #7: 0x000055555d44e4e3 crystal`llvm::GlobalObject::GlobalObject(this=0x000055555f68d130, Ty=0x000055555dc23388, VTy=GlobalVariableVal, Ops=0x000055555f68d110, NumOps=0, Linkage=ExternalLinkage, Name=0x00007fffffffca00, AddressSpace=0) at GlobalObject.h:46:70
    frame #8: 0x000055555d455011 crystal`llvm::GlobalVariable::GlobalVariable(this=0x000055555f68d130, M=0x000055555dc24da0, Ty=0x000055555dc23388, constant=false, Link=ExternalLinkage, InitVal=0x0000000000000000, Name=0x00007fffffffca00, Before=0x0000000000000000, TLMode=NotThreadLocal, AddressSpace= Has Value=false , isExternallyInitialized=false) at Globals.cpp:438:62
    frame #9: 0x000055555d36fb3e crystal`::LLVMAddGlobal(M=0x000055555dc24da0, Ty=0x000055555dc23388, Name="X") at Core.cpp:2087:77
    frame #10: 0x000055555a2fd072 crystal`add at global_collection.cr:8:15
    frame #11: 0x000055555a26f35c crystal`declare_const at const.cr:46:14
    frame #12: 0x000055555a26fe6d crystal`declare_const_and_initialized_flag at const.cr:71:6
    frame #13: 0x000055555a26fbc1 crystal`initialize_const at const.cr:127:32
    frame #14: 0x000055555a29add5 crystal`codegen_assign at codegen.cr:1012:9
    frame #15: 0x000055555a29acba crystal`visit at codegen.cr:1001:7
    frame #16: 0x00005555593d838e crystal`accept at visitor.cr:27:12
    frame #17: 0x000055555a266c31 crystal`accept at codegen.cr:2334:7
    frame #18: 0x000055555a29a291 crystal`visit at codegen.cr:679:9
    frame #19: 0x00005555593d7d62 crystal`accept at visitor.cr:27:12
    frame #20: 0x000055555a266c31 crystal`accept at codegen.cr:2334:7
    frame #21: 0x00005555591c6df6 crystal`codegen at codegen.cr:74:7
    frame #22: 0x00005555591c6dad crystal`codegen:debug:single_module at codegen.cr:72:5
    frame #23: 0x000055555a239aea crystal`codegen at compiler.cr:257:9
    frame #24: 0x000055555a23e41b crystal`compile at compiler.cr:162:16
    frame #25: 0x000055555a3a69b5 crystal`compile at command.cr:338:3
    frame #26: 0x000055555a39e320 crystal`run_command at command.cr:221:5
    frame #27: 0x000055555a398bdb crystal`run at command.cr:122:7
    frame #28: 0x000055555a3980ab crystal`run at command.cr:51:5
    frame #29: 0x000055555a39801e crystal`run at command.cr:50:3
    frame #30: 0x0000555558f3d562 crystal`__crystal_main at crystal.cr:11:1
    frame #31: 0x000055555913a343 crystal`main_user_code at main.cr:115:5
    frame #32: 0x000055555913a2aa crystal`main at main.cr:101:7
    frame #33: 0x0000555558f4aa46 crystal`main at main.cr:127:3
    frame #34: 0x00007ffff799618a libc.so.6`___lldb_unnamed_symbol3155 + 122
    frame #35: 0x00007ffff7996245 libc.so.6`__libc_start_main + 133
    frame #36: 0x0000555558f3ca61 crystal`_start + 33