inspect_artifact/
inspect_artifact.rs1use ic_host_tools::{
4 artifact::{read_file, verify_reader},
5 wasm::{InspectionLimits, inspect},
6};
7use std::{error::Error, io, path::Path};
8
9fn main() -> Result<(), Box<dyn Error>> {
10 let mut args = std::env::args_os().skip(1);
11 let path = args.next().ok_or_else(|| invalid_argument("usage: inspect_artifact PATH MAX_BYTES MAX_SECTIONS MAX_EXPORTS MAX_CUSTOM_SECTIONS [SHA256]"))?;
12 let mut number = || -> Result<usize, Box<dyn Error>> {
13 Ok(args
14 .next()
15 .ok_or_else(|| invalid_argument("missing inspection limit"))?
16 .to_str()
17 .ok_or_else(|| invalid_argument("inspection limits must be UTF-8"))?
18 .parse()?)
19 };
20 let limits = InspectionLimits {
21 module_bytes: number()?,
22 sections: number()?,
23 exports: u32::try_from(number()?)?,
24 custom_sections: number()?,
25 };
26 let expected = args
27 .next()
28 .map(|value| {
29 value
30 .into_string()
31 .map_err(|_| invalid_argument("digest must be UTF-8"))
32 })
33 .transpose()?;
34 if args.next().is_some() {
35 return Err(invalid_argument("unexpected argument").into());
36 }
37 let bytes = read_file(Path::new(&path), limits.module_bytes)?;
38 let identity = if let Some(expected) = expected {
39 verify_reader(
40 bytes.as_slice(),
41 limits.module_bytes as u64,
42 expected.parse()?,
43 )?
44 } else {
45 ic_host_tools::artifact::hash_reader(bytes.as_slice(), limits.module_bytes as u64)?
46 };
47 let facts = inspect(&bytes, limits)?;
48 println!(
49 "raw_bytes={} sha256={} code_section_bytes={} data_section_bytes={} defined_functions={} data_segments={}",
50 identity.bytes,
51 identity.sha256,
52 facts.code_section_bytes,
53 facts.data_section_bytes,
54 facts.defined_functions,
55 facts.data_segments
56 );
57 for (name, export) in facts.exports {
58 println!(
59 "export={name:?} kind={:?} index={}",
60 export.kind, export.index
61 );
62 }
63 for section in facts.custom_sections {
64 println!(
65 "custom_section={:?} bytes={}",
66 section.name,
67 section.data.len()
68 );
69 }
70 Ok(())
71}
72
73fn invalid_argument(message: &'static str) -> io::Error {
74 io::Error::new(io::ErrorKind::InvalidInput, message)
75}