Invalid memory access (signal 11) during specs

I know we’ve all seen this error before, and there’s been a few other issues and forum posts on it. I’m starting this one because all of the others seem to veer off in to more specific OS related deals, but I feel like this is a much larger issue.

I’m currently working on an update for Avram, and during this update I ripped out a bunch of code. As I slowly put some in, there’s several places where adding in macro code will throw this stack when running specs.

avram (refactors/operation)$ crystal spec spec/operation_specs/save_operation_spec.cr
Invalid memory access (signal 11) at address 0x0
[0x1097f8e97] *CallStack::print_backtrace:Int32 +39
[0x10974247a] __crystal_sigfault_handler +522
[0x7fff6c5075fd] _sigtramp +29
[0x10a7f0047] _ZN4llvm11PointerType3getEPNS_4TypeEj +21
[0x10a3c40cf] _ZN4llvm17GetElementPtrInst16getGEPReturnTypeEPNS_4TypeEPNS_5ValueENS_8ArrayRefIS4_EE +63
[0x10a3e117b] _ZN4llvm17GetElementPtrInst6CreateEPNS_4TypeEPNS_5ValueENS_8ArrayRefIS4_EERKNS_5TwineEPNS_11InstructionE +107
[0x109c1c762] _ZN4llvm9IRBuilderINS_14ConstantFolderENS_24IRBuilderDefaultInserterEE17CreateInBoundsGEPEPNS_4TypeEPNS_5ValueENS_8ArrayRefIS7_EERKNS_5TwineE +130
[0x10a763fec] LLVMBuildInBoundsGEP +84
[0x10935bec8] *Crystal::CodeGenVisitor@Crystal::LLVMBuilderHelper#gep<LLVM::Value, Int32, Int32, String>:LLVM::Value +168
[0x1093bce24] *Crystal::CodeGenVisitor#read_instance_var<Crystal::Type+, Crystal::Type+, String, LLVM::Value>:Bool +100
[0x1093840b6] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +23542
[0x1093c89ca] *Crystal::CodeGenVisitor#visit<Crystal::Assign>:(Bool | Nil) +298
[0x10937ea71] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +1457
[0x1093cd9db] *Crystal::CodeGenVisitor#codegen_cond<Crystal::ASTNode+>:LLVM::Value +27
[0x109381e56] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14742
[0x10939aff7] *Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function +4711
[0x1093b73a8] *Crystal::CodeGenVisitor#target_def_fun<Crystal::Def+, Crystal::Type+>:LLVM::Function +2664
[0x1093a9718] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +2632
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x10939aff7] *Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function +4711
[0x1093b73a8] *Crystal::CodeGenVisitor#target_def_fun<Crystal::Def+, Crystal::Type+>:LLVM::Function +2664
[0x1093a9718] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +2632
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x1093b5015] *Crystal::CodeGenVisitor#prepare_call_args_non_external<Crystal::Call, Crystal::Def+, Crystal::Type+>:Tuple(Array(LLVM::Value), Bool) +1461
[0x1093a96f0] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +2592
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x10937eb49] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +1673
[0x109386618] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +33112
[0x1093b4b5b] *Crystal::CodeGenVisitor#prepare_call_args_non_external<Crystal::Call, Crystal::Def+, Crystal::Type+>:Tuple(Array(LLVM::Value), Bool) +251
[0x1093a96f0] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +2592
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x1093b5015] *Crystal::CodeGenVisitor#prepare_call_args_non_external<Crystal::Call, Crystal::Def+, Crystal::Type+>:Tuple(Array(LLVM::Value), Bool) +1461
[0x1093a96f0] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +2592
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x10937eb49] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +1673
[0x1093a9edd] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +4621
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x10939aff7] *Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function +4711
[0x1093b73a8] *Crystal::CodeGenVisitor#target_def_fun<Crystal::Def+, Crystal::Type+>:LLVM::Function +2664
[0x1093a9718] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +2632
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x1093b4b5b] *Crystal::CodeGenVisitor#prepare_call_args_non_external<Crystal::Call, Crystal::Def+, Crystal::Type+>:Tuple(Array(LLVM::Value), Bool) +251
[0x1093a96f0] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +2592
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x10939aff7] *Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function +4711
[0x1093b73a8] *Crystal::CodeGenVisitor#target_def_fun<Crystal::Def+, Crystal::Type+>:LLVM::Function +2664
[0x1093a9718] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +2632
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x1093b4b5b] *Crystal::CodeGenVisitor#prepare_call_args_non_external<Crystal::Call, Crystal::Def+, Crystal::Type+>:Tuple(Array(LLVM::Value), Bool) +251
[0x1093a96f0] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +2592
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x10939aff7] *Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function +4711
[0x1093b73a8] *Crystal::CodeGenVisitor#target_def_fun<Crystal::Def+, Crystal::Type+>:LLVM::Function +2664
[0x1093a9718] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +2632
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x1093cd9db] *Crystal::CodeGenVisitor#codegen_cond<Crystal::ASTNode+>:LLVM::Value +27
[0x109381e56] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14742
[0x10937eb49] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +1673
[0x1093a9edd] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +4621
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x10937eb49] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +1673
[0x10939aff7] *Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function +4711
[0x109380651] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +8593
[0x1093a8fbe] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +750
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x10937eb49] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +1673
[0x10939aff7] *Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function +4711
[0x109380651] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +8593
[0x1093a8fbe] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +750
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x10937eb49] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +1673
[0x10939aff7] *Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function +4711
[0x109380651] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +8593
[0x1093a8fbe] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +750
[0x109381c38] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +14200
[0x10937eb49] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +1673
[0x10937ef54] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +2708
[0x10937eb49] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +1673
[0x109331a39] *Crystal::Compiler#codegen<Crystal::Program, Crystal::ASTNode+, Array(Crystal::Compiler::Source), String>:(Tuple(Array(Crystal::Compiler::CompilationUnit), Array(String)) | Nil) +185
[0x10975c8d7] *Crystal::Compiler#compile<Array(Crystal::Compiler::Source), String>:Crystal::Compiler::Result +135
[0x1097cfb13] *Crystal::Command#spec:NoReturn +3043
[0x10974f2ec] *Crystal::Command#run:(Bool | Crystal::Compiler::Result | Nil) +27948
[0x108daae64] __crystal_main +6836
[0x108db00e1] main +65

In this case, adding these lines will cause is to blow up. If I remove these lines and run the spec, I’m able to get a normal compile error.

In my case here, this seems to be localized to the SaveOperation stuff. If I remove that, then I can get specs to pass. The difference between a SaveOperation and the Operation is that SaveOperation will map DB columns to methods for use when saving a model. These lines will generate a cast_value method that takes the type of each DB column.

Even more interesting that I noticed in the first few lines of the stack was mention of Bool. It also appears that the column that’s trying to be mapped is a Bool column. So maybe this is trying to cast Bool to String and then Crystal freaks out?

I’m currently using 0.34. I wanted to try this out with 0.35, but there’s a lot more issues to fix to get it to compile with that.

avram (refactors/operation)$ crystal -v
Crystal 0.34.0 (2020-04-07)

LLVM: 10.0.0
Default target: x86_64-apple-macosx

I also mentioned some references to a similar error in this issue, but I think that original issue is something separate and not really related. There’s also a lot of other related issues so I didn’t want to just open a new one, and I’m not sure which error to append this to.

If anyone has any insight, or ideas of how to get around this, I’d very much appreciate it. I’ll keep hammering away until then though.

Thanks!

1 Like

Actual repro:

git init               
git fetch https://github.com/luckyframework/avram b02502dab8b7fc304bcd2d7bf21a165dfa3dac57
git checkout FETCH_HEAD
shards install
crystal build spec/operation_specs/save_operation_spec.cr

Invalid memory access (signal 11) at address 0x0

1 Like

Thanks for posting that!

I managed to reduce it to this:

class Foo
  @x = 0
end

class Bar(T) < Foo
  @x = 0
end

class Baz < Bar(Int32)
  @y = true
end

Baz.new
1 Like

wow. that’s wild. Great find! Sorry you had to dig through a mountain to find it :frowning_face: but this will be huge!