Skip to content

Commit

Permalink
[wasi] Fix alignment of size pointer for some WASI calls
Browse files Browse the repository at this point in the history
  • Loading branch information
titzer committed Nov 11, 2024
1 parent 3d01119 commit 02e37a9
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions rt/wasm-wasi1/System.v3
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ component System {
def fileLoad(path: string) -> Array<byte> {
var fd = fileOpen(path, true);
if (fd < 0) return null;
var bufptr = aligned_sizeptr();
var r = wasi_snapshot_preview1.fd_seek(fd, 0, wasi_whence.END.tag, bufptr);
var sizeptr = aligned_sizeptr();
var r = wasi_snapshot_preview1.fd_seek(fd, 0, wasi_whence.END.tag, sizeptr);
if (r < 0) {
wasi_snapshot_preview1.fd_close(fd);
return null;
}
var len = int.!(bufptr.load<u64>());
r = wasi_snapshot_preview1.fd_seek(fd, 0, wasi_whence.SET.tag, bufptr);
var len = int.!(sizeptr.load<u64>());
r = wasi_snapshot_preview1.fd_seek(fd, 0, wasi_whence.SET.tag, sizeptr);
if (r < 0) {
wasi_snapshot_preview1.fd_close(fd);
return null;
Expand Down Expand Up @@ -130,8 +130,9 @@ component System {
return fs_read(fd, Pointer.atContents(data), data.length);
}
private def wasiNs() -> u64 {
wasi_snapshot_preview1.clock_time_get(0, 10000, Pointer.atContents(iobuf));
var tv_nsecs = Pointer.atContents(iobuf).load<u64>();
var sizebuf = aligned_sizeptr(); // TODO: use a proper layout here
wasi_snapshot_preview1.clock_time_get(0, 10000, sizebuf);
var tv_nsecs = sizebuf.load<u64>();
return tv_nsecs;
}
}
Expand All @@ -144,7 +145,7 @@ def MAX_PREOPEN = 10;
// @thread-local @lazy buffer for write integers and chars to System.out
def numbuf = Array<byte>.new(16);
def iobuf = Array<byte>.new(16);
def retbuf = Array<byte>.new(16);
def retbuf = Array<byte>.new(32);

def BCE = "BoundsCheckException";
def EMPTY = "";
Expand All @@ -158,15 +159,15 @@ def fs_write(fd: int, data: Pointer, len: int) -> int {
var p = Pointer.atContents(iobuf);
(p + 0).store<int>(data - Pointer.NULL);
(p + 4).store<int>(len);
var sizeptr = Pointer.atContents(retbuf);
var sizeptr = aligned_sizeptr();
var r = wasi_snapshot_preview1.fd_write(fd, p, 1, sizeptr);
return if(r == 0, sizeptr.load<int>());
}
def fs_read(fd: int, data: Pointer, len: int) -> int {
var p = Pointer.atContents(iobuf);
(p + 0).store<int>(data - Pointer.NULL);
(p + 4).store<int>(len);
var sizeptr = Pointer.atContents(retbuf);
var sizeptr = aligned_sizeptr();
var r = wasi_snapshot_preview1.fd_read(fd, p, 1, sizeptr);
return if(r == 0, sizeptr.load<int>());
}
Expand Down

0 comments on commit 02e37a9

Please sign in to comment.