I’m running into a really strange issue. The wasm files I’m getting from Zig 0.15.2 all have bad data segments whenever they contain calls to std.debug.print(). The following compiles correctly:
It happens at all optimization levels. I’ve also tried -Dcpu=bleeding_edge and -Dcpu=generic. Linking in libc yield the following warnings:
warning(object): unimplemented: element section in /home/cleong/.cache/zig/o/b732b09b1f3d21ce1b83c9cc8f803c5a/libc.a /home/cleong/.cache/zig/o/a4bcede13b1c8babe68df1355c3867bb/scandirat.o
warning(object): unimplemented: element section in /home/cleong/.cache/zig/o/b732b09b1f3d21ce1b83c9cc8f803c5a/libc.a /home/cleong/.cache/zig/o/93e053a668595ec9545bc2784f4139f8/preopens.o
warning(object): unimplemented: element section in /home/cleong/.cache/zig/o/b732b09b1f3d21ce1b83c9cc8f803c5a/libc.a /home/cleong/.cache/zig/o/40161c11888d600861002b3215a04f3c/posix.o
warning(object): unimplemented: element section in /home/cleong/.cache/zig/o/b732b09b1f3d21ce1b83c9cc8f803c5a/libc.a /home/cleong/.cache/zig/o/dc0433cd97e3c17c3e73bbc832d27a21/stdout.o
warning(object): unimplemented: element section in /home/cleong/.cache/zig/o/b732b09b1f3d21ce1b83c9cc8f803c5a/libc.a /home/cleong/.cache/zig/o/f0566cdd72f2e2d53d1a07588cd424bc/stderr.o
warning(object): unimplemented: element section in /home/cleong/.cache/zig/o/b732b09b1f3d21ce1b83c9cc8f803c5a/libc.a /home/cleong/.cache/zig/o/839094a270b3eedcb89f137dab3e64d4/fopencookie.o
warning(object): unimplemented: element section in /home/cleong/.cache/zig/o/b732b09b1f3d21ce1b83c9cc8f803c5a/libc.a /home/cleong/.cache/zig/o/e6d5b8871c3700df4c1ffbb9ba309673/stdin.o
warning(object): unimplemented: element section in /home/cleong/.cache/zig/o/b732b09b1f3d21ce1b83c9cc8f803c5a/libc.a /home/cleong/.cache/zig/o/153c1e39ad07a7d4bf92a641e0618214/qsort_nr.o
warning(object): unimplemented: element section in /home/cleong/.cache/zig/o/b732b09b1f3d21ce1b83c9cc8f803c5a/libc.a /home/cleong/.cache/zig/o/cc456186ab6bf200915dc0d8a5da1962/atexit.o
...
I should note that I’m not compiling the file in isolation. The code in question is compiled alongside other stuff. If I compile just the one file, the problem does not occur. In that that case the .wasm file would only contain one data segment.
I’m going to investigate this further to see if the problem is triggered by my code or if it happens when there’re multiple data segments.
You were actually on the right track. I had forgotten that I added this line in my build file because code generated by the x86-64 backend wasn’t working quite right when multithreaded:
.use_llvm = cfg.multithreaded,
That ends up disabling llvm for wasm. I wonder if I should report the issue nonetheless. It appears to have something to do with threadlocal variables.
Your original code incorrectly copied writer.interface to stderr. stderr.print internally calls File.Writer.drain(), you can look at its implementation. It attempts to find the writer to which stderr belongs and modify its state. However, your stderr is a copy, so it no longer belongs to a writer, making the state modification a memory corruption.