Skip to content
Merged
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
16 changes: 9 additions & 7 deletions crates/test-programs/src/bin/p1_cli_hostcall_fuel.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::ptr;
use test_programs::preview1::BlockingMode;

fn main() {
big_poll();
big_string();
big_iovecs();
big_iovecs(BlockingMode::Blocking);
big_iovecs(BlockingMode::NonBlocking);
}

fn big_string() {
Expand All @@ -18,7 +20,7 @@ fn big_string() {
);
}

fn big_iovecs() {
fn big_iovecs(blocking_mode: BlockingMode) {
let mut iovs = Vec::new();
let mut ciovs = Vec::new();
for _ in 0..10_000 {
Expand All @@ -40,14 +42,14 @@ fn big_iovecs() {
wasip1::OFLAGS_CREAT,
wasip1::RIGHTS_FD_WRITE | wasip1::RIGHTS_FD_READ,
0,
0,
blocking_mode.fd_flags(),
)
.unwrap()
};

unsafe {
assert_eq!(wasip1::fd_write(fd, &ciovs), Err(wasip1::ERRNO_NOMEM));
assert_eq!(wasip1::fd_read(fd, &iovs), Err(wasip1::ERRNO_NOMEM));
assert_eq!(blocking_mode.write(fd, &ciovs), Err(wasip1::ERRNO_NOMEM));
assert_eq!(blocking_mode.read(fd, &iovs), Err(wasip1::ERRNO_NOMEM));
assert_eq!(wasip1::fd_pwrite(fd, &ciovs, 0), Err(wasip1::ERRNO_NOMEM));
assert_eq!(wasip1::fd_pread(fd, &iovs, 0), Err(wasip1::ERRNO_NOMEM));
}
Expand All @@ -63,8 +65,8 @@ fn big_iovecs() {
buf_len: 10_000,
});
unsafe {
assert_eq!(wasip1::fd_write(fd, &ciovs), Err(wasip1::ERRNO_NOMEM));
assert_eq!(wasip1::fd_read(fd, &iovs), Err(wasip1::ERRNO_NOMEM));
assert_eq!(blocking_mode.write(fd, &ciovs), Err(wasip1::ERRNO_NOMEM));
assert_eq!(blocking_mode.read(fd, &iovs), Err(wasip1::ERRNO_NOMEM));
assert_eq!(wasip1::fd_pwrite(fd, &ciovs, 0), Err(wasip1::ERRNO_NOMEM));
assert_eq!(wasip1::fd_pread(fd, &iovs, 0), Err(wasip1::ERRNO_NOMEM));
}
Expand Down
113 changes: 60 additions & 53 deletions crates/test-programs/src/bin/p1_fd_flags_set.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]

use std::{env, process};
use test_programs::preview1::open_scratch_directory;
use test_programs::preview1::{BlockingMode, open_scratch_directory};

unsafe fn test_fd_fdstat_set_flags(dir_fd: wasip1::Fd) {
unsafe fn test_fd_fdstat_set_flags(dir_fd: wasip1::Fd, blocking_mode: BlockingMode) {
const FILE_NAME: &str = "file";
let data = &[0u8; 100];

Expand All @@ -14,20 +14,21 @@ unsafe fn test_fd_fdstat_set_flags(dir_fd: wasip1::Fd) {
wasip1::OFLAGS_CREAT,
wasip1::RIGHTS_FD_READ | wasip1::RIGHTS_FD_WRITE,
0,
wasip1::FDFLAGS_APPEND,
blocking_mode.fd_flags() | wasip1::FDFLAGS_APPEND,
)
.expect("opening a file");

// Write some data and then verify the written data
assert_eq!(
wasip1::fd_write(
file_fd,
&[wasip1::Ciovec {
buf: data.as_ptr(),
buf_len: data.len(),
}],
)
.expect("writing to a file"),
blocking_mode
.write(
file_fd,
&[wasip1::Ciovec {
buf: data.as_ptr(),
buf_len: data.len(),
}],
)
.expect("writing to a file"),
data.len(),
"should write {} bytes",
data.len(),
Expand All @@ -38,14 +39,15 @@ unsafe fn test_fd_fdstat_set_flags(dir_fd: wasip1::Fd) {
let buffer = &mut [0u8; 100];

assert_eq!(
wasip1::fd_read(
file_fd,
&[wasip1::Iovec {
buf: buffer.as_mut_ptr(),
buf_len: buffer.len(),
}]
)
.expect("reading file"),
blocking_mode
.read(
file_fd,
&[wasip1::Iovec {
buf: buffer.as_mut_ptr(),
buf_len: buffer.len(),
}]
)
.expect("reading file"),
buffer.len(),
"should read {} bytes",
buffer.len()
Expand All @@ -59,14 +61,15 @@ unsafe fn test_fd_fdstat_set_flags(dir_fd: wasip1::Fd) {
wasip1::fd_seek(file_fd, 0, wasip1::WHENCE_SET).expect("seeking file");

assert_eq!(
wasip1::fd_write(
file_fd,
&[wasip1::Ciovec {
buf: data.as_ptr(),
buf_len: data.len(),
}],
)
.expect("writing to a file"),
blocking_mode
.write(
file_fd,
&[wasip1::Ciovec {
buf: data.as_ptr(),
buf_len: data.len(),
}],
)
.expect("writing to a file"),
data.len(),
"should write {} bytes",
data.len(),
Expand All @@ -75,37 +78,39 @@ unsafe fn test_fd_fdstat_set_flags(dir_fd: wasip1::Fd) {
wasip1::fd_seek(file_fd, 100, wasip1::WHENCE_SET).expect("seeking file");

assert_eq!(
wasip1::fd_read(
file_fd,
&[wasip1::Iovec {
buf: buffer.as_mut_ptr(),
buf_len: buffer.len(),
}]
)
.expect("reading file"),
blocking_mode
.read(
file_fd,
&[wasip1::Iovec {
buf: buffer.as_mut_ptr(),
buf_len: buffer.len(),
}]
)
.expect("reading file"),
buffer.len(),
"should read {} bytes",
buffer.len()
);

assert_eq!(&data[..], &buffer[..]);

wasip1::fd_fdstat_set_flags(file_fd, 0).expect("disabling flags");
wasip1::fd_fdstat_set_flags(file_fd, blocking_mode.fd_flags()).expect("disabling flags");

// Overwrite some existing data to ensure the append mode is now off
wasip1::fd_seek(file_fd, 0, wasip1::WHENCE_SET).expect("seeking file");

let data = &[2u8; 100];

assert_eq!(
wasip1::fd_write(
file_fd,
&[wasip1::Ciovec {
buf: data.as_ptr(),
buf_len: data.len(),
}],
)
.expect("writing to a file"),
blocking_mode
.write(
file_fd,
&[wasip1::Ciovec {
buf: data.as_ptr(),
buf_len: data.len(),
}],
)
.expect("writing to a file"),
data.len(),
"should write {} bytes",
data.len(),
Expand All @@ -114,14 +119,15 @@ unsafe fn test_fd_fdstat_set_flags(dir_fd: wasip1::Fd) {
wasip1::fd_seek(file_fd, 0, wasip1::WHENCE_SET).expect("seeking file");

assert_eq!(
wasip1::fd_read(
file_fd,
&[wasip1::Iovec {
buf: buffer.as_mut_ptr(),
buf_len: buffer.len(),
}]
)
.expect("reading file"),
blocking_mode
.read(
file_fd,
&[wasip1::Iovec {
buf: buffer.as_mut_ptr(),
buf_len: buffer.len(),
}]
)
.expect("reading file"),
buffer.len(),
"should read {} bytes",
buffer.len()
Expand Down Expand Up @@ -157,6 +163,7 @@ fn main() {
};

unsafe {
test_fd_fdstat_set_flags(dir_fd);
test_fd_fdstat_set_flags(dir_fd, BlockingMode::Blocking);
test_fd_fdstat_set_flags(dir_fd, BlockingMode::NonBlocking);
}
}
51 changes: 35 additions & 16 deletions crates/test-programs/src/bin/p1_file_read_write.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]

use std::{env, process};
use test_programs::preview1::open_scratch_directory;
use test_programs::preview1::{BlockingMode, open_scratch_directory};

unsafe fn test_file_read_write(dir_fd: wasip1::Fd) {
unsafe fn test_file_read_write(dir_fd: wasip1::Fd, blocking_mode: BlockingMode) {
// Create a file in the scratch directory.
let file_fd = wasip1::path_open(
dir_fd,
Expand All @@ -12,7 +12,7 @@ unsafe fn test_file_read_write(dir_fd: wasip1::Fd) {
wasip1::OFLAGS_CREAT,
wasip1::RIGHTS_FD_READ | wasip1::RIGHTS_FD_WRITE,
0,
0,
blocking_mode.fd_flags(),
)
.expect("opening a file");
assert!(
Expand All @@ -25,7 +25,9 @@ unsafe fn test_file_read_write(dir_fd: wasip1::Fd) {
buf: contents.as_ptr() as *const _,
buf_len: contents.len(),
};
let mut nwritten = wasip1::fd_write(file_fd, &[ciovec]).expect("writing bytes at offset 0");
let mut nwritten = blocking_mode
.write(file_fd, &[ciovec])
.expect("writing bytes at offset 0");
assert_eq!(nwritten, 4, "nwritten bytes check");

let contents = &mut [0u8; 4];
Expand All @@ -34,7 +36,9 @@ unsafe fn test_file_read_write(dir_fd: wasip1::Fd) {
buf_len: contents.len(),
};
wasip1::fd_seek(file_fd, 0, wasip1::WHENCE_SET).expect("seeking to offset 0");
let mut nread = wasip1::fd_read(file_fd, &[iovec]).expect("reading bytes at offset 0");
let mut nread = blocking_mode
.read(file_fd, &[iovec])
.expect("reading bytes at offset 0");
assert_eq!(nread, 4, "nread bytes check");
assert_eq!(contents, &[0u8, 1, 2, 3], "written bytes equal read bytes");

Expand All @@ -61,8 +65,9 @@ unsafe fn test_file_read_write(dir_fd: wasip1::Fd) {
buf_len: remaining,
});

nwritten =
wasip1::fd_write(file_fd, ciovecs.as_slice()).expect("writing bytes at offset 0");
nwritten = blocking_mode
.write(file_fd, ciovecs.as_slice())
.expect("writing bytes at offset 0");

offset += nwritten;
if offset == contents.len() {
Expand Down Expand Up @@ -91,7 +96,9 @@ unsafe fn test_file_read_write(dir_fd: wasip1::Fd) {
buf_len: 2,
},
];
nread = wasip1::fd_read(file_fd, iovecs).expect("reading bytes at offset 0");
nread = blocking_mode
.read(file_fd, iovecs)
.expect("reading bytes at offset 0");
if nread == 0 {
break;
}
Expand All @@ -107,7 +114,9 @@ unsafe fn test_file_read_write(dir_fd: wasip1::Fd) {
buf_len: contents.len(),
};
wasip1::fd_seek(file_fd, 2, wasip1::WHENCE_SET).expect("seeking to offset 2");
nread = wasip1::fd_read(file_fd, &[iovec]).expect("reading bytes at offset 2");
nread = blocking_mode
.read(file_fd, &[iovec])
.expect("reading bytes at offset 2");
assert_eq!(nread, 2, "nread bytes check");
assert_eq!(contents, &[2u8, 3, 0, 0], "file cursor was overwritten");

Expand All @@ -117,7 +126,9 @@ unsafe fn test_file_read_write(dir_fd: wasip1::Fd) {
buf_len: contents.len(),
};
wasip1::fd_seek(file_fd, 2, wasip1::WHENCE_SET).expect("seeking to offset 2");
nwritten = wasip1::fd_write(file_fd, &[ciovec]).expect("writing bytes at offset 2");
nwritten = blocking_mode
.write(file_fd, &[ciovec])
.expect("writing bytes at offset 2");
assert_eq!(nwritten, 2, "nwritten bytes check");

let contents = &mut [0u8; 4];
Expand All @@ -126,15 +137,17 @@ unsafe fn test_file_read_write(dir_fd: wasip1::Fd) {
buf_len: contents.len(),
};
wasip1::fd_seek(file_fd, 0, wasip1::WHENCE_SET).expect("seeking to offset 0");
nread = wasip1::fd_read(file_fd, &[iovec]).expect("reading bytes at offset 0");
nread = blocking_mode
.read(file_fd, &[iovec])
.expect("reading bytes at offset 0");
assert_eq!(nread, 4, "nread bytes check");
assert_eq!(contents, &[0u8, 1, 1, 0], "file cursor was overwritten");

wasip1::fd_close(file_fd).expect("closing a file");
wasip1::path_unlink_file(dir_fd, "file").expect("removing a file");
}

unsafe fn test_file_write_and_file_pos(dir_fd: wasip1::Fd) {
unsafe fn test_file_write_and_file_pos(dir_fd: wasip1::Fd, blocking_mode: BlockingMode) {
let path = "file2";
let file_fd = wasip1::path_open(
dir_fd,
Expand All @@ -160,7 +173,9 @@ unsafe fn test_file_write_and_file_pos(dir_fd: wasip1::Fd) {
buf_len: 0,
};
wasip1::fd_seek(file_fd, 2, wasip1::WHENCE_SET).expect("seeking to offset 2");
let n = wasip1::fd_write(file_fd, &[ciovec]).expect("writing bytes at offset 2");
let n = blocking_mode
.write(file_fd, &[ciovec])
.expect("writing bytes at offset 2");
assert_eq!(n, 0);

assert_eq!(wasip1::fd_tell(file_fd).unwrap(), 2);
Expand All @@ -174,7 +189,9 @@ unsafe fn test_file_write_and_file_pos(dir_fd: wasip1::Fd) {
buf_len: buf.len(),
};
wasip1::fd_seek(file_fd, 50, wasip1::WHENCE_SET).expect("seeking to offset 50");
let n = wasip1::fd_write(file_fd, &[ciovec]).expect("writing bytes at offset 50");
let n = blocking_mode
.write(file_fd, &[ciovec])
.expect("writing bytes at offset 50");
assert_eq!(n, 1);

assert_eq!(wasip1::fd_tell(file_fd).unwrap(), 51);
Expand Down Expand Up @@ -206,7 +223,9 @@ fn main() {

// Run the tests.
unsafe {
test_file_read_write(dir_fd);
test_file_write_and_file_pos(dir_fd);
test_file_read_write(dir_fd, BlockingMode::Blocking);
test_file_read_write(dir_fd, BlockingMode::NonBlocking);
test_file_write_and_file_pos(dir_fd, BlockingMode::Blocking);
test_file_write_and_file_pos(dir_fd, BlockingMode::NonBlocking);
}
}
Loading
Loading