Skip to content

Commit

Permalink
adds internal fsapi.Errno to begin decoupling from the syscall package
Browse files Browse the repository at this point in the history
This cherry-picks errors we use in our wasip1 implementation as
`fsapi.Errno`. This is to start progress towards compiling on all GOOS
values (such as plan9), to make future changes a lot easier.

See #1578

Signed-off-by: Adrian Cole <[email protected]>
  • Loading branch information
Adrian Cole committed Jul 15, 2023
1 parent 0addbd0 commit 2fd46c3
Show file tree
Hide file tree
Showing 95 changed files with 1,598 additions and 1,413 deletions.
2 changes: 1 addition & 1 deletion cmd/wazero/wazero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ Consider switching to GOOS=wasip1.
==> go.runtime.getRandomData(r_len=8)
<==
==> go.syscall/js.valueCall(fs.open(path=/bear.txt,flags=,perm=----------))
<== (err=function not implemented,fd=0)
<== (err=functionality not supported,fd=0)
`, // Test only shows logging happens in two scopes; it is ok to fail.
expectedExitCode: 1,
}
Expand Down
14 changes: 7 additions & 7 deletions imports/wasi_snapshot_preview1/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package wasi_snapshot_preview1

import (
"context"
"syscall"

"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/internal/fsapi"
"github.com/tetratelabs/wazero/internal/wasip1"
"github.com/tetratelabs/wazero/internal/wasm"
)
Expand All @@ -23,7 +23,7 @@ import (
// Result (Errno)
//
// The return value is ErrnoSuccess except the following error conditions:
// - syscall.EFAULT: there is not enough memory to write results
// - fsapi.EFAULT: there is not enough memory to write results
//
// For example, if argsSizesGet wrote argc=2 and argvLen=5 for arguments:
// "a" and "bc" parameters argv=7 and argvBuf=1, this function writes the below
Expand All @@ -43,7 +43,7 @@ import (
// See https://en.wikipedia.org/wiki/Null-terminated_string
var argsGet = newHostFunc(wasip1.ArgsGetName, argsGetFn, []api.ValueType{i32, i32}, "argv", "argv_buf")

func argsGetFn(_ context.Context, mod api.Module, params []uint64) syscall.Errno {
func argsGetFn(_ context.Context, mod api.Module, params []uint64) fsapi.Errno {
sysCtx := mod.(*wasm.ModuleInstance).Sys
argv, argvBuf := uint32(params[0]), uint32(params[1])
return writeOffsetsAndNullTerminatedValues(mod.Memory(), sysCtx.Args(), argv, argvBuf, sysCtx.ArgsSize())
Expand All @@ -61,7 +61,7 @@ func argsGetFn(_ context.Context, mod api.Module, params []uint64) syscall.Errno
// Result (Errno)
//
// The return value is ErrnoSuccess except the following error conditions:
// - syscall.EFAULT: there is not enough memory to write results
// - fsapi.EFAULT: there is not enough memory to write results
//
// For example, if args are "a", "bc" and parameters resultArgc=1 and
// resultArgvLen=6, this function writes the below to api.Memory:
Expand All @@ -80,18 +80,18 @@ func argsGetFn(_ context.Context, mod api.Module, params []uint64) syscall.Errno
// See https://en.wikipedia.org/wiki/Null-terminated_string
var argsSizesGet = newHostFunc(wasip1.ArgsSizesGetName, argsSizesGetFn, []api.ValueType{i32, i32}, "result.argc", "result.argv_len")

func argsSizesGetFn(_ context.Context, mod api.Module, params []uint64) syscall.Errno {
func argsSizesGetFn(_ context.Context, mod api.Module, params []uint64) fsapi.Errno {
sysCtx := mod.(*wasm.ModuleInstance).Sys
mem := mod.Memory()
resultArgc, resultArgvLen := uint32(params[0]), uint32(params[1])

// argc and argv_len offsets are not necessarily sequential, so we have to
// write them independently.
if !mem.WriteUint32Le(resultArgc, uint32(len(sysCtx.Args()))) {
return syscall.EFAULT
return fsapi.EFAULT
}
if !mem.WriteUint32Le(resultArgvLen, sysCtx.ArgsSize()) {
return syscall.EFAULT
return fsapi.EFAULT
}
return 0
}
26 changes: 13 additions & 13 deletions imports/wasi_snapshot_preview1/clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package wasi_snapshot_preview1

import (
"context"
"syscall"

"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/internal/fsapi"
"github.com/tetratelabs/wazero/internal/wasip1"
"github.com/tetratelabs/wazero/internal/wasm"
)
Expand All @@ -21,9 +21,9 @@ import (
// Result (Errno)
//
// The return value is 0 except the following error conditions:
// - syscall.ENOTSUP: the clock ID is not supported.
// - syscall.EINVAL: the clock ID is invalid.
// - syscall.EFAULT: there is not enough memory to write results
// - fsapi.ENOTSUP: the clock ID is not supported.
// - fsapi.EINVAL: the clock ID is invalid.
// - fsapi.EFAULT: there is not enough memory to write results
//
// For example, if the resolution is 100ns, this function writes the below to
// api.Memory:
Expand All @@ -39,7 +39,7 @@ import (
// See https://linux.die.net/man/3/clock_getres
var clockResGet = newHostFunc(wasip1.ClockResGetName, clockResGetFn, []api.ValueType{i32, i32}, "id", "result.resolution")

func clockResGetFn(_ context.Context, mod api.Module, params []uint64) syscall.Errno {
func clockResGetFn(_ context.Context, mod api.Module, params []uint64) fsapi.Errno {
sysCtx := mod.(*wasm.ModuleInstance).Sys
id, resultResolution := uint32(params[0]), uint32(params[1])

Expand All @@ -50,11 +50,11 @@ func clockResGetFn(_ context.Context, mod api.Module, params []uint64) syscall.E
case wasip1.ClockIDMonotonic:
resolution = uint64(sysCtx.NanotimeResolution())
default:
return syscall.EINVAL
return fsapi.EINVAL
}

if !mod.Memory().WriteUint64Le(resultResolution, resolution) {
return syscall.EFAULT
return fsapi.EFAULT
}
return 0
}
Expand All @@ -73,9 +73,9 @@ func clockResGetFn(_ context.Context, mod api.Module, params []uint64) syscall.E
// Result (Errno)
//
// The return value is 0 except the following error conditions:
// - syscall.ENOTSUP: the clock ID is not supported.
// - syscall.EINVAL: the clock ID is invalid.
// - syscall.EFAULT: there is not enough memory to write results
// - fsapi.ENOTSUP: the clock ID is not supported.
// - fsapi.EINVAL: the clock ID is invalid.
// - fsapi.EFAULT: there is not enough memory to write results
//
// For example, if time.Now returned exactly midnight UTC 2022-01-01
// (1640995200000000000), and parameters resultTimestamp=1, this function
Expand All @@ -92,7 +92,7 @@ func clockResGetFn(_ context.Context, mod api.Module, params []uint64) syscall.E
// See https://linux.die.net/man/3/clock_gettime
var clockTimeGet = newHostFunc(wasip1.ClockTimeGetName, clockTimeGetFn, []api.ValueType{i32, i64, i32}, "id", "precision", "result.timestamp")

func clockTimeGetFn(_ context.Context, mod api.Module, params []uint64) syscall.Errno {
func clockTimeGetFn(_ context.Context, mod api.Module, params []uint64) fsapi.Errno {
sysCtx := mod.(*wasm.ModuleInstance).Sys
id := uint32(params[0])
// TODO: precision is currently ignored.
Expand All @@ -106,11 +106,11 @@ func clockTimeGetFn(_ context.Context, mod api.Module, params []uint64) syscall.
case wasip1.ClockIDMonotonic:
val = sysCtx.Nanotime()
default:
return syscall.EINVAL
return fsapi.EINVAL
}

if !mod.Memory().WriteUint64Le(resultTimestamp, uint64(val)) {
return syscall.EFAULT
return fsapi.EFAULT
}
return 0
}
14 changes: 7 additions & 7 deletions imports/wasi_snapshot_preview1/environ.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package wasi_snapshot_preview1

import (
"context"
"syscall"

"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/internal/fsapi"
"github.com/tetratelabs/wazero/internal/wasip1"
"github.com/tetratelabs/wazero/internal/wasm"
)
Expand All @@ -24,7 +24,7 @@ import (
// Result (Errno)
//
// The return value is 0 except the following error conditions:
// - syscall.EFAULT: there is not enough memory to write results
// - fsapi.EFAULT: there is not enough memory to write results
//
// For example, if environSizesGet wrote environc=2 and environLen=9 for
// environment variables: "a=b", "b=cd" and parameters environ=11 and
Expand All @@ -43,7 +43,7 @@ import (
// See https://en.wikipedia.org/wiki/Null-terminated_string
var environGet = newHostFunc(wasip1.EnvironGetName, environGetFn, []api.ValueType{i32, i32}, "environ", "environ_buf")

func environGetFn(_ context.Context, mod api.Module, params []uint64) syscall.Errno {
func environGetFn(_ context.Context, mod api.Module, params []uint64) fsapi.Errno {
sysCtx := mod.(*wasm.ModuleInstance).Sys
environ, environBuf := uint32(params[0]), uint32(params[1])

Expand All @@ -63,7 +63,7 @@ func environGetFn(_ context.Context, mod api.Module, params []uint64) syscall.Er
// Result (Errno)
//
// The return value is 0 except the following error conditions:
// - syscall.EFAULT: there is not enough memory to write results
// - fsapi.EFAULT: there is not enough memory to write results
//
// For example, if environ are "a=b","b=cd" and parameters resultEnvironc=1 and
// resultEnvironvLen=6, this function writes the below to api.Memory:
Expand All @@ -83,18 +83,18 @@ func environGetFn(_ context.Context, mod api.Module, params []uint64) syscall.Er
// and https://en.wikipedia.org/wiki/Null-terminated_string
var environSizesGet = newHostFunc(wasip1.EnvironSizesGetName, environSizesGetFn, []api.ValueType{i32, i32}, "result.environc", "result.environv_len")

func environSizesGetFn(_ context.Context, mod api.Module, params []uint64) syscall.Errno {
func environSizesGetFn(_ context.Context, mod api.Module, params []uint64) fsapi.Errno {
sysCtx := mod.(*wasm.ModuleInstance).Sys
mem := mod.Memory()
resultEnvironc, resultEnvironvLen := uint32(params[0]), uint32(params[1])

// environc and environv_len offsets are not necessarily sequential, so we
// have to write them independently.
if !mem.WriteUint32Le(resultEnvironc, uint32(len(sysCtx.Environ()))) {
return syscall.EFAULT
return fsapi.EFAULT
}
if !mem.WriteUint32Le(resultEnvironvLen, sysCtx.EnvironSize()) {
return syscall.EFAULT
return fsapi.EFAULT
}
return 0
}
Loading

0 comments on commit 2fd46c3

Please sign in to comment.