Crystal: Crash: Invalid memory access (signal 11) at address 0x0

Created on 14 May 2017  ยท  4Comments  ยท  Source: crystal-lang/crystal

crystal build errors with a Invalid memory access (signal 11) at address 0x0.

Affected versions

crystal --version: Crystal 0.22.0 (2017-04-20) LLVM 4.0.0

Crystal GitHub master is also affected.

uname -a: Darwin Tahontaenrat.local 16.5.0 Darwin Kernel Version 16.5.0: Fri Mar 3 16:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64 x86_64 i386 MacBookPro8,1 Darwin

Steps to reproduce

Create a file with the contents:

lib LibRuby
  type VALUE = Void*
  type ID = Void*

  $rb_cObject : VALUE

  fun rb_const_get(value : VALUE, id : ID)

  fun rb_str_new_cstr(str : UInt8*) : VALUE
  fun rb_intern(name : UInt8*) : ID
  fun rb_hash_new() : VALUE

  fun rb_funcall(obj : VALUE, func : ID, args : Int32, ...) : VALUE
end

class String
  def to_ruby
    LibRuby.rb_str_new_cstr(self)
  end
end

class Hash
  def to_ruby
    LibRuby.rb_hash_new
  end
end

module DMark
  class ElementNode
    ID_NEW = LibRuby.rb_intern("new")

    getter :attributes

    def initialize(@attributes : Hash(String, String))
    end

    def to_ruby
      mod_dmark = LibRuby.rb_const_get(LibRuby.rb_cObject, LibRuby.rb_intern("DMark"))
      class_dmark_elementnode = LibRuby.rb_const_get(mod_dmark, LibRuby.rb_intern("ElementNode"))

      LibRuby.rb_funcall(class_dmark_elementnode, ID_NEW, 1, @attributes.to_ruby)
    end
  end
end

DMark::ElementNode.new({ "foo" => "bar" }).to_ruby

Run crystal build foo.cr.

Expected output

The source file successfully compiles.

Actual output

Crash in the Crystal compiler:

Invalid memory access (signal 11) at address 0x0
[0x10410f262] __crystal_sigfault_handler +66
[0x7fffd97f0b3a] _sigtramp +26
[0x1065cc193] _ZN4llvm11PointerType3getEPNS_4TypeEj +21
[0x106549617] _ZN4llvm25ConstantFoldGetElementPtrEPNS_4TypeEPNS_8ConstantEbNS_8OptionalIjEENS_8ArrayRefIPNS_5ValueEEE +297
[0x106559680] _ZN4llvm12ConstantExpr16getGetElementPtrEPNS_4TypeEPNS_8ConstantENS_8ArrayRefIPNS_5ValueEEEbNS_8OptionalIjEES2_ +110
[0x105cbe134] _ZN4llvm12ConstantExpr24getInBoundsGetElementPtrEPNS_4TypeEPNS_8ConstantENS_8ArrayRefIPNS_5ValueEEE +36
[0x10609cb84] _ZN4llvm9IRBuilderINS_14ConstantFolderENS_24IRBuilderDefaultInserterEE17CreateInBoundsGEPEPNS_4TypeEPNS_5ValueENS_8ArrayRefIS7_EERKNS_5TwineE +84
[0x1065680c0] LLVMBuildInBoundsGEP +55
[0x10502132d] *Crystal::CodeGenVisitor@Crystal::LLVMBuilderHelper#gep<LLVM::Value, Int32, Int32, String>:LLVM::Value +173
[0x10508d816] *Crystal::CodeGenVisitor#codegen_direct_abi_call<LLVM::Value, LLVM::ABI::ArgType>:LLVM::Value +182
[0x1050800cf] *Crystal::CodeGenVisitor#visit<Crystal::Call>:Bool +7775
[0x105049ccd] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +3485
[0x10504a0f3] *Crystal::ASTNode+@Crystal::ASTNode#accept<Crystal::CodeGenVisitor>:Nil +4547
[0x10506c57c] *Crystal::CodeGenVisitor#codegen_fun<String, Crystal::Def+, Crystal::Type+, Bool, Crystal::CodeGenVisitor::ModuleInfo, Bool, Bool>:LLVM::Function +8604
[0x105072c70] *Crystal::CodeGenVisitor#target_def_fun<Crystal::Def+, Crystal::Type+>:LLVM::Function +16032
[0x1050822d7] *Crystal::CodeGenVisitor#visit<Crystal:โŽ
bug compiler

Most helpful comment

I fixed the crash but I also made it so that now when you assign or pass a lib fun call that has a void value you will get a compile error. So the original snippet now gives this error:

Error in foo.cr:46: instantiating 'DMark::ElementNode#to_ruby()'

DMark::ElementNode.new({"foo" => "bar"}).to_ruby
                                         ^~~~~~~

in foo.cr:38: assigning Void return value of lib fun call has no effect

      mod_dmark = LibRuby.rb_const_get(LibRuby.rb_cObject, LibRuby.rb_intern("DMark"))
                          ^~~~~~~~~~~~

All 4 comments

The compiler crash is due to a missing return value type for rb_const_get

Shouldn't crash the compiler though.

Reduced:

lib LibFoo
  fun foo(Void*, ...)
end

x = nil
LibFoo.foo(x)

I fixed the crash but I also made it so that now when you assign or pass a lib fun call that has a void value you will get a compile error. So the original snippet now gives this error:

Error in foo.cr:46: instantiating 'DMark::ElementNode#to_ruby()'

DMark::ElementNode.new({"foo" => "bar"}).to_ruby
                                         ^~~~~~~

in foo.cr:38: assigning Void return value of lib fun call has no effect

      mod_dmark = LibRuby.rb_const_get(LibRuby.rb_cObject, LibRuby.rb_intern("DMark"))
                          ^~~~~~~~~~~~
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Papierkorb picture Papierkorb  ยท  3Comments

jhass picture jhass  ยท  3Comments

will picture will  ยท  3Comments

costajob picture costajob  ยท  3Comments

lgphp picture lgphp  ยท  3Comments