brain-labs / brain

An esoteric programming language compiler on top of LLVM based on Brainfuck
https://brain-labs.github.io/brain/docs/html/index.html
GNU General Public License v3.0
133 stars 19 forks source link

Segmentation fault! #54

Closed rdebath closed 6 years ago

rdebath commented 7 years ago

Happens with v1.0 and master.

debian-jessie64(robert)brain-labs$ git describe --tags
v1.0
debian-jessie64(robert)brain-labs$ clang++ --version
clang version 5.0.0 (tags/RELEASE_500/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
debian-jessie64(robert)brain-labs$
debian-jessie64(robert)brain-labs$ ~/bfi/bf.bin/bfi -c tests/helloworld.b > c.cpp
debian-jessie64(robert)brain-labs$ clang++ -O3 -o c c.cpp
debian-jessie64(robert)brain-labs$ ./c
Hello World!
debian-jessie64(robert)brain-labs$
debian-jessie64(robert)brain-labs$ gdb -args bin/brain tests/helloworld.b                                                                           GNU gdb (Debian 7.7.1+dfsg-5) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from bin/brain...done.
(gdb) run
Starting program: /home/robert/brain-labs/bin/brain tests/helloworld.b
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x0000000000e9c55b in llvm::Value::setNameImpl(llvm::Twine const&) ()
(gdb) bt
#0  0x0000000000e9c55b in llvm::Value::setNameImpl(llvm::Twine const&) ()
#1  0x0000000000e9c9b9 in llvm::Value::setName(llvm::Twine const&) ()
#2  0x0000000000e3cfd5 in llvm::Function::BuildLazyArguments() const ()
#3  0x000000000073c2b4 in llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) ()
#4  0x000000000073b5a3 in llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) ()
#5  0x00000000004c2071 in (anonymous namespace)::X86DAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&) ()
#6  0x00000000008ae0b6 in llvm::MachineFunctionPass::runOnFunction(llvm::Function&) ()
#7  0x0000000000e6afd3 in llvm::FPPassManager::runOnFunction(llvm::Function&) ()
#8  0x0000000000e6b1c3 in llvm::FPPassManager::runOnModule(llvm::Module&) ()
#9  0x0000000000e6b5c8 in llvm::legacy::PassManagerImpl::run(llvm::Module&) ()
#10 0x0000000000b49b8c in llvm::MCJIT::emitObject(llvm::Module*) ()
#11 0x0000000000b49da4 in llvm::MCJIT::generateCodeForModule(llvm::Module*) ()
#12 0x0000000000b4a7c1 in llvm::MCJIT::finalizeObject() ()
#13 0x000000000042de8c in Bootstrap::init (this=0x1698170, argc=<optimized out>, argv=<optimized out>) at src/utils/Bootstrap.cpp:119
#14 0x00007ffff69ddb45 in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6
#15 0x000000000042c6e7 in _start ()
(gdb)
luizperes commented 7 years ago

Hi @rdebath, do you have the source code for your helloworld.b ?

Is it the one located in the tests folder?

I will check it and answer as soon as possible! Thanks!

rdebath commented 7 years ago

Yes, it's the one in your tests folder, from Wikipedia. However, the problem seems to happen whatever is in the file, even just a newline (an empty file gives "No such file" !?)

++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
luizperes commented 7 years ago

I think I had a linking problem related to that (and I fixed locally), but not cool, will fix it as soon as possible.

This line may be causing it. We are depending currently on C and math.h for IO (must be removed later).

Just to test, try to run the command: brain ./helloworld.b -c && $(CC) helloworld.o -o hello -lm && rm hello.o

luizperes commented 7 years ago

I forgot to ask, is it on the dev branch? Which branch are you using? @rdebath

rdebath commented 7 years ago

Okay, I've now switched to the dev branch, recomplied and added the -c argument. Still getting a segfault.

debian-jessie64(robert)brain-labs$ git describe --long --tags
v1.0-84-g7b65871
debian-jessie64(robert)brain-labs$ gdb -args bin/brain tests/helloworld.b -c
GNU gdb (Debian 7.7.1+dfsg-5) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from bin/brain...done.
(gdb) run
Starting program: /home/robert/brain-labs/bin/brain tests/helloworld.b -c
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x0000000000ea7e52 in llvm::TypeFinder::incorporateType(llvm::Type*) ()
(gdb) bt
#0  0x0000000000ea7e52 in llvm::TypeFinder::incorporateType(llvm::Type*) ()
#1  0x0000000000ea7abb in llvm::TypeFinder::run(llvm::Module const&, bool) ()
#2  0x0000000000e9724b in llvm::Module::getIdentifiedStructTypes() const ()
#3  0x000000000043d20b in llvm::IRMover::move(std::unique_ptr<llvm::Module, std::default_delete<llvm::Module> >, llvm::ArrayRef<llvm::GlobalValue*>, std::function<void (llvm::GlobalValue&, std::function<void (llvm::GlobalValue&)>)>, bool) ()
#4  0x000000000043786b in llvm::Linker::linkInModule(std::unique_ptr<llvm::Module, std::default_delete<llvm::Module> >, unsigned int, std::function<void (llvm::Module&, llvm::StringSet<llvm::MallocAllocator> const&)>) ()
#5  0x0000000000437e7a in llvm::Linker::linkModules(llvm::Module&, std::unique_ptr<llvm::Module, std::default_delete<llvm::Module> >, unsigned int, std::function<void (llvm::Module&, llvm::StringSet<llvm::MallocAllocator> const&)>) ()
#6  0x000000000042e683 in Bootstrap::init (this=0x16a8170, argc=<optimized out>, argv=<optimized out>) at src/utils/Bootstrap.cpp:109
#7  0x00007ffff69ddb45 in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6
#8  0x000000000042c7d7 in _start ()
(gdb)
luizperes commented 7 years ago

I will check that when I get home! Please take a look to it too @rafaelcn. Will get back soon to you, @rdebath!

rafaelcn commented 7 years ago

Sorry @rdebath but I couldn't reproduce your bug. It seems, superficially that something has changed between clang++ version 3.9 and clang++ version 5.0, but nothing to assure yet.

Here's what I've done:

  1. Compiled Brain with make debug
  2. Tried gdb -args bin/brain tests/helloworld.b -c

Here's the output. I've removed GNU's copyright notice.

ranu@ranu-laptop ‹ dev ↑● › : ~/Github/brain-labs/brain/bin
[0] % gdb -args brain_debug ../tests/helloworld.b -c

Reading symbols from brain_debug...done.
(gdb) run
Starting program: /home/ranu/Github/brain-labs/brain/bin/brain_debug ../tests/helloworld.b -c
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[Inferior 1 (process 30172) exited normally]
(gdb)

Again I tried with brain and I've got the same behavior.

  1. make
  2. Tried gdb -args bin/brain tests/helloworld.b -c
ranu@ranu-laptop ‹ dev ↑● › : ~/Github/brain-labs/brain/bin
[0] % gdb -args brain ../tests/helloworld.b -c 
Reading symbols from brain...done.
(gdb) run
Starting program: /home/ranu/Github/brain-labs/brain/bin/brain ../tests/helloworld.b -c
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[Inferior 1 (process 30488) exited normally]
rafaelcn commented 7 years ago

Anyway, gonna inspect that profoundly later!

rdebath commented 7 years ago

Okay, I set off a build of clang 4.0.1, amd64 and then downgraded from 5.0.0. Version 4.0.1 works for me.

rafaelcn commented 7 years ago

That's quite weird! I was discussing this issue on PR #55. It looks like a problem when linking dwo files. We are injecting C compiled code inside Brain when it is compiling.

luizperes commented 7 years ago

We’re actually attaching LLVM IR (we’re compiling .c to .ll and only them attaching) as this is one of the plans of Brain for the future (attach other libraries into the code, so we can use all the power of LLVM), but the IO must go built-in later, the current solution has a lot of problems and is better to avoid them in the future. We already have a couple of issues regarding that and we will give priority to that. Thanks a lot for posting this issue @rdebath!

luizperes commented 7 years ago

Not so sure if the error above is related to that @rafaelcn. I guess I’ll install clang 5 over the weekend and try to replicate/fix it

rafaelcn commented 7 years ago

That was just a guess by the error provided in that SO answer.

rafaelcn commented 7 years ago

Yet, we need to discuss the future of the Math and the IO library :v

luizperes commented 7 years ago

Yeah for sure, #7 and #29 are still open for this reason. Will try to start working on that ASAP

rdebath commented 7 years ago

WRT the math library the below should work fine. It depends on the fact that you already have Log base 256 of the maximum integer size (called sizeof) and simply converts that to a log10 by multiplying by 2.41 (log(256)/log(10) --> 2.408239965311849) and adding the necessary padding for sign, nul and the last digit.

diff --git a/libs/io.c b/libs/io.c
index 40dd1ad..3d935b7 100644
--- a/libs/io.c
+++ b/libs/io.c
@@ -5,10 +5,6 @@

 // you can overwrite those functions! :)

-int number_size(int number) {
-    return floor(log10(abs(number))) + 1 + (number > 0 ? 0 : 1);
-}
-
 void b_show_tape(int idx, int *cells, int size) {
     // TODO: ellipsize values based on index
     size = 12;
@@ -34,9 +30,9 @@ void b_show_tape(int idx, int *cells, int size) {
         tape[tape_idx++] = ' ';
         // Has a value allocated on register
         if (*cells) {
-            unsigned int value_size = number_size(*cells);
-            char number[value_size];
-            sprintf(number, "%d", *cells);
+            unsigned int value_size;
+            char number[sizeof(*cells)*241/100+3];
+            value_size = sprintf(number, "%d", *cells);
             // Append each number character
             for (j = 0; j < value_size; j++) {
                 tape[tape_idx++] = number[j];
luizperes commented 7 years ago

I could finally reproduce that on clang 5, will check it

luizperes commented 7 years ago

Hi @rdebath, do you want to open a PR on this change you made on the io.c? Thanks for the heads up!

BTW, i am trying to run on clang 5 and so far i do not have any idea about what that can be xD

luizperes commented 6 years ago

I could narrow down this problem. The problem is on the module, not on the engine

luizperes commented 6 years ago

Hi @rdebath, I am so sorry for the delay to fix this PR! School was killing me! It has been fixed on https://github.com/brain-labs/brain/pull/59