Commit 0263bf1
authored
fix(parser): do not fuse local.get/local.tee across a branch target (#56)
I was trying to compile TCC to WASI and run it using tinywasm thanks to
the exception implementation (sigh, which is because of the use of
setjmp/longjmp for compiler rewind, that's the whole reason, WASI
syscall is really not that hard to say the least), but it went into a
mysterious bug that keeps trapping on the same address.
And yes, that means I did have a WASIp1 implementaion and I actually
implemented it with a VFS (nix the network socket just yet), so I can
theoretically have a cross-platform JIT compiler where I can have safe
reentrancy, and it is not until recently the TCC mob branches have their
TLS implementations, but it is all too late and they still rely on CRT.
It is also theoretically possible.
So my plan is to compile libtcc as small as possible to WASI using fat
LTO, and I keep getting trapped on the same address. Under NodeJS with a
shim WASI implementation, the whole libtcc fat LTO WASI actually works.
So it has to be a problem in tinywasm
Claude said this (it is Fable 5.1 btw):
> `visit_local_tee` peeked at the last emitted instruction and, when it
was a `local.get`, replaced the pair with `LocalCopy(src, dst);
LocalGet(dst)`. It never checked whether the tee's position is a jump
target. When the `local.get` is a block's fallthrough result and the
`local.tee` is the first instruction after `end`, every branch to that
label lands on `LocalGet(dst)` instead of the tee: the branch's value is
left dangling on the stack and a stale local is pushed on top. Loop
starts and if/else joins have the same shape.
>
> TinyCC compiled with clang -Os/-Oz hit this in musl's realloc: the `if
(!p) { r = malloc(n); br 1 }` path returned n instead of r, so the
caller wrote into address 1200 and later trapped with an out-of-bounds
load, while V8 and wabt ran the same module correctly.
>
> The lowering step now always emits a plain `LocalTee`. The fusion
moves into the peephole rewriter, which only matches within a basic
block and so cannot cross a label. Regression test covers the block-end,
if/else-join and loop-start cases.
>
I will try to use https://www.npmjs.com/package/@yowasp/clang to see if
there are more VM shenanigans, though a simple benchmark shows me that
the interpreter is 17x slower than native, and I guess the clang in
tinywasm wouldn't take less than 3 minutes to get a meaningful outcome
-- except if it blows up midway during execution.1 parent c67ce64 commit 0263bf1
3 files changed
Lines changed: 96 additions & 23 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
859 | 859 | | |
860 | 860 | | |
861 | 861 | | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
862 | 865 | | |
863 | 866 | | |
864 | 867 | | |
| |||
931 | 934 | | |
932 | 935 | | |
933 | 936 | | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
934 | 940 | | |
935 | 941 | | |
936 | 942 | | |
| |||
967 | 973 | | |
968 | 974 | | |
969 | 975 | | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
970 | 979 | | |
971 | 980 | | |
972 | 981 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
735 | 735 | | |
736 | 736 | | |
737 | 737 | | |
738 | | - | |
739 | | - | |
740 | | - | |
741 | | - | |
742 | | - | |
743 | | - | |
744 | | - | |
745 | | - | |
746 | | - | |
747 | | - | |
748 | | - | |
749 | | - | |
750 | | - | |
751 | | - | |
752 | | - | |
753 | | - | |
754 | | - | |
755 | | - | |
756 | | - | |
757 | | - | |
758 | | - | |
759 | | - | |
760 | | - | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
761 | 748 | | |
762 | 749 | | |
763 | 750 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
0 commit comments