Rewrite for recursive safety

This commit rewrites the runtime crate to provide safety in the face
of recursive calls to the guest. The basic principle is that
`GuestMemory` is now a trait which dynamically returns the
pointer/length pair. This also has an implicit contract (hence the
`unsafe` trait) that the pointer/length pair point to a valid list of
bytes in host memory "until something is reentrant".

After this changes the various suite of `Guest*` types were rewritten.
`GuestRef` and `GuestRefMut` were both removed since they cannot safely
exist. The `GuestPtrMut` type was removed for simplicity, and the final
`GuestPtr` type subsumes `GuestString` and `GuestArray`. This means
that there's only one guest pointer type, `GuestPtr<'a, T>`, where `'a`
is the borrow into host memory, basically borrowing the `GuestMemory`
trait object itself.

Some core utilities are exposed on `GuestPtr`, but they're all 100%
safe. Unsafety is now entirely contained within a few small locations:

* Implementations of the `GuestType` for primitive types (e.g. `i8`,
  `u8`, etc) use `unsafe` to read/write memory. The `unsafe` trait of
  `GuestMemory` though should prove that they're safe.

* `GuestPtr<'_, str>` has a method which validates utf-8 contents, and
  this requires `unsafe` internally to read all the bytes. This is
  guaranteed to be safe however given the contract of `GuestMemory`.

And that's it! Everything else is a bunch of safe combinators all built
up on the various utilities provided by `GuestPtr`. The general idioms
are roughly the same as before, with various tweaks here and there. A
summary of expected idioms are:

* For small values you'd `.read()` or `.write()` very quickly. You'd
  pass around the type itself.

* For strings, you'd pass `GuestPtr<'_, str>` down to the point where
  it's actually consumed. At that moment you'd either decide to copy it
  out (a safe operation) or you'd get a raw view to the string (an
  unsafe operation) and assert that you won't call back into wasm while
  you're holding that pointer.

* Arrays are similar to strings, passing around `GuestPtr<'_, [T]>`.
  Arrays also have a `iter()` method which yields an iterator of
  `GuestPtr<'_, T>` for convenience.

Overall there's still a lot of missing documentation on the runtime
crate specifically around the safety of the `GuestMemory` trait as well
as how the utilities/methods are expected to be used. Additionally
there's utilities which aren't currently implemented which would be easy
to implement. For example there's no method to copy out a string or a
slice, although that would be pretty easy to add.

In any case I'm curious to get feedback on this approach and see what
y'all think!
This commit is contained in:
Alex Crichton
2020-03-04 10:21:34 -08:00
parent 3764204250
commit ca9f33b6d9
28 changed files with 751 additions and 2013 deletions

View File

@@ -1,4 +1,4 @@
use wiggle_runtime::{GuestError, GuestErrorType, GuestPtr, GuestPtrMut, GuestString};
use wiggle_runtime::{GuestError, GuestErrorType, GuestPtr};
use wiggle_test::WasiCtx;
wiggle::from_witx!({
@@ -25,8 +25,8 @@ impl GuestErrorType for types::Errno {
impl crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
fn args_get(
&self,
_argv: GuestPtrMut<GuestPtrMut<u8>>,
_argv_buf: GuestPtrMut<u8>,
_argv: GuestPtr<GuestPtr<u8>>,
_argv_buf: GuestPtr<u8>,
) -> Result<()> {
unimplemented!("args_get")
}
@@ -37,8 +37,8 @@ impl crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
fn environ_get(
&self,
_environ: GuestPtrMut<GuestPtrMut<u8>>,
_environ_buf: GuestPtrMut<u8>,
_environ: GuestPtr<GuestPtr<u8>>,
_environ_buf: GuestPtr<u8>,
) -> Result<()> {
unimplemented!("environ_get")
}
@@ -137,7 +137,7 @@ impl crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
fn fd_prestat_dir_name(
&self,
_fd: types::Fd,
_path: GuestPtrMut<u8>,
_path: GuestPtr<u8>,
_path_len: types::Size,
) -> Result<()> {
unimplemented!("fd_prestat_dir_name")
@@ -159,7 +159,7 @@ impl crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
fn fd_readdir(
&self,
_fd: types::Fd,
_buf: GuestPtrMut<u8>,
_buf: GuestPtr<u8>,
_buf_len: types::Size,
_cookie: types::Dircookie,
) -> Result<types::Size> {
@@ -191,7 +191,7 @@ impl crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
unimplemented!("fd_write")
}
fn path_create_directory(&self, _fd: types::Fd, _path: &GuestString<'_>) -> Result<()> {
fn path_create_directory(&self, _fd: types::Fd, _path: &GuestPtr<'_, str>) -> Result<()> {
unimplemented!("path_create_directory")
}
@@ -199,7 +199,7 @@ impl crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
&self,
_fd: types::Fd,
_flags: types::Lookupflags,
_path: &GuestString<'_>,
_path: &GuestPtr<'_, str>,
) -> Result<types::Filestat> {
unimplemented!("path_filestat_get")
}
@@ -208,7 +208,7 @@ impl crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
&self,
_fd: types::Fd,
_flags: types::Lookupflags,
_path: &GuestString<'_>,
_path: &GuestPtr<'_, str>,
_atim: types::Timestamp,
_mtim: types::Timestamp,
_fst_flags: types::Fstflags,
@@ -220,9 +220,9 @@ impl crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
&self,
_old_fd: types::Fd,
_old_flags: types::Lookupflags,
_old_path: &GuestString<'_>,
_old_path: &GuestPtr<'_, str>,
_new_fd: types::Fd,
_new_path: &GuestString<'_>,
_new_path: &GuestPtr<'_, str>,
) -> Result<()> {
unimplemented!("path_link")
}
@@ -231,7 +231,7 @@ impl crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
&self,
_fd: types::Fd,
_dirflags: types::Lookupflags,
_path: &GuestString<'_>,
_path: &GuestPtr<'_, str>,
_oflags: types::Oflags,
_fs_rights_base: types::Rights,
_fs_rights_inherting: types::Rights,
@@ -243,44 +243,44 @@ impl crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
fn path_readlink(
&self,
_fd: types::Fd,
_path: &GuestString<'_>,
_buf: GuestPtrMut<u8>,
_path: &GuestPtr<'_, str>,
_buf: GuestPtr<u8>,
_buf_len: types::Size,
) -> Result<types::Size> {
unimplemented!("path_readlink")
}
fn path_remove_directory(&self, _fd: types::Fd, _path: &GuestString<'_>) -> Result<()> {
fn path_remove_directory(&self, _fd: types::Fd, _path: &GuestPtr<'_, str>) -> Result<()> {
unimplemented!("path_remove_directory")
}
fn path_rename(
&self,
_fd: types::Fd,
_old_path: &GuestString<'_>,
_old_path: &GuestPtr<'_, str>,
_new_fd: types::Fd,
_new_path: &GuestString<'_>,
_new_path: &GuestPtr<'_, str>,
) -> Result<()> {
unimplemented!("path_rename")
}
fn path_symlink(
&self,
_old_path: &GuestString<'_>,
_old_path: &GuestPtr<'_, str>,
_fd: types::Fd,
_new_path: &GuestString<'_>,
_new_path: &GuestPtr<'_, str>,
) -> Result<()> {
unimplemented!("path_symlink")
}
fn path_unlink_file(&self, _fd: types::Fd, _path: &GuestString<'_>) -> Result<()> {
fn path_unlink_file(&self, _fd: types::Fd, _path: &GuestPtr<'_, str>) -> Result<()> {
unimplemented!("path_unlink_file")
}
fn poll_oneoff(
&self,
_in_: GuestPtr<types::Subscription>,
_out: GuestPtrMut<types::Event>,
_out: GuestPtr<types::Event>,
_nsubscriptions: types::Size,
) -> Result<types::Size> {
unimplemented!("poll_oneoff")
@@ -298,7 +298,7 @@ impl crate::wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
unimplemented!("sched_yield")
}
fn random_get(&self, _buf: GuestPtrMut<u8>, _buf_len: types::Size) -> Result<()> {
fn random_get(&self, _buf: GuestPtr<u8>, _buf_len: types::Size) -> Result<()> {
unimplemented!("random_get")
}