fetchlet 0.1.0

Rust-hosted JavaScript functions and Fetch handlers with explicit host services
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! The frames between an isolated instance's parent and its child, over the
//! child's standard input and output: a one-byte kind, a four-byte
//! little-endian length and the payload, JSON or raw bytes by kind.
//!
//! Lengths are bounded only by the four-byte field. A reader never reserves
//! more than the bytes that actually arrive, so a frame that claims more
//! than it carries costs nothing, and the content of each frame is bounded
//! where it is used, by the same budgets as in-process (`Limits`).

use std::io::{self, Read, Write};

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::execution::{Interrupted, StopReason};
use crate::{Backend, Error, Limits, Requirements};

/// What the child needs to build its engine.
#[derive(Serialize, Deserialize)]
pub(crate) struct Init {
    pub backend: Backend,
    pub source: String,
    pub requirements: Requirements,
    pub limits: Limits,
    /// The native stack of the child's engine thread; None for Rust's
    /// default.
    pub stack_bytes: Option<usize>,
    /// The memory budget the child applies to itself where the parent
    /// cannot (Linux); None otherwise.
    pub memory_bytes: Option<usize>,
}

pub(crate) enum Frame {
    // Parent to child.
    Init(Box<Init>),
    /// An evaluation, with the time left until its deadline.
    Evaluate {
        expression: String,
        timeout_micros: Option<u64>,
    },
    /// A direct call of an export, as `Instance::call_json` makes it.
    Call {
        export: String,
        args: Vec<Value>,
    },
    /// The JSON reply of a host operation.
    Reply(String),
    /// The handle of bytes the child put into the byte store.
    /// Bytes the child took from the byte store.
    Bytes(Result<Vec<u8>, Failure>),
    /// A host future the engine may wait on is ready.
    Wake,
    /// The client of the current invocation went away.
    Disconnected,

    // Child to parent.
    Ready,
    Failed(Failure),
    Outcome {
        outcome: Outcome,
        reusable: bool,
    },
    Dispatch {
        operation: String,
        input: String,
    },
    /// A buffer the engine stored, under the handle the child picked from
    /// the guest range. The parent stores it without a reply, before the
    /// frame that carries its handle.
    PutBytes {
        handle: u64,
        bytes: Vec<u8>,
    },
    TakeBytes(f64),
}

/// A `fetchlet::Error` as it crosses the boundary: its stable code and the
/// detail that stays on the Rust side.
#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct Failure {
    code: String,
    detail: String,
}

impl From<&Error> for Failure {
    fn from(error: &Error) -> Self {
        let detail = match error {
            Error::Configuration(detail)
            | Error::InvalidRequest(detail)
            | Error::JavaScript(detail)
            | Error::Transport(detail)
            | Error::WorkerFailed(detail)
            | Error::InstanceLost(detail) => detail.clone(),
            Error::LimitExceeded(resource) => (*resource).to_owned(),
            _ => String::new(),
        };
        Self {
            code: error.code().to_owned(),
            detail,
        }
    }
}

impl Failure {
    pub fn into_error(self) -> Error {
        match self.code.as_str() {
            "CONFIGURATION" => Error::Configuration(self.detail),
            "INVALID_REQUEST" => Error::InvalidRequest(self.detail),
            "PERMISSION_DENIED" => Error::PermissionDenied,
            "LIMIT_EXCEEDED" => Error::LimitExceeded(limit_name(&self.detail)),
            "INVOCATION_CLOSED" => Error::InvocationClosed,
            "DEADLINE_EXCEEDED" => Error::DeadlineExceeded,
            "JAVASCRIPT" => Error::JavaScript(self.detail),
            "TRANSPORT" => Error::Transport(self.detail),
            "REENTRANT" => Error::Reentrant,
            "INSTANCE_LOST" => Error::InstanceLost(self.detail),
            _ => Error::InstanceUnavailable,
        }
    }
}

/// The resource names of the limits that cross the boundary, which
/// `Error::LimitExceeded` keeps as static strings.
fn limit_name(detail: &str) -> &'static str {
    const NAMES: &[&str] = &[
        "bridge message bytes",
        "engine busy time",
        "engine heap",
        "process memory",
    ];
    NAMES
        .iter()
        .find(|name| **name == detail)
        .copied()
        .unwrap_or("isolated instance resource")
}

/// How an evaluation ended in the child.
#[derive(Clone, Serialize, Deserialize)]
pub(crate) enum Outcome {
    Value(Value),
    Interrupted(StopReason),
    JavaScript(String),
    /// The engine reported an allocation that failed at the child's memory
    /// budget; the parent retires the instance.
    ProcessMemory,
}

impl Outcome {
    pub fn from_result(result: anyhow::Result<Value>) -> Self {
        match result {
            Ok(value) => Self::Value(value),
            Err(error) => match error.downcast::<Interrupted>() {
                Ok(interrupted) => Self::Interrupted(interrupted.0),
                Err(error) => Self::JavaScript(error.to_string()),
            },
        }
    }

    pub fn into_result(self) -> anyhow::Result<Value> {
        match self {
            Self::Value(value) => Ok(value),
            Self::Interrupted(reason) => Err(Interrupted(reason).into()),
            Self::JavaScript(message) => Err(anyhow::Error::msg(message)),
            Self::ProcessMemory => Err(Error::LimitExceeded("process memory").into()),
        }
    }
}

const INIT: u8 = 1;
const EVALUATE: u8 = 2;
const CALL: u8 = 3;
const REPLY: u8 = 4;
const DISCONNECTED: u8 = 5;
const BYTES: u8 = 6;
const BYTES_FAILED: u8 = 7;
const WAKE: u8 = 8;
const READY: u8 = 20;
const FAILED: u8 = 21;
const OUTCOME: u8 = 22;
const DISPATCH: u8 = 23;
const PUT_BYTES: u8 = 24;
const TAKE_BYTES: u8 = 25;

#[derive(Serialize, Deserialize)]
struct EvaluatePayload {
    expression: String,
    timeout_micros: Option<u64>,
}

#[derive(Serialize, Deserialize)]
struct CallPayload {
    export: String,
    args: Vec<Value>,
}

#[derive(Serialize, Deserialize)]
struct OutcomePayload {
    outcome: Outcome,
    reusable: bool,
}

#[derive(Serialize, Deserialize)]
struct DispatchPayload {
    operation: String,
    input: String,
}

fn json(value: &impl Serialize) -> Vec<u8> {
    serde_json::to_vec(value).expect("frames serialize to JSON")
}

fn parse<T: for<'de> Deserialize<'de>>(payload: &[u8]) -> io::Result<T> {
    serde_json::from_slice(payload)
        .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))
}

impl Frame {
    fn encode(&self) -> (u8, Vec<u8>) {
        match self {
            Self::Init(init) => (INIT, json(init)),
            Self::Evaluate {
                expression,
                timeout_micros,
            } => (
                EVALUATE,
                json(&EvaluatePayload {
                    expression: expression.clone(),
                    timeout_micros: *timeout_micros,
                }),
            ),
            Self::Call { export, args } => (
                CALL,
                json(&CallPayload {
                    export: export.clone(),
                    args: args.clone(),
                }),
            ),
            Self::Reply(reply) => (REPLY, reply.as_bytes().to_vec()),
            Self::Disconnected => (DISCONNECTED, Vec::new()),
            Self::Bytes(Ok(bytes)) => (BYTES, bytes.clone()),
            Self::Bytes(Err(failure)) => (BYTES_FAILED, json(failure)),
            Self::Wake => (WAKE, Vec::new()),
            Self::Ready => (READY, Vec::new()),
            Self::Failed(failure) => (FAILED, json(failure)),
            Self::Outcome { outcome, reusable } => (
                OUTCOME,
                json(&OutcomePayload {
                    outcome: outcome.clone(),
                    reusable: *reusable,
                }),
            ),
            Self::Dispatch { operation, input } => (
                DISPATCH,
                json(&DispatchPayload {
                    operation: operation.clone(),
                    input: input.clone(),
                }),
            ),
            Self::PutBytes { handle, bytes } => {
                let mut payload = Vec::with_capacity(8 + bytes.len());
                payload.extend_from_slice(&handle.to_le_bytes());
                payload.extend_from_slice(bytes);
                (PUT_BYTES, payload)
            }
            Self::TakeBytes(handle) => (TAKE_BYTES, handle.to_le_bytes().to_vec()),
        }
    }

    fn decode(kind: u8, payload: Vec<u8>) -> io::Result<Self> {
        Ok(match kind {
            INIT => Self::Init(Box::new(parse(&payload)?)),
            EVALUATE => {
                let EvaluatePayload {
                    expression,
                    timeout_micros,
                } = parse(&payload)?;
                Self::Evaluate {
                    expression,
                    timeout_micros,
                }
            }
            CALL => {
                let CallPayload { export, args } = parse(&payload)?;
                Self::Call { export, args }
            }
            REPLY => Self::Reply(
                String::from_utf8(payload)
                    .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?,
            ),
            DISCONNECTED => Self::Disconnected,
            BYTES => Self::Bytes(Ok(payload)),
            BYTES_FAILED => Self::Bytes(Err(parse(&payload)?)),
            WAKE => Self::Wake,
            READY => Self::Ready,
            FAILED => Self::Failed(parse(&payload)?),
            OUTCOME => {
                let OutcomePayload { outcome, reusable } = parse(&payload)?;
                Self::Outcome { outcome, reusable }
            }
            DISPATCH => {
                let DispatchPayload { operation, input } = parse(&payload)?;
                Self::Dispatch { operation, input }
            }
            PUT_BYTES => {
                if payload.len() < 8 {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        "bad byte handle",
                    ));
                }
                let handle = u64::from_le_bytes(payload[..8].try_into().expect("eight bytes"));
                Self::PutBytes {
                    handle,
                    bytes: payload[8..].to_vec(),
                }
            }
            TAKE_BYTES => {
                let bytes: [u8; 8] = payload
                    .try_into()
                    .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "bad byte handle"))?;
                Self::TakeBytes(f64::from_le_bytes(bytes))
            }
            _ => {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "unknown frame kind",
                ));
            }
        })
    }
}

/// Write one frame and flush it.
pub(crate) fn write(writer: &mut impl Write, frame: &Frame) -> io::Result<()> {
    let (kind, payload) = frame.encode();
    let length = u32::try_from(payload.len())
        .map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "frame too large"))?;
    let mut header = [0u8; 5];
    header[0] = kind;
    header[1..].copy_from_slice(&length.to_le_bytes());
    writer.write_all(&header)?;
    writer.write_all(&payload)?;
    writer.flush()
}

/// Read one frame, or None when the other side closed the stream between
/// frames.
pub(crate) fn read(reader: &mut impl Read) -> io::Result<Option<Frame>> {
    let mut header = [0u8; 5];
    let mut filled = 0;
    while filled < header.len() {
        match reader.read(&mut header[filled..])? {
            0 if filled == 0 => return Ok(None),
            0 => return Err(io::ErrorKind::UnexpectedEof.into()),
            count => filled += count,
        }
    }
    let length = u32::from_le_bytes(header[1..].try_into().expect("four bytes"));
    let mut payload = Vec::new();
    reader
        .by_ref()
        .take(u64::from(length))
        .read_to_end(&mut payload)?;
    if payload.len() != length as usize {
        return Err(io::ErrorKind::UnexpectedEof.into());
    }
    Frame::decode(header[0], payload).map(Some)
}

/// Read frames from untrusted bytes as either end reads its pipe, and check
/// that each frame read writes back to bytes that read to the same frame.
/// For the `isolation_frames` fuzz target.
#[cfg(feature = "fuzzing")]
pub fn fuzz_frames(data: &[u8]) {
    let mut input = data;
    while let Ok(Some(frame)) = read(&mut input) {
        let mut written = Vec::new();
        write(&mut written, &frame).expect("a frame read writes back");
        let again = read(&mut written.as_slice())
            .expect("a written frame reads")
            .expect("a written frame is whole");
        let mut rewritten = Vec::new();
        write(&mut rewritten, &again).expect("a frame read writes back");
        assert_eq!(written, rewritten);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn round_trip(frame: Frame) -> Frame {
        let mut buffer = Vec::new();
        write(&mut buffer, &frame).unwrap();
        read(&mut buffer.as_slice()).unwrap().unwrap()
    }

    #[test]
    fn frames_survive_the_stream() {
        assert!(matches!(
            round_trip(Frame::Dispatch {
                operation: "url.parse".into(),
                input: "[\"https://a.test\"]".into(),
            }),
            Frame::Dispatch { operation, .. } if operation == "url.parse"
        ));
        assert!(matches!(
            round_trip(Frame::Disconnected),
            Frame::Disconnected
        ));
        assert!(matches!(
            round_trip(Frame::PutBytes { handle: 1 << 52, bytes: vec![0, 255] }),
            Frame::PutBytes { handle, bytes } if handle == 1 << 52 && bytes == [0, 255]
        ));
        assert!(matches!(
            round_trip(Frame::TakeBytes(42.0)),
            Frame::TakeBytes(handle) if handle == 42.0
        ));
        assert!(matches!(
            round_trip(Frame::Bytes(Ok(vec![1, 2, 3]))),
            Frame::Bytes(Ok(bytes)) if bytes == [1, 2, 3]
        ));
        let failure = Failure::from(&Error::LimitExceeded("engine heap"));
        let Frame::Failed(failure) = round_trip(Frame::Failed(failure)) else {
            panic!("expected Failed");
        };
        assert!(matches!(
            failure.into_error(),
            Error::LimitExceeded("engine heap")
        ));
    }

    #[test]
    fn a_closed_stream_between_frames_is_the_end() {
        assert!(read(&mut [].as_slice()).unwrap().is_none());
    }

    #[test]
    fn truncated_or_unknown_frames_are_refused_without_reserving_their_length() {
        // A header that claims 4 GiB but carries three bytes.
        let lying = [DISPATCH, 0xff, 0xff, 0xff, 0xff, b'{', b'}', b'x'];
        assert!(read(&mut lying.as_slice()).is_err());
        let unknown = [99, 0, 0, 0, 0];
        assert!(read(&mut unknown.as_slice()).is_err());
        let half_header = [DISPATCH, 1];
        assert!(read(&mut half_header.as_slice()).is_err());
    }
}