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
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions components/readonly/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ license = "Apache-2.0"
crate-type = ["cdylib"]

[dependencies]
componentized-rt = { workspace = true, features = ["executor"] }
wit-bindgen = { workspace = true }

[dev-dependencies]
tempfile = { workspace = true }
test-harness = { workspace = true }
tokio = { workspace = true }
wasmtime = { workspace = true }
6 changes: 5 additions & 1 deletion components/readonly/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# `readonly`

Virtualizes the wasi:filesystem interfaces making the exported file system read-only.
Virtualizes the `wasi:filesystem` interfaces making the exported file system read-only.

Every operation that reads the filesystem is passed through, every operation that would change it fails with `read-only`, including opening a descriptor with flags that would allow a change. `sync` and `sync-data` are denied as well.

`write-via-stream` and `append-via-stream` are synchronous and resolve their result on a background thread, so the host must support component model threads, e.g. `wasmtime -W component-model-threading`.
7 changes: 7 additions & 0 deletions components/readonly/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
// componentized_rt::executor's thread is started with thread.new-indirect, which calls through
// the exported function table
if std::env::var("CARGO_CFG_TARGET_FAMILY").as_deref() == Ok("wasm") {
println!("cargo:rustc-link-arg-cdylib=--export-table");
}
}
34 changes: 18 additions & 16 deletions components/readonly/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::path::PathBuf;

use exports::wasi::filesystem::preopens::Guest as Preopens;
use exports::wasi::filesystem::types::{
Advice, Descriptor, DescriptorBorrow, DescriptorFlags, DescriptorStat, DescriptorType,
Expand All @@ -17,7 +15,7 @@ impl Preopens for FilesystemReadOnly {
preopens::get_directories()
.into_iter()
.map(|(fd, path)| {
let fd = Descriptor::new(ReadOnlyDescriptor::new(fd, path.clone().into()));
let fd = Descriptor::new(ReadOnlyDescriptor::new(fd));
(fd, path)
})
.collect()
Expand All @@ -30,12 +28,11 @@ impl Types for FilesystemReadOnly {

struct ReadOnlyDescriptor {
fd: types::Descriptor,
path: PathBuf,
}

impl ReadOnlyDescriptor {
fn new(fd: types::Descriptor, path: PathBuf) -> Self {
Self { fd, path }
fn new(fd: types::Descriptor) -> Self {
Self { fd }
}
}

Expand All @@ -55,18 +52,14 @@ impl exports::wasi::filesystem::types::GuestDescriptor for ReadOnlyDescriptor {
_data: wit_bindgen::StreamReader<u8>,
_offset: Filesize,
) -> wit_bindgen::FutureReader<Result<(), ErrorCode>> {
let (tx, rx) = wit_future::new(|| Err(ErrorCode::ReadOnly));
tx.write(Err(ErrorCode::ReadOnly));
rx
read_only()
}

fn append_via_stream(
&self,
_data: wit_bindgen::StreamReader<u8>,
) -> wit_bindgen::FutureReader<Result<(), ErrorCode>> {
let (tx, rx) = wit_future::new(|| Err(ErrorCode::ReadOnly));
tx.write(Err(ErrorCode::ReadOnly));
rx
read_only()
}

async fn advise(
Expand Down Expand Up @@ -171,6 +164,7 @@ impl exports::wasi::filesystem::types::GuestDescriptor for ReadOnlyDescriptor {
|| flags.contains(DescriptorFlags::FILE_INTEGRITY_SYNC)
|| flags.contains(DescriptorFlags::DATA_INTEGRITY_SYNC)
|| flags.contains(DescriptorFlags::REQUESTED_WRITE_SYNC)
|| flags.contains(DescriptorFlags::MUTATE_DIRECTORY)
{
return Err(ErrorCode::ReadOnly);
}
Expand All @@ -180,10 +174,7 @@ impl exports::wasi::filesystem::types::GuestDescriptor for ReadOnlyDescriptor {
.open_at(path_flags, path.clone(), open_flags, flags)
.await
{
Ok(fd) => Ok(Descriptor::new(ReadOnlyDescriptor::new(
fd,
self.path.clone().join(path),
))),
Ok(fd) => Ok(Descriptor::new(ReadOnlyDescriptor::new(fd))),
Err(error_code) => Err(error_code),
}
}
Expand Down Expand Up @@ -231,6 +222,17 @@ impl exports::wasi::filesystem::types::GuestDescriptor for ReadOnlyDescriptor {
}
}

/// A future resolved to `read-only`, for the sync exports that return a future.
fn read_only() -> wit_bindgen::FutureReader<Result<(), ErrorCode>> {
let (tx, rx) = wit_future::new(|| Err(ErrorCode::ReadOnly));
// a sync export can't wait for the write, the reader has not been returned yet, so the write
// completes in the background
componentized_rt::executor::spawn(async move {
let _ = tx.write(Err(ErrorCode::ReadOnly)).await;
});
rx
}

wit_bindgen::generate!({
path: "../wit",
world: "filesystem",
Expand Down
Loading
Loading