Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions engine/wasi_fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package engine

import (
"io/fs"

"github.com/tetratelabs/wazero"
experimentalsys "github.com/tetratelabs/wazero/experimental/sys"
wazerosysfs "github.com/tetratelabs/wazero/experimental/sysfs"
)

type readOnlyMountFS struct {
*wazerosysfs.AdaptFS
}

func withReadOnlyFSMount(cfg wazero.FSConfig, fsys fs.FS, guest string) wazero.FSConfig {
sysCfg := cfg.(wazerosysfs.FSConfig)
return sysCfg.WithSysFSMount(&readOnlyMountFS{
AdaptFS: &wazerosysfs.AdaptFS{FS: fsys},
}, guest)
}

func (f *readOnlyMountFS) Readlink(path string) (string, experimentalsys.Errno) {
if _, errno := f.Lstat(path); errno != 0 {
return "", errno
}
return "", experimentalsys.EINVAL
}
2 changes: 1 addition & 1 deletion engine/wazero.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func applyWASIConfig(mc wazero.ModuleConfig, cfg *InstanceConfig) wazero.ModuleC
for _, mnt := range cfg.Mounts {
switch {
case mnt.FS != nil:
fsCfg = fsCfg.WithFSMount(mnt.FS, mnt.Guest)
fsCfg = withReadOnlyFSMount(fsCfg, mnt.FS, mnt.Guest)
case mnt.ReadOnly:
fsCfg = fsCfg.WithReadOnlyDirMount(mnt.Host, mnt.Guest)
default:
Expand Down
58 changes: 58 additions & 0 deletions engine/wazero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strings"
"testing"
"testing/fstest"

"github.com/wippyai/wasm-runtime/wat"
)
Expand Down Expand Up @@ -81,6 +82,63 @@ func TestWazeroEngine_Close(t *testing.T) {
}
}

func TestWazeroEngine_FSMountReadlinkRegularFile(t *testing.T) {
ctx := context.Background()

eng, err := NewWazeroEngine(ctx)
if err != nil {
t.Fatalf("NewWazeroEngine: %v", err)
}
defer eng.Close(ctx)

wasmBytes, err := wat.Compile(`(module
(import "wasi_snapshot_preview1" "path_readlink"
(func $path_readlink (param i32 i32 i32 i32 i32 i32) (result i32)))
(memory (export "memory") 1)
(data (i32.const 8) "file.txt")
(func (export "readlink_errno") (result i32)
(call $path_readlink
(i32.const 3)
(i32.const 8)
(i32.const 8)
(i32.const 32)
(i32.const 64)
(i32.const 4))))`)
if err != nil {
t.Fatalf("compile WAT: %v", err)
}

mod, err := eng.LoadModule(ctx, wasmBytes)
if err != nil {
t.Fatalf("LoadModule: %v", err)
}

inst, err := mod.InstantiateWithConfig(ctx, &InstanceConfig{
Mounts: []Mount{{
Guest: "/data",
FS: fstest.MapFS{"file.txt": &fstest.MapFile{Data: []byte("ok")}},
ReadOnly: true,
}},
})
if err != nil {
t.Fatalf("InstantiateWithConfig: %v", err)
}
defer inst.Close(ctx)

fn := inst.instance.ExportedFunction("readlink_errno")
if fn == nil {
t.Fatal("readlink_errno function not exported")
}
results, err := fn.Call(ctx)
if err != nil {
t.Fatalf("readlink_errno: %v", err)
}
const wasiErrnoInval = uint64(28)
if got := results[0]; got != wasiErrnoInval {
t.Fatalf("path_readlink regular file errno = %d, want %d", got, wasiErrnoInval)
}
}

// TestWazeroEngine_HTTPComponentExports verifies HTTP component loading and export resolution.
func TestWazeroEngine_HTTPComponentExports(t *testing.T) {
ctx := context.Background()
Expand Down