Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error "out of bounds memory access" when using std.compress.zlib.compress() with wasm32-freestanding target #21857

Open
falconandy opened this issue Oct 30, 2024 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@falconandy
Copy link

falconandy commented Oct 30, 2024

Zig Version

0.13.0, 0.14.0-dev.2064+b5cafe223

Steps to Reproduce and Observed Behavior

Simplified example with wasmtime engine (actually my host environment is web browsers):

src/main.zig:

const std = @import("std");

pub fn main() void {
    var w = std.ArrayList(u8).init(std.heap.page_allocator);
    defer w.deinit();

    var stream = std.io.fixedBufferStream("01234567890");
    std.compress.zlib.compress(stream.reader(), w.writer(), std.compress.zlib.Options{}) catch {};
}

Command:
zig build-exe ./src/main.zig -target wasm32-freestanding && WASMTIME_BACKTRACE_DETAILS=1 wasmtime main.wasm

Output:

Error: failed to run main module `main.wasm`

Caused by:
    0: failed to invoke command default
    1: error while executing at wasm backtrace:
           0: 0x190f - compress.flate.bit_writer.BitWriter(io.GenericWriter(*array_list.ArrayListAligned(u8,null),error{OutOfMemory},(function 'appendWrite'))).init
                           at /home/falconandy/zig/lib/std/compress/flate/bit_writer.zig:38:13
           1: 0x15f4 - compress.flate.block_writer.BlockWriter(io.GenericWriter(*array_list.ArrayListAligned(u8,null),error{OutOfMemory},(function 'appendWrite'))).init
                           at /home/falconandy/zig/lib/std/compress/flate/block_writer.zig:40:49
           2: 0x12a1 - compress.flate.deflate.Deflate(.zlib,io.GenericWriter(*array_list.ArrayListAligned(u8,null),error{OutOfMemory},(function 'appendWrite')),compress.flate.block_writer.BlockWriter(io.GenericWriter(*array_list.ArrayListAligned(u8,null),error{OutOfMemory},(function 'appendWrite')))).init
                           at /home/falconandy/zig/lib/std/compress/flate/deflate.zig:141:53
           3: 0x2426 - compress.flate.deflate.compressor__anon_1861
                           at /home/falconandy/zig/lib/std/compress/flate/deflate.zig:67:59
           4: 0x116cf - compress.flate.deflate.compress__anon_1566
                           at /home/falconandy/zig/lib/std/compress/flate/deflate.zig:57:27
           5:  0x61c - compress.zlib.compress__anon_1549
                           at /home/falconandy/zig/lib/std/compress/zlib.zig:24:25
           6:  0x3cb - main.main
                           at /home/falconandy/CLionProjects/untitled3/src/main.zig:8:31
           7:  0x1bc - start.callMain
                           at /home/falconandy/zig/lib/std/start.zig:514:22              - start.wasm_freestanding_start
                           at /home/falconandy/zig/lib/std/start.zig:199:5
    2: memory fault at wasm address 0xffde5880 in linear memory of size 0x170000
    3: wasm trap: out of bounds memory access

Expected Behavior

No errors.
It looks like wasm32-wasi target works fine:

Command:
zig build-exe ./src/main.zig -target wasm32-wasi && WASMTIME_BACKTRACE_DETAILS=1 wasmtime main.wasm

Output: empty, no errors.

@falconandy falconandy added the bug Observed behavior contradicts documented or intended behavior label Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

1 participant