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

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

Open
gabrieldemarmiesse opened this issue May 12, 2024 · 1 comment
Open
Labels
blocker bug Something isn't working mojo-lang Tag for all issues related to language. mojo-repo Tag all issues with this label

Comments

@gabrieldemarmiesse
Copy link
Contributor

gabrieldemarmiesse commented May 12, 2024

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 #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 #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(uninitialized=True)
        self.data = UnsafePointer[Int8]()
        self.data = UnsafePointer(Reference(self._small_buffer)).bitcast[Int8]()
 
    fn __del__(owned self):
        if self.data and self.data != UnsafePointer(Reference(self._small_buffer)).bitcast[Int8]():
            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
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocker bug Something isn't working mojo-lang Tag for all issues related to language. mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

2 participants