Skip to content

wasm: Write to address zero optimizes into unreachable #21877

Closed as not planned
Closed as not planned
@rijenkii

Description

Zig Version

0.13.0

Steps to Reproduce and Observed Behavior

  1. Create file main.zig with the following contents:
    export fn write() void {
        @as(*allowzero u8, @ptrFromInt(0)).* = 1;
    }
  2. Compile the library into wasm
    zig build-exe mem.zig -target wasm32-freestanding -fno-entry -O ReleaseSmall --export=write
  3. 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)))

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions