Skip to content

[bug] Calling wasi_snapshot_preview1 path_open without "extra steps" returns BADF (error 8) #161

@glebbash

Description

@glebbash

When the following is compiled with WASI SDK to test.wasm:

#include <stdio.h>

int main(int argc, const char **argv)
{
    FILE *file = fopen("test.txt", "r");
    if (file == NULL)
    {
        return 1;
    }

    char buffer[1024];
    while (fgets(buffer, sizeof(buffer), file) != NULL)
    {
        fputs(buffer, stdout);
    }
    fclose(file);

    return 0;
}

and executed using this setup:

import { Wasm } from "@vscode/wasm-wasi";

// ...
const wasm: Wasm = await Wasm.api();
const pty = wasm.createPseudoterminal();
const terminal = vscode.window.createTerminal({
    name: "test",
    pty,
    isTransient: true,
});
terminal.show(true);

try {
    const process = await wasm.createProcess(
        "test.wasm",
        currentCompilerModule,
        {
            stdio: pty.stdio,
            mountPoints: [
                {
                    kind: "vscodeFileSystem",
                    uri: workspaceUri,
                    mountPoint: "/",
                },
            ],
        }
    );

    const exitCode = await process.run();
    if (exitCode !== 0) {
        vscode.window.showErrorMessage(
            `Process exited with code: ${exitCode}`
        );
    }
} catch (error) {
    vscode.window.showErrorMessage((error as Error).message);
    terminal.dispose();
}

VSCode properly reads test.txt file from the current workspace and print it to created terminal.

However:

Trying to call wasi_snapshot_preview1 path_open function with fd=3, path="test.txt" directly always returns EBADF (code 8).

Minimal reproduction:

(; repro.wat ;)
(module
  (type (;0;) (func (param i32 i32 i32 i32 i32 i64 i64 i32 i32) (result i32)))
  (type (;1;) (func (param i32)))
  (type (;2;) (func))
  (import "wasi_snapshot_preview1" "path_open" (func (;0;) (type 0)))
  (import "wasi_snapshot_preview1" "proc_exit" (func (;1;) (type 1)))
  (func $_start (type 2)
    (local i32)
    i32.const 3
    i32.const 1
    i32.const 68314
    i32.const 8
    i32.const 0
    i64.const 264240830
    i64.const 268435455
    i32.const 0
    i32.const 67188
    call 0
    local.set 0
    local.get 0
    call 1)
  (memory (;0;) 40)
  (export "memory" (memory 0))
  (export "_start" (func $_start))
  (data (;0;) (i32.const 68314) "test.txt"))

P.S: Arguments to the function were extracted by using node:wasi and logging calls of all import functions.

Running repro.wat works fine in both node:wasi and wasmtime.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue identified by VS Code Team member as probable bug

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions