verify_component_adapter/
main.rs

1use anyhow::{bail, Result};
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                    }
41                }
42            }
43            Payload::TableSection(_) => {}
44            Payload::MemorySection(_) => {
45                bail!("preview1.wasm should import memory");
46            }
47            Payload::GlobalSection(_) => {}
48
49            Payload::ExportSection(_) => {}
50
51            Payload::FunctionSection(_) => {}
52
53            Payload::CodeSectionStart { .. } => {}
54            Payload::CodeSectionEntry(_) => {}
55            Payload::CustomSection(_) => {}
56
57            // sections that shouldn't appear in the specially-crafted core wasm
58            // adapter self we're processing
59            _ => {
60                bail!("unsupported section {payload:?} found in preview1.wasm")
61            }
62        }
63    }
64
65    Ok(())
66}