svhawkins / B-Minor-Compiler

COMP 4060 B-Minor Compiler
2 stars 0 forks source link

Investigate cause(s) of segfaults with arrays. #17

Closed svhawkins closed 3 weeks ago

svhawkins commented 2 months ago

Expressions, declarations, and whatnot are segfaulting with the involvement of arrays. #16 described a few instances, but there are now others:

Have fun with valgrind and gdb.

svhawkins commented 2 months ago

04.28.2024::13:50: Why the segfaults are occuring is definitely stack related. Going through GDB at decl_resolve in test function test_decl_codegen_array_matrix() it fails to find strdup file, when it's defintiely there since all of the previous tests pass. Then it just jumps to expr_destroy() :

decl_resolve (st=0x55555557da70, d=0x55555557db80) at src/decl.c:235
235       is_const_expr = ((d->value && kind == SYMBOL_GLOBAL) || d->type->size);
(gdb) p kind
$7 = SYMBOL_GLOBAL
(gdb) x d->value
0x55555557e560: 0x00000017
(gdb) p d->value
$8 = (struct expr *) 0x55555557e560
(gdb) s
237       d->symbol = symbol_create(kind, type_copy(d->type), strdup(d->name));
(gdb) p is_const_expr
$9 = true
(gdb) s
__GI___strdup (s=0x5555555819d0 "foo") at strdup.c:40
40      strdup.c: No such file or directory.
(gdb) s
41      in strdup.c
(gdb) s
42      in strdup.c
(gdb) s

Program received signal SIGSEGV, Segmentation fault.
expr_destroy (e=0x59) at src/expr.c:351
351       if (!(*e)) { return; }
(gdb) 

FOR NOW, limit the coode generation to single dimensional arrays. This to be a later issue with later refactoring to fix basically everything after abstract syntax tree generation:

  1. how member fields are allocated
  2. make functions iterative to limit recursion depth
  3. limit use of local variables to reduce stack size
  4. avoid overuse of dynamic memory allocation + strdup.
svhawkins commented 3 weeks ago

2024.06.16:19:09: Memory errors due to buggy <struct>_copy() functions and uninitialized values with printfs here and there. Unrelated segfaults in expr_codegen (logic errors) were also fixed. decl_array_codegen, and decl_codegen are able to run without issues from valgrind.

Fixed memory leak in grammar.bison. There is still a leak, but the cause is from the generated scanner, not from me. All but one of the text executables (test_expr_codegen) are able to run without any error. Some tests still fail, but that's better than having memory issues.