make runtime compatible with wasm

This commit is contained in:
Li Jie
2025-04-08 16:50:47 +08:00
parent 7c81d9293b
commit be4737461a
183 changed files with 14122 additions and 647 deletions

View File

@@ -724,7 +724,7 @@ func forkAndExecInChild1(argv0 *c.Char, argv, envv **c.Char, chroot, dir *c.Char
childerror:
// send error code on pipe
os.Write(c.Int(pipe), unsafe.Pointer(&err1), unsafe.Sizeof(err1))
os.Write(c.Int(pipe), unsafe.Pointer(&err1), c.SizeT(unsafe.Sizeof(err1)))
for {
os.Exit(253)
}

View File

@@ -0,0 +1,325 @@
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build wasip1
package syscall
import (
"structs"
)
func init() {
// Try to set stdio to non-blocking mode before the os package
// calls NewFile for each fd. NewFile queries the non-blocking flag
// but doesn't change it, even if the runtime supports non-blocking
// stdio. Since WebAssembly modules are single-threaded, blocking
// system calls temporarily halt execution of the module. If the
// runtime supports non-blocking stdio, the Go runtime is able to
// use the WASI net poller to poll for read/write readiness and is
// able to schedule goroutines while waiting.
SetNonblock(0, true)
SetNonblock(1, true)
SetNonblock(2, true)
}
type uintptr32 = uint32
type size = uint32
type fdflags = uint32
type filesize = uint64
type filetype = uint8
type lookupflags = uint32
type oflags = uint32
type rights = uint64
type timestamp = uint64
type dircookie = uint64
type filedelta = int64
type fstflags = uint32
type iovec struct {
_ structs.HostLayout
buf uintptr32
bufLen size
}
const (
LOOKUP_SYMLINK_FOLLOW = 0x00000001
)
const (
OFLAG_CREATE = 0x0001
OFLAG_DIRECTORY = 0x0002
OFLAG_EXCL = 0x0004
OFLAG_TRUNC = 0x0008
)
const (
FDFLAG_APPEND = 0x0001
FDFLAG_DSYNC = 0x0002
FDFLAG_NONBLOCK = 0x0004
FDFLAG_RSYNC = 0x0008
FDFLAG_SYNC = 0x0010
)
const (
RIGHT_FD_DATASYNC = 1 << iota
RIGHT_FD_READ
RIGHT_FD_SEEK
RIGHT_FDSTAT_SET_FLAGS
RIGHT_FD_SYNC
RIGHT_FD_TELL
RIGHT_FD_WRITE
RIGHT_FD_ADVISE
RIGHT_FD_ALLOCATE
RIGHT_PATH_CREATE_DIRECTORY
RIGHT_PATH_CREATE_FILE
RIGHT_PATH_LINK_SOURCE
RIGHT_PATH_LINK_TARGET
RIGHT_PATH_OPEN
RIGHT_FD_READDIR
RIGHT_PATH_READLINK
RIGHT_PATH_RENAME_SOURCE
RIGHT_PATH_RENAME_TARGET
RIGHT_PATH_FILESTAT_GET
RIGHT_PATH_FILESTAT_SET_SIZE
RIGHT_PATH_FILESTAT_SET_TIMES
RIGHT_FD_FILESTAT_GET
RIGHT_FD_FILESTAT_SET_SIZE
RIGHT_FD_FILESTAT_SET_TIMES
RIGHT_PATH_SYMLINK
RIGHT_PATH_REMOVE_DIRECTORY
RIGHT_PATH_UNLINK_FILE
RIGHT_POLL_FD_READWRITE
RIGHT_SOCK_SHUTDOWN
RIGHT_SOCK_ACCEPT
)
const (
WHENCE_SET = 0
WHENCE_CUR = 1
WHENCE_END = 2
)
const (
FILESTAT_SET_ATIM = 0x0001
FILESTAT_SET_ATIM_NOW = 0x0002
FILESTAT_SET_MTIM = 0x0004
FILESTAT_SET_MTIM_NOW = 0x0008
)
const (
// Despite the rights being defined as a 64 bits integer in the spec,
// wasmtime crashes the program if we set any of the upper 32 bits.
fullRights = rights(^uint32(0))
readRights = rights(RIGHT_FD_READ | RIGHT_FD_READDIR)
writeRights = rights(RIGHT_FD_DATASYNC | RIGHT_FD_WRITE | RIGHT_FD_ALLOCATE | RIGHT_PATH_FILESTAT_SET_SIZE)
// Some runtimes have very strict expectations when it comes to which
// rights can be enabled on files opened by path_open. The fileRights
// constant is used as a mask to retain only bits for operations that
// are supported on files.
fileRights rights = RIGHT_FD_DATASYNC |
RIGHT_FD_READ |
RIGHT_FD_SEEK |
RIGHT_FDSTAT_SET_FLAGS |
RIGHT_FD_SYNC |
RIGHT_FD_TELL |
RIGHT_FD_WRITE |
RIGHT_FD_ADVISE |
RIGHT_FD_ALLOCATE |
RIGHT_PATH_CREATE_DIRECTORY |
RIGHT_PATH_CREATE_FILE |
RIGHT_PATH_LINK_SOURCE |
RIGHT_PATH_LINK_TARGET |
RIGHT_PATH_OPEN |
RIGHT_FD_READDIR |
RIGHT_PATH_READLINK |
RIGHT_PATH_RENAME_SOURCE |
RIGHT_PATH_RENAME_TARGET |
RIGHT_PATH_FILESTAT_GET |
RIGHT_PATH_FILESTAT_SET_SIZE |
RIGHT_PATH_FILESTAT_SET_TIMES |
RIGHT_FD_FILESTAT_GET |
RIGHT_FD_FILESTAT_SET_SIZE |
RIGHT_FD_FILESTAT_SET_TIMES |
RIGHT_PATH_SYMLINK |
RIGHT_PATH_REMOVE_DIRECTORY |
RIGHT_PATH_UNLINK_FILE |
RIGHT_POLL_FD_READWRITE
// Runtimes like wasmtime and wasmedge will refuse to open directories
// if the rights requested by the application exceed the operations that
// can be performed on a directory.
dirRights rights = RIGHT_FD_SEEK |
RIGHT_FDSTAT_SET_FLAGS |
RIGHT_FD_SYNC |
RIGHT_PATH_CREATE_DIRECTORY |
RIGHT_PATH_CREATE_FILE |
RIGHT_PATH_LINK_SOURCE |
RIGHT_PATH_LINK_TARGET |
RIGHT_PATH_OPEN |
RIGHT_FD_READDIR |
RIGHT_PATH_READLINK |
RIGHT_PATH_RENAME_SOURCE |
RIGHT_PATH_RENAME_TARGET |
RIGHT_PATH_FILESTAT_GET |
RIGHT_PATH_FILESTAT_SET_SIZE |
RIGHT_PATH_FILESTAT_SET_TIMES |
RIGHT_FD_FILESTAT_GET |
RIGHT_FD_FILESTAT_SET_TIMES |
RIGHT_PATH_SYMLINK |
RIGHT_PATH_REMOVE_DIRECTORY |
RIGHT_PATH_UNLINK_FILE
)
type preopentype = uint8
const (
preopentypeDir preopentype = iota
)
type prestatDir struct {
_ structs.HostLayout
prNameLen size
}
type prestat struct {
_ structs.HostLayout
typ preopentype
dir prestatDir
}
//go:wasmimport wasi_snapshot_preview1 fd_prestat_get
//go:noescape
func fd_prestat_get(fd int32, prestat *prestat) Errno
//go:wasmimport wasi_snapshot_preview1 fd_prestat_dir_name
//go:noescape
func fd_prestat_dir_name(fd int32, path *byte, pathLen size) Errno
type opendir struct {
fd int32
name string
}
// List of preopen directories that were exposed by the runtime. The first one
// is assumed to the be root directory of the file system, and others are seen
// as mount points at sub paths of the root.
var preopens []opendir
// Current working directory. We maintain this as a string and resolve paths in
// the code because wasmtime does not allow relative path lookups outside of the
// scope of a directory; a previous approach we tried consisted in maintaining
// open a file descriptor to the current directory so we could perform relative
// path lookups from that location, but it resulted in breaking path resolution
// from the current directory to its parent.
var cwd string
func Openat(dirFd int, path string, openmode int, perm uint32) (int, error) {
panic("not implemented")
}
func CloseOnExec(fd int) {
// nothing to do - no exec
}
func Mkdir(path string, perm uint32) error {
panic("not implemented")
}
func ReadDir(fd int, buf []byte, cookie dircookie) (int, error) {
panic("not implemented")
}
func Fstat(fd int, st *Stat_t) error {
panic("not implemented")
}
func Unlink(path string) error {
panic("not implemented")
}
func Rmdir(path string) error {
panic("not implemented")
}
func Chmod(path string, mode uint32) error {
var stat Stat_t
return Stat(path, &stat)
}
func Fchmod(fd int, mode uint32) error {
var stat Stat_t
return Fstat(fd, &stat)
}
func Chown(path string, uid, gid int) error {
panic("not implemented")
}
func Fchown(fd int, uid, gid int) error {
panic("not implemented")
}
func Lchown(path string, uid, gid int) error {
panic("not implemented")
}
func UtimesNano(path string, ts []Timespec) error {
panic("not implemented")
}
func Rename(from, to string) error {
panic("not implemented")
}
func Truncate(path string, length int64) error {
panic("not implemented")
}
func Ftruncate(fd int, length int64) error {
panic("not implemented")
}
const ImplementsGetwd = true
func Chdir(path string) error {
panic("not implemented")
}
func Readlink(path string, buf []byte) (n int, err error) {
panic("not implemented")
}
func Link(path, link string) error {
panic("not implemented")
}
func Symlink(path, link string) error {
panic("not implemented")
}
func Fsync(fd int) error {
panic("not implemented")
}
func Write(fd int, b []byte) (int, error) {
panic("not implemented")
}
func Pread(fd int, b []byte, offset int64) (int, error) {
panic("not implemented")
}
func Pwrite(fd int, b []byte, offset int64) (int, error) {
panic("not implemented")
}
func Dup(fd int) (int, error) {
panic("not implemented")
}
func Dup2(fd, newfd int) error {
panic("not implemented")
}

View File

@@ -0,0 +1,81 @@
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build wasip1
package syscall
import "syscall"
const (
SHUT_RD = 0x1
SHUT_WR = 0x2
SHUT_RDWR = SHUT_RD | SHUT_WR
)
type sdflags = uint32
//go:wasmimport wasi_snapshot_preview1 sock_accept
//go:noescape
func sock_accept(fd int32, flags fdflags, newfd *int32) Errno
//go:wasmimport wasi_snapshot_preview1 sock_shutdown
//go:noescape
func sock_shutdown(fd int32, flags sdflags) Errno
func Socket(proto, sotype, unused int) (fd int, err error) {
panic("not implemented")
}
func Bind(fd int, sa syscall.Sockaddr) error {
panic("not implemented")
}
func StopIO(fd int) error {
panic("not implemented")
}
func Listen(fd int, backlog int) error {
panic("not implemented")
}
func Connect(fd int, sa syscall.Sockaddr) error {
panic("not implemented")
}
func Recvfrom(fd int, p []byte, flags int) (n int, from syscall.Sockaddr, err error) {
panic("not implemented")
}
func Sendto(fd int, p []byte, flags int, to syscall.Sockaddr) error {
panic("not implemented")
}
func Recvmsg(fd int, p, oob []byte, flags int) (n, oobn, recvflags int, from syscall.Sockaddr, err error) {
panic("not implemented")
}
func SendmsgN(fd int, p, oob []byte, to syscall.Sockaddr, flags int) (n int, err error) {
panic("not implemented")
}
func GetsockoptInt(fd, level, opt int) (value int, err error) {
panic("not implemented")
}
func SetsockoptInt(fd, level, opt int, value int) error {
panic("not implemented")
}
func SetReadDeadline(fd int, t int64) error {
panic("not implemented")
}
func SetWriteDeadline(fd int, t int64) error {
panic("not implemented")
}
func Shutdown(fd int, how int) error {
panic("not implemented")
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build unix
//go:build unix && !wasm
package syscall

View File

@@ -83,30 +83,6 @@ func Getpid() (pid int) {
return int(os.Getpid())
}
func Kill(pid int, signum Signal) (err error) {
ret := os.Kill(os.PidT(pid), c.Int(signum))
if ret == 0 {
return nil
}
return Errno(ret)
}
func fork() (uintptr, Errno) {
ret := os.Fork()
if ret >= 0 {
return uintptr(ret), Errno(0)
}
return 0, Errno(os.Errno())
}
func wait4(pid int, wstatus *c.Int, options int, rusage *syscall.Rusage) (wpid int, err error) {
ret := os.Wait4(os.PidT(pid), wstatus, c.Int(options), rusage)
if ret >= 0 {
return int(ret), nil
}
return 0, Errno(os.Errno())
}
func Open(path string, mode int, perm uint32) (fd int, err error) {
ret := os.Open(c.AllocaCStr(path), c.Int(mode), os.ModeT(perm))
if ret >= 0 {
@@ -165,38 +141,6 @@ func Stat(path string, stat *Stat_t) (err error) {
return Errno(os.Errno())
}
func Pipe(p []int) (err error) {
if len(p) != 2 {
return Errno(syscall.EINVAL)
}
var q [2]c.Int
ret := os.Pipe(&q)
if ret == 0 {
p[0] = int(q[0])
p[1] = int(q[1])
return nil
}
return Errno(ret)
}
type Rlimit syscall.Rlimit
func Getrlimit(which int, lim *Rlimit) (err error) {
ret := os.Getrlimit(c.Int(which), (*syscall.Rlimit)(lim))
if ret == 0 {
return nil
}
return Errno(ret)
}
func setrlimit(which int, lim *Rlimit) (err error) {
ret := os.Setrlimit(c.Int(which), (*syscall.Rlimit)(lim))
if ret == 0 {
return nil
}
return Errno(ret)
}
func BytePtrFromString(s string) (*byte, error) {
a, err := ByteSliceFromString(s)
if err != nil {
@@ -217,3 +161,7 @@ func ByteSliceFromString(s string) ([]byte, error) {
func Accept(fd int) (nfd int, sa origSyscall.Sockaddr, err error) {
panic("todo: syscall.Accept")
}
func Kill(pid int, signum Signal) error {
return syscall.Kill(pid, syscall.Signal(signum))
}

View File

@@ -0,0 +1,57 @@
//go:build !wasm
package syscall
import (
c "github.com/goplus/llgo/runtime/internal/clite"
"github.com/goplus/llgo/runtime/internal/clite/os"
"github.com/goplus/llgo/runtime/internal/clite/syscall"
)
type Rlimit syscall.Rlimit
func Getrlimit(which int, lim *Rlimit) (err error) {
ret := os.Getrlimit(c.Int(which), (*syscall.Rlimit)(lim))
if ret == 0 {
return nil
}
return Errno(ret)
}
func setrlimit(which int, lim *Rlimit) (err error) {
ret := os.Setrlimit(c.Int(which), (*syscall.Rlimit)(lim))
if ret == 0 {
return nil
}
return Errno(ret)
}
func wait4(pid int, wstatus *c.Int, options int, rusage *syscall.Rusage) (wpid int, err error) {
ret := os.Wait4(os.PidT(pid), wstatus, c.Int(options), rusage)
if ret >= 0 {
return int(ret), nil
}
return 0, Errno(os.Errno())
}
func fork() (uintptr, Errno) {
ret := os.Fork()
if ret >= 0 {
return uintptr(ret), Errno(0)
}
return 0, Errno(os.Errno())
}
func Pipe(p []int) (err error) {
if len(p) != 2 {
return Errno(syscall.EINVAL)
}
var q [2]c.Int
ret := os.Pipe(&q)
if ret == 0 {
p[0] = int(q[0])
p[1] = int(q[1])
return nil
}
return Errno(ret)
}

View File

@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build unix
//go:build unix && !wasm
package syscall

View File

@@ -0,0 +1,366 @@
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build wasip1
package syscall
import (
"strconv"
"github.com/goplus/llgo/runtime/internal/clite/syscall"
)
const (
DT_UNKNOWN = 0
DT_FIFO = 1
DT_CHR = 2
DT_DIR = 4
DT_BLK = 6
DT_REG = 8
DT_LNK = 10
DT_SOCK = 12
DT_WHT = 14
)
type Dircookie = uint64
type Filetype = uint8
const (
FILETYPE_UNKNOWN Filetype = iota
FILETYPE_BLOCK_DEVICE
FILETYPE_CHARACTER_DEVICE
FILETYPE_DIRECTORY
FILETYPE_REGULAR_FILE
FILETYPE_SOCKET_DGRAM
FILETYPE_SOCKET_STREAM
FILETYPE_SYMBOLIC_LINK
)
type Dirent struct {
// The offset of the next directory entry stored in this directory.
Next Dircookie
// The serial number of the file referred to by this directory entry.
Ino uint64
// The length of the name of the directory entry.
Namlen uint32
// The type of the file referred to by this directory entry.
Type Filetype
// Name of the directory entry.
Name *byte
}
// An Errno is an unsigned number describing an error condition.
// It implements the error interface. The zero Errno is by convention
// a non-error, so code to convert from Errno to error should use:
//
// var err = nil
// if errno != 0 {
// err = errno
// }
type Errno syscall.Errno
func (e Errno) Error() string {
return syscall.Errno(e).Error()
}
func (e Errno) Is(target error) bool {
return syscall.Errno(e).Is(target)
}
// A Signal is a number describing a process signal.
// It implements the [os.Signal] interface.
type Signal uint8
const (
SIGNONE Signal = iota
SIGHUP
SIGINT
SIGQUIT
SIGILL
SIGTRAP
SIGABRT
SIGBUS
SIGFPE
SIGKILL
SIGUSR1
SIGSEGV
SIGUSR2
SIGPIPE
SIGALRM
SIGTERM
SIGCHLD
SIGCONT
SIGSTOP
SIGTSTP
SIGTTIN
SIGTTOU
SIGURG
SIGXCPU
SIGXFSZ
SIGVTARLM
SIGPROF
SIGWINCH
SIGPOLL
SIGPWR
SIGSYS
)
func (s Signal) Signal() {}
func (s Signal) String() string {
switch s {
case SIGNONE:
return "no signal"
case SIGHUP:
return "hangup"
case SIGINT:
return "interrupt"
case SIGQUIT:
return "quit"
case SIGILL:
return "illegal instruction"
case SIGTRAP:
return "trace/breakpoint trap"
case SIGABRT:
return "abort"
case SIGBUS:
return "bus error"
case SIGFPE:
return "floating point exception"
case SIGKILL:
return "killed"
case SIGUSR1:
return "user defined signal 1"
case SIGSEGV:
return "segmentation fault"
case SIGUSR2:
return "user defined signal 2"
case SIGPIPE:
return "broken pipe"
case SIGALRM:
return "alarm clock"
case SIGTERM:
return "terminated"
case SIGCHLD:
return "child exited"
case SIGCONT:
return "continued"
case SIGSTOP:
return "stopped (signal)"
case SIGTSTP:
return "stopped"
case SIGTTIN:
return "stopped (tty input)"
case SIGTTOU:
return "stopped (tty output)"
case SIGURG:
return "urgent I/O condition"
case SIGXCPU:
return "CPU time limit exceeded"
case SIGXFSZ:
return "file size limit exceeded"
case SIGVTARLM:
return "virtual timer expired"
case SIGPROF:
return "profiling timer expired"
case SIGWINCH:
return "window changed"
case SIGPOLL:
return "I/O possible"
case SIGPWR:
return "power failure"
case SIGSYS:
return "bad system call"
default:
return "signal " + strconv.Itoa(int(s))
}
}
const (
Stdin = 0
Stdout = 1
Stderr = 2
)
const (
O_RDONLY = 0
O_WRONLY = 1
O_RDWR = 2
O_CREAT = 0100
O_CREATE = O_CREAT
O_TRUNC = 01000
O_APPEND = 02000
O_EXCL = 0200
O_SYNC = 010000
O_DIRECTORY = 020000
O_NOFOLLOW = 0400
O_CLOEXEC = 0
)
const (
F_DUPFD = 0
F_GETFD = 1
F_SETFD = 2
F_GETFL = 3
F_SETFL = 4
F_GETOWN = 5
F_SETOWN = 6
F_GETLK = 7
F_SETLK = 8
F_SETLKW = 9
F_RGETLK = 10
F_RSETLK = 11
F_CNVT = 12
F_RSETLKW = 13
F_RDLCK = 1
F_WRLCK = 2
F_UNLCK = 3
F_UNLKSYS = 4
)
const (
S_IFMT = 0000370000
S_IFSHM_SYSV = 0000300000
S_IFSEMA = 0000270000
S_IFCOND = 0000260000
S_IFMUTEX = 0000250000
S_IFSHM = 0000240000
S_IFBOUNDSOCK = 0000230000
S_IFSOCKADDR = 0000220000
S_IFDSOCK = 0000210000
S_IFSOCK = 0000140000
S_IFLNK = 0000120000
S_IFREG = 0000100000
S_IFBLK = 0000060000
S_IFDIR = 0000040000
S_IFCHR = 0000020000
S_IFIFO = 0000010000
S_UNSUP = 0000370000
S_ISUID = 0004000
S_ISGID = 0002000
S_ISVTX = 0001000
S_IREAD = 0400
S_IWRITE = 0200
S_IEXEC = 0100
S_IRWXU = 0700
S_IRUSR = 0400
S_IWUSR = 0200
S_IXUSR = 0100
S_IRWXG = 070
S_IRGRP = 040
S_IWGRP = 020
S_IXGRP = 010
S_IRWXO = 07
S_IROTH = 04
S_IWOTH = 02
S_IXOTH = 01
)
type WaitStatus uint32
func (w WaitStatus) Exited() bool { return false }
func (w WaitStatus) ExitStatus() int { return 0 }
func (w WaitStatus) Signaled() bool { return false }
func (w WaitStatus) Signal() Signal { return 0 }
func (w WaitStatus) CoreDump() bool { return false }
func (w WaitStatus) Stopped() bool { return false }
func (w WaitStatus) Continued() bool { return false }
func (w WaitStatus) StopSignal() Signal { return 0 }
func (w WaitStatus) TrapCause() int { return 0 }
// Rusage is a placeholder to allow compilation of the [os/exec] package
// because we need Go programs to be portable across platforms. WASI does
// not have a mechanism to spawn processes so there is no reason for an
// application to take a dependency on this type.
type Rusage struct {
Utime Timeval
Stime Timeval
}
// ProcAttr is a placeholder to allow compilation of the [os/exec] package
// because we need Go programs to be portable across platforms. WASI does
// not have a mechanism to spawn processes so there is no reason for an
// application to take a dependency on this type.
type ProcAttr struct {
Dir string
Env []string
Files []uintptr
Sys *SysProcAttr
}
type SysProcAttr struct {
}
func Getuid() int {
return 1
}
func Getgid() int {
return 1
}
func Geteuid() int {
return 1
}
func Getegid() int {
return 1
}
func Getgroups() ([]int, error) {
return []int{1}, nil
}
func Getppid() int {
return 2
}
func Umask(mask int) int {
return 0
}
func setTimespec(sec, nsec int64) Timespec {
return Timespec{Sec: sec, Nsec: nsec}
}
func setTimeval(sec, usec int64) Timeval {
return Timeval{Sec: sec, Usec: usec}
}
type clockid = uint32
const (
clockRealtime clockid = iota
clockMonotonic
clockProcessCPUTimeID
clockThreadCPUTimeID
)
func SetNonblock(fd int, nonblocking bool) error {
panic("todo: syscall.SetNonblock")
}
const (
RLIMIT_NOFILE = iota
)
func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err error) {
panic("not implemented")
}
func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, err error) {
panic("not implemented")
}