microsoft / microsoft/vscode-wasm

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

Open
#161 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
TypeScript
Stars
456
Forks
40
Avg merge
3d 2h
Merged PRs (30d)
4

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the wasi_snapshot_preview1 path_open implementation used by @vscode/wasm-wasi and compare its handling of the repro.wat call with node:wasi and Wasmtime. Reproduce the fd=3 call through createProcess and verify that direct path_open succeeds without breaking the existing C fopen("test.txt", "r") case.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript, wasm
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.