yasl-lang / yasl

Bytecode Interpreter for Yet Another Scripting Language (YASL).
MIT License
65 stars 14 forks source link

API: YASL_setglobal bug #232

Closed ryco117 closed 4 years ago

ryco117 commented 4 years ago

When the global var being set is a string or list that was declared in a previously run script, setting it using YASL_setglobal causes weird errors to appear later in the code, where the error is different depending on what code I've commented. Here is the most direct stack trace I've seen, setting a variable previously of type string

1   dec_strong_ref                                                                                                                                                                                                               refcount.c                99   0x4486da       
2   dec_ref                                                                                                                                                                                                                      refcount.c                140  0x448854       
3   del_item                                                                                                                                                                                                                     YASL_Table.c              22   0x442330       
4   YASL_Table_insert                                                                                                                                                                                                            YASL_Table.c              115  0x442817       
5   YASL_setglobal                                                                                                                                                                                                               yasl.c                    184  0x436dd0       
6   BackgroundWorker::doWork                                                                                                                                                                                                     backgroundworker.cpp      296  0x41555b       
... <More>                                                                                                                                                                                                                                                                     

Valgrind gives the following errors when using setglobal.

Invalid write of size 8
==114056==    at 0x448749: dec_strong_ref (refcount.c:115)
==114056==    by 0x448853: dec_ref (refcount.c:140)
==114056==    by 0x44232F: del_item (YASL_Table.c:22)
==114056==    by 0x442816: YASL_Table_insert (YASL_Table.c:115)
==114056==    by 0x436DCF: YASL_setglobal (yasl.c:184)
==114056==    by 0x41555A: BackgroundWorker::doWork
...
Address 0x190fc770 is 0 bytes inside a block of size 16 free'd
==114056==    at 0x483AA0C: free (vg_replace_malloc.c:540)
==114056==    by 0x448411: rc_del (refcount.c:16)
==114056==    by 0x44C859: ud_del_rc (userdata.c:20)
==114056==    by 0x44878F: dec_strong_ref (refcount.c:118)
==114056==    by 0x448853: dec_ref (refcount.c:140)
==114056==    by 0x436D6C: YASL_setglobal (yasl.c:182)
==114056==    by 0x41555A: BackgroundWorker::doWork
...
Block was alloc'd at
==114056==    at 0x483980B: malloc (vg_replace_malloc.c:309)
==114056==    by 0x4483D8: rc_new (refcount.c:9)
==114056==    by 0x442480: rcht_new_sized (YASL_Table.c:50)
==114056==    by 0x4424B2: rcht_new (YASL_Table.c:57)
==114056==    by 0x447BD3: YASL_Table (YASL_Object.c:84)
==114056==    by 0x4470A7: vm_run (VM.c:984)
==114056==    by 0x436B23: YASL_execute (yasl.c:148)
==114056==    by 0x413743: AtomicState::AtomicState() (atomicstate.cpp:24)
...

Note This error does not appear when the variable is a table or ints, which are the only other two I checked

ryco117 commented 4 years ago

A new stack-trace error is

6   rc_del                                                                                                                                                                               refcount.c                  16   0x44869c       
7   str_del                                                                                                                                                                              YASL_String.c               89   0x44adad       
8   env_contains                                                                                                                                                                         env.c                       58   0x43b90c       
9   YASL_setglobal                                                                                                                                                                       yasl.c                      171  0x436f2b       
10  MainWindow::onConversationSelected                                                                                                                                                   mainwindow.cpp              192  0x41a4d1       
... <More>                                                                                                                                                                                                                               

New valgrind

==178660== Invalid read of size 8
==178660==    at 0x448A3E: dec_strong_ref (refcount.c:122)
==178660==    by 0x448ADD: dec_ref (refcount.c:140)
==178660==    by 0x4425B9: del_item (YASL_Table.c:22)
==178660==    by 0x442AA0: YASL_Table_insert (YASL_Table.c:115)
==178660==    by 0x437059: YASL_setglobal (yasl.c:184)
==178660==    by 0x41A4A5: MainWindow::onConversationSelected(QTreeWidgetItem*, QTreeWidgetItem*) (mainwindow.cpp:187)
==178660==  Address 0x19335c00 is 0 bytes inside a block of size 16 free'd
==178660==    at 0x483AA0C: free (vg_replace_malloc.c:540)
==178660==    by 0x44869B: rc_del (refcount.c:16)
==178660==    by 0x447D1B: cfn_del_rc (YASL_Object.c:42)
==178660==    by 0x448A79: dec_strong_ref (refcount.c:125)
==178660==    by 0x448ADD: dec_ref (refcount.c:140)
==178660==    by 0x436FF6: YASL_setglobal (yasl.c:182)
==178660==    by 0x41A4A5: MainWindow::onConversationSelected(QTreeWidgetItem*, QTreeWidgetItem*) (mainwindow.cpp:187)
==178660==  Block was alloc'd at
==178660==    at 0x483980B: malloc (vg_replace_malloc.c:309)
==178660==    by 0x448662: rc_new (refcount.c:9)
==178660==    by 0x447CE8: new_cfn (YASL_Object.c:34)
==178660==    by 0x437460: YASL_pushcfunction (yasl.c:256)
==178660==    by 0x41A492: MainWindow::onConversationSelected(QTreeWidgetItem*, QTreeWidgetItem*) (mainwindow.cpp:186)
...