verify_component_adapter/
main.rs

1use anyhow::{Result, bail};
2use std::env;
3use wasmparser::*;
4
5fn main() -> Result<()> {
6    let file = env::args()
7        .nth(1)
8        .expect("must pass wasm file as an argument");
9    let wasm = wat::parse_file(&file)?;
10
11    let mut validator = Validator::new();
12    for payload in Parser::new(0).parse_all(&wasm) {
13        let payload = payload?;
14        validator.payload(&payload)?;
15        match payload {
16            Payload::Version { encoding, .. } => {
17                if encoding != Encoding::Module {
18                    bail!("adapter must be a core wasm module, not a component");
19                }
20            }
21            Payload::End(_) => {}
22            Payload::TypeSection(_) => {}
23            Payload::ImportSection(s) => {
24                for i in s {
25                    let i = i?;
26                    match i.ty {
27                        TypeRef::Func(_) => {
28                            if i.module.starts_with("wasi:") {
29                                continue;
30                            }
31                            if i.module == "__main_module__" {
32                                continue;
33                            }
34                            bail!("import from unknown module `{}`", i.module);
35                        }
36                        TypeRef::Table(_) => bail!("should not import table"),
37                        TypeRef::Global(_) => bail!("should not import globals"),
38                        TypeRef::Memory(_) => {}
39                        TypeRef::Tag(_) => bail!("unsupported `tag` type"),
40                        TypeRef::FuncExact(_) => bail!("unsupported exact `func` type"),
41                    }
42                }
43            }
44            Payload::TableSection(_) => {}
45            Payload::MemorySection(_) => {
46                bail!("preview1.wasm should import memory");
47            }
48            Payload::GlobalSection(_) => {}
49
50            Payload::ExportSection(_) => {}
51
52            Payload::FunctionSection(_) => {}
53
54            Payload::CodeSectionStart { .. } => {}
55            Payload::CodeSectionEntry(_) => {}
56            Payload::CustomSection(_) => {}
57
58            // sections that shouldn't appear in the specially-crafted core wasm
59            // adapter self we're processing
60            _ => {
61                bail!("unsupported section {payload:?} found in preview1.wasm")
62            }
63        }
64    }
65
66    Ok(())
67}