Skip to content
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

Interpreter: invalid memory access with StaticArray#sort_by StaticArray#unstable_sort_by #12253

Closed
cyangle opened this issue Jul 10, 2022 · 1 comment · Fixed by #12259
Closed
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:interpreter

Comments

@cyangle
Copy link
Contributor

cyangle commented Jul 10, 2022

Bug Report

Example code:

a = StaticArray["foo", "a", "hello"]
puts a.unstable_sort_by(&.size)
puts a.sort_by(&.size)

Interpreter crashes with invalid memory access error:

Invalid memory access (signal 11) at address 0x8
[0x56388624e046] *Exception::CallStack::print_backtrace:Nil +118 in /home/chao/git/personal/crystal/.build/crystal
[0x56388621b34a] ~procProc(Int32, Pointer(LibC::SiginfoT), Pointer(Void), Nil) +330 in /home/chao/git/personal/crystal/.build/crystal
[0x7f10a9530520] ?? +139709536994592 in /lib/x86_64-linux-gnu/libc.so.6
[0x7f10a968e995] ?? +139709538429333 in /lib/x86_64-linux-gnu/libc.so.6
[0x563886241d1c] *Pointer(UInt8) +108 in /home/chao/git/personal/crystal/.build/crystal
[0x563886241ca1] *Pointer(UInt8) +17 in /home/chao/git/personal/crystal/.build/crystal
[0x563886241e71] *Pointer(UInt8) +17 in /home/chao/git/personal/crystal/.build/crystal
[0x563887159e27] *Crystal::Repl::Interpreter#interpret<Crystal::ASTNode+, Crystal::Type+>:Crystal::Repl::Value +394199 in /home/chao/git/personal/crystal/.build/crystal
[0x5638870f8fc9] *Crystal::Repl::Interpreter#interpret<Crystal::ASTNode+, Hash(String, Crystal::MetaVar)>:Crystal::Repl::Value +2281 in /home/chao/git/personal/crystal/.build/crystal
[0x5638876d8d3a] *Crystal::Repl#interpret<Crystal::Expressions>:Crystal::Repl::Value +106 in /home/chao/git/personal/crystal/.build/crystal
[0x5638876d8a56] *Crystal::Repl#interpret_and_exit_on_error<Crystal::Expressions>:Crystal::Repl::Value +38 in /home/chao/git/personal/crystal/.build/crystal
[0x5638876d9626] *Crystal::Repl#run_file<String, Array(String)>:Crystal::Repl::Value +230 in /home/chao/git/personal/crystal/.build/crystal
[0x563887615075] *Crystal::Command#repl:(Crystal::Repl::Value | Nil) +1669 in /home/chao/git/personal/crystal/.build/crystal
[0x56388760eb46] *Crystal::Command#run:(Bool | Crystal::Repl::Value | Nil) +1414 in /home/chao/git/personal/crystal/.build/crystal
[0x56388760e43c] *Crystal::Command::run<Array(String)>:(Bool | Crystal::Repl::Value | Nil) +44 in /home/chao/git/personal/crystal/.build/crystal
[0x56388760e3ae] *Crystal::Command::run:(Bool | Crystal::Repl::Value | Nil) +46 in /home/chao/git/personal/crystal/.build/crystal
[0x5638861f89ec] __crystal_main +2668 in /home/chao/git/personal/crystal/.build/crystal
[0x563886401485] *Crystal::main_user_code<Int32, Pointer(Pointer(UInt8))>:Nil +37 in /home/chao/git/personal/crystal/.build/crystal
[0x5638864013ce] *Crystal::main<Int32, Pointer(Pointer(UInt8))>:Int32 +62 in /home/chao/git/personal/crystal/.build/crystal
[0x563886206026] main +6 in /home/chao/git/personal/crystal/.build/crystal
[0x7f10a9517d90] ?? +139709536894352 in /lib/x86_64-linux-gnu/libc.so.6
[0x7f10a9517e40] __libc_start_main +128 in /lib/x86_64-linux-gnu/libc.so.6
[0x5638861f7eb5] _start +37 in /home/chao/git/personal/crystal/.build/crystal
[0x0] ???
@cyangle cyangle added the kind:bug A bug in the code. Does not apply to documentation, specs, etc. label Jul 10, 2022
@asterite
Copy link
Member

It seems there's a dup call inside StaticArray that doesn't return what it should.

Here's a reduction:

struct Foo
  def initialize(@x : Int32)
  end

  def foo
    dup
  end
end

a = Foo.new(1)
b = a.foo
puts b # Foo(@x=65748992) ???

When I change dup to self in StaticArray it starts working fine, so there must be something wrong in the interpreter regarding dup. It's strange because dup just returns self.

Here's another reduction that doesn't depend on dup:

struct Foo
  def initialize(@x : Int32)
  end

  def foo
    me
  end

  def me
    self
  end
end

a = Foo.new(1)
b = a.foo
puts b # => Foo(@x=70352896)

This should be easy to fix. I'll check it later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:interpreter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants