c3lang / c3c

Compiler for the C3 language
https://c3-lang.org
GNU Lesser General Public License v3.0
2.75k stars 162 forks source link

Missing stack overflow. #983

Open pierrec opened 1 year ago

pierrec commented 1 year ago

Expecting a stack overflow (I think?) and getting a segfault.

module oups;

struct Foo
{
    char[8 << 20] buffer;
}

fn void! main()
{
    Foo foo;
}
lerno commented 1 year ago

This will probably get better when the updated stacktrace is done.

lerno commented 1 year ago

Can you get this to properly make a stacktrace with sanitizers? I am not getting a signal callback here.

pierrec commented 1 year ago

Can you get this to properly make a stacktrace with sanitizers? I am not getting a signal callback here.

I am not sure what you are asking for here. I still get a segfault with 0.4.654. Do you mean that the signal is not captured there?

lerno commented 1 year ago

If you write something similar in C, does sanitizers pick it up?

pierrec commented 1 year ago

Yes it does:

a.out(12498,0x7ff8574d5700) malloc: nano zone abandoned due to inability to reserve vm space.
AddressSanitizer:DEADLYSIGNAL
=================================================================
==12498==ERROR: AddressSanitizer: stack-overflow on address 0x7ff7ba950504 (pc 0x000104db2f49 bp 0x7ff7bb150510 sp 0x7ff7ba950500 T0)
    #0 0x104db2f49 in main+0x19 (a.out:x86_64+0x100003f49)
    #1 0x7ff813aa241e in start+0x76e (dyld:x86_64+0xfffffffffff6e41e)

SUMMARY: AddressSanitizer: stack-overflow (a.out:x86_64+0x100003f49) in main+0x19
==12498==ABORTING
zsh: abort      ./a.out
lerno commented 1 year ago

Can you output the llvm ir generated?

pierrec commented 1 year ago

main.ll.zip

lerno commented 1 year ago

It turns out that this is not simple. In order to handle this, you need to give the signal handler its own stack. But if it has its own stack then backtrace stops working on MacOS.

lerno commented 1 year ago

I have to change this to "enhancement". There are various issues here:

  1. The stack has overflowed, so no additional stack memory can be used, meaning we can't make any calls to grab the backtrace.
  2. It is possible to reserve a separate stack on Posix, but MacOS will destroy the backtrace when that happens.
  3. For Linux it is still possible to get it to work, but it's not really done yet.
  4. Windows is lacking this as of yet, but uses a different way to grab the stacktrace in general, and this should be implemented as well.