Browse Source

dont try to make a virtfs today. instead, delete virtfs example.

pull/2629/head
Pat Hickey 4 years ago
parent
commit
d8f3a62b32
  1. 13
      Cargo.lock
  2. 2
      Cargo.toml
  3. 18
      crates/wasi-common/virtfs/Cargo.toml
  4. 82
      crates/wasi-common/virtfs/src/dir.rs
  5. 73
      crates/wasi-common/virtfs/src/file.rs
  6. 2
      crates/wasi-common/virtfs/src/lib.rs
  7. 21
      docs/examples-rust-wasi-fs.md
  8. 122
      examples/wasi-fs/main.c
  9. 46
      examples/wasi-fs/main.rs
  10. 1
      examples/wasi-fs/test.txt
  11. 10
      examples/wasi-fs/wasm/Cargo.toml
  12. 4
      examples/wasi-fs/wasm/wasi-fs.rs

13
Cargo.lock

@ -1121,10 +1121,6 @@ dependencies = [
name = "example-fib-debug-wasm"
version = "0.0.0"
[[package]]
name = "example-wasi-fs-wasm"
version = "0.0.0"
[[package]]
name = "example-wasi-wasm"
version = "0.0.0"
@ -3128,15 +3124,6 @@ dependencies = [
"zeroize",
]
[[package]]
name = "wasi-virtfs"
version = "0.22.0"
dependencies = [
"anyhow",
"cap-std",
"wasi-common",
]
[[package]]
name = "wasm-encoder"
version = "0.4.0"

2
Cargo.toml

@ -77,10 +77,8 @@ members = [
"crates/wiggle/wasmtime",
"crates/wasi-common",
"crates/wasi-common/cap-std-sync",
"crates/wasi-common/virtfs",
"examples/fib-debug/wasm",
"examples/wasi/wasm",
"examples/wasi-fs/wasm",
"fuzz",
]

18
crates/wasi-common/virtfs/Cargo.toml

@ -1,18 +0,0 @@
[package]
name = "wasi-virtfs"
version = "0.22.0"
authors = ["The Wasmtime Project Developers"]
description = "WASI implementation in Rust"
license = "Apache-2.0 WITH LLVM-exception"
categories = ["wasm"]
keywords = ["webassembly", "wasm"]
repository = "https://github.com/bytecodealliance/wasmtime"
readme = "README.md"
edition = "2018"
include = ["src/**/*", "LICENSE" ]
publish = false
[dependencies]
wasi-common = { path = "../", version = "0.22.0" }
anyhow = "1.0"
cap-std = "0.12"

82
crates/wasi-common/virtfs/src/dir.rs

@ -1,82 +0,0 @@
use crate::file::File;
use std::any::Any;
use std::path::{Path, PathBuf};
use wasi_common::{
dir::{ReaddirCursor, ReaddirEntity, WasiDir},
file::{FdFlags, FileCaps, FileType, Filestat, OFlags, WasiFile},
Error, ErrorExt,
};
pub struct Dir;
impl Dir {}
impl WasiDir for Dir {
fn as_any(&self) -> &dyn Any {
self
}
fn open_file(
&self,
symlink_follow: bool,
path: &str,
oflags: OFlags,
caps: FileCaps,
fdflags: FdFlags,
) -> Result<Box<dyn WasiFile>, Error> {
todo!()
}
fn open_dir(&self, symlink_follow: bool, path: &str) -> Result<Box<dyn WasiDir>, Error> {
todo!()
}
fn create_dir(&self, path: &str) -> Result<(), Error> {
todo!()
}
fn readdir(
&self,
cursor: ReaddirCursor,
) -> Result<Box<dyn Iterator<Item = Result<(ReaddirEntity, String), Error>>>, Error> {
todo!()
}
fn symlink(&self, src_path: &str, dest_path: &str) -> Result<(), Error> {
todo!()
}
fn remove_dir(&self, path: &str) -> Result<(), Error> {
todo!()
}
fn unlink_file(&self, path: &str) -> Result<(), Error> {
todo!()
}
fn read_link(&self, path: &str) -> Result<PathBuf, Error> {
todo!()
}
fn get_filestat(&self) -> Result<Filestat, Error> {
todo!()
}
fn get_path_filestat(&self, path: &str, follow_symlinks: bool) -> Result<Filestat, Error> {
todo!()
}
fn rename(&self, src_path: &str, dest_dir: &dyn WasiDir, dest_path: &str) -> Result<(), Error> {
todo!()
}
fn hard_link(
&self,
src_path: &str,
target_dir: &dyn WasiDir,
target_path: &str,
) -> Result<(), Error> {
todo!()
}
fn set_times(
&self,
path: &str,
atime: Option<wasi_common::SystemTimeSpec>,
mtime: Option<wasi_common::SystemTimeSpec>,
follow_symlinks: bool,
) -> Result<(), Error> {
todo!()
}
}

73
crates/wasi-common/virtfs/src/file.rs

@ -1,73 +0,0 @@
use cap_std::Advice;
use std::any::Any;
use std::convert::TryInto;
use std::io;
use wasi_common::{
file::{FdFlags, FileType, Filestat, WasiFile},
Error,
};
pub struct File;
impl File {}
impl WasiFile for File {
fn as_any(&self) -> &dyn Any {
self
}
fn datasync(&self) -> Result<(), Error> {
Ok(())
}
fn sync(&self) -> Result<(), Error> {
Ok(())
}
fn get_filetype(&self) -> Result<FileType, Error> {
todo!()
}
fn get_fdflags(&self) -> Result<FdFlags, Error> {
todo!()
}
fn set_fdflags(&mut self, fdflags: FdFlags) -> Result<(), Error> {
todo!()
}
fn get_filestat(&self) -> Result<Filestat, Error> {
todo!()
}
fn set_filestat_size(&self, size: u64) -> Result<(), Error> {
todo!()
}
fn advise(&self, _offset: u64, _len: u64, _advice: Advice) -> Result<(), Error> {
Ok(())
}
fn allocate(&self, offset: u64, len: u64) -> Result<(), Error> {
todo!()
}
fn set_times(
&self,
atime: Option<wasi_common::SystemTimeSpec>,
mtime: Option<wasi_common::SystemTimeSpec>,
) -> Result<(), Error> {
todo!()
}
fn read_vectored(&self, bufs: &mut [io::IoSliceMut]) -> Result<u64, Error> {
todo!()
}
fn read_vectored_at(&self, bufs: &mut [io::IoSliceMut], offset: u64) -> Result<u64, Error> {
todo!()
}
fn write_vectored(&self, bufs: &[io::IoSlice]) -> Result<u64, Error> {
todo!()
}
fn write_vectored_at(&self, bufs: &[io::IoSlice], offset: u64) -> Result<u64, Error> {
todo!()
}
fn seek(&self, pos: std::io::SeekFrom) -> Result<u64, Error> {
todo!()
}
fn peek(&self, buf: &mut [u8]) -> Result<u64, Error> {
todo!()
}
fn num_ready_bytes(&self) -> Result<u64, Error> {
todo!()
}
}

2
crates/wasi-common/virtfs/src/lib.rs

@ -1,2 +0,0 @@
pub mod file;
pub mod dir;

21
docs/examples-rust-wasi-fs.md

@ -1,21 +0,0 @@
# WASI
You can also [browse this source code online][code] and clone the wasmtime
repository to run the example locally.
[code]: https://github.com/bytecodealliance/wasmtime/blob/main/examples/wasi/main.rs
This example shows off how to run a wasi binary with a memory filesystem.
## Wasm Source code
```rust,ignore
{{#include ../examples/wasi-fs/wasm/wasi-fs.rs}}
```
## `wasi-fs.rs`
```rust,ignore
{{#include ../examples/wasi-fs/main.rs}}
```

122
examples/wasi-fs/main.c

@ -1,122 +0,0 @@
/*
Example of instantiating a WebAssembly which uses WASI imports.
You can compile and run this example on Linux with:
cargo build --release -p wasmtime-c-api
cc examples/wasi-fs/main.c \
-I crates/c-api/include \
-I crates/c-api/wasm-c-api/include \
target/release/libwasmtime.a \
-lpthread -ldl -lm \
-o wasi-fs
./wasi-fs
Note that on Windows and macOS the command will be similar, but you'll need
to tweak the `-lpthread` and such annotations.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <wasm.h>
#include <wasi.h>
#include <wasmtime.h>
#define MIN(a, b) ((a) < (b) ? (a) : (b))
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap);
int main() {
int ret = 0;
// Set up our context
wasm_engine_t *engine = wasm_engine_new();
assert(engine != NULL);
wasm_store_t *store = wasm_store_new(engine);
assert(store != NULL);
wasm_byte_vec_t wasm;
// Load our input file to parse it next
FILE* file = fopen("target/wasm32-wasi/debug/wasi-fs.wasm", "rb");
if (!file) {
printf("> Error loading file!\n");
exit(1);
}
fseek(file, 0L, SEEK_END);
size_t file_size = ftell(file);
wasm_byte_vec_new_uninitialized(&wasm, file_size);
fseek(file, 0L, SEEK_SET);
if (fread(wasm.data, file_size, 1, file) != 1) {
printf("> Error loading module!\n");
exit(1);
}
fclose(file);
// Compile our modules
wasm_module_t *module = NULL;
wasmtime_error_t *error = wasmtime_module_new(engine, &wasm, &module);
if (!module)
exit_with_error("failed to compile module", error, NULL);
wasm_byte_vec_delete(&wasm);
// Instantiate wasi
wasi_config_t *wasi_config = wasi_config_new();
assert(wasi_config);
wasi_config_inherit_argv(wasi_config);
wasi_config_inherit_env(wasi_config);
wasi_config_inherit_stdin(wasi_config);
wasi_config_inherit_stdout(wasi_config);
wasi_config_inherit_stderr(wasi_config);
wasi_config_preopen_dir(wasi_config, "examples/wasi-fs", ".");
wasm_trap_t *trap = NULL;
wasi_instance_t *wasi = wasi_instance_new(store, "wasi_snapshot_preview1", wasi_config, &trap);
if (wasi == NULL)
exit_with_error("failed to instantiate WASI", NULL, trap);
wasmtime_linker_t *linker = wasmtime_linker_new(store);
error = wasmtime_linker_define_wasi(linker, wasi);
if (error != NULL)
exit_with_error("failed to link wasi", error, NULL);
// Instantiate the module
wasm_name_t empty;
wasm_name_new_from_string(&empty, "");
wasm_instance_t *instance = NULL;
error = wasmtime_linker_module(linker, &empty, module);
if (error != NULL)
exit_with_error("failed to instantiate module", error, NULL);
// Run it.
wasm_func_t* func;
wasmtime_linker_get_default(linker, &empty, &func);
if (error != NULL)
exit_with_error("failed to locate default export for module", error, NULL);
wasm_val_vec_t args_vec = WASM_EMPTY_VEC;
wasm_val_vec_t results_vec = WASM_EMPTY_VEC;
error = wasmtime_func_call(func, &args_vec, &results_vec, &trap);
if (error != NULL)
exit_with_error("error calling default export", error, trap);
// Clean up after ourselves at this point
wasm_name_delete(&empty);
wasm_module_delete(module);
wasm_store_delete(store);
wasm_engine_delete(engine);
return 0;
}
static void exit_with_error(const char *message, wasmtime_error_t *error, wasm_trap_t *trap) {
fprintf(stderr, "error: %s\n", message);
wasm_byte_vec_t error_message;
if (error != NULL) {
wasmtime_error_message(error, &error_message);
wasmtime_error_delete(error);
} else {
wasm_trap_message(trap, &error_message);
wasm_trap_delete(trap);
}
fprintf(stderr, "%.*s\n", (int) error_message.size, error_message.data);
wasm_byte_vec_delete(&error_message);
exit(1);
}

46
examples/wasi-fs/main.rs

@ -1,46 +0,0 @@
//! Example of running a wasi binary in a memory filesystem
// The corresponding wasm binary can be built with:
// `cargo build -p example-wasi-fs-wasm --target wasm32-wasi`
//
// then you can execute this example with `cargo run --example wasi-fs`
use anyhow::Result;
use std::collections::HashMap;
use wasmtime::*;
use wasmtime_wasi::virtfs::{VecFileContents, VirtualDirEntry};
use wasmtime_wasi::{Wasi, WasiCtxBuilder};
fn main() -> Result<()> {
tracing_subscriber::FmtSubscriber::builder()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.with_ansi(true)
.init();
let store = Store::default();
let mut linker = Linker::new(&store);
// Create an instance of `Wasi` which contains a `WasiCtx`. Note that
// `WasiCtx` provides a number of ways to configure what the target program
// will have access to.
let entry = VirtualDirEntry::File(Box::new(VecFileContents::with_content(
"world".as_bytes().to_owned(),
)));
let mut map = HashMap::new();
map.insert("test.txt".to_string(), entry);
let dir = VirtualDirEntry::Directory(map);
let ctx = WasiCtxBuilder::new()
.inherit_stdout()
.inherit_stderr()
.preopened_virt(dir, ".")
.build()?;
let wasi = Wasi::new(&store, ctx);
wasi.add_to_linker(&mut linker)?;
// Instantiate our module with the imports we've created, and run it.
let module = Module::from_file(store.engine(), "target/wasm32-wasi/debug/wasi-fs.wasm")?;
linker.module("", &module)?;
linker.get_default("")?.get0::<()>()?()?;
Ok(())
}

1
examples/wasi-fs/test.txt

@ -1 +0,0 @@
world

10
examples/wasi-fs/wasm/Cargo.toml

@ -1,10 +0,0 @@
[package]
name = "example-wasi-fs-wasm"
version = "0.0.0"
authors = ["The Wasmtime Project Developers"]
edition = "2018"
publish = false
[[bin]]
path = "wasi-fs.rs"
name = "wasi-fs"

4
examples/wasi-fs/wasm/wasi-fs.rs

@ -1,4 +0,0 @@
fn main() {
let contents = std::fs::read_to_string("test.txt").unwrap();
println!("Hello, {}!", contents);
}
Loading…
Cancel
Save