sysprog21 / shecc

A self-hosting and educational C optimizing compiler
BSD 2-Clause "Simplified" License
1.11k stars 118 forks source link

Error on make: /bin/sh: line 1: 21530 Segmentation fault out/shecc --dump-ir -o out/shecc-stage1.elf #55

Closed arteze closed 1 year ago

arteze commented 3 years ago

I have a mistake when doing make.

make
  SHECC out/shecc-stage1.elf
/bin/sh: line 1: 21530 Segmentation fault      out/shecc --dump-ir -o out/shecc-stage1.elf src/main.c > out/shecc-stage1.log
make: *** [Makefile:71: out/shecc-stage1.elf] Error 139
jserv commented 3 years ago

Can you use GDB to narrow down the problem?

gdb -q out/shecc

Then, execute the following command inside GDB:

run --dump-ir -o out/shecc-stage1.elf src/main.c 

The program is supposedly stopped. You can use command bt to print backtrace of all stack frames.

arteze commented 3 years ago

Yes, it's puppylinux (a Slackware fork), I could already install the gdb.

Out of gdb:

gdb -q out/shecc 
Reading symbols from out/shecc...

At run:

(gdb) run --dump-ir -o out/shecc-stage1.elf src/main.c 
Starting program: /initrd/mnt/dev_save/descargas/shecc-master/shecc-master/out/shecc --dump-ir -o out/shecc-stage1.elf src/main.c

Program received signal SIGSEGV, Segmentation fault.
0x00000000004012a6 in add_block (parent=parent@entry=0x0, func=func@entry=0x0) at src/globals.c:64
64      blk->index = blocks_idx++;

Command bt:

(gdb) bt
#0  0x00000000004012a6 in add_block (parent=parent@entry=0x0, func=func@entry=0x0) at src/globals.c:64
#1  0x0000000000405961 in parse_internal () at src/cfront.c:2235
#2  0x0000000000405bac in parse (file=file@entry=0x7fffffffe7da "src/main.c") at src/cfront.c:2313
#3  0x00000000004087d6 in main (argc=<optimized out>, argv=<optimized out>) at src/main.c:64
jserv commented 3 years ago
(gdb) run --dump-ir -o out/shecc-stage1.elf src/main.c 
Starting program: /initrd/mnt/dev_save/descargas/shecc-master/shecc-master/out/shecc --dump-ir -o out/shecc-stage1.elf src/main.c

Program received signal SIGSEGV, Segmentation fault.
0x00000000004012a6 in add_block (parent=parent@entry=0x0, func=func@entry=0x0) at src/globals.c:64
64        blk->index = blocks_idx++;

At present, shecc is inefficient to allocate the blocks, and it is likely to fail to allocate BLOCK (src/globals.c:212). Can you enable overcommit? See https://www.kernel.org/doc/Documentation/vm/overcommit-accounting You can set the overcommit policy 1 via the sysctl `vm.overcommit_memory'.

arteze commented 3 years ago

I could change the ratio:

sysctl vm.overcommit_ratio=1 
vm.overcommit_ratio = 1

I have already been able to do make correctly, although I do not know if that's why.

But... Always, the first time after doingmake clean can not compile with make, but it does compile the second attempt to make with make.

At clean:

make clean 
rm -f out/shecc out/shecc-stage1.elf out/shecc-stage2.elf
rm -f out/src/main.o out/src/main.o.d
rm -f out/tests/fib.elf out/tests/hello.elf out/tests/*.log out/tests/*.lst
rm -f out/shecc*.log
rm -f out/inliner out/libc.inc out/target config src/codegen.c

First make:

make
Target machine code switch to arm
  CC+LD out/inliner
  GEN   out/libc.inc
  CC    out/src/main.o
  LD    out/shecc
  SHECC out/shecc-stage1.elf
  SHECC out/shecc-stage2.elf
out/shecc-stage1.elf: out/shecc-stage1.elf: cannot execute binary file
make: *** [Makefile:76: out/shecc-stage2.elf] Error 126

Second make (without errors):

make
  SHECC out/shecc-stage2.elf

If assign overcommit to 0 it returns to not be able to compile, neither the first attempt. If after this I will assign it again 1 compile correctly again, but to the second attempt.

jserv commented 3 years ago

Second make (without errors):

make
  SHECC   out/shecc-stage2.elf

Then, can you run make check to validate as well? I suspect that there is not enough memory since shecc never releases the memory during its compilation process.

arteze commented 3 years ago

Yes, apparently everything is correct... I did not know those tests, I see that none does printf.

make check
  SHECC out/tests/fib.elf
Running out/tests/fib.elf ...
F(10) = 55
 Passed
  SHECC out/tests/hello.elf
Running out/tests/hello.elf ...
1
Hello World
 Passed
tests/driver.sh
int main(int argc, int argv) { exit(0); } => 0
int main(int argc, int argv) { exit(42); } => 42
int main(int argc, int argv) { exit(24 + 18); } => 42
int main(int argc, int argv) { exit(58 - 28); } => 30
int main(int argc, int argv) { exit(5 * 2); } => 10
int main(int argc, int argv) { exit(16 >> 2); } => 4
int main(int argc, int argv) { exit(8 + 3 * 4); } => 20
int main(int argc, int argv) { exit((11 - 2) * 6); } => 54
int main(int argc, int argv) { exit(9 / 3 + 7); } => 10
int main(int argc, int argv) { exit(8 / (4 - 3)); } => 8
int main(int argc, int argv) { exit(8 + 3 * 5 + 2 * 6); } => 35
int main(int argc, int argv) { exit(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10); } => 55
int main(int argc, int argv) { exit(((((((((1 + 2) + 3) + 4) + 5) + 6) + 7) + 8) + 9) + 10); } => 55
int main(int argc, int argv) { exit(1 + (2 + (3 + (4 + (5 + (6 + (7 + (8 + (9 + 10))))))))); } => 55
int main(int argc, int argv) { exit(5 % 3); } => 2
int main(int argc, int argv) { exit(111 % 7); } => 6
int main(int argc, int argv) { exit(10 > 5); } => 1
int main(int argc, int argv) { exit(3+3 > 5); } => 1
int main(int argc, int argv) { exit(30 == 20); } => 0
int main(int argc, int argv) { exit(5 >= 10); } => 0
int main(int argc, int argv) { exit(5 >= 5); } => 1
int main(int argc, int argv) { exit(30 != 20); } => 1
int main(int argc, int argv) { exit(!237); } => 0
int main(int argc, int argv) { exit(~237); } => 18
int main(int argc, int argv) { exit(0 || 0); } => 0
int main(int argc, int argv) { exit(1 || 0); } => 1
int main(int argc, int argv) { exit(1 || 1); } => 1
int main(int argc, int argv) { exit(0 && 0); } => 0
int main(int argc, int argv) { exit(1 && 0); } => 0
int main(int argc, int argv) { exit(1 && 1); } => 1
int main(int argc, int argv) { exit(2 << 3); } => 16
int main(int argc, int argv) { exit(256 >> 3); } => 32
int main(int argc, int argv) { exit(237 | 106); } => 239
int main(int argc, int argv) { exit(237 ^ 106); } => 135
int main(int argc, int argv) { exit(237 & 106); } => 104
int main(int argc, int argv) { return 1; } => 1
int main(int argc, int argv) { return 2*21; } => 42
int main(int argc, int argv) { int var; var = 10; return var; } => 10
int main(int argc, int argv) { int va; int vb; va = 11; vb = 31; int vc; vc = va + vb; return vc; } => 42
int main(int argc, int argv) { int v; v = 30; v = 50; return v; } => 50
int main(int argc, int argv) { if (1) return 5; else return 20; } => 5
int main(int argc, int argv) { if (0) return 5; else if (0) return 20; else return 10; } => 10
int main(int argc, int argv) { int a; a = 0; int b; b = 0; if (a) b = 10; else if (0) return a; else if (a) return b; else return 10; } => 10
int main(int argc, int argv) { int a; a = 15; int b; b = 2; if(a - 15) b = 10; else if (b) return a + b + 10; else if (a) return b; else return 10; } => 27
int main(int argc, int argv) { { return 5; } } => 5
int main(int argc, int argv) { { int a; a = 5; { a = 5 + a; } return a; } } => 10
int main(int argc, int argv) { int a; a = 10; if (1) { a = 20; } else { a = 10; } return a; } => 20
int main(int argc, int argv) { int a; a = 10; if (a) { if (a - 10) { a = a + 1; } else { a = a + 20; } a = a - 10; } else { a = a + 5; } return a + 10; } => 30
int main(int argc, int argv) { int acc; int p; acc = 0; p = 10; while (p) { acc = acc + p; p = p - 1; } return acc; } => 55
int main(int argc, int argv) { int acc; acc = 15; do { acc = acc * -2; } while (acc < 0); return acc; } => 60
int main(int argc, int argv) { int i; int acc; acc = 0; for (i = 0; i < 10; ++i) { acc = acc + i; } return acc; } => 45
int main(int argc, int argv) { int i; int j; i=0; j=0; while (i<10) { j=j+i; i=i+1; } return j; } => 45
int main(int argc, int argv) { int x; x=0; do {x = x + 1; break;} while (1); return x; } => 1
int main(int argc, int argv) { int x; x=0; do {x++; continue; abort();} while (x < 2); return x; } => 2
int main(int argc, int argv) { int x; x=0; while(x < 2){x++; continue; abort();} return x; } => 2
int main(int argc, int argv) { int i; i=0; int j; for (j = 0; j < 10; j++) { if (j < 3) continue; i = i + 1; } return i; } => 7
int main(int argc, int argv) { while(0); return 10; } => 10
int main(int argc, int argv) { while(1) break; return 10; } => 10
int main(int argc, int argv) { for(;;) break; return 10; } => 10
int main(int argc, int argv) { int x; for(x = 10; x > 0; x--); return x; } => 0
int main(int argc, int argv) { int i; int acc; i = 0; acc = 0; do { i = i + 1; if (i - 1 < 5) continue; acc = acc + i; if (i == 9) break; } while (i < 10); return acc; } => 30
int main(int argc, int argv) { int acc; acc = 0; int i; for (i = 0; i < 100; i++) { if (i < 5) continue; if (i == 9) break; acc = acc + i; } return acc; } => 26
int sum(int m, int n) {
    int acc;
    acc = 0;
    int i;
    for (i = m; i <= n; i = i + 1)
        acc = acc + i;
    return acc;
}

int main() {
    return sum(1, 10);
} => 55
int fact(int x) {
    if (x == 0) {
        return 1;
    } else {
        return x * fact(x - 1);
    }
}

int main() {
    return fact(5);
} => 120
int is_odd(int x);

int is_even(int x) {
    if (x == 0) {
        return 1;
    } else {
        return is_odd(x - 1);
    }
}

int is_odd(int x) {
    if (x == 0) {
        return 0;
    } else {
        return is_even(x - 1);
    }
}

int main() {
    return is_even(20);
} => 1
int ack(int m, int n) {
    if (m == 0) {
        return n + 1;
    } else if (n == 0) {
        return ack(m - 1, 1);
    } else {
        return ack(m - 1, ack(m, n - 1));
    }
}

int main() {
    return ack(3, 5);
} => 253
int main(int argc, int argv) { int x; int *y; x = 3; y = &x; return y[0]; } => 3
int main(int argc, int argv) { int b; int *a; b = 10; a = &b; a[0] = 5; return b; } => 5
int change_it(int *p) {
    if (p[0] == 0) {
        p[0] = 10;
    } else {
        p[0] = p[0] - 1;
    }
}

int main() {
  int v;
  v = 2;
  change_it(&v);
  change_it(&v);
  change_it(&v);
  return v;
} => 10
typedef struct {
    int (*ta)();
    int (*tb)(int);
} fptrs;
int t1() { return 7; }
int t2(int x) { return x + 1; }
int main() {
    fptrs fb;
    fptrs *fs = &fb;
    fs->ta = t1;
    fs->tb = t2;
    return fs->ta() + fs->tb(10);
} => 18
int nth_of(int *a, int i) {
    return a[i];
}

int main() {
    int ary[5];
    int i;
    int v0;
    int v1;
    int v2;

    for (i = 0; i < 5; i++) {
        ary[i] = i * 2;
    }

    v0 = nth_of(ary, 0);
    v1 = nth_of(ary, 2);
    v2 = nth_of(ary, 4);
    return v0 + v1 + v2;
} => 12
int a = 5 * 2;
int b = -4 * 3 + 7 + 9 / 3 * 5;
int main()
{
    return a + b;
} => 20
int main(int argc, int argv) { exit(1 ? 10 : 5); } => 10
int main(int argc, int argv) { exit(0 ? 10 : 25); } => 25
int main(int argc, int argv) { int a; a = 2; a += 3; return a; } => 5
int main(int argc, int argv) { int a; a = 10; a -= 5; return a; } => 5
int main(int argc, int argv) { int *p; int a[3]; a[0] = 10; a[1] = 20; a[2] = 30; p = a; p+=1; return p[0]; } => 20
int main(int argc, int argv) { exit(sizeof(int)); } => 4
int main(int argc, int argv) { exit(sizeof(char)); } => 1
int main(int argc, int argv) { int a; a = 0; switch (3) { case 0: return 2; case 3: a = 10; break; case 1: return 0; } return a; } => 10
int main(int argc, int argv) { int a; a = 0; switch (3) { case 0: return 2; default: a = 10; break; } return a; } => 10
typedef enum { enum1 = 5, enum2 } enum_t;
int main() { enum_t v = enum2; return v; } => 6
int main()
{
    /* change test bench if different scheme apply */
    int *a = malloc(sizeof(int) * 5);
    free(a);
    if (a == NULL)
        abort();
    int *b = malloc(sizeof(int) * 3);

    /* "malloc" will reuse memory free'd by "free(a)" */
    return a == b;
} => 1
int main()
{
    char *ptr = "hello";
    return (0 == strcmp(ptr, "hello")) == (!strcmp(ptr, "hello"));
} => 1
OK
eecheng87 commented 1 year ago

Close this because it seems to be solved.