use std::{collections::HashMap, fs, io::Read as _, str::FromStr};
use camino::Utf8PathBuf;
use compact_str::{CompactString, ToCompactString as _};
use eyre::{Context as _, ContextCompat};
use itertools::Itertools;
use crate::{
args::{self, TargetMemoryType},
compile,
config_checker::TomlRestorers,
unique_name::{MemoryUniqueName, UniqueName},
util::{CaminoUtilModule as _, WasmName, WasmNameHolder},
};
#[derive(Debug, Clone)]
pub struct GeneratorCtx {
pub vfs_name: WasmName,
pub target_names: Box<[WasmName]>,
pub target_names_with_self: Box<[WasmName]>,
pub target_memory_type: TargetMemoryType,
pub unstable_print_debug: bool,
pub dwarf: bool,
pub threads: bool,
pub adjust_abi: bool,
pub keep_build_artifacts: bool,
pub vfs_is_library: bool,
pub wrap_unreachable_targets: std::collections::HashSet<String>,
pub main_void_synthesized_targets: Option<std::collections::HashSet<String>>,
pub own_memory: bool,
pub detect_deadlock: bool,
pub stack_config: StackConfig,
pub validate: bool,
}
#[derive(Debug, Clone, Default)]
pub struct StackConfig {
pub sizes: HashMap<String, u32>,
pub slots: HashMap<String, u32>,
}
impl StackConfig {
fn from_options(options: args::StackOptions) -> eyre::Result<Self> {
let mut config = Self::default();
for size in options.sizes {
if config
.sizes
.insert(size.module.clone(), size.bytes)
.is_some()
{
eyre::bail!("duplicate --stack-size for module `{}`", size.module);
}
}
for slots in options.slots {
if config
.slots
.insert(slots.module.clone(), slots.slots)
.is_some()
{
eyre::bail!("duplicate --stack-slots for module `{}`", slots.module);
}
}
Ok(config)
}
pub fn vfs_size(&self, vfs_name: &str) -> Option<u32> {
self.sizes
.get(vfs_name)
.or_else(|| self.sizes.get("vfs"))
.copied()
}
pub fn target_size(&self, target_name: &str) -> Option<u32> {
self.sizes.get(target_name).copied()
}
}
#[derive(Debug, Default)]
pub struct ComponentCtx {
pub vfs_name: Option<compact_str::CompactString>,
pub target_names: Option<Box<[compact_str::CompactString]>>,
pub target_memory_type: Option<args::TargetMemoryType>,
pub unstable_print_debug: Option<bool>,
pub dwarf: bool,
pub threads: Option<bool>,
pub adjust_abi: bool,
pub own_memory: bool,
pub detect_deadlock: bool,
}
impl ComponentCtx {
pub fn new(
vfs_name: compact_str::CompactString,
target_names: Box<[compact_str::CompactString]>,
target_memory_type: TargetMemoryType,
unstable_print_debug: bool,
dwarf: bool,
threads: bool,
adjust_abi: bool,
own_memory: bool,
detect_deadlock: bool,
) -> Self {
Self {
vfs_name: Some(vfs_name),
target_names: Some(target_names),
target_memory_type: Some(target_memory_type),
unstable_print_debug: Some(unstable_print_debug),
dwarf,
threads: Some(threads),
adjust_abi,
own_memory,
detect_deadlock,
}
}
pub fn vfs_name(&self) -> &compact_str::CompactString {
self.vfs_name.as_ref().unwrap()
}
pub fn target_names(&self) -> &Box<[compact_str::CompactString]> {
self.target_names.as_ref().unwrap()
}
pub fn target_memory_type(&self) -> TargetMemoryType {
self.target_memory_type.unwrap()
}
pub fn unstable_print_debug(&self) -> bool {
self.unstable_print_debug.unwrap()
}
pub fn dwarf(&self) -> bool {
self.dwarf
}
pub fn threads(&self) -> bool {
self.threads.unwrap()
}
}
#[derive(Debug)]
pub struct GeneratorRunner {
pub ctx: GeneratorCtx,
pub path: WasmPath,
pub targets: Box<[WasmPath]>,
pub vfs_build_opts: args::VfsBuildOptions,
pub target_vfs_build_opts: Box<[args::VfsBuildOptions]>,
pub toml_restorers: Option<TomlRestorers>,
pub memory_hint: HashMap<WasmName, usize>,
pub wasm_name_holder: WasmNameHolder,
}
#[derive(Debug)]
pub struct ComponentRunner {
pub ctx: Option<ComponentCtx>,
pub path: WasmPath,
pub wasm_name_holder: Option<WasmNameHolder>,
}
pub(crate) trait WrapRunner<T> {
#[allow(unused_variables)]
fn wrap_run(
self,
path: &mut WasmPath,
dwarf: bool,
keep_build_artifacts: bool,
stream_pipeline: Option<crate::wasm_stream::pipeline::Pipeline>,
validate: bool,
) -> eyre::Result<T>
where
Self: Sized;
}
impl<T, F: FnOnce() -> eyre::Result<T>> WrapRunner<T> for F {
fn wrap_run(
self,
path: &mut WasmPath,
_dwarf: bool,
keep_build_artifacts: bool,
mut stream_pipeline: Option<crate::wasm_stream::pipeline::Pipeline>,
validate: bool,
) -> eyre::Result<T> {
let old_path = path.path()?.clone();
let result = (self)()?;
if let Some(pipeline) = &mut stream_pipeline {
let input_wasm = fs::read(&old_path).wrap_err("Failed to read Wasm file")?;
let output_wasm = pipeline
.run(&input_wasm)
.wrap_err("Failed to run StreamPipeline pre-walrus")?;
let new_path = old_path.with_extension("adjusted.wasm");
if fs::metadata(&new_path).is_ok() {
fs::remove_file(&new_path)
.wrap_err_with(|| format!("Failed to remove existing file {new_path}"))?;
}
std::fs::write(&new_path, &output_wasm)
.wrap_err_with(|| format!("Failed to write adjusted Wasm to {new_path}"))?;
if validate {
wasmparser::Validator::new_with_features(wasmparser::WasmFeatures::all())
.validate_all(&output_wasm)
.wrap_err_with(|| format!("Wasm validation failed for {}", new_path))?;
}
if !keep_build_artifacts && !path.is_original(&old_path) {
std::fs::remove_file(&old_path).unwrap_or_else(|e| {
log::warn!("Failed to remove intermediate file {old_path}: {e}")
});
}
path.set_path(new_path)?;
}
Ok(result)
}
}
pub(crate) trait EndWithOpt<T> {
#[allow(unused_variables)]
fn with_opt(
self,
path: &mut WasmPath,
dwarf: bool,
keep_build_artifacts: bool,
skip_opt: bool,
validate: bool,
) -> eyre::Result<T>
where
Self: Sized;
#[allow(dead_code)]
fn with_opt_args(
self,
path: &mut WasmPath,
args: &[&str],
require_update: bool,
dwarf: bool,
keep_build_artifacts: bool,
skip_opt: bool,
validate: bool,
) -> eyre::Result<T>
where
Self: Sized;
}
impl<T, F: FnOnce(&mut WasmPath) -> eyre::Result<T>> EndWithOpt<T> for F {
fn with_opt(
self,
path: &mut WasmPath,
dwarf: bool,
keep_build_artifacts: bool,
skip_opt: bool,
validate: bool,
) -> eyre::Result<T>
where
Self: Sized,
{
let result = (self)(path).wrap_err("Failed to run with with_opt")?;
if skip_opt {
println!("Skipping Wasm optimization...");
return Ok(result);
}
println!("Optimizing Wasm...");
let old_path = path.path()?.clone();
let new_path = compile::optimize_wasm(&old_path, &[], false, dwarf)
.wrap_err("Failed to optimize Wasm")?;
if validate {
let optimized_wasm = std::fs::read(&new_path)
.wrap_err_with(|| format!("Failed to read optimized Wasm from {new_path}"))?;
wasmparser::Validator::new_with_features(wasmparser::WasmFeatures::all())
.validate_all(&optimized_wasm)
.wrap_err_with(|| format!("Wasm validation failed for {}", new_path))?;
}
if !keep_build_artifacts && old_path != new_path && !path.is_original(&old_path) {
std::fs::remove_file(&old_path)
.wrap_err_with(|| format!("Failed to remove existing file {old_path}"))?;
}
path.set_path(new_path)?;
Ok(result)
}
fn with_opt_args(
self,
path: &mut WasmPath,
args: &[&str],
require_update: bool,
dwarf: bool,
keep_build_artifacts: bool,
skip_opt: bool,
validate: bool,
) -> eyre::Result<T>
where
Self: Sized,
{
let result = (self)(path).wrap_err("Failed to run with with_opt_args")?;
if skip_opt {
println!("Skipping Wasm optimization...");
return Ok(result);
}
println!("Optimizing Wasm... with args: {}", args.iter().join(" "));
let old_path = path.path()?.clone();
let new_path = compile::optimize_wasm(&old_path, args, require_update, dwarf)
.wrap_err("Failed to optimize Wasm")?;
if validate {
let optimized_wasm = std::fs::read(&new_path)
.wrap_err_with(|| format!("Failed to read optimized Wasm from {new_path}"))?;
wasmparser::Validator::new_with_features(wasmparser::WasmFeatures::all())
.validate_all(&optimized_wasm)
.wrap_err_with(|| format!("Wasm validation failed for {}", new_path))?;
}
if !keep_build_artifacts && old_path != new_path && !path.is_original(&old_path) {
std::fs::remove_file(&old_path)
.wrap_err_with(|| format!("Failed to remove existing file {old_path}"))?;
}
path.set_path(new_path)?;
Ok(result)
}
}
impl GeneratorRunner {
pub fn new(
path: WasmPath,
targets: Box<[WasmPath]>,
threads: bool,
dwarf: bool,
unstable_print_debug: bool,
adjust_abi: bool,
keep_build_artifacts: bool,
memory_type: TargetMemoryType,
vfs_build_opts: args::VfsBuildOptions,
target_vfs_build_opts: Box<[args::VfsBuildOptions]>,
toml_restorers: TomlRestorers,
memory_hint: Box<[Option<usize>]>,
own_memory: bool,
detect_deadlock: bool,
stack_options: args::StackOptions,
validate: bool,
) -> eyre::Result<Self> {
let target_names_with_self = core::iter::once(Ok(path.name()?.to_compact_string()))
.chain(targets.iter().map(|t| Ok(t.name()?.to_compact_string())))
.collect::<eyre::Result<Box<_>>>()?;
let wasm_name_holder = WasmNameHolder::new(target_names_with_self);
let mut wasm_name_holder_iter = wasm_name_holder.iter();
let vfs_name = wasm_name_holder_iter
.next()
.ok_or_else(|| eyre::eyre!("Failed to get VFS name"))?;
let target_names = wasm_name_holder_iter.collect::<Box<_>>();
for name in target_names.iter() {
log::info!("Targeting module: {name}");
}
log::info!("VFS module: {vfs_name}");
let target_names_with_self = target_names
.iter()
.cloned()
.chain(core::iter::once(vfs_name.clone()))
.collect::<Box<_>>();
let memory_hint = memory_hint
.into_iter()
.zip(target_names.iter().cloned())
.filter_map(|(hint, name)| hint.map(|h| (name, h)))
.collect::<HashMap<_, _>>();
let stack_config = StackConfig::from_options(stack_options)?;
if stack_config.slots.contains_key("vfs")
|| stack_config.slots.contains_key(vfs_name.as_ref())
{
eyre::bail!("--stack-slots is not valid for the VFS module");
}
if memory_type == TargetMemoryType::Single && !stack_config.slots.is_empty() {
eyre::bail!("--stack-slots is only valid with multi-memory targets");
}
if memory_type == TargetMemoryType::Single
&& target_names
.iter()
.any(|name| stack_config.target_size(name.as_ref()).is_some())
&& stack_config.vfs_size(vfs_name.as_ref()).is_none()
{
eyre::bail!("single-memory target stack isolation requires --stack-size vfs=SIZE");
}
for module in stack_config.sizes.keys().chain(stack_config.slots.keys()) {
if module != "vfs"
&& module != vfs_name.as_ref()
&& !target_names.iter().any(|name| name.as_ref() == module)
{
eyre::bail!("stack configuration names unknown module `{module}`");
}
}
Ok(Self {
ctx: GeneratorCtx {
vfs_name,
target_names,
target_names_with_self,
target_memory_type: memory_type,
unstable_print_debug,
dwarf,
threads,
adjust_abi,
keep_build_artifacts,
vfs_is_library: false,
wrap_unreachable_targets: std::collections::HashSet::new(),
main_void_synthesized_targets: None,
own_memory,
detect_deadlock,
stack_config,
validate,
},
path,
targets,
vfs_build_opts,
target_vfs_build_opts,
toml_restorers: Some(toml_restorers),
memory_hint,
wasm_name_holder,
})
}
#[deprecated(
note = "Ensure this function is self-contained. This is a temporary measure for debugging purposes."
)]
pub const fn path(&self) -> &WasmPath {
&self.path
}
#[deprecated(
note = "Ensure this function is self-contained. This is a temporary measure for debugging purposes."
)]
pub const fn targets(&self) -> &Box<[WasmPath]> {
&self.targets
}
pub const fn ctx(&self) -> &GeneratorCtx {
&self.ctx
}
pub fn definitely(&mut self) -> eyre::Result<()> {
self.path
.definitely(self.ctx.threads, &self.vfs_build_opts)?;
for (i, target) in self.targets.iter_mut().enumerate() {
target.definitely(self.ctx.threads, &self.target_vfs_build_opts[i])?;
}
Ok(())
}
pub fn run_layers_to_component(
mut self,
out_dir: &Utf8PathBuf,
keep_build_artifacts: bool,
) -> eyre::Result<ComponentRunner> {
self.definitely()?;
let toml_restorers = self
.toml_restorers
.take()
.ok_or_else(|| eyre::eyre!("TomlRestorers already taken"))?;
toml_restorers
.restore()
.wrap_err("Failed to restore toml files")?;
let dwarf = self.ctx.dwarf;
let validate = self.ctx.validate;
println!("Remove existing output directory...");
if std::fs::metadata(&out_dir).is_ok() {
std::fs::remove_dir_all(&out_dir).expect("Failed to remove existing directory");
}
std::fs::create_dir_all(&out_dir).expect("Failed to create output directory");
println!("Adjusting VFS Wasm...");
let skip_vfs_opt = self.vfs_build_opts.no_opt > 0 || self.vfs_build_opts.no_opt_all > 0;
(|path: &mut WasmPath| {
let wasm_bytes = std::fs::read(&path.path()?).unwrap();
let mut vfs_is_library = true;
let mut wrap_unreachable_targets = std::collections::HashSet::new();
let mut detected_own_memory = self.ctx.own_memory;
for payload in wasmparser::Parser::new(0).parse_all(&wasm_bytes) {
if let Ok(wasmparser::Payload::StartSection { .. }) = payload {
vfs_is_library = false;
}
if let Ok(wasmparser::Payload::ExportSection(s)) = payload {
for e in s {
if let Ok(e) = e {
if e.name == "_start" {
vfs_is_library = false;
}
if crate::wasm_stream::own_memory_abi::is_own_memory_mode_export(e.name)
{
detected_own_memory = true;
}
for target in self.ctx.target_names.iter() {
let marker = format!(
"__wasip1_virt_layer_{}_wrap_unreachable",
target.as_ref()
);
if e.name == marker {
wrap_unreachable_targets.insert(target.as_ref().to_string());
}
}
}
}
}
}
self.ctx.vfs_is_library = vfs_is_library;
self.ctx.wrap_unreachable_targets = wrap_unreachable_targets;
self.ctx.own_memory = detected_own_memory;
let pipeline_is_library = vfs_is_library;
let cloned_ctx = self.ctx.clone();
(|| {
if pipeline_is_library {
log::info!(
"VFS module `{}` has no start section — treating as a library.",
self.ctx.vfs_name
);
}
Ok(())
})
.wrap_run(
path,
dwarf,
keep_build_artifacts,
{
let mut pipeline = crate::wasm_stream::pipeline::Pipeline::new();
use crate::wasm_stream::passes::{
abi_connect::{
ConnectWasip1ABIPreVfsStreamPass, NonRecursiveWasiABIPreVfsStreamPass,
},
anonymous::AnonymousStreamPass,
check::CheckUseWasiVirtLayerChecker,
dummy_injector::DummyInjectorStreamPass,
export_stack::ExportStackPreVfsStreamPass,
patch_component::PatchComponentStreamPass,
pre_vfs_memory_refuge::TemporaryRefugeMemoryStreamPass,
starts_pre::StartsPreStreamPass,
};
let check_pass = crate::wasm_stream::pipeline::ParallelCheckStreamPass::new(
vec![Box::new(CheckUseWasiVirtLayerChecker::new())],
);
pipeline.add_pass(Box::new(check_pass));
pipeline.add_pass(Box::new(AnonymousStreamPass::new(cloned_ctx.clone())));
pipeline.add_pass(Box::new(ConnectWasip1ABIPreVfsStreamPass::new()));
pipeline.add_pass(Box::new(
crate::wasm_stream::passes::threads_spawn::ThreadsSpawnPreVfsStreamPass::new(
cloned_ctx.threads,
),
));
pipeline.add_pass(Box::new(NonRecursiveWasiABIPreVfsStreamPass::new()));
pipeline.add_pass(Box::new(PatchComponentStreamPass::new()));
let fn_in_starts =
crate::wasm_stream::passes::fn_in_starts::FnInStarts::new::<String>(&[]);
pipeline.add_pass(Box::new(StartsPreStreamPass::new(
true,
pipeline_is_library,
fn_in_starts.flesh_vfs_start.clone(),
)));
pipeline.add_pass(Box::new(ExportStackPreVfsStreamPass::new(
cloned_ctx
.stack_config
.vfs_size(cloned_ctx.vfs_name.as_ref()),
)));
pipeline.add_pass(Box::new(DummyInjectorStreamPass::new(vec![
fn_in_starts.init_offset_global.clone(),
fn_in_starts.save_target_memory.clone(),
fn_in_starts.simple_debug_pre_init.clone(),
])));
pipeline.add_pass(Box::new(TemporaryRefugeMemoryStreamPass::new(None)));
Some(pipeline)
},
cloned_ctx.validate,
)
})
.with_opt(
&mut self.path,
dwarf,
keep_build_artifacts,
skip_vfs_opt,
validate,
)?;
println!("Adjusting target Wasm...");
for (i, (target, target_name)) in self
.targets
.iter_mut()
.zip(self.ctx.target_names.clone())
.enumerate()
{
let skip_target_opt = self.vfs_build_opts.no_opt_all > 0
|| self.target_vfs_build_opts[i].no_opt > 0
|| self.target_vfs_build_opts[i].no_opt_all > 0;
let _cloned_ctx = self.ctx.clone();
let is_synthesized = std::sync::Arc::new(std::sync::Mutex::new(false));
(|path: &mut WasmPath| {
(|| {
Ok(())
})
.wrap_run(path, dwarf, keep_build_artifacts, {
let mut pipeline = crate::wasm_stream::pipeline::Pipeline::new();
use crate::wasm_stream::passes::{
abi_connect::{
ConnectWasip1ABIPreTargetStreamPass,
ConnectWasip1ThreadsABIPreTargetStreamPass,
},
check::IsRustWasmChecker,
dummy_injector::DummyInjectorStreamPass,
pre_vfs_memory_refuge::TemporaryRefugeMemoryStreamPass,
producer::ProducerStreamPass,
starts_pre::StartsPreStreamPass,
special_func::SpecialFuncPreTargetStreamPass,
wrap_unreachable::WrapUnreachablePreTargetStreamPass,
atomic_patch::AtomicPatchStreamPass,
export_stack::ExportStackPreTargetStreamPass,
};
pipeline.add_pass(Box::new(ProducerStreamPass::new()));
pipeline.add_pass(Box::new(SpecialFuncPreTargetStreamPass::new(
target_name.to_string(),
is_synthesized.clone(),
)));
let is_opted_in = _cloned_ctx.wrap_unreachable_targets.contains(&target_name.to_string());
pipeline.add_pass(Box::new(WrapUnreachablePreTargetStreamPass::new(
target_name.to_string(),
is_opted_in,
)));
pipeline.add_pass(Box::new(
crate::wasm_stream::passes::deadlock_thread_id::DeadlockThreadIdPreTargetStreamPass::for_target(
_cloned_ctx.detect_deadlock,
target_name.to_string(),
),
));
if _cloned_ctx.target_memory_type == TargetMemoryType::Single {
pipeline.add_pass(Box::new(ExportStackPreTargetStreamPass::new(
target_name.to_string(),
crate::unique_name::UniqueName::WASIP1_ABI_MODULE.to_string(),
i as u32,
_cloned_ctx.stack_config.target_size(target_name.as_ref()),
)));
} else if let (Some(size), Some(slots)) = (
_cloned_ctx.stack_config.target_size(target_name.as_ref()),
_cloned_ctx.stack_config.slots.get(target_name.as_ref()).copied(),
) {
if slots > 0 {
let arena_pass = crate::wasm_stream::passes::export_stack_arena::ExportStackArenaStreamPass::new(size, slots);
let arena_size = arena_pass.arena_offset() as u32;
pipeline.add_pass(Box::new(arena_pass));
pipeline.add_pass(Box::new(
crate::wasm_stream::passes::export_stack_multi_memory_target::ExportStackMultiMemoryTargetStreamPass::new(
target_name.to_string(),
crate::unique_name::UniqueName::WASIP1_ABI_MODULE.to_string(),
arena_size,
size,
),
));
}
}
pipeline.add_pass(Box::new(ConnectWasip1ABIPreTargetStreamPass::new(
target_name.to_string(),
)));
pipeline.add_pass(Box::new(ConnectWasip1ThreadsABIPreTargetStreamPass::new(
target_name.to_string(),
)));
pipeline.add_pass(Box::new(crate::wasm_stream::passes::threads_spawn::ThreadsSpawnPreTargetStreamPass::new(
_cloned_ctx.threads,
target_name.to_string(),
)));
pipeline.add_pass(Box::new(AtomicPatchStreamPass::new(
_cloned_ctx.threads,
i as u32,
_cloned_ctx.detect_deadlock,
)));
let check_pass = crate::wasm_stream::pipeline::ParallelCheckStreamPass::new(
vec![Box::new(IsRustWasmChecker::new())],
);
pipeline.add_pass(Box::new(check_pass));
let export_name = format!("__flesh_{}_start", target_name);
let thread_start_export_name =
format!("__wasip1_vfs_{}__thread_start", target_name);
pipeline.add_pass(Box::new(
StartsPreStreamPass::new(false, false, export_name.clone())
.with_thread_start_export_name(thread_start_export_name),
));
pipeline.add_pass(Box::new(DummyInjectorStreamPass::new(vec![export_name])));
let new_memory_name =
crate::generator::UniqueName::Memory(&MemoryUniqueName::Memory(
&target_name,
))
.to_string();
pipeline.add_pass(Box::new(TemporaryRefugeMemoryStreamPass::new(Some(
new_memory_name,
))));
Some(pipeline)
}, _cloned_ctx.validate)
})
.with_opt(target, dwarf, keep_build_artifacts, skip_target_opt, validate)?;
if *is_synthesized.lock().unwrap() {
self.ctx
.main_void_synthesized_targets
.get_or_insert_default()
.insert(target_name.to_string());
}
}
let skip_all_opt = self.vfs_build_opts.no_opt_all > 0
|| self
.target_vfs_build_opts
.iter()
.any(|opts| opts.no_opt_all > 0);
println!("Combining Wasm modules...");
let output = format!("{out_dir}/merged.wasm");
let mut defined_funcs_counts = Vec::new();
(|path: &mut WasmPath| {
let old_path = path.path()?.clone();
defined_funcs_counts.push(count_defined_funcs(&old_path)?);
for target in self.targets.iter() {
defined_funcs_counts.push(count_defined_funcs(target.path()?)?);
}
merge(
&old_path,
&self
.targets
.iter()
.map(|t| t.path())
.collect::<eyre::Result<Vec<_>>>()?,
&output,
self.ctx.threads,
dwarf,
)
.wrap_err("Failed to combine Wasm modules")?;
if !keep_build_artifacts {
std::fs::remove_file(&old_path)
.wrap_err_with(|| format!("Failed to remove existing file {old_path}"))?;
for target in self.targets.iter() {
if let Ok(target_path) = target.path() {
if !target.is_original(target_path) {
std::fs::remove_file(target_path).unwrap_or_else(|e| {
log::warn!(
"Failed to remove intermediate target file {target_path}: {e}"
);
});
}
}
}
}
path.set_path(output.into())
})
.with_opt(
&mut self.path,
dwarf,
keep_build_artifacts,
skip_all_opt,
validate,
)?;
println!("Adjusting Merged Wasm (streaming pipeline)...");
(|path: &mut WasmPath| {
let old_path = path.path()?.clone();
let input_wasm = std::fs::read(&old_path).wrap_err("Failed to read Wasm file")?;
let mut pipeline = crate::wasm_stream::pipeline::Pipeline::new();
let target_names: Vec<String> = self.ctx.target_names.iter().map(|n| n.as_ref().to_string()).collect();
let vfs_name = self.ctx.vfs_name.as_ref().to_string();
pipeline.add_pass(Box::new(
crate::wasm_stream::passes::post_combine::PostCombineStreamPass::new(
vfs_name,
target_names.clone(),
defined_funcs_counts.clone(),
self.ctx.threads,
)
));
if self.ctx.target_memory_type == TargetMemoryType::Single {
println!("Generating single memory Merged Wasm (streaming lowering)...");
pipeline.add_pass(Box::new(
crate::wasm_stream::passes::multi_memory_lowering::MultiMemoryLoweringStreamPass::new(self.ctx.threads, self.ctx.own_memory, target_names.clone(), true)
));
pipeline.add_pass(Box::new(
crate::wasm_stream::passes::shared_global::SharedGlobalStreamPass::new(self.ctx.threads, self.ctx.own_memory, target_names)
));
} else if self.ctx.own_memory {
println!("Adjusting own memory for Multi Memory Merged Wasm (streaming lowering)...");
pipeline.add_pass(Box::new(
crate::wasm_stream::passes::multi_memory_lowering::MultiMemoryLoweringStreamPass::new(self.ctx.threads, self.ctx.own_memory, target_names.clone(), false)
));
}
let output_wasm = pipeline
.run(&input_wasm)
.wrap_err("Failed to run StreamPipeline")?;
let new_path = old_path.with_extension("lowered.wasm");
std::fs::write(&new_path, &output_wasm).wrap_err("Failed to write lowered Wasm file")?;
if self.ctx.validate {
wasmparser::Validator::new_with_features(wasmparser::WasmFeatures::all())
.validate_all(&output_wasm)
.wrap_err_with(|| format!("Wasm validation failed for {}", new_path))?;
}
if !keep_build_artifacts {
std::fs::remove_file(&old_path).wrap_err_with(|| format!("Failed to remove existing file {old_path}"))?;
}
path.set_path(new_path)?;
Ok(())
}).with_opt(&mut self.path, dwarf, keep_build_artifacts, skip_all_opt, validate)?;
println!("Translating Wasm to Component...");
let old_path = self.path.path()?.clone();
let component = compile::wasm_to_component(&old_path, &self.ctx.target_names)
.wrap_err("Failed to translate Wasm to Component")?;
let new_component = format!("{out_dir}/{}.component.wasm", self.ctx.vfs_name);
let component_bytes = std::fs::read(&component)?;
let component_bytes_with_ctx =
crate::wasm_stream::passes::component_ctx::append_component_ctx(
&component_bytes,
&self.ctx,
)?;
std::fs::write(&new_component, &component_bytes_with_ctx)?;
if self.ctx.validate {
wasmparser::Validator::new_with_features(wasmparser::WasmFeatures::all())
.validate_all(&component_bytes_with_ctx)
.wrap_err_with(|| format!("Wasm validation failed for {}", new_component))?;
}
if !keep_build_artifacts {
std::fs::remove_file(&component)
.wrap_err_with(|| format!("Failed to remove existing file {component}"))?;
std::fs::remove_file(&old_path)
.wrap_err_with(|| format!("Failed to remove existing file {old_path}"))?;
}
self.path.set_path(Utf8PathBuf::from(new_component))?;
Ok(ComponentRunner {
ctx: None,
path: self.path,
wasm_name_holder: None,
})
}
}
impl ComponentRunner {
pub fn new(path: WasmPath) -> Self {
Self {
ctx: None,
path,
wasm_name_holder: None,
}
}
pub fn component_to_files(
&mut self,
parsed_args: &(impl args::PostBuildContext + ?Sized),
dwarf: bool,
only_core: bool,
) -> eyre::Result<(bool, CompactString, HashMap<CompactString, (u64, u64)>)> {
let out_dir = parsed_args.out_dir();
let name = self.path.name()?;
println!("Translating Component to JS...");
let component_bytes = std::fs::read(&self.path.path()?)?;
self.ctx =
Some(crate::wasm_stream::passes::component_ctx::read_component_ctx(&component_bytes)?);
let core_wasm_path = (|path: &mut WasmPath| {
let old_path = path.path()?.clone();
let binary = std::fs::read(&old_path).wrap_err("Failed to read component")?;
let transpiled = parsed_args
.transpile_to_js(&binary, &name)
.wrap_err("Failed to transpile to JS")?;
let mut core_wasm = None;
for (name, data) in transpiled.files.iter() {
let name = camino::Utf8PathBuf::from(name);
let file_name = out_dir.join(&name);
if std::fs::metadata(&file_name).is_ok() {
std::fs::remove_file(&file_name).wrap_err_with(|| {
eyre::eyre!("Failed to remove existing file: {file_name}")
})?;
}
if name.as_str().ends_with(".core.wasm") {
let file_name = camino::Utf8PathBuf::from(file_name);
std::fs::write(&file_name, &data).wrap_err_with(|| {
eyre::eyre!("Failed to write core wasm file: {file_name}")
})?;
core_wasm = Some(file_name);
} else {
if let Some(parent) = name.parent() {
if !parent.as_str().is_empty() {
let dir = name.ancestors().nth(1).wrap_err_with(|| {
eyre::eyre!("Failed to get parent directory: {}", name)
})?;
let joined_dir = out_dir.join(dir);
if !std::fs::metadata(&joined_dir).is_ok() {
if dir.as_str() != "interfaces" {
log::warn!("Creating directory: {joined_dir}");
}
if only_core {
continue;
}
std::fs::create_dir_all(&joined_dir).wrap_err_with(|| {
eyre::eyre!("Failed to create directory: {joined_dir}")
})?;
}
}
}
if only_core {
continue;
}
std::fs::write(&file_name, &data).wrap_err_with(|| {
eyre::eyre!("Failed to write transpiled file: {file_name}")
})?;
}
}
let core_wasm = core_wasm
.as_ref()
.ok_or_else(|| eyre::eyre!("Failed to find core wasm"))?;
if !parsed_args.keep_build_artifacts() {
std::fs::remove_file(&old_path)
.wrap_err_with(|| format!("Failed to remove existing file {old_path}"))?;
}
path.set_path(core_wasm.clone())?;
Ok(core_wasm.clone())
})
.with_opt(
&mut self.path,
dwarf,
parsed_args.keep_build_artifacts(),
parsed_args.dev(),
parsed_args.validate(),
)?;
let mem_sizes = {
let wasm_bytes =
std::fs::read(&self.path.path()?).wrap_err("Failed to read core wasm")?;
crate::wasm_stream::passes::extract_mem_sizes::extract_memory_sizes(&wasm_bytes)?
};
println!("Adjusting component Merged Wasm...");
(|path: &mut WasmPath| {
let old_path = path.path()?.clone();
let input_wasm = std::fs::read(&old_path).wrap_err("Failed to read Wasm file")?;
let mut pipeline = crate::wasm_stream::pipeline::Pipeline::new();
pipeline.add_pass(Box::new(
crate::wasm_stream::passes::memory_post_components::PostComponentsMemoryFixStreamPass::new(self.ctx.as_ref().unwrap().threads.unwrap_or(false))
));
let output_wasm = pipeline
.run(&input_wasm)
.wrap_err("Failed to run StreamPipeline")?;
let new_path = old_path.with_extension("post-comp-stream.wasm");
std::fs::write(&new_path, &output_wasm).wrap_err("Failed to write Wasm file")?;
if parsed_args.validate() {
wasmparser::Validator::new_with_features(wasmparser::WasmFeatures::all())
.validate_all(&output_wasm)
.wrap_err_with(|| format!("Wasm validation failed for {}", new_path))?;
}
if !parsed_args.keep_build_artifacts() {
std::fs::remove_file(&old_path).unwrap_or_default();
}
path.set_path(new_path)?;
Ok(())
})
.with_opt(
&mut self.path,
dwarf,
parsed_args.keep_build_artifacts(),
parsed_args.dev(),
parsed_args.validate(),
)?;
let _dwarf = {
let new_dwarf = self.ctx.as_ref().unwrap().dwarf;
if dwarf && !new_dwarf {
log::warn!(
"Dwarf was disabled in component processing, you should re-run with --dwarf"
);
}
new_dwarf
};
std::fs::rename(self.path.path()?, &core_wasm_path).wrap_err_with(|| {
eyre::eyre!(
"Failed to rename final wasm from {} to {}",
self.path.path().unwrap(),
core_wasm_path
)
})?;
Ok((
self.ctx.as_ref().unwrap().threads.unwrap_or(false),
core_wasm_path
.get_file_main_name()
.ok_or_else(|| eyre::eyre!("Failed to get file name"))?,
mem_sizes,
))
}
}
#[derive(Debug, Clone, Hash)]
pub enum WasmPath {
Maybe {
manifest_path: Utf8PathBuf,
package: String,
},
Definitely(Utf8PathBuf),
Component(Utf8PathBuf),
Original {
current: Utf8PathBuf,
original: Utf8PathBuf,
},
}
impl FromStr for WasmPath {
type Err = eyre::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
fn parse_path_and_package(s: &str) -> eyre::Result<Option<WasmPath>> {
let split = s.split("::").collect::<Vec<_>>();
if split.len() == 2 {
let manifest_path = Utf8PathBuf::from_str(split[0])
.wrap_err_with(|| format!("Failed to parse manifest path: {}", split[0]))?;
let package = split[1].to_string();
return Ok(Some(WasmPath::Maybe {
manifest_path,
package,
}));
}
Ok(None)
}
if let Some(wasm_path) =
parse_path_and_package(s).wrap_err("Failed to parse path and package")?
{
return Ok(wasm_path);
}
if s.ends_with(".wasm") {
let path = Utf8PathBuf::from_str(s)
.wrap_err_with(|| format!("Failed to parse wasm path: {s}"))?;
return WasmPath::with_wasm(path);
}
if s.ends_with("Cargo.toml") {
let manifest_path = Utf8PathBuf::from_str(s)
.wrap_err_with(|| format!("Failed to parse manifest path: {s}"))?;
return WasmPath::with_maybe_only_manifest(manifest_path);
}
WasmPath::with_maybe_only_package(s.to_string())
.wrap_err_with(|| format!("Failed to parse package name: {s}"))
}
}
impl WasmPath {
pub fn name(&self) -> eyre::Result<CompactString> {
match self {
WasmPath::Maybe { package, .. } => {
let normalized = package.replace('-', "_");
Ok(normalized.to_compact_string())
}
WasmPath::Definitely(path)
| WasmPath::Component(path)
| WasmPath::Original { current: path, .. } => path
.get_file_main_name()
.ok_or_else(|| eyre::eyre!("Failed to get file name from {path}")),
}
}
pub fn manifest_path(&self) -> Option<&Utf8PathBuf> {
match self {
WasmPath::Maybe { manifest_path, .. } => Some(manifest_path),
WasmPath::Definitely(_) | WasmPath::Component(_) | WasmPath::Original { .. } => None,
}
}
pub fn root_manifest_path(&self) -> Option<Utf8PathBuf> {
match self {
WasmPath::Maybe { manifest_path, .. } => {
let cargo_metadata = {
let mut metadata_command = cargo_metadata::MetadataCommand::new();
metadata_command.manifest_path(&manifest_path);
metadata_command.exec().unwrap()
};
Some(cargo_metadata.workspace_root.join("Cargo.toml"))
}
WasmPath::Definitely(_) | WasmPath::Component(_) | WasmPath::Original { .. } => None,
}
}
pub const fn with_maybe(manifest_path: Utf8PathBuf, package: String) -> Self {
Self::Maybe {
manifest_path,
package,
}
}
pub fn with_maybe_only_manifest(manifest_path: Utf8PathBuf) -> eyre::Result<Self> {
let cargo_metadata = {
let mut metadata_command = cargo_metadata::MetadataCommand::new();
metadata_command.manifest_path(&manifest_path);
metadata_command.exec().unwrap()
};
let building_crate = compile::get_building_crate(&cargo_metadata, &None)?;
Ok(Self::Maybe {
manifest_path,
package: building_crate.name.to_string(),
})
}
pub fn with_maybe_only_package(package: String) -> eyre::Result<Self> {
let cargo_metadata = {
let metadata_command = cargo_metadata::MetadataCommand::new();
metadata_command.exec().unwrap()
};
let building_crate = compile::get_building_crate(&cargo_metadata, &Some(package.clone()))?;
Ok(Self::Maybe {
manifest_path: building_crate.manifest_path,
package: building_crate.name.to_string(),
})
}
pub fn with_maybe_none() -> eyre::Result<Self> {
let cargo_metadata = {
let metadata_command = cargo_metadata::MetadataCommand::new();
metadata_command.exec().unwrap()
};
let building_crate = compile::get_building_crate(&cargo_metadata, &None)?;
Ok(Self::Maybe {
manifest_path: building_crate.manifest_path,
package: building_crate.name.to_string(),
})
}
pub fn with_wasm(path: Utf8PathBuf) -> eyre::Result<Self> {
if path.extension() != Some("wasm") {
eyre::bail!("Wasm file does not have .wasm extension: {path}");
}
if !fs::metadata(&path).is_ok() {
return Ok(Self::Original {
current: path.clone(),
original: path,
});
}
let mut file =
fs::File::open(&path).wrap_err_with(|| format!("Failed to open wasm file: {path}"))?;
let mut magic = [0u8; 8];
file.read_exact(&mut magic)
.wrap_err_with(|| format!("Failed to read magic number from wasm file: {path}"))?;
if !magic.starts_with(b"\0asm") {
eyre::bail!("Wasm file does not have valid magic number: {path}");
}
let version = &magic[4..8];
if version == [0x0D, 0x00, 0x01, 0x00] {
return Ok(Self::Component(path));
}
if version != [0x01, 0x00, 0x00, 0x00] {
eyre::bail!("Wasm file does not have valid version: {path}");
}
Ok(Self::Original {
current: path.clone(),
original: path,
})
}
pub fn definitely(
&mut self,
threads: bool,
vfs_build_opts: &args::VfsBuildOptions,
) -> eyre::Result<()> {
if let WasmPath::Maybe {
manifest_path,
package,
} = self
{
let cargo_metadata = {
let mut metadata_command = cargo_metadata::MetadataCommand::new();
metadata_command.manifest_path(&manifest_path);
metadata_command.exec().unwrap()
};
let building_crate =
compile::get_building_crate(&cargo_metadata, &Some(package.clone()))?;
let vfs_name = building_crate.name.to_string();
let path = compile::build_vfs(
Some(&manifest_path.to_string()),
&building_crate,
threads,
vfs_build_opts,
)
.wrap_err_with(|| eyre::eyre!("Failed to build VFS: {vfs_name}"))?;
*self = WasmPath::Definitely(path);
}
Ok(())
}
pub fn path(&self) -> eyre::Result<&Utf8PathBuf> {
match self {
WasmPath::Maybe { .. } => {
return Err(eyre::eyre!("WasmPath is not definitely set: {self:?}"));
}
WasmPath::Definitely(p)
| WasmPath::Component(p)
| WasmPath::Original { current: p, .. } => Ok(p),
}
}
pub fn is_original(&self, path: &Utf8PathBuf) -> bool {
matches!(self, WasmPath::Original { original, .. } if original == path)
}
pub fn set_path(&mut self, path: Utf8PathBuf) -> eyre::Result<()> {
match self {
WasmPath::Maybe { .. } => {
return Err(eyre::eyre!("WasmPath is not definitely set: {path}"));
}
WasmPath::Original { current, .. } => {
*current = path;
}
_ => {
*self = WasmPath::Definitely(path);
}
}
Ok(())
}
}
fn count_defined_funcs(wasm_path: &camino::Utf8PathBuf) -> eyre::Result<u32> {
let wasm = std::fs::read(wasm_path)?;
for payload in wasmparser::Parser::new(0).parse_all(&wasm) {
let payload = payload.map_err(|e| eyre::eyre!("Parse error: {}", e))?;
if let wasmparser::Payload::FunctionSection(s) = payload {
return Ok(s.count());
}
}
Ok(0)
}
pub fn merge(
vfs: &Utf8PathBuf,
wasm: &[impl AsRef<std::path::Path>],
output: impl AsRef<std::path::Path>,
_threads: bool,
dwarf: bool,
) -> eyre::Result<()> {
let mut inputs = Vec::with_capacity(wasm.len() + 1);
inputs.push(crate::wasm_stream::merge::MergeInput {
alias: UniqueName::WASIP1_ABI_MODULE.to_string(),
path: vfs.as_std_path(),
});
for wasm in wasm {
inputs.push(crate::wasm_stream::merge::MergeInput {
alias: format!(
"wasip1_vfs_{}",
wasm.as_ref()
.get_file_main_name()
.ok_or_else(|| eyre::eyre!("Failed to get Wasm module name"))?
),
path: wasm.as_ref(),
});
}
crate::wasm_stream::merge::merge_modules(&inputs, output, dwarf)
}
macro_rules! _add_generators_by_type {
($runner:expr, $($ty:ty),* $(,)?) => {
$(
if let Some(_) = $runner.get_generator_ref::<$ty>().ok() {
panic!("Generator of type {} already exists", std::any::type_name::<$ty>());
}
$runner.add_generator(<$ty>::default());
)*
};
}