chtisgit / bfjit

A platform independent ahead-of-time compiler for Brainfuck (for x86 processors)
MIT License
3 stars 0 forks source link

Segfault with Debian Stretch #1

Open rdebath opened 5 years ago

rdebath commented 5 years ago

Segfault with:

Working with:

Optimisation level makes no difference. GDB says the segfault is at line 419, the "print_stub" asm code.

chtisgit commented 5 years ago

Thank you for reporting this bug! I could reproduce this behavior on Ubuntu 18.04.

The problem arises because gcc uses the register eax in the assembly code for the symbol constrained with "m" (which I thought it wouldn't ever do). Later on, eax which contains a value 0-255 is dereferenced... So, I first added eax to the clobber list, which didn't help. Then I looked at the line that caused the segfault and its context and it turns out it wasn't necessary. I'm going to check the documentation of gcc later though.

I removed the problematic line in the branch fix_issue1. Could you try that one and report back? There might be undefined behavior in other asm statements too, it's pretty old code.

rdebath commented 5 years ago

That bit's working better, but I'm getting a segfault after the call returns for some programs and still getting one from print_stub:419, now in malloc(), for this code.

+[>[<-[]>+[>+++>[+++++++++++>][>]-[<]>-]]++++++++++<]>
>>>>>----.<<+++.<-..+++.<-.>>>.<<.+++.------.>-.<<+.<.
Reading symbols from ./bfjit...done.
(gdb) r
Starting program: /home/robert/etc/BrainFuck/bb/chtisgit-bfjit/bfjit hell-loopy.b

Program received signal SIGSEGV, Segmentation fault.
_int_malloc (av=av@entry=0xf7fa0780 <main_arena>, bytes=bytes@entry=1024) at malloc.c:3626
3626    malloc.c: No such file or directory.
(gdb) bt
#0  _int_malloc (av=av@entry=0xf7fa0780 <main_arena>, bytes=bytes@entry=1024) at malloc.c:3626
#1  0xf7e5ebf5 in __GI___libc_malloc (bytes=1024) at malloc.c:2928
#2  0xf7e4a44f in __GI__IO_file_doallocate (fp=0xf7fa0d60 <_IO_2_1_stdout_>) at filedoalloc.c:101
#3  0xf7e58a86 in __GI__IO_doallocbuf (fp=0xf7fa0d60 <_IO_2_1_stdout_>) at genops.c:398
#4  0xf7e57d81 in _IO_new_file_overflow (f=0xf7fa0d60 <_IO_2_1_stdout_>, ch=72) at fileops.c:828
#5  0xf7e4e6b5 in putchar (c=72) at putchar.c:28
#6  0x56556731 in print_stub () at bfjit.c:419
#7  0x5655c070 in ?? ()
#8  0xf7e05286 in __libc_start_main (main=0x2, argc=1448433728, argv=0x0, init=0x56555871 <_start+49>,
    fini=0x56557daf <main>, rtld_fini=0x2, stack_end=0xffffdbc4) at ../csu/libc-start.c:291
#9  0x5655a000 in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
(gdb) q
chtisgit commented 5 years ago

It seems position-independent executables are now the default, and somehow the code added by the compiler interferes with this program's use of inline assembly... Well, for now I probably fixed it with the gcc options -no-pie -fno-pic. I also added a Makefile with the necessary options to the branch fix_issue1 and changed a bit of code to silence compiler warnings and make small improvements.