wasm: Write to address zero optimizes into unreachable
#21877
Closed as not planned
Description
Zig Version
0.13.0
Steps to Reproduce and Observed Behavior
- Create file
main.zig
with the following contents:export fn write() void { @as(*allowzero u8, @ptrFromInt(0)).* = 1; }
- Compile the library into wasm
zig build-exe mem.zig -target wasm32-freestanding -fno-entry -O ReleaseSmall --export=write
- Run
wat2wasm
to view the compiled instructions(module (type (;0;) (func)) (func (;0;) (type 0) unreachable) (memory (;0;) 16) (global (;0;) (mut i32) (i32.const 1048576)) (export "memory" (memory 0)) (export "write" (func 0)))
This does not happen when the program is compiled without -O ReleaseSmall
or when the address is changed from 0
.
Expected Behavior
Expected following wasm to be generated:
(module
(type (;0;) (func))
(func (;0;) (type 0)
i32.const 0
i32.const 0
i32.store8 offset=1)
(memory (;0;) 16)
(global (;0;) (mut i32) (i32.const 1048576))
(export "memory" (memory 0))
(export "write" (func 0)))