forked from ariel-os/trevm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprecompile_wasm.rs
More file actions
executable file
·299 lines (242 loc) · 9.79 KB
/
precompile_wasm.rs
File metadata and controls
executable file
·299 lines (242 loc) · 9.79 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
#!/usr/bin/env -S cargo +nightly -Zscript
---cargo
[package]
edition = "2024"
[dependencies]
clap = { version = "4.5.40", features = ["derive"]}
wasmtime = { git = "https://github.com/bytecodealliance/wasmtime", rev = "3dc6b5ec5572ab8b304c668d5b929bbc7f49cbcf", default-features = false, features = ["component-model", "async", "cranelift", "pulley"] }
miette = { version = "7.2.0", features = ["fancy"] }
thiserror = { version = "2.0.12" }
---
#![feature(trim_prefix_suffix)]
use std::{fs, io, path::{PathBuf, Path}, process::Command};
use std::io::BufRead as _;
use std::format;
use clap::{Parser, ValueEnum, builder::PossibleValue};
use miette::Diagnostic;
use wasmtime::{Config, Engine, OptLevel};
#[derive(Clone, Copy, Debug)]
enum CLIOptLevel {
Three,
S,
Z,
}
impl ValueEnum for CLIOptLevel {
fn value_variants<'a>() -> &'a [Self] {
&[Self::S, Self::Three, Self::Z]
}
fn to_possible_value(&self) -> Option<PossibleValue> {
match self {
Self::Three => Some(PossibleValue::new("3")),
Self::S => Some(PossibleValue::new("s")),
Self::Z => Some(PossibleValue::new("z")),
}
}
}
/// Simple CLI that takes a compiled .wasm file and turns into a precompiled-component
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Path to the compiled Wasm file or to a manifest for a lib that will be compiled
#[arg(short, long)]
path: PathBuf,
/// Path to an optional `config.toml` file when `path` points to a manifest
#[arg(long)]
config: Option<PathBuf>,
/// Toolchain for the compilation when `path` points to a manifest
#[arg(short, long)]
toolchain: Option<String>,
/// Additionnal compilation options propagated to cargo
#[arg(long = "compile_options", short = 'C')]
additional: Vec<String>,
#[arg(long, default_value = "pulley32")]
target: String,
/// Turn fuel instrumentation on
#[arg(short, long)]
fuel: bool,
/// Override default opt-level
#[arg(short = 'O', long = "opt-level", value_enum, default_value_t = CLIOptLevel::S)]
opt_level: CLIOptLevel,
/// Path of the output file
#[arg(short, long)]
output: Option<PathBuf>,
/// Path to wasm-tools if isn't not in $PATH
#[arg(short, long)]
wasm_tools: Option<PathBuf>,
/// Outputs a Module instead of a component
#[arg(short, long)]
module: bool,
/// Conserve the intermediate files under `ouput.wasm` and `output.comp.wasm`
#[arg(long = "conserve-artifacts", short = 'a')]
conserve: bool
}
#[derive(Debug, thiserror::Error, Diagnostic)]
enum Error {
#[error("I/O error: {0}")]
Io(#[from] io::Error),
#[error("Precompilation Error: {0}")]
Precomp(#[from] wasmtime::Error),
}
fn main() -> miette::Result<()> {
let args = Args::parse();
let Args { path, config, toolchain, additional, fuel, output, wasm_tools, module, opt_level, target, conserve } = args;
// Check that the path exists
assert!(fs::exists(&path).map_err(Error::from)?);
let new_path = match path.extension().map(std::ffi::OsStr::to_str).flatten() {
// If it's a already a compiled wasm do nothing
Some("wasm") => {
path
},
// Means that it's a directory
Some("toml") => {
std::println!("Compiling Library");
let manifest = io::BufReader::new(fs::File::open(&path).map_err(Error::from)?);
let name = manifest.lines().into_iter().filter_map(
|line| {
match line {
Ok(l) => {
if l.starts_with("name =") {
Some(l.split("=").nth(1).unwrap().split("\"").nth(1).unwrap().to_owned().replace('-', "_"))
}
else {
None
}
}
Err(_) => None
}
}
).next().unwrap();
let mut args: Vec<&str> = match toolchain {
Some(toolchain) => {
if toolchain.starts_with("+") {
vec![toolchain.leak()]
}
else {
vec![format!("+{}", toolchain).leak()]
}
}
None => {
// Use nightly by default since it's needed for --page-size=1
vec!["+nightly"]
}
};
args.extend(["rustc", "--release", "--manifest-path", path.as_path().to_str().unwrap()]);
if let Some(config_path) = config {
match config_path.extension().map(std::ffi::OsStr::to_str).flatten() {
Some("toml") => {
args.push("--config");
args.push(config_path.to_string_lossy().into_owned().leak());
}
_ => Err(io::Error::new(io::ErrorKind::InvalidInput, "--config should point to a cargo config file")).map_err(Error::from)?,
}
}
args.extend(additional.iter().map(String::as_str));
args.extend(["--target-dir", "temp"]);
let opt_string = match opt_level {
CLIOptLevel::Three => "-Copt-level=3",
CLIOptLevel::S => "-Copt-level=s",
CLIOptLevel::Z => "-Copt-level=z",
};
args.extend(["--", opt_string]);
std::println!("{:?}", args);
let output = Command::new("cargo")
.args(&args)
.output()
.map_err(Error::from)?;
let std::process::Output {status, stdout: _, stderr} = output;
if !status.success() {
std::println!(
"{}", String::from_utf8_lossy(&stderr)
);
return Err(io::Error::new(io::ErrorKind::InvalidInput, "Module compilation failed")).map_err(Error::from)?;
}
format!("temp/wasm32v1-none/release/{name}.wasm").into()
},
_ => {
Err(io::Error::new(io::ErrorKind::InvalidInput, "--path should be wasm file or a path to a Cargo.toml manifest")).map_err(Error::from)?
}
};
if !module {
// Turn into a component
std::println!("Turning the Module into a component");
let wasm_tools_path = if wasm_tools.is_some() {
wasm_tools.unwrap()
} else {
"wasm-tools".into()
};
// Note: On Unix, non UTF8 strings are invalid Paths so the unwrap() is infallible
let output = std::process::Command::new(wasm_tools_path)
.args(["component", "new", new_path.as_path().to_str().unwrap(), "-o", "temp.wasm"])
.output()
.map_err(Error::from)?;
let std::process::Output {status, stdout: _, stderr} = output;
if !status.success() {
std::println!(
"{}", String::from_utf8_lossy(&stderr)
);
return Err(io::Error::new(io::ErrorKind::InvalidInput, "Componentization failed")).map_err(Error::from)?;
}
}
let out = if output.is_some() {
output.unwrap()
} else {
String::from("payload.cwasm").into()
};
if conserve {
// Derive the artifacts names from output
let mut art_path = PathBuf::from(out.clone());
// initial.wasm
art_path.set_extension("wasm");
std::fs::copy(&new_path, &art_path).map_err(Error::from)?;
if !module {
art_path.set_extension("comp.wasm");
std::fs::copy("temp.wasm", &art_path).map_err(Error::from)?;
}
}
if !module {
precompile("temp.wasm", &target, fuel, out, module)?;
std::fs::remove_file("temp.wasm").map_err(Error::from)?;
} else {
precompile(&new_path, &target, fuel, out, module)?;
}
if std::fs::exists("temp").map_err(Error::from)? {
std::fs::remove_dir_all("temp").map_err(Error::from)?;
}
Ok(())
}
fn precompile<P: AsRef<Path>>(path: P, target: &str, fuel: bool, out: PathBuf, module: bool) -> miette::Result<()> {
std::println!("Precompiling Wasm Module/Component");
let mut config = Config::new();
// Options found to reduce the output code size the most at least for components
config.memory_init_cow(false);
config.generate_address_map(false);
config.table_lazy_init(false);
config.cranelift_opt_level(OptLevel::Speed);
config.wasm_custom_page_sizes(true);
config.target(target).map_err(Error::from)?;
// 0 means limiting ourselves to what the module asked
// This needs to be set at pre-compile time and at runtime in the engine
config.memory_reservation(0);
// Disabling this allows runtime optimizations but means that the maximum memory
// that the module can have is
// S = min(initial_memory, memory_reservation) + memory_reserver_for_growth
// since it can grow by reallocating.
config.memory_may_move(false);
// Enable fuel intstrumentation to prevent malevolent code from running indefinitely in the VM
config.consume_fuel(fuel);
if module { // Ensures that the runtime using this doesn't try to use the component Model
config.wasm_component_model(false);
}
// Create an `Engine` with that configuration.
let engine = Engine::new(&config).map_err(Error::from)?;
std::println!("Reading Module/Component File");
let wasm = fs::read(path).map_err(Error::from)?;
let precompiled = if !module {
engine.precompile_component(&wasm).map_err(Error::from)?
} else {
engine.precompile_module(&wasm).map_err(Error::from)?
};
std::println!("Writing the precompiled file");
fs::write(out, &precompiled).map_err(Error::from)?;
Ok(())
}