From 61958cdcf665df5a28cfc4958dd1408d3b6f6b26 Mon Sep 17 00:00:00 2001 From: mohanson Date: Sat, 16 Aug 2025 18:21:49 +0800 Subject: [PATCH] Use linux style pwrite --- example/wasi_httpbin.py | 2 ++ pywasm/wasi.py | 17 +++++++++++++++-- test/wasi.py | 9 ++++----- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/example/wasi_httpbin.py b/example/wasi_httpbin.py index 7821396..dd69010 100644 --- a/example/wasi_httpbin.py +++ b/example/wasi_httpbin.py @@ -1,3 +1,4 @@ +import os import pywasm import socket @@ -16,6 +17,7 @@ wasi = pywasm.wasi.Preview1(['wasi_httpbin.wasm'], {}, {}) wasi.fd.append(pywasm.wasi.Preview1.File( host_fd=client_socket.fileno(), + host_flag=os.O_RDWR, host_name=client_socket.getsockname(), host_status=pywasm.wasi.Preview1.FILE_STATUS_OPENED, pipe=None, diff --git a/pywasm/wasi.py b/pywasm/wasi.py index 2bfd289..d4243e3 100644 --- a/pywasm/wasi.py +++ b/pywasm/wasi.py @@ -1,4 +1,5 @@ import dataclasses +import fcntl import os import platform import pywasm.core @@ -395,6 +396,7 @@ class Preview1: @dataclasses.dataclass class File: host_fd: int + host_flag: int host_name: str host_status: int pipe: typing.Optional[typing.BinaryIO] @@ -414,6 +416,7 @@ def __init__(self, args: typing.List[str], dirs: typing.Dict[str, str], envs: ty self.fd: typing.List[Preview1.File] = [] self.fd.append(self.File( host_fd=sys.stdin.fileno(), + host_flag=0, host_name=sys.stdin.name, host_status=self.FILE_STATUS_OPENED, pipe=None, @@ -427,6 +430,7 @@ def __init__(self, args: typing.List[str], dirs: typing.Dict[str, str], envs: ty )) self.fd.append(self.File( host_fd=sys.stdout.fileno(), + host_flag=0, host_name=sys.stdout.name, host_status=self.FILE_STATUS_OPENED, pipe=None, @@ -440,6 +444,7 @@ def __init__(self, args: typing.List[str], dirs: typing.Dict[str, str], envs: ty )) self.fd.append(self.File( host_fd=sys.stderr.fileno(), + host_flag=0, host_name=sys.stderr.name, host_status=self.FILE_STATUS_OPENED, pipe=None, @@ -456,6 +461,7 @@ def __init__(self, args: typing.List[str], dirs: typing.Dict[str, str], envs: ty v = os.path.abspath(os.path.normpath(v)) self.fd.append(self.File( host_fd=os.open(v, os.O_RDONLY | os.O_DIRECTORY), + host_flag=os.O_RDONLY | os.O_DIRECTORY, host_name=v, host_status=self.FILE_STATUS_OPENED, pipe=None, @@ -652,12 +658,17 @@ def fd_fdstat_set_flags(self, _: pywasm.core.Machine, args: typing.List[int]) -> if file.wasm_flag ^ args[1] | fyes != fyes: # Only support changing the NONBLOCK or APPEND flags. return [self.ERRNO_INVAL] + file.host_flag &= ~os.O_APPEND + file.host_flag &= ~os.O_NONBLOCK file.wasm_flag &= ~self.FDFLAGS_APPEND file.wasm_flag &= ~self.FDFLAGS_NONBLOCK if args[1] & self.FDFLAGS_APPEND: + file.host_flag |= os.O_APPEND file.wasm_flag |= self.FDFLAGS_APPEND if args[1] & self.FDFLAGS_NONBLOCK: + file.host_flag |= os.O_NONBLOCK file.wasm_flag |= self.FDFLAGS_NONBLOCK + fcntl.fcntl(file.host_fd, fcntl.F_SETFL, file.host_flag) return [self.ERRNO_SUCCESS] def fd_fdstat_set_rights(self, _: pywasm.core.Machine, args: typing.List[int]) -> typing.List[int]: @@ -871,6 +882,7 @@ def fd_renumber(self, _: pywasm.core.Machine, args: typing.List[int]) -> typing. if dest.wasm_type != self.FILETYPE_CHARACTER_DEVICE: os.close(dest.host_fd) dest.host_fd = stem.host_fd + dest.host_flag = stem.host_flag dest.host_name = stem.host_name dest.host_status = stem.host_status dest.pipe = stem.pipe @@ -951,8 +963,6 @@ def fd_write(self, m: pywasm.core.Machine, args: typing.List[int]) -> typing.Lis elem = mems.get(elem_ptr, elem_len) iovs_ptr += 8 data.extend(elem) - if file.wasm_flag & self.FDFLAGS_APPEND: - os.lseek(file.host_fd, 0, os.SEEK_END) size = [ lambda: os.write(file.host_fd, data), lambda: file.pipe.write(data), @@ -1171,6 +1181,8 @@ def path_open(self, m: pywasm.core.Machine, args: typing.List[int]) -> typing.Li flag |= os.O_WRONLY if args[5] & self.RIGHTS_FD_READ + self.RIGHTS_FD_WRITE == self.RIGHTS_FD_READ + self.RIGHTS_FD_WRITE: flag |= os.O_RDWR + if args[7] & self.FDFLAGS_APPEND: + flag |= os.O_APPEND wasm_fd = len(self.fd) try: host_fd = os.open(name, flag, 0o644, dir_fd=self.fd[args[0]].host_fd) @@ -1217,6 +1229,7 @@ def path_open(self, m: pywasm.core.Machine, args: typing.List[int]) -> typing.Li rights_root &= ~void self.fd.append(Preview1.File( host_fd=host_fd, + host_flag=flag, host_name=host_name, host_status=self.FILE_STATUS_OPENED, pipe=None, diff --git a/test/wasi.py b/test/wasi.py index 64734a0..7df3162 100644 --- a/test/wasi.py +++ b/test/wasi.py @@ -30,11 +30,10 @@ def cd(dst: str) -> typing.Generator[None, typing.Any, None]: if os.path.exists(e) and os.path.isdir(e): shutil.rmtree(e) case = [] -case.extend(sorted(glob.glob('res/wasi-testsuite/tests/assemblyscript/testsuite/*.wasm'))) -case.extend(sorted(glob.glob('res/wasi-testsuite/tests/c/testsuite/*.wasm'))) -case.extend(sorted(glob.glob('res/wasi-testsuite/tests/rust/testsuite/*.wasm'))) -skip = [ -] +case.extend(sorted(glob.glob('res/wasi-testsuite/tests/assemblyscript/testsuite/wasm32-wasip1/*.wasm'))) +case.extend(sorted(glob.glob('res/wasi-testsuite/tests/c/testsuite/wasm32-wasip1/*.wasm'))) +case.extend(sorted(glob.glob('res/wasi-testsuite/tests/rust/testsuite/wasm32-wasip1/*.wasm'))) +skip = [] for wasm_path in case: if wasm_path in skip: continue