submilli-engine 0.4.3

Compiles Submilli programs, a TypeScript subset, to WebAssembly and runs them under capability policy
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Filesystem services supplied by the embedding runtime.
//!
//! Capability checks and fuel admission belong to the host caller. Embedders
//! supply an effect ID and optional work budget with each call.
use super::durable::{EffectId, RunId};
use super::fs::{ContainError, ContentPath, FileIdentity};
use super::fuel::WorkBudget;
use super::vfs::{Access, Placement, VfsMode};
use super::{OpenFileGuard, QuotaCharge, QuotaExceeded, Vfs};
use std::fs::File;
use std::io::{BufWriter, Read, Seek, SeekFrom};
use std::sync::Arc;

#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum ReaderOptions {
    Bytes { chunk_size: usize },
    Lines { maximum_bytes: u64 },
}
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ReadPosition {
    pub offset: u64,
    pub bom_handled: bool,
    pub closed: bool,
}
#[derive(serde::Serialize, serde::Deserialize)]
pub enum ReadItem {
    Bytes(#[serde(with = "super::effects::binary")] Vec<u8>),
    Line(String),
}
#[derive(serde::Serialize, serde::Deserialize)]
pub struct ReadOutput {
    pub item: Option<ReadItem>,
    #[serde(skip)]
    pub(crate) _charge: Option<super::limits::HostBytes>,
}
impl ReadOutput {
    /// Keep a pre-admitted allocation charged until output marshalling finishes.
    pub fn new(item: Option<ReadItem>, reservation: super::limits::HostBytes) -> Self {
        Self {
            item,
            _charge: Some(reservation),
        }
    }
}

#[derive(Debug, thiserror::Error)]
pub enum FilesystemError {
    #[error(transparent)]
    Containment(#[from] ContainError),
    #[error(transparent)]
    Io(#[from] std::io::Error),
    #[error(transparent)]
    Quota(#[from] QuotaExceeded),
    #[error("unsupported filesystem operation: {0}")]
    Unsupported(&'static str),
    #[error("{0}")]
    Execution(wasmtime::Error),
    #[error("{0}")]
    Rejected(String),
    #[error(transparent)]
    Exhausted(#[from] super::fuel::Exhausted),
    #[error("filesystem backend failed: {0}")]
    Backend(Box<dyn std::error::Error + Send + Sync>),
}

#[derive(Clone)]
pub struct MountInfo {
    pub path: String,
    pub volume: String,
    pub access: Access,
    pub maximum_bytes: Option<u64>,
}
#[derive(Clone)]
pub struct FilesystemInfo {
    pub cwd: String,
    pub mode: VfsMode,
    pub access: Access,
    pub volume: Option<String>,
    pub maximum_bytes: Option<u64>,
    pub mounts: Vec<MountInfo>,
}
impl FilesystemInfo {
    pub fn from_vfs(vfs: &Vfs) -> Self {
        Self {
            cwd: vfs.cwd().into(),
            mode: vfs.mode(),
            access: vfs.access(),
            volume: vfs.volume().map(str::to_owned),
            maximum_bytes: vfs.quota().map(|q| q.limit()),
            mounts: vfs
                .mounts()
                .iter()
                .map(|m| MountInfo {
                    path: m.guest_path().into(),
                    volume: m.volume().into(),
                    access: m.access(),
                    maximum_bytes: m.quota().map(|q| q.limit()),
                })
                .collect(),
        }
    }
}

/// Resource values contain persistent identity and optional process-local handles.
/// They never retain a backend or persistence services. Every resource belongs to
/// a run; process-local handles are omitted from history and absent after decoding.
#[derive(serde::Serialize, serde::Deserialize)]
pub struct FileReader {
    pub run_id: RunId,
    pub id: EffectId,
    pub options: ReaderOptions,
    #[serde(skip)]
    pub(crate) local: Option<ReaderHandle>,
}
impl FileReader {
    pub fn new(run_id: RunId, id: EffectId, options: ReaderOptions) -> Self {
        Self {
            run_id,
            id,
            options,
            local: None,
        }
    }
}

#[derive(serde::Serialize, serde::Deserialize)]
pub struct FileWriter {
    pub run_id: RunId,
    pub id: EffectId,
    pub closed: bool,
    #[serde(skip)]
    pub(crate) local: Option<WriterHandle>,
}
impl FileWriter {
    pub fn new(run_id: RunId, id: EffectId) -> Self {
        Self {
            run_id,
            id,
            closed: false,
            local: None,
        }
    }
}

#[derive(serde::Serialize, serde::Deserialize)]
pub struct DirectoryReader {
    pub run_id: RunId,
    pub id: EffectId,
    #[serde(skip)]
    pub(crate) local: Option<DirectoryHandle>,
}
impl DirectoryReader {
    pub fn new(run_id: RunId, id: EffectId) -> Self {
        Self {
            run_id,
            id,
            local: None,
        }
    }
}

/// An open reader's process-local file, cursor and disk-quota hold.
pub(crate) struct ReaderHandle {
    pub(crate) file: Option<File>,
    pub(crate) position: ReadPosition,
    pub(crate) held: Option<OpenFileGuard>,
}

/// A directory iterator is process-local and closes when dropped.
pub(crate) struct DirectoryHandle {
    pub(crate) entries: Option<Box<dyn Iterator<Item = DirectoryEntry> + Send + Sync>>,
}

/// Buffered writer to a temp sibling file. Explicit [`close`](Self::close) fsyncs and
/// atomically renames it onto `final_path`; `Drop` (the safety net for an un-`close`d
/// writer) discards the temp file without renaming, removing it only while its name
/// still refers to the writer's file.
pub(crate) struct WriterHandle {
    /// `None` once the writer is closed.
    pub(crate) open: Option<OpenWrite>,
    /// Both sides stay contained paths for the whole lifetime of the writer. The commit
    /// happens through the same handle the open resolved against, so a link swapped over
    /// the parent between `writer()` and `close()` cannot redirect it — and the drop path
    /// cannot unlink something under the server's working directory.
    pub(crate) temp: TempFile,
    pub(crate) final_path: ContentPath,
}

/// The temp file a writer streams into. The program can reach it by name while the
/// writer is open, so before committing or discarding it, the writer checks that the
/// name still refers to the file it created.
pub(crate) struct TempFile {
    pub(crate) path: ContentPath,
    pub(crate) identity: FileIdentity,
}

/// An open writer's buffer and its claim on the VFS's size limit. Program code runs
/// between writes, so the claim replaces nothing up front: every byte written counts
/// until `close` covers the file it replaces.
pub(crate) struct OpenWrite {
    pub(crate) writer: BufWriter<File>,
    pub(crate) disk_charge: QuotaCharge,
    /// Registers the temp file as the writer's own, so a removal of it by name
    /// leaves its bytes for the writer to settle.
    pub(crate) held: Option<OpenFileGuard>,
}

#[derive(Clone, serde::Serialize)]
pub struct DirectoryEntry {
    pub name: String,
    pub path: String,
    pub kind: &'static str,
    pub bytes: u64,
}
impl<'de> serde::Deserialize<'de> for DirectoryEntry {
    fn deserialize<D: serde::Deserializer<'de>>(decoder: D) -> Result<Self, D::Error> {
        #[derive(serde::Deserialize)]
        struct Entry {
            name: String,
            path: String,
            kind: String,
            bytes: u64,
        }
        let entry = Entry::deserialize(decoder)?;
        let kind = match entry.kind.as_str() {
            "file" => "file",
            "directory" => "directory",
            "symlink" => "symlink",
            "other" => "other",
            _ => return Err(serde::de::Error::custom("invalid filesystem entry kind")),
        };
        Ok(Self {
            name: entry.name,
            path: entry.path,
            kind,
            bytes: entry.bytes,
        })
    }
}
#[async_trait::async_trait]
pub trait Filesystem: Send + Sync {
    /// Whether resources can be used after decoding without their local handles.
    fn supports_resource_replay(&self) -> bool {
        false
    }

    fn info(&self) -> FilesystemInfo;
    fn cwd(&self) -> &str;
    /// Additional fuel to admit before resolving a guest path.
    fn path_cost(&self, _path: &str) -> u64 {
        0
    }
    fn placement(&self, path: &str) -> Result<Placement, FilesystemError>;
    async fn metadata(
        &self,
        effect_id: &EffectId,
        work: Option<Arc<WorkBudget>>,
        path: &str,
        follow: bool,
    ) -> Result<cap_std::fs::Metadata, FilesystemError>;
    async fn read(
        &self,
        effect_id: &EffectId,
        work: Option<Arc<WorkBudget>>,
        path: &str,
        offset: u64,
        maximum_bytes: u64,
    ) -> Result<Vec<u8>, FilesystemError>;
    /// Atomically replace the file contents, creating the file if it is missing.
    async fn write(
        &self,
        effect_id: &EffectId,
        work: Option<Arc<WorkBudget>>,
        path: &str,
        bytes: Vec<u8>,
    ) -> Result<(), FilesystemError>;
    /// Append bytes to the file, creating the file if it is missing.
    async fn append(
        &self,
        effect_id: &EffectId,
        work: Option<Arc<WorkBudget>>,
        path: &str,
        bytes: Vec<u8>,
    ) -> Result<(), FilesystemError>;
    async fn mkdir(
        &self,
        effect_id: &EffectId,
        work: Option<Arc<WorkBudget>>,
        path: &str,
        recursive: bool,
    ) -> Result<(), FilesystemError>;
    async fn remove(
        &self,
        effect_id: &EffectId,
        work: Option<Arc<WorkBudget>>,
        path: &str,
        recursive: bool,
        budget: &mut dyn MutationBudget,
    ) -> Result<(), FilesystemError>;
    async fn move_entry(
        &self,
        effect_id: &EffectId,
        work: Option<Arc<WorkBudget>>,
        from: &str,
        to: &str,
        budget: &mut dyn MutationBudget,
    ) -> Result<(), FilesystemError>;
    async fn copy(
        &self,
        effect_id: &EffectId,
        work: Option<Arc<WorkBudget>>,
        from: &str,
        to: &str,
        recursive: bool,
        budget: &mut dyn MutationBudget,
    ) -> Result<(), FilesystemError>;
    async fn next_reader(
        &self,
        effect_id: &EffectId,
        work: Option<Arc<WorkBudget>>,
        reader: &mut FileReader,
        limits: &super::limits::TenantLimits,
    ) -> Result<ReadOutput, FilesystemError>;
    async fn close_reader(
        &self,
        effect_id: &EffectId,
        work: Option<Arc<WorkBudget>>,
        reader: &mut FileReader,
    ) -> Result<(), FilesystemError>;
    async fn next_directory(
        &self,
        effect_id: &EffectId,
        work: Option<Arc<WorkBudget>>,
        directory: &mut DirectoryReader,
        limits: &super::limits::TenantLimits,
    ) -> Result<Option<DirectoryEntry>, FilesystemError>;
    async fn close_directory(
        &self,
        effect_id: &EffectId,
        work: Option<Arc<WorkBudget>>,
        directory: &mut DirectoryReader,
        limits: &super::limits::TenantLimits,
    ) -> Result<(), FilesystemError>;
    async fn write_chunk(
        &self,
        effect_id: &EffectId,
        work: Option<Arc<WorkBudget>>,
        writer: &mut FileWriter,
        bytes: Vec<u8>,
    ) -> Result<(), FilesystemError>;
    /// Only explicit close publishes. Dropping a resource never publishes it.
    async fn close_writer(
        &self,
        effect_id: &EffectId,
        work: Option<Arc<WorkBudget>>,
        writer: &mut FileWriter,
    ) -> Result<(), FilesystemError>;
    async fn open_reader(
        &self,
        effect_id: &EffectId,
        work: Option<Arc<WorkBudget>>,
        path: &str,
        options: ReaderOptions,
    ) -> Result<FileReader, FilesystemError>;
    async fn open_directory(
        &self,
        effect_id: &EffectId,
        work: Option<Arc<WorkBudget>>,
        path: &str,
        recursive: bool,
        limits: &super::limits::TenantLimits,
    ) -> Result<DirectoryReader, FilesystemError>;
    async fn open_writer(
        &self,
        effect_id: &EffectId,
        work: Option<Arc<WorkBudget>>,
        path: &str,
    ) -> Result<FileWriter, FilesystemError>;
}

pub fn bounded_read(
    mut reader: impl Read + Seek,
    offset: u64,
    maximum_bytes: u64,
) -> Result<Vec<u8>, FilesystemError> {
    reader.seek(SeekFrom::Start(offset))?;
    let mut bytes = Vec::new();
    let mut bounded = reader.take(maximum_bytes);
    let mut chunk = [0; 8192];
    loop {
        let read = bounded.read(&mut chunk)?;
        if read == 0 {
            return Ok(bytes);
        }
        bytes
            .try_reserve(read)
            .map_err(|e| FilesystemError::Backend(Box::new(e)))?;
        bytes.extend_from_slice(&chunk[..read]);
    }
}
/// Resource accounting for native tree operations. Once a mutation succeeds,
/// subsequent work must settle fuel rather than refuse. Heap admission stays fallible.
pub trait MutationBudget: Send {
    fn begin(&mut self) -> wasmtime::Result<()>;
    fn reserve(&mut self, bytes: u64) -> wasmtime::Result<()>;
    fn end(&mut self);
    fn effect(&mut self);
    fn syscalls(&mut self, count: u64, settled: bool) -> wasmtime::Result<()>;
    fn copied(&mut self, entries: u64, bytes: u64) -> wasmtime::Result<()>;
}