redevplugin-wasm-abi 0.6.24

WASM worker ABI validation for the ReDevPlugin runtime
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
use std::collections::HashMap;
use std::fmt;
use wasmparser::{Encoding, ExternalKind, FuncType, Parser, Payload, RefType, TypeRef, ValType};

pub const WASM_WORKER_ABI_VERSION: &str = "redevplugin-wasm-worker-v2";
pub const EXPORT_MEMORY: &str = "memory";
pub const EXPORT_WORKER_ALLOC: &str = "redevplugin_worker_alloc";
pub const EXPORT_WORKER_DEALLOC: &str = "redevplugin_worker_dealloc";
pub const EXPORT_WORKER_INVOKE: &str = "redevplugin_worker_invoke";
pub const REQUIRED_EXPORT_INVOKE: &str = EXPORT_WORKER_INVOKE;
pub const IMPORT_STORAGE: &str = "redevplugin.storage";
pub const IMPORT_NETWORK: &str = "redevplugin.network";
pub const MAX_TABLE_ELEMENTS: u64 = 65_536;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValueType {
    I32,
    I64,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FunctionContract {
    pub export_name: String,
    pub params: Vec<ValueType>,
    pub results: Vec<ValueType>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MemoryContract {
    pub initial_pages: u64,
    pub maximum_pages: Option<u64>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HostcallImport {
    pub module: String,
    pub name: String,
    pub params: Vec<ValueType>,
    pub results: Vec<ValueType>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValidatedWorkerModule {
    pub byte_len: usize,
    pub memory: MemoryContract,
    pub alloc_export: FunctionContract,
    pub dealloc_export: FunctionContract,
    pub invoke_export: FunctionContract,
    pub imports: Vec<HostcallImport>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValidationErrorCategory {
    InvalidModule,
    UnsupportedImport,
    InvalidMemory,
    InvalidTable,
    MissingExport,
    InvalidSignature,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValidationError {
    category: ValidationErrorCategory,
    message: String,
}

impl ValidationError {
    pub fn category(&self) -> ValidationErrorCategory {
        self.category
    }

    fn new(category: ValidationErrorCategory, message: impl Into<String>) -> Self {
        Self {
            category,
            message: message.into(),
        }
    }
}

impl fmt::Display for ValidationError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.message)
    }
}

impl std::error::Error for ValidationError {}

pub fn validate_worker_module(bytes: &[u8]) -> Result<ValidatedWorkerModule, ValidationError> {
    wasmparser::Validator::new()
        .validate_all(bytes)
        .map_err(|err| invalid_module(format!("wasm validation failed: {err}")))?;

    let mut types = Vec::new();
    let mut function_type_indices = Vec::new();
    let mut imported_function_type_indices = Vec::new();
    let mut imports = Vec::new();
    let mut memories = Vec::new();
    let mut table_count = 0_u32;
    let mut exports = HashMap::new();

    for payload in Parser::new(0).parse_all(bytes) {
        match payload.map_err(|err| invalid_module(format!("parse wasm module: {err}")))? {
            Payload::Version { encoding, .. } if encoding != Encoding::Module => {
                return Err(invalid_module(
                    "worker artifact must be a core WebAssembly module",
                ));
            }
            Payload::TypeSection(reader) => {
                for function_type in reader.into_iter_err_on_gc_types() {
                    types.push(function_type.map_err(|err| {
                        invalid_module(format!("parse wasm function type: {err}"))
                    })?);
                }
            }
            Payload::ImportSection(reader) => {
                for imported in reader {
                    let imported = imported
                        .map_err(|err| invalid_module(format!("parse wasm import: {err}")))?;
                    let TypeRef::Func(type_index) = imported.ty else {
                        return Err(ValidationError::new(
                            ValidationErrorCategory::UnsupportedImport,
                            format!(
                                "worker import {}/{} must be a function",
                                imported.module, imported.name
                            ),
                        ));
                    };
                    let function_type = types.get(type_index as usize).ok_or_else(|| {
                        invalid_module(format!(
                            "worker import {}/{} references missing type {type_index}",
                            imported.module, imported.name
                        ))
                    })?;
                    require_hostcall(imported.module, imported.name, function_type)?;
                    let (params, results) = function_contract_types(function_type)?;
                    imports.push(HostcallImport {
                        module: imported.module.to_string(),
                        name: imported.name.to_string(),
                        params,
                        results,
                    });
                    imported_function_type_indices.push(type_index);
                }
            }
            Payload::FunctionSection(reader) => {
                for type_index in reader {
                    function_type_indices
                        .push(type_index.map_err(|err| {
                            invalid_module(format!("parse wasm function: {err}"))
                        })?);
                }
            }
            Payload::TableSection(reader) => {
                for table in reader {
                    let table =
                        table.map_err(|err| invalid_module(format!("parse wasm table: {err}")))?;
                    table_count += 1;
                    if table_count > 1 {
                        return Err(ValidationError::new(
                            ValidationErrorCategory::InvalidTable,
                            "worker must define at most one table",
                        ));
                    }
                    if table.ty.table64
                        || table.ty.shared
                        || !matches!(table.ty.element_type, RefType::FUNCREF | RefType::EXTERNREF)
                        || table.ty.initial > MAX_TABLE_ELEMENTS
                        || table
                            .ty
                            .maximum
                            .is_some_and(|maximum| maximum > MAX_TABLE_ELEMENTS)
                    {
                        return Err(ValidationError::new(
                            ValidationErrorCategory::InvalidTable,
                            "worker table contract is unsupported",
                        ));
                    }
                }
            }
            Payload::MemorySection(reader) => {
                for memory in reader {
                    let memory = memory
                        .map_err(|err| invalid_module(format!("parse wasm memory: {err}")))?;
                    memories.push(memory);
                }
            }
            Payload::ExportSection(reader) => {
                for exported in reader {
                    let exported = exported
                        .map_err(|err| invalid_module(format!("parse wasm export: {err}")))?;
                    if exports
                        .insert(exported.name.to_string(), (exported.kind, exported.index))
                        .is_some()
                    {
                        return Err(invalid_module(format!(
                            "worker export {:?} is duplicated",
                            exported.name
                        )));
                    }
                }
            }
            _ => {}
        }
    }

    if memories.len() != 1 {
        return Err(ValidationError::new(
            ValidationErrorCategory::InvalidMemory,
            format!(
                "worker must define exactly one linear memory, found {}",
                memories.len()
            ),
        ));
    }
    let memory = memories[0];
    if memory.memory64 || memory.shared || memory.page_size_log2.is_some() {
        return Err(ValidationError::new(
            ValidationErrorCategory::InvalidMemory,
            "worker memory must be an unshared 32-bit memory with standard pages",
        ));
    }
    match exports.get(EXPORT_MEMORY) {
        Some((ExternalKind::Memory, 0)) => {}
        _ => {
            return Err(ValidationError::new(
                ValidationErrorCategory::MissingExport,
                "worker must export its only linear memory as \"memory\"",
            ));
        }
    }

    let alloc_export = require_function_export(
        EXPORT_WORKER_ALLOC,
        &[ValueType::I32],
        &[ValueType::I32],
        &exports,
        &types,
        &imported_function_type_indices,
        &function_type_indices,
    )?;
    let dealloc_export = require_function_export(
        EXPORT_WORKER_DEALLOC,
        &[ValueType::I32, ValueType::I32],
        &[],
        &exports,
        &types,
        &imported_function_type_indices,
        &function_type_indices,
    )?;
    let invoke_export = require_function_export(
        EXPORT_WORKER_INVOKE,
        &[ValueType::I32, ValueType::I32],
        &[ValueType::I64],
        &exports,
        &types,
        &imported_function_type_indices,
        &function_type_indices,
    )?;

    Ok(ValidatedWorkerModule {
        byte_len: bytes.len(),
        memory: MemoryContract {
            initial_pages: memory.initial,
            maximum_pages: memory.maximum,
        },
        alloc_export,
        dealloc_export,
        invoke_export,
        imports,
    })
}

fn require_hostcall(
    module: &str,
    name: &str,
    function_type: &FuncType,
) -> Result<(), ValidationError> {
    let allowed = matches!(
        (module, name),
        (IMPORT_STORAGE, "files" | "kv" | "sqlite") | (IMPORT_NETWORK, "execute")
    );
    if !allowed {
        return Err(ValidationError::new(
            ValidationErrorCategory::UnsupportedImport,
            format!("worker import {module}/{name} is unsupported"),
        ));
    }
    let (params, results) = function_contract_types(function_type)?;
    if params != [ValueType::I32; 4] || results != [ValueType::I32] {
        return Err(ValidationError::new(
            ValidationErrorCategory::InvalidSignature,
            format!("worker import {module}/{name} has an invalid function signature"),
        ));
    }
    Ok(())
}

fn require_function_export(
    name: &str,
    expected_params: &[ValueType],
    expected_results: &[ValueType],
    exports: &HashMap<String, (ExternalKind, u32)>,
    types: &[FuncType],
    imported_function_type_indices: &[u32],
    function_type_indices: &[u32],
) -> Result<FunctionContract, ValidationError> {
    let Some((ExternalKind::Func, function_index)) = exports.get(name).copied() else {
        return Err(ValidationError::new(
            ValidationErrorCategory::MissingExport,
            format!("required function export {name:?} is missing"),
        ));
    };
    let type_index =
        if let Some(type_index) = imported_function_type_indices.get(function_index as usize) {
            *type_index
        } else {
            let defined_index = function_index as usize - imported_function_type_indices.len();
            *function_type_indices.get(defined_index).ok_or_else(|| {
                invalid_module(format!(
                    "function export {name:?} references missing function {function_index}"
                ))
            })?
        };
    let function_type = types.get(type_index as usize).ok_or_else(|| {
        invalid_module(format!(
            "function export {name:?} references missing type {type_index}"
        ))
    })?;
    let (params, results) = function_contract_types(function_type)?;
    if params != expected_params || results != expected_results {
        return Err(ValidationError::new(
            ValidationErrorCategory::InvalidSignature,
            format!("worker export {name:?} has an invalid function signature"),
        ));
    }
    Ok(FunctionContract {
        export_name: name.to_string(),
        params,
        results,
    })
}

fn function_contract_types(
    function_type: &FuncType,
) -> Result<(Vec<ValueType>, Vec<ValueType>), ValidationError> {
    let params = function_type
        .params()
        .iter()
        .map(value_type)
        .collect::<Result<Vec<_>, _>>()?;
    let results = function_type
        .results()
        .iter()
        .map(value_type)
        .collect::<Result<Vec<_>, _>>()?;
    Ok((params, results))
}

fn value_type(value: &ValType) -> Result<ValueType, ValidationError> {
    match value {
        ValType::I32 => Ok(ValueType::I32),
        ValType::I64 => Ok(ValueType::I64),
        _ => Err(ValidationError::new(
            ValidationErrorCategory::InvalidSignature,
            format!("worker ABI function uses unsupported value type {value}"),
        )),
    }
}

fn invalid_module(message: impl Into<String>) -> ValidationError {
    ValidationError::new(ValidationErrorCategory::InvalidModule, message)
}

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

    #[test]
    fn validates_complete_worker_contract() {
        let module = wat::parse_str(valid_worker_wat()).expect("compile valid worker");
        let validated = validate_worker_module(&module).expect("validate worker module");
        assert_eq!(validated.byte_len, module.len());
        assert_eq!(validated.memory.initial_pages, 1);
        assert_eq!(validated.alloc_export.export_name, EXPORT_WORKER_ALLOC);
        assert_eq!(validated.dealloc_export.export_name, EXPORT_WORKER_DEALLOC);
        assert_eq!(validated.invoke_export.export_name, EXPORT_WORKER_INVOKE);
        assert_eq!(validated.imports.len(), 2);
    }

    #[test]
    fn rejects_missing_memory_export() {
        let module = wat::parse_str(valid_worker_wat().replace("(export \"memory\")", ""))
            .expect("compile worker");
        let error = validate_worker_module(&module).expect_err("missing memory export");
        assert_eq!(error.category(), ValidationErrorCategory::MissingExport);
    }

    #[test]
    fn rejects_invalid_required_export_signatures() {
        for (name, replacement) in [
            (
                "alloc",
                "(func (export \"redevplugin_worker_alloc\") (param i64) (result i32) i32.const 0)",
            ),
            (
                "dealloc",
                "(func (export \"redevplugin_worker_dealloc\") (param i32))",
            ),
            (
                "invoke",
                "(func (export \"redevplugin_worker_invoke\") (param i32 i32) (result i32) i32.const 0)",
            ),
        ] {
            let source = valid_worker_wat();
            let invalid = match name {
                "alloc" => source.replace(
                    "(func (export \"redevplugin_worker_alloc\") (param i32) (result i32) i32.const 0)",
                    replacement,
                ),
                "dealloc" => source.replace(
                    "(func (export \"redevplugin_worker_dealloc\") (param i32 i32))",
                    replacement,
                ),
                _ => source.replace(
                    "(func (export \"redevplugin_worker_invoke\") (param i32 i32) (result i64) i64.const 0)",
                    replacement,
                ),
            };
            let module = wat::parse_str(invalid).expect("compile invalid-signature worker");
            let error = validate_worker_module(&module).expect_err(name);
            assert_eq!(error.category(), ValidationErrorCategory::InvalidSignature);
        }
    }

    #[test]
    fn rejects_unsupported_or_mistyped_imports() {
        for import in [
            "(import \"wasi_snapshot_preview1\" \"fd_write\" (func (param i32 i32 i32 i32) (result i32)))",
            "(import \"redevplugin.storage\" \"files\" (func (param i32) (result i32)))",
            "(import \"redevplugin.storage\" \"memory\" (memory 1))",
        ] {
            let source = valid_worker_wat().replace(
                "(import \"redevplugin.storage\" \"files\" (func (param i32 i32 i32 i32) (result i32)))",
                import,
            );
            let module = wat::parse_str(source).expect("compile worker with invalid import");
            assert!(
                validate_worker_module(&module).is_err(),
                "accepted {import}"
            );
        }
    }

    #[test]
    fn rejects_invalid_memory_and_table_contracts() {
        for replacement in [
            "(memory (export \"memory\") i64 1)",
            "(memory (export \"memory\") 1 2 shared)",
            "(memory (export \"memory\") 1) (memory 1)",
            "(memory (export \"memory\") 1) (table 65537 funcref)",
            "(memory (export \"memory\") 1) (table 1 funcref) (table 1 funcref)",
        ] {
            let source = valid_worker_wat().replace("(memory (export \"memory\") 1)", replacement);
            let module = wat::parse_str(source).expect("compile worker with invalid resources");
            assert!(
                validate_worker_module(&module).is_err(),
                "accepted {replacement}"
            );
        }
    }

    #[test]
    fn rejects_shared_invalid_opcode_fixture() {
        let module = decode_hex(include_str!("../testdata/wasm/invalid-final-opcode.hex"));
        let error = validate_worker_module(&module).expect_err("invalid opcode");
        assert_eq!(error.category(), ValidationErrorCategory::InvalidModule);
    }

    #[test]
    fn rejects_shared_table_maximum_fixture() {
        let module = decode_hex(include_str!(
            "../testdata/wasm/table-maximum-exceeds-limit.hex"
        ));
        let error = validate_worker_module(&module).expect_err("table maximum above limit");
        assert_eq!(error.category(), ValidationErrorCategory::InvalidTable);
    }

    fn valid_worker_wat() -> &'static str {
        r#"(module
            (import "redevplugin.storage" "files" (func (param i32 i32 i32 i32) (result i32)))
            (import "redevplugin.network" "execute" (func (param i32 i32 i32 i32) (result i32)))
            (memory (export "memory") 1)
            (func (export "redevplugin_worker_alloc") (param i32) (result i32) i32.const 0)
            (func (export "redevplugin_worker_dealloc") (param i32 i32))
            (func (export "redevplugin_worker_invoke") (param i32 i32) (result i64) i64.const 0)
        )"#
    }

    fn decode_hex(input: &str) -> Vec<u8> {
        let input = input.trim().as_bytes();
        assert_eq!(input.len() % 2, 0);
        input
            .chunks_exact(2)
            .map(|pair| {
                let text = std::str::from_utf8(pair).expect("hex fixture is UTF-8");
                u8::from_str_radix(text, 16).expect("hex fixture byte")
            })
            .collect()
    }
}