maxrt101 / ff

Scripting language
MIT License
4 stars 0 forks source link

Crash in parser (assignment -> expression -> rvalue) #28

Closed maxrt101 closed 2 years ago

maxrt101 commented 2 years ago

Traces:

On MacOS 10.15.7 with clang 11.0.3 (lldb)

TOKEN_FN(fn) TOKEN_IDENTIFIER(main) TOKEN_LEFT_PAREN(() TOKEN_RIGHT_PAREN()) TOKEN_RIGHT_ARROW(->) TOKEN_LEFT_BRACE({) TOKEN_VAR(var) TOKEN_IDENTIFIER(x) TOKEN_EQUAL(=) TOKEN_NUMBER(10) TOKEN_SEMICOLON(;) TOKEN_IDENTIFIER(x) TOKEN_DOT(.) TOKEN_IDENTIFIER(x) TOKEN_EQUAL(=) TOKEN_NUMBER(123) TOKEN_SEMICOLON(;) TOKEN_PRINT(print) TOKEN_IDENTIFIER(x) TOKEN_DOT(.) TOKEN_IDENTIFIER(x) TOKEN_SEMICOLON(;) TOKEN_PRINT(print) TOKEN_IDENTIFIER(x) TOKEN_SEMICOLON(;) TOKEN_RIGHT_BRACE(}) TOKEN_EOF()
statement: var
rvalue: prev=10
statement: x
assign: value=x.x
rvalue: prev=123
Process 66620 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x10)
    frame #0: 0x00007fff6da0ebd3 libsystem_malloc.dylib`tiny_malloc_from_free_list + 961
libsystem_malloc.dylib`tiny_malloc_from_free_list:
->  0x7fff6da0ebd3 <+961>: movq   %rcx, (%r11)
    0x7fff6da0ebd6 <+964>: movq   (%r13), %rcx
    0x7fff6da0ebda <+968>: movq   %rcx, (%rdx)
    0x7fff6da0ebdd <+971>: movq   0x8(%r13), %rcx
Target 0: (ff) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x10)
  * frame #0: 0x00007fff6da0ebd3 libsystem_malloc.dylib`tiny_malloc_from_free_list + 961
    frame #1: 0x00007fff6da0e297 libsystem_malloc.dylib`tiny_malloc_should_clear + 288
    frame #2: 0x00007fff6da0d0c6 libsystem_malloc.dylib`szone_malloc_should_clear + 66
    frame #3: 0x00007fff6da0bd7a libsystem_malloc.dylib`malloc_zone_malloc + 104
    frame #4: 0x00007fff6da0bcf5 libsystem_malloc.dylib`malloc + 21
    frame #5: 0x000000010004df04 ff`ff::Parser::rvalue(this=0x00007ffeefbff068, isReturnValueExpected=true) at parser.cc:705:25
    frame #6: 0x000000010004dbc0 ff`ff::Parser::cast(this=0x00007ffeefbff068, isReturnValueExpected=true) at parser.cc:690:21
    frame #7: 0x000000010004db3b ff`ff::Parser::unary(this=0x00007ffeefbff068, isReturnValueExpected=true) at parser.cc:686:10
    frame #8: 0x000000010004d7e1 ff`ff::Parser::factor(this=0x00007ffeefbff068, isReturnValueExpected=true) at parser.cc:668:21
    frame #9: 0x000000010004d5f1 ff`ff::Parser::term(this=0x00007ffeefbff068, isReturnValueExpected=true) at parser.cc:656:21
    frame #10: 0x000000010004d3f1 ff`ff::Parser::comparison(this=0x00007ffeefbff068, isReturnValueExpected=true) at parser.cc:644:21
    frame #11: 0x000000010004d201 ff`ff::Parser::equality(this=0x00007ffeefbff068, isReturnValueExpected=true) at parser.cc:632:21
    frame #12: 0x000000010004d011 ff`ff::Parser::logic(this=0x00007ffeefbff068, isReturnValueExpected=true) at parser.cc:620:21
    frame #13: 0x0000000100046cf5 ff`ff::Parser::expression(this=0x00007ffeefbff068, isReturnValueExpected=true) at parser.cc:616:10
    frame #14: 0x0000000100047bd1 ff`ff::Parser::statement(this=0x00007ffeefbff068, isInOtherStatement=false) at parser.cc:362:27
    frame #15: 0x000000010004a720 ff`ff::Parser::statementList(this=0x00007ffeefbff068) at parser.cc:436:21
    frame #16: 0x0000000100046b8c ff`ff::Parser::block(this=0x00007ffeefbff068) at parser.cc:424:14
    frame #17: 0x0000000100044c2e ff`ff::Parser::fndecl(this=0x00007ffeefbff068) at parser.cc:185:12
    frame #18: 0x0000000100042db6 ff`ff::Parser::program(this=0x00007ffeefbff068, checkEnd=true) at parser.cc:96:23
    frame #19: 0x0000000100042a5a ff`ff::Parser::parse(this=0x00007ffeefbff068) at parser.cc:40:10
    frame #20: 0x000000010000234e ff`run(filename="test.ff", src="\nfn main() -> {\n  var x = 10;\n  x.x = 123;\n  print x.x;\n  print x;\n}\n") at main.cc:38:28
    frame #21: 0x0000000100001acf ff`main(argc=3, argv=0x00007ffeefbff698) at main.cc:157:10
    frame #22: 0x00007fff6d855cc9 libdyld.dylib`start + 1
    frame #23: 0x00007fff6d855cc9 libdyld.dylib`start + 1

On ubuntu 20.04 with gcc 9.4.0

{
  fn main(): any -> {
    var x: any = 10;
    x.x = 123;
    print x.x;
    print x;
  }
}
ff: malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted (core dumped)

Code

fn main() -> {
  var x = 10;
  x.x = 123;
  print x.x;
  print x;
}
maxrt101 commented 2 years ago

Commit dcb9276434 works, but next one (40cbf960a8) doesn't. Seems that annotations (ASTAnnotation) support somehow broke field assignment

maxrt101 commented 2 years ago

Removed annotations support in b14236f9d8 (branch issue-28-remove-annotation-support)

maxrt101 commented 2 years ago

If std::vector<std::string> m_annotations; is removed from ff::ast::Node - everything works

maxrt101 commented 2 years ago

It seems that adding any field to ff::ast::Node breaks rvalue in parser

maxrt101 commented 2 years ago
frame #5: 0x000000010004c734 ff`ff::Parser::rvalue(this=0x00007ffeefbff068, isReturnValueExpected=true) at parser.cc:705:25

It's this line:

return new ast::IntegerLiteral(previous());
maxrt101 commented 2 years ago

Temporary fix - remove all support of ASTAnnotations. TODO: look into this in depth, maybe reimplement the annotations

maxrt101 commented 2 years ago

Closing this for now (Fix implemented in: 3f33bc03a7)