These functions also maintain barrier invariant(s). Also extend the
primtive collection optimization to tuples, for which it is much
simpler. For tuples, only maintain the invariant lazily. If on first
collection, all elements are primitive, we flag as such and never check
again.
The mark phase of the collector would previously visit every index in
arrays unconditionally, even if we could deduce that the array only
contained primitives (Janet values that don't contain pointers to heap)
This change adds a flag on arrays where we know all values are primitive
in the array. If an array is dicovered to only contain primitives during
the mark phase, it is flagged for faster processing in the next mark
phase. We also thread this "primitive write barrier" through array
access throughout our code.
Arrays create directly in C will not have this flag set initially to
avoid breaking _too much_ existing C code. This change also makes some
technically unnecessary changes to existing code that uses arrays to
avoid ambiguity, but it is likely that code of the form:
```c
JanetArray *array = janet_getarray(argv, 0); // Array of unknown origin
array->data[0] = some_value /* Incorrect as this can violate the
primitive flag invariant if some_value is
not a primitive value - may result in
use-after-free if GC collection runs. */
```
will no longer be correct.
However, in the scope of eventually making an incremental GC, this sort
of write-barrier will be needed anyway, so we are free to make
this optimization without extra burden on programmers.
We will likely need to make other changes as well to the JanetGCObject
type. Keeping a naive linked-list of all allocated blocks will likely
hurt any possible perf gain. Instead, we would move towards keeping
global arrays of the different colors of blocks. The internal *next
pointer would become an index into those specific arrays, with the color
determined by GC flags on the object.
Add testcase for os/shell to test/suite-ev.janet
While we may not want to completely deprecate os/shell, we should at
least try to redirect users to better and safer interfaces like
os/execute.
I encountered this issue with `janet -e '(os/shell "echo")'` on Mac, system malloc lib will complain as follows:
```
janet(42817,0x7ff854547780) malloc: *** error for object 0x60000000b730: pointer being freed was not allocated
janet(42817,0x7ff854547780) malloc: *** set a breakpoint in malloc_error_break to debug
```
So I followed the suggestion and here's the backtrace:
```
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
* frame #0: 0x00007ff810cb6883 libsystem_malloc.dylib`malloc_error_break
frame #1: 0x00007ff810ca76d3 libsystem_malloc.dylib`malloc_vreport + 761
frame #2: 0x00007ff810caab31 libsystem_malloc.dylib`malloc_report + 151
frame #3: 0x0000000100025402 janet`janet_ev_default_threaded_callback(return_value=JanetEVGenericMessage @ 0x00007ff7bfefad90) at ev.c:2412:13 [opt]
frame #4: 0x0000000100024b1f janet`janet_loop1_impl [inlined] janet_ev_handle_selfpipe at ev.c:1643:13 [opt]
frame #5: 0x0000000100024ac9 janet`janet_loop1_impl(has_timeout=<unavailable>, timeout=0) at ev.c:2028:13 [opt]
frame #6: 0x0000000100024714 janet`janet_loop1 at ev.c:1588:13 [opt]
frame #7: 0x00000001000497f5 janet`janet_loop_fiber at ev.c:1609:41 [opt]
frame #8: 0x00000001000497b3 janet`janet_loop_fiber(fiber=0x0000600002903790) at run.c:150:5 [opt]
frame #9: 0x0000000100075266 janet`main(argc=<unavailable>, argv=0x00007ff7bfeff210) at shell.c:1284:14 [opt]
frame #10: 0x00007ff810b1041f dyld`start + 1903
```
Found out that it's actually a missing break in the code which leads to double free.
BTW, I'm thinking that if we can implement a debug allocator to be used in the test, so maybe this kind of bugs can be captured earlier.
Change-Id: I74d324b70b39ddc7d6f509b0382e2be36a6a6964
Signed-off-by: Tw <tw19881113@gmail.com>
on 32 bit systems in some cases.
Since most internal arrays in Janet have 32 bit limits, this is is not
hit in practice on 64 bit systems, except in cases of user input. Apply
a fix more generally to simply avoid the issue by coding "defensively".
Some applications are not strictly needed.
Also match shell convention, with strict searching from left to right
instead of right to left by default. The _first_ element will be bound
to (dyn *syspath*), not the last.
Potentially breaking change for complicated, custom setups
Flag collisions was causing some unexpected behavior in some situations.
Removing the source of collisions resolve odd cancelation behavior in
the ev/ module where multiple calls to ev/cancel on the same fiber would
mess with the internal fiber flags and cause bad behavior and the wrong
error message return (but still otherwise behave correctly, so unless
you were parsing the returned error messages, things likely worked).
Also leave comments and test to avoid regressions.