Skip to content

CheckStackSize error with default operand_stack_size #223

Description

@mgord9518

When running my test, the main file will crash with a CheckStackSize error. If I raise the operand_stack_size to 2048, it appears to fix the issue, but I don't know very much about WASM and I'm not sure if this is actually a good solution.

Stranger yet, when the WASM file is built in any release mode, the issue doesn't happen.

In order to run the test, a couple of the wasi implementation files in this repo have to be updated to 0.12.0, but they're all very small changes (like math.absCast to @abs)

This is the test file built to wasm32-wasi:

const std = @import("std");

pub fn main() !void {
    std.debug.print("All your {s} codebase are belong to us.\n", .{"ligma"});

    const stdout_file = std.io.getStdOut().writer();
    var bw = std.io.bufferedWriter(stdout_file);
    const stdout = bw.writer();

    var i: usize = 0;
    while (i < 100) : (i += 1) {
        try stdout.print("{d}\n", .{i});
    }

    try bw.flush();
}

And the program that loads it:

const std = @import("std");
const posix = std.posix;
const zware = @import("zware");
const Store = zware.Store;
const Module = zware.Module;
const Instance = zware.Instance;
const GeneralPurposeAllocator = std.heap.GeneralPurposeAllocator;
var gpa = GeneralPurposeAllocator(.{}){};

pub fn initHostFunctions(store: *zware.Store) !void {
    try store.exposeHostFunction("wasi_snapshot_preview1", "proc_exit", zware.wasi.proc_exit, &[_]zware.ValType{.I32}, &[_]zware.ValType{});
    try store.exposeHostFunction("wasi_snapshot_preview1", "fd_write", zware.wasi.fd_write, &[_]zware.ValType{ .I32, .I32, .I32, .I32 }, &[_]zware.ValType{.I32});
    try store.exposeHostFunction("wasi_snapshot_preview1", "args_get", zware.wasi.args_get, &[_]zware.ValType{ .I32, .I32 }, &[_]zware.ValType{.I32});
    try store.exposeHostFunction("wasi_snapshot_preview1", "args_sizes_get", zware.wasi.args_sizes_get, &[_]zware.ValType{ .I32, .I32 }, &[_]zware.ValType{.I32});
}

pub fn main() !void {
    defer _ = gpa.deinit();
    const alloc = gpa.allocator();

    const bytes = @embedFile("test.wasm");

    var store = Store.init(alloc);
    defer store.deinit();

    try initHostFunctions(&store);

    var module = Module.init(alloc, bytes);
    defer module.deinit();
    try module.decode();

    var instance = Instance.init(alloc, &store, module);
    try instance.instantiate();
    defer instance.deinit();

    var in = [0]u64{};
    var out = [0]u64{};

    try instance.addWasiPreopen(0, "stdin", posix.STDIN_FILENO);
    try instance.addWasiPreopen(0, "stdout", posix.STDOUT_FILENO);
    try instance.addWasiPreopen(0, "stderr", posix.STDERR_FILENO);

    try instance.invoke("_start", in[0..], out[0..], .{
        //.operand_stack_size = 2048,
    });
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions