forked from bytecodealliance/wasm-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
411 lines (372 loc) · 13.4 KB
/
Copy pathlib.rs
File metadata and controls
411 lines (372 loc) · 13.4 KB
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
//! Shared input/output routines amongst most `wasm-tools` subcommands
use anyhow::{Context, Result, bail};
use std::fs::File;
use std::io::IsTerminal;
use std::io::{BufWriter, Read, Write};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use termcolor::{Ansi, ColorChoice, NoColor, StandardStream, WriteColor};
#[cfg(any(feature = "addr2line", feature = "validate"))]
pub mod addr2line;
#[cfg(any(feature = "component", feature = "wit-dylib"))]
pub mod wit;
#[derive(clap::Parser)]
pub struct GeneralOpts {
/// Use verbose output (-v info, -vv debug, -vvv trace).
#[clap(long = "verbose", short = 'v', action = clap::ArgAction::Count)]
verbose: u8,
/// Configuration over whether terminal colors are used in output.
///
/// Supports one of `auto|never|always|always-ansi`. The default is to
/// detect what to do based on the terminal environment, for example by
/// using `isatty`.
#[clap(long = "color", default_value = "auto")]
pub color: ColorChoice,
}
impl GeneralOpts {
/// Initializes the logger based on the verbosity level.
pub fn init_logger(&self) {
let default = match self.verbose {
0 => "warn",
1 => "info",
2 => "debug",
_ => "trace",
};
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or(default))
.format_target(false)
.init();
}
}
// This is intended to be included in a struct as:
//
// #[clap(flatten)]
// io: wasm_tools::InputOutput,
//
// and then the methods are used to read the arguments,
#[derive(clap::Parser)]
pub struct InputOutput {
#[clap(flatten)]
input: InputArg,
#[clap(flatten)]
output: OutputArg,
#[clap(flatten)]
general: GeneralOpts,
}
fn parse_optionally_name_file(s: &str) -> (&str, &str) {
let mut parts = s.splitn(2, '=');
let name_or_path = parts.next().unwrap();
match parts.next() {
Some(path) => (name_or_path, path),
None => {
let name = Path::new(name_or_path)
.file_name()
.unwrap()
.to_str()
.unwrap();
let name = match name.find('.') {
Some(i) => &name[..i],
None => name,
};
(name, name_or_path)
}
}
}
fn parse_adapter(s: &str) -> Result<(String, Vec<u8>)> {
let (name, path) = parse_optionally_name_file(s);
let wasm = wat::parse_file(path)?;
Ok((name.to_string(), wasm))
}
#[derive(clap::Parser)]
pub struct AdaptersArg {
/// The path to an adapter module to satisfy imports not otherwise bound to
/// WIT interfaces.
///
/// An adapter module can be used to translate the `wasi_snapshot_preview1`
/// ABI, for example, to one that uses the component model. The first
/// `[NAME=]` specified in the argument is inferred from the name of file
/// specified by `MODULE` if not present and is the name of the import
/// module that's being implemented (e.g. `wasi_snapshot_preview1.wasm`).
///
/// The second part of this argument is the path to the adapter module.
#[clap(long = "adapt", value_name = "[NAME=]MODULE", value_parser = parse_adapter)]
pub adapters: Vec<(String, Vec<u8>)>,
}
#[derive(clap::Parser)]
pub struct GenerateDwarfArg {
/// Optionally generate DWARF debugging information from WebAssembly text
/// files.
///
/// When the input to this command is a WebAssembly text file, such as
/// `*.wat`, then this option will instruct the text parser to insert DWARF
/// debugging information to map binary locations back to the original
/// source locations in the input `*.wat` file. This option has no effect if
/// the `INPUT` argument is already a WebAssembly binary or if the text
/// format uses `(module binary ...)`.
#[clap(
long,
value_name = "lines|full",
conflicts_with = "generate_full_dwarf"
)]
generate_dwarf: Option<GenerateDwarf>,
/// Shorthand for `--generate-dwarf full`
#[clap(short, conflicts_with = "generate_dwarf")]
generate_full_dwarf: bool,
}
#[derive(clap::Parser)]
pub struct InputArg {
/// Input file to process.
///
/// If not provided or if this is `-` then stdin is read entirely and
/// processed. Note that for most subcommands this input can either be a
/// binary `*.wasm` file or a textual format `*.wat` file.
input: Option<PathBuf>,
}
#[derive(Copy, Clone)]
enum GenerateDwarf {
Lines,
Full,
}
impl FromStr for GenerateDwarf {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<GenerateDwarf> {
match s {
"lines" => Ok(GenerateDwarf::Lines),
"full" => Ok(GenerateDwarf::Full),
other => bail!("unknown `--generate-dwarf` setting: {other}"),
}
}
}
impl InputArg {
pub fn get_binary_wasm(
&self,
generate_dwarf_optional: Option<&GenerateDwarfArg>,
) -> Result<Vec<u8>> {
let mut parser = wat::Parser::new();
match generate_dwarf_optional {
None => {}
Some(generate_dwarf) => match (
generate_dwarf.generate_full_dwarf,
generate_dwarf.generate_dwarf,
) {
(false, Some(GenerateDwarf::Lines)) => {
parser.generate_dwarf(wat::GenerateDwarf::Lines);
}
(true, _) | (false, Some(GenerateDwarf::Full)) => {
parser.generate_dwarf(wat::GenerateDwarf::Full);
}
(false, None) => {}
},
}
if let Some(path) = &self.input {
if path != Path::new("-") {
let bytes = parser.parse_file(path)?;
return Ok(bytes);
}
}
let mut stdin = Vec::new();
std::io::stdin()
.read_to_end(&mut stdin)
.context("failed to read <stdin>")?;
let bytes = parser.parse_bytes(Some("<stdin>".as_ref()), &stdin)?;
Ok(bytes.into_owned())
}
}
#[derive(clap::Parser)]
pub struct OutputArg {
/// Where to place output.
///
/// Required when printing WebAssembly binary output.
///
/// If not provided, then stdout is used.
#[clap(short, long)]
output: Option<PathBuf>,
}
pub enum Output<'a> {
#[cfg(feature = "component")]
Wit {
wit: &'a wit_component::DecodedWasm,
printer: wit_component::WitPrinter,
},
Wasm(&'a [u8]),
Wat {
wasm: &'a [u8],
config: wasmprinter::Config,
},
Json(&'a str),
}
impl InputOutput {
pub fn parse_input_wasm(&self, generate_dwarf: Option<&GenerateDwarfArg>) -> Result<Vec<u8>> {
let ret = self.get_input_wasm(generate_dwarf)?;
parse_binary_wasm(wasmparser::Parser::new(0), &ret)?;
Ok(ret)
}
pub fn get_input_wasm(&self, generate_dwarf: Option<&GenerateDwarfArg>) -> Result<Vec<u8>> {
self.input.get_binary_wasm(generate_dwarf)
}
pub fn output_wasm(&self, wasm: &[u8], wat: bool) -> Result<()> {
if wat {
self.output(Output::Wat {
wasm,
config: Default::default(),
})
} else {
self.output(Output::Wasm(wasm))
}
}
pub fn output(&self, bytes: Output<'_>) -> Result<()> {
self.output.output(&self.general, bytes)
}
pub fn output_writer(&self) -> Result<Box<dyn WriteColor>> {
self.output.output_writer(self.general.color)
}
pub fn output_path(&self) -> Option<&Path> {
self.output.output.as_deref()
}
pub fn input_path(&self) -> Option<&Path> {
self.input.input.as_deref()
}
pub fn general_opts(&self) -> &GeneralOpts {
&self.general
}
}
impl OutputArg {
pub fn output_wasm(&self, general: &GeneralOpts, wasm: &[u8], wat: bool) -> Result<()> {
if wat {
self.output(
general,
Output::Wat {
wasm,
config: Default::default(),
},
)
} else {
self.output(general, Output::Wasm(wasm))
}
}
pub fn output(&self, general: &GeneralOpts, output: Output<'_>) -> Result<()> {
match output {
Output::Wat { wasm, config } => {
let mut writer = self.output_writer(general.color)?;
config.print(wasm, &mut wasmprinter::PrintTermcolor(&mut writer))
}
Output::Wasm(bytes) => {
match &self.output {
Some(path) => {
std::fs::write(path, bytes)
.context(format!("failed to write `{}`", path.display()))?;
}
None => {
let mut stdout = std::io::stdout();
if stdout.is_terminal() {
bail!(
"cannot print binary wasm output to a terminal, pass the `-t` flag to print the text format"
);
}
stdout
.write_all(bytes)
.context("failed to write to stdout")?;
}
}
Ok(())
}
Output::Json(s) => self.output_str(s),
#[cfg(feature = "component")]
Output::Wit { wit, mut printer } => {
let resolve = wit.resolve();
let ids = resolve
.packages
.iter()
.map(|(id, _)| id)
.filter(|id| *id != wit.package())
.collect::<Vec<_>>();
printer.print(resolve, wit.package(), &ids)?;
let output = printer.output.to_string();
self.output_str(&output)
}
}
}
fn output_str(&self, output: &str) -> Result<()> {
match &self.output {
Some(path) => {
std::fs::write(path, output)
.context(format!("failed to write `{}`", path.display()))?;
}
None => std::io::stdout()
.write_all(output.as_bytes())
.context("failed to write to stdout")?,
}
Ok(())
}
pub fn output_path(&self) -> Option<&Path> {
self.output.as_deref()
}
pub fn output_writer(&self, color: ColorChoice) -> Result<Box<dyn WriteColor>> {
match &self.output {
Some(output) => {
let writer = BufWriter::new(File::create(&output)?);
if color == ColorChoice::AlwaysAnsi {
Ok(Box::new(Ansi::new(writer)))
} else {
Ok(Box::new(NoColor::new(writer)))
}
}
None => {
let stdout = std::io::stdout();
if color == ColorChoice::Auto && !stdout.is_terminal() {
Ok(Box::new(StandardStream::stdout(ColorChoice::Never)))
} else {
Ok(Box::new(StandardStream::stdout(color)))
}
}
}
}
}
pub fn parse_binary_wasm(parser: wasmparser::Parser, bytes: &[u8]) -> Result<()> {
for payload in parser.parse_all(&bytes) {
match payload? {
wasmparser::Payload::TypeSection(s) => parse_section(s)?,
wasmparser::Payload::ImportSection(s) => parse_section(s)?,
wasmparser::Payload::FunctionSection(s) => parse_section(s)?,
wasmparser::Payload::TableSection(s) => parse_section(s)?,
wasmparser::Payload::MemorySection(s) => parse_section(s)?,
wasmparser::Payload::TagSection(s) => parse_section(s)?,
wasmparser::Payload::GlobalSection(s) => parse_section(s)?,
wasmparser::Payload::ExportSection(s) => parse_section(s)?,
wasmparser::Payload::ElementSection(s) => parse_section(s)?,
wasmparser::Payload::DataSection(s) => parse_section(s)?,
wasmparser::Payload::CodeSectionEntry(body) => {
let mut locals = body.get_locals_reader()?.into_iter();
for item in locals.by_ref() {
let _ = item?;
}
let mut ops = locals.into_operators_reader();
while !ops.eof() {
ops.read()?;
}
ops.finish()?;
}
wasmparser::Payload::InstanceSection(s) => parse_section(s)?,
wasmparser::Payload::CoreTypeSection(s) => parse_section(s)?,
wasmparser::Payload::ComponentInstanceSection(s) => parse_section(s)?,
wasmparser::Payload::ComponentAliasSection(s) => parse_section(s)?,
wasmparser::Payload::ComponentTypeSection(s) => parse_section(s)?,
wasmparser::Payload::ComponentCanonicalSection(s) => parse_section(s)?,
wasmparser::Payload::ComponentImportSection(s) => parse_section(s)?,
wasmparser::Payload::ComponentExportSection(s) => parse_section(s)?,
wasmparser::Payload::UnknownSection { id, .. } => {
bail!("malformed section id: {}", id)
}
_ => (),
}
}
return Ok(());
fn parse_section<'a, T>(s: wasmparser::SectionLimited<'a, T>) -> Result<()>
where
T: wasmparser::FromReader<'a>,
{
for item in s {
let _ = item?;
}
Ok(())
}
}