modularml / mojo

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

[BUG] Incorrect pointer behavior when materializing a type #2637

Open gabrieldemarmiesse opened 6 months ago

gabrieldemarmiesse commented 6 months ago

Bug description

I'm trying to implement small buffer optimization. To do this, I have a pointer which can point to some stack allocated data, but can also point to the heap. To know if we need to free at the end, we verify that the pointer does not point to the stack allocated array. This doesn't cause issue in normal settings (the whole test suite of the stdlib in https://github.com/modularml/mojo/pull/2613 is passing with all buffer sizes from 0 to 10 so I'm pretty confident here). But is causes issues when materializing the type.

This is blocking for https://github.com/modularml/mojo/issues/2467

Steps to reproduce

alias sbo_size = 10
alias BufferType = InlineArray[Int8, sbo_size]

struct CustomList:

    var _small_buffer: BufferType
    var data: UnsafePointer[Int8]

    fn __init__(inout self):
        self._small_buffer = BufferType(unsafe_uninitialized=True)
        self.data = UnsafePointer[Int8]()
        self.data = self._small_buffer.unsafe_ptr()

    fn __del__(owned self):
        if self.data and self.data != self._small_buffer.unsafe_ptr():
            print("Freeing data!!!! This shouldn't happen!")
            self.data.free()

fn foo():
    alias my_list = CustomList()
    print("Materializing my_list")
    var my_list_materialized = my_list # <-- bug here
    print("all done, exiting function")

def main():
    foo()
    print("main exiting.")
Materializing my_list
Freeing data!!!! This shouldn't happen!
src/tcmalloc.cc:302] Attempt to free invalid pointer 0x7ffe46490a70
[8443:8443:20240512,225543.509140:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[8443:8443:20240512,225543.509193:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)
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:
0. Program arguments: mojo trying_stuff3.mojo
 #0 0x000055c0229aedd7 (/root/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x1291dd7)
 #1 0x000055c0229acc2e (/root/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x128fc2e)
 #2 0x000055c0229af46f (/root/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x129246f)
 #3 0x00007fdcca085520 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007fdcca0d99fc pthread_kill (/usr/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x00007fdcca085476 gsignal (/usr/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x00007fdcca06b7f3 abort (/usr/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x000055c02417ec93 (/root/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x2a61c93)
 #8 0x000055c0241764bc (/root/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x2a594bc)
 #9 0x000055c0241783f7 (/root/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x2a5b3f7)
#10 0x000055c024dbd7b4 (/root/.modular/pkg/packages.modular.com_nightly_mojo/bin/mojo+0x36a07b4)
#11 0x00007fdcc81bef08 KGEN_CompilerRT_AlignedFree (/root/.modular/pkg/packages.modular.com_nightly_mojo/lib/libKGENCompilerRTShared.so.19.0git+0x3bf08)
#12 0x00007fdc6400168a
mojo crashed!
Please file a bug report.
[1] 8441 IOT instruction MODULAR_MOJO_NIGHTLY_IMPORT_PATH=./build mojo trying_stuff3.mojo

System information

ubuntu 22.04 in docker in wsl 2
modular 0.7.2 (d0adc668)
mojo 2024.5.1102 (8530deea)
JoeLoser commented 6 months ago

@Mogball any ideas here? This is in the way of SSO work for String.