-
Notifications
You must be signed in to change notification settings - Fork 822
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
608: Implement more wasi syscalls r=MarkMcCaskey a=MarkMcCaskey Co-authored-by: Mark McCaskey <mark@wasmer.io> Co-authored-by: Mark McCaskey <markmccaskey@users.noreply.github.com>
- Loading branch information
Showing
38 changed files
with
1,332 additions
and
374 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#[test] | ||
fn test_fd_allocate() { | ||
assert_wasi_output!( | ||
"../../wasitests/fd_allocate.wasm", | ||
"fd_allocate", | ||
vec![], | ||
vec![( | ||
".".to_string(), | ||
::std::path::PathBuf::from("wasitests/test_fs/temp") | ||
),], | ||
vec![], | ||
"../../wasitests/fd_allocate.out" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#[test] | ||
fn test_fd_pread() { | ||
assert_wasi_output!( | ||
"../../wasitests/fd_pread.wasm", | ||
"fd_pread", | ||
vec![], | ||
vec![( | ||
".".to_string(), | ||
::std::path::PathBuf::from("wasitests/test_fs/hamlet") | ||
),], | ||
vec![], | ||
"../../wasitests/fd_pread.out" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#[test] | ||
fn test_fd_sync() { | ||
assert_wasi_output!( | ||
"../../wasitests/fd_sync.wasm", | ||
"fd_sync", | ||
vec![], | ||
vec![( | ||
".".to_string(), | ||
::std::path::PathBuf::from("wasitests/test_fs/temp") | ||
),], | ||
vec![], | ||
"../../wasitests/fd_sync.out" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#[test] | ||
fn test_path_link() { | ||
assert_wasi_output!( | ||
"../../wasitests/path_link.wasm", | ||
"path_link", | ||
vec![], | ||
vec![ | ||
( | ||
"act5".to_string(), | ||
::std::path::PathBuf::from("wasitests/test_fs/hamlet/act5") | ||
), | ||
( | ||
"temp".to_string(), | ||
::std::path::PathBuf::from("wasitests/test_fs/temp") | ||
), | ||
], | ||
vec![], | ||
"../../wasitests/path_link.out" | ||
); | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
171 | ||
1405 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Args: | ||
// mapdir: .:wasitests/test_fs/temp | ||
|
||
use std::fs; | ||
#[cfg(target_os = "wasi")] | ||
use std::os::wasi::prelude::AsRawFd; | ||
use std::path::PathBuf; | ||
|
||
#[cfg(target_os = "wasi")] | ||
#[link(wasm_import_module = "wasi_unstable")] | ||
extern "C" { | ||
fn fd_allocate(fd: u32, offset: u64, length: u64) -> u16; | ||
} | ||
|
||
#[cfg(target_os = "wasi")] | ||
fn allocate(fd: u32, offset: u64, length: u64) -> u16 { | ||
unsafe { fd_allocate(fd, offset, length) } | ||
} | ||
|
||
fn main() { | ||
#[cfg(target_os = "wasi")] | ||
let mut base = PathBuf::from("."); | ||
#[cfg(target_os = "wasi")] | ||
{ | ||
base.push("fd_allocate_file.txt"); | ||
let mut file = fs::OpenOptions::new() | ||
.create(true) | ||
.write(true) | ||
.truncate(true) | ||
.open(&base) | ||
.expect("Could not create file"); | ||
let mut buffer = [0u8; 64]; | ||
|
||
{ | ||
use std::io::Write; | ||
// example text from https://www.un.org/en/universal-declaration-human-rights/ | ||
file.write_all(b"All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.\n").unwrap(); | ||
let raw_fd = file.as_raw_fd(); | ||
file.flush(); | ||
let len = file.metadata().unwrap().len(); | ||
println!("{}", len); | ||
assert_eq!(len, 171); | ||
allocate(raw_fd, len, 1234); | ||
let len = file.metadata().unwrap().len(); | ||
println!("{}", len); | ||
assert_eq!(len, 1234 + 171); | ||
} | ||
} | ||
#[cfg(target_os = "wasi")] | ||
std::fs::remove_file(&base).unwrap(); | ||
|
||
#[cfg(not(target_os = "wasi"))] | ||
{ | ||
// eh, just print the output directly | ||
println!("171"); | ||
println!("1405"); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
POLONIUS | ||
|
||
He will come straight. Look you lay home to him: | ||
|
||
POLONIUS | ||
|
||
He will come straight. Look you lay home to him: | ||
|
||
Read the same data? true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Args: | ||
// mapdir: .:wasitests/test_fs/hamlet | ||
|
||
use std::fs; | ||
#[cfg(target_os = "wasi")] | ||
use std::os::wasi::prelude::AsRawFd; | ||
use std::path::PathBuf; | ||
|
||
#[cfg(target_os = "wasi")] | ||
#[repr(C)] | ||
struct WasiIovec { | ||
pub buf: u32, | ||
pub buf_len: u32, | ||
} | ||
|
||
#[cfg(target_os = "wasi")] | ||
#[link(wasm_import_module = "wasi_unstable")] | ||
extern "C" { | ||
fn fd_pread(fd: u32, iovs: u32, iovs_len: u32, offset: u64, nread: u32) -> u16; | ||
} | ||
|
||
#[cfg(target_os = "wasi")] | ||
fn pread(fd: u32, iovs: &[&mut [u8]], offset: u64) -> u32 { | ||
let mut nread = 0; | ||
let mut processed_iovs = vec![]; | ||
|
||
for iov in iovs { | ||
processed_iovs.push(WasiIovec { | ||
buf: iov.as_ptr() as usize as u32, | ||
buf_len: iov.len() as u32, | ||
}) | ||
} | ||
|
||
unsafe { | ||
fd_pread( | ||
fd, | ||
processed_iovs.as_ptr() as usize as u32, | ||
processed_iovs.len() as u32, | ||
offset, | ||
&mut nread as *mut u32 as usize as u32, | ||
); | ||
} | ||
nread | ||
} | ||
|
||
fn main() { | ||
#[cfg(not(target_os = "wasi"))] | ||
let mut base = PathBuf::from("wasitests/test_fs/hamlet"); | ||
#[cfg(target_os = "wasi")] | ||
let mut base = PathBuf::from("."); | ||
|
||
base.push("act3/scene4.txt"); | ||
let mut file = fs::File::open(&base).expect("Could not open file"); | ||
let mut buffer = [0u8; 64]; | ||
|
||
#[cfg(target_os = "wasi")] | ||
{ | ||
let raw_fd = file.as_raw_fd(); | ||
assert_eq!(pread(raw_fd, &[&mut buffer], 75), 64); | ||
let str_val = std::str::from_utf8(&buffer[..]).unwrap().to_string(); | ||
println!("{}", &str_val); | ||
for i in 0..buffer.len() { | ||
buffer[i] = 0; | ||
} | ||
assert_eq!(pread(raw_fd, &[&mut buffer], 75), 64); | ||
let str_val2 = std::str::from_utf8(&buffer[..]).unwrap().to_string(); | ||
println!("{}", &str_val2); | ||
|
||
println!("Read the same data? {}", str_val == str_val2); | ||
} | ||
|
||
#[cfg(not(target_os = "wasi"))] | ||
{ | ||
// eh, just print the output directly | ||
print!( | ||
" POLONIUS | ||
He will come straight. Look you lay home to him: | ||
POLONIUS | ||
He will come straight. Look you lay home to him: | ||
Read the same data? true" | ||
); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
170 | ||
1404 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Args: | ||
// mapdir: .:wasitests/test_fs/temp | ||
|
||
use std::fs; | ||
use std::path::PathBuf; | ||
|
||
#[cfg(target_os = "wasi")] | ||
#[link(wasm_import_module = "wasi_unstable")] | ||
extern "C" { | ||
fn fd_sync(fd: u32) -> u16; | ||
} | ||
|
||
#[cfg(target_os = "wasi")] | ||
fn sync(fd: u32) -> u16 { | ||
unsafe { fd_sync(fd) } | ||
} | ||
|
||
fn main() { | ||
#[cfg(target_os = "wasi")] | ||
let mut base = PathBuf::from("."); | ||
#[cfg(target_os = "wasi")] | ||
{ | ||
base.push("fd_sync_file.txt"); | ||
let mut file = fs::OpenOptions::new() | ||
.create(true) | ||
.write(true) | ||
.truncate(true) | ||
.open(&base) | ||
.expect("Could not create file"); | ||
let mut buffer = [0u8; 64]; | ||
|
||
{ | ||
use std::io::Write; | ||
// example text from https://www.un.org/en/universal-declaration-human-rights/ | ||
file.write_all(b"All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.").unwrap(); | ||
file.sync_all(); | ||
let len = file.metadata().unwrap().len(); | ||
println!("{}", len); | ||
assert_eq!(len, 170); | ||
file.set_len(170 + 1234); | ||
file.sync_all(); | ||
let len = file.metadata().unwrap().len(); | ||
println!("{}", len); | ||
assert_eq!(len, 1234 + 170); | ||
} | ||
} | ||
#[cfg(target_os = "wasi")] | ||
std::fs::remove_file(&base).unwrap(); | ||
|
||
#[cfg(not(target_os = "wasi"))] | ||
{ | ||
// eh, just print the output directly | ||
println!("170"); | ||
println!("1404"); | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
ACT V | ||
SCENE I. A churchyard. | ||
|
||
Enter two Clowns, with spades, | ||
ACT V | ||
SCENE I. A churchyard. | ||
|
||
Enter two Clowns, with spades, | ||
Path still exists |
Oops, something went wrong.