File: factc.rs

package info (click to toggle)
rust-wasmtime 26.0.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 48,492 kB
  • sloc: ansic: 4,003; sh: 561; javascript: 542; cpp: 254; asm: 175; ml: 96; makefile: 55
file content (198 lines) | stat: -rw-r--r-- 7,137 bytes parent folder | download
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
use anyhow::{bail, Context, Result};
use clap::Parser;
use std::io::{IsTerminal, Write};
use std::path::PathBuf;
use wasmparser::{Validator, WasmFeatures};
use wasmtime_environ::component::*;
use wasmtime_environ::fact::Module;

/// A small helper utility to explore generated adapter modules from Wasmtime's
/// adapter fusion compiler.
///
/// This utility takes a `*.wat` file as input which is expected to be a valid
/// WebAssembly component. The component is parsed and any type definition for a
/// component function gets a generated adapter for it as if the caller/callee
/// used that type as the adapter.
///
/// For example with an input that looks like:
///
///     (component
///         (type (func (param u32) (result (list u8))))
///     )
///
/// This tool can be used to generate an adapter for that signature.
#[derive(Parser)]
struct Factc {
    /// Whether or not debug code is inserted into the generated adapter.
    #[arg(long)]
    debug: bool,

    /// Whether or not the lifting options (the callee of the exported adapter)
    /// uses a 64-bit memory as opposed to a 32-bit memory.
    #[arg(long)]
    lift64: bool,

    /// Whether or not the lowering options (the caller of the exported adapter)
    /// uses a 64-bit memory as opposed to a 32-bit memory.
    #[arg(long)]
    lower64: bool,

    /// Whether or not a call to a `post-return` configured function is enabled
    /// or not.
    #[arg(long)]
    post_return: bool,

    /// Whether or not to skip validation of the generated adapter module.
    #[arg(long)]
    skip_validate: bool,

    /// Where to place the generated adapter module. Standard output is used if
    /// this is not specified.
    #[arg(short, long)]
    output: Option<PathBuf>,

    /// Output the text format for WebAssembly instead of the binary format.
    #[arg(short, long)]
    text: bool,

    #[arg(long, value_parser = parse_string_encoding, default_value = "utf8")]
    lift_str: StringEncoding,

    #[arg(long, value_parser = parse_string_encoding, default_value = "utf8")]
    lower_str: StringEncoding,

    /// TODO
    input: PathBuf,
}

fn parse_string_encoding(name: &str) -> Result<StringEncoding> {
    Ok(match name {
        "utf8" => StringEncoding::Utf8,
        "utf16" => StringEncoding::Utf16,
        "compact-utf16" => StringEncoding::CompactUtf16,
        other => bail!("invalid string encoding: `{other}`"),
    })
}

fn main() -> Result<()> {
    Factc::parse().execute()
}

impl Factc {
    fn execute(self) -> Result<()> {
        env_logger::init();

        // Manufactures a unique `CoreDef` so all function imports get unique
        // function imports.
        let mut next_def = 0;
        let mut dummy_def = || {
            next_def += 1;
            dfg::CoreDef::Adapter(dfg::AdapterId::from_u32(next_def))
        };

        // Manufactures a `CoreExport` for a memory with the shape specified. Note
        // that we can't import as many memories as functions so these are
        // intentionally limited. Once a handful of memories are generated of each
        // type then they start getting reused.
        let mut next_memory = 0;
        let mut memories32 = Vec::new();
        let mut memories64 = Vec::new();
        let mut dummy_memory = |memory64: bool| {
            let dst = if memory64 {
                &mut memories64
            } else {
                &mut memories32
            };
            let idx = if dst.len() < 5 {
                next_memory += 1;
                dst.push(next_memory - 1);
                next_memory - 1
            } else {
                dst[0]
            };
            dfg::CoreExport {
                instance: dfg::InstanceId::from_u32(idx),
                item: ExportItem::Name(String::new()),
            }
        };

        let mut validator = Validator::new();
        let mut types = ComponentTypesBuilder::new(&validator);

        let mut adapters = Vec::new();
        let input = wat::parse_file(&self.input)?;
        let wasm_types = validator
            .validate_all(&input)
            .context("failed to validate input wasm")?;
        let wasm_types = wasm_types.as_ref();
        for i in 0..wasm_types.component_type_count() {
            let ty = match wasm_types.component_any_type_at(i) {
                wasmparser::types::ComponentAnyTypeId::Func(id) => id,
                _ => continue,
            };
            let ty = types.convert_component_func_type(wasm_types, ty)?;
            adapters.push(Adapter {
                lift_ty: ty,
                lower_ty: ty,
                lower_options: AdapterOptions {
                    instance: RuntimeComponentInstanceIndex::from_u32(0),
                    string_encoding: self.lower_str,
                    memory64: self.lower64,
                    // Pessimistically assume that memory/realloc are going to be
                    // required for this trampoline and provide it. Avoids doing
                    // calculations to figure out whether they're necessary and
                    // simplifies the fuzzer here without reducing coverage within FACT
                    // itself.
                    memory: Some(dummy_memory(self.lower64)),
                    realloc: Some(dummy_def()),
                    // Lowering never allows `post-return`
                    post_return: None,
                },
                lift_options: AdapterOptions {
                    instance: RuntimeComponentInstanceIndex::from_u32(1),
                    string_encoding: self.lift_str,
                    memory64: self.lift64,
                    memory: Some(dummy_memory(self.lift64)),
                    realloc: Some(dummy_def()),
                    post_return: if self.post_return {
                        Some(dummy_def())
                    } else {
                        None
                    },
                },
                func: dummy_def(),
            });
        }

        let mut fact_module = Module::new(&types, self.debug);
        for (i, adapter) in adapters.iter().enumerate() {
            fact_module.adapt(&format!("adapter{i}"), adapter);
        }
        let wasm = fact_module.encode();

        let output = if self.text {
            wasmprinter::print_bytes(&wasm)
                .context("failed to convert binary wasm to text")?
                .into_bytes()
        } else if self.output.is_none() && std::io::stdout().is_terminal() {
            bail!("cannot print binary wasm output to a terminal unless `-t` flag is passed")
        } else {
            wasm.clone()
        };

        match &self.output {
            Some(file) => std::fs::write(file, output).context("failed to write output file")?,
            None => std::io::stdout()
                .write_all(&output)
                .context("failed to write to stdout")?,
        }

        if !self.skip_validate {
            Validator::new_with_features(WasmFeatures::default() | WasmFeatures::MEMORY64)
                .validate_all(&wasm)
                .context("failed to validate generated module")?;
        }

        Ok(())
    }
}