Use rsix to make system calls in Wasmtime. (#3355)

* Use rsix to make system calls in Wasmtime.

`rsix` is a system call wrapper crate that we use in `wasi-common`,
which can provide the following advantages in the rest of Wasmtime:

 - It eliminates some `unsafe` blocks in Wasmtime's code. There's
   still an `unsafe` block in the library, but this way, the `unsafe`
   is factored out and clearly scoped.

 - And, it makes error handling more consistent, factoring out code for
   checking return values and `io::Error::last_os_error()`, and code that
   does `errno::set_errno(0)`.

This doesn't cover *all* system calls; `rsix` doesn't implement
signal-handling APIs, and this doesn't cover calls made through `std` or
crates like `userfaultfd`, `rand`, and `region`.
This commit is contained in:
Dan Gohman
2021-09-17 15:28:56 -07:00
committed by GitHub
parent 6a98fe2104
commit 47490b4383
25 changed files with 174 additions and 234 deletions

View File

@@ -4,6 +4,7 @@
))]
mod tests {
use anyhow::Result;
use rsix::io::{mprotect, MprotectFlags};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use wasmtime::unix::StoreExt;
@@ -59,7 +60,7 @@ mod tests {
// So we can later trigger SIGSEGV by performing a read
unsafe {
libc::mprotect(base as *mut libc::c_void, length, libc::PROT_NONE);
mprotect(base as *mut std::ffi::c_void, length, MprotectFlags::NONE).unwrap();
}
println!("memory: base={:?}, length={}", base, length);
@@ -81,11 +82,12 @@ mod tests {
let result = (si_addr as u64) < (base as u64) + (length as u64);
// Remove protections so the execution may resume
unsafe {
libc::mprotect(
mprotect(
base as *mut libc::c_void,
length,
libc::PROT_READ | libc::PROT_WRITE,
);
MprotectFlags::READ | MprotectFlags::WRITE,
)
.unwrap();
}
println!("signal handled: {}", result);
result
@@ -213,11 +215,12 @@ mod tests {
let instance1_handler_triggered = instance1_handler_triggered.clone();
move |_signum, _siginfo, _context| {
// Remove protections so the execution may resume
libc::mprotect(
mprotect(
base1 as *mut libc::c_void,
length1,
libc::PROT_READ | libc::PROT_WRITE,
);
MprotectFlags::READ | MprotectFlags::WRITE,
)
.unwrap();
instance1_handler_triggered.store(true, Ordering::SeqCst);
println!(
"Hello from instance1 signal handler! {}",
@@ -258,11 +261,12 @@ mod tests {
let instance2_handler_triggered = instance2_handler_triggered.clone();
move |_signum, _siginfo, _context| {
// Remove protections so the execution may resume
libc::mprotect(
mprotect(
base2 as *mut libc::c_void,
length2,
libc::PROT_READ | libc::PROT_WRITE,
);
MprotectFlags::READ | MprotectFlags::WRITE,
)
.unwrap();
instance2_handler_triggered.store(true, Ordering::SeqCst);
println!(
"Hello from instance2 signal handler! {}",