modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
22.92k stars 2.58k forks source link

[BUG] running Mojo code with run ownership parameter causes crash on Mac without LLVM installed #3231

Open nzhang opened 2 months ago

nzhang commented 2 months ago

Bug description

I tried to run the following example program with (intended) wrong ownership annotation of one of the parameter. It output the correct error message but followed by a crash. I then realized that I didn't have LLVM installed on my mac. After installing LLVM (and llvm-symbolizer) the crash went away. So it is possible that the crash is caused by the fact that llvm-symbolizer is not found.

Steps to reproduce

fn pets(): var a = MyPet("Loki", 4) var b = MyPet("Sylvie", 2) print(a.name)

Zn: @value is a little confusing as it doe not mean immutable class.

# Zn: how to define a immutable struct in Mojo?
a.name = "fake"
# a.__del__() runs here for "Loki"

a = MyPet("Charlie", 8)
# a.__del__() runs immediately because "Charlie" is never used

print(b.name)
# b.__del__() runs here

fn main(): pets()


- If applicable, add screenshots to help explain the problem.
================ The error message: ===================

(base) nings-mbp:del ningzhang$ '/Users/ningzhang/.modular/pkg/packages.modular.com_max/bin/mojo' '/Users/ningzhang/test/mojo_max/del/del.mojo' /Users/ningzhang/test/mojo_max/del/del.mojo:15:33: error: existing value argument must be passed as owned fn moveinit(inout self, rhs: MyPet): ^ Please submit a bug report to https://github.com/modularml/mojo/issues and include the crash backtrace along with all the relevant source codes. Stack dump:

  1. Program arguments: /Users/ningzhang/.modular/pkg/packages.modular.com_max/bin/mojo /Users/ningzhang/test/mojo_max/del/del.mojo
  2. Crash resolving decl body at loc("/Users/ningzhang/test/mojo_max/del/del.mojo":2:8)

    struct MyPet: ^..... var name: String var age: Int

    fn __init__(inout self, owned n: String, a: Int):

    Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var LLVM_SYMBOLIZER_PATH to point to it): 0 mojo 0x0000000102f00c04 llvm_strlcpy + 51480 1 mojo 0x0000000102efeef0 llvm_strlcpy + 44036 2 mojo 0x0000000102f012a4 llvm_strlcpy + 53176 3 libsystem_platform.dylib 0x000000019d58f584 _sigtramp + 56 4 mojo 0x0000000103244488 jit_debug_register_code + 709568 5 mojo 0x0000000103244bbc __jit_debug_register_code + 711412 6 mojo 0x00000001031be52c jit_debug_register_code + 160868 7 mojo 0x00000001031cacb0 jit_debug_register_code + 211944 8 mojo 0x00000001031cb17c __jit_debug_register_code + 213172 9 mojo 0x00000001031d4788 jit_debug_register_code + 251584 10 mojo 0x00000001031d49a8 __jit_debug_register_code + 252128 11 mojo 0x0000000102e4e8c8 12 mojo 0x0000000102e606b0 13 mojo 0x0000000102e49940 14 dyld 0x000000019d1d60e0 start + 2360 mojo crashed! Please file a bug report. [30546:4088829:20240712,155400.429252:WARNING crash_report_exception_handler.cc:257] UniversalExceptionRaise: (os/kern) failure (5) Segmentation fault: 11

    
    - If using the Playground, name the pre-existing notebook that failed and the steps that led to failure.
    - Include anything else that might help us debug the issue.
    llvm (and llvm-symbolizer) was not installed when the crash happened.

System information

- What OS did you do install Mojo on ? 
macOSX
- Provide version information for Mojo by pasting the output of `mojo -v`
mojo 24.4.0 (59977802)
- Provide Modular CLI version by pasting the output of `modular -v`
modular 0.8.0 (39a426b5)
soraros commented 2 months ago

Might be related to #3099.

# Zn: @value is a little confusing as it doe not mean immutable class. 
# Zn: how to define a immutable struct in Mojo?

@value means "make it a value type", there is nothing about mutability. Also, there is no way to make an immutable structure in Mojo currently.

nzhang commented 2 months ago

Thank you for the quick response @soraros , particularly answering my own questions while still learning the language :)

3099 seems related but may not be exactly the same. The crash I got didn't need a @value decorator.