use crate::wasm_stream::own_memory_abi;
use crate::wasm_stream::passes::fn_in_starts::{FnInStarts, ResolvedStartFuncs};
use crate::wasm_stream::pipeline::StreamPass;
use crate::wasm_stream::translator::Rebind;
use eyre::{Context, Result};
use std::collections::{HashMap, HashSet};
use wasm_encoder::{
CodeSection, EntityType, ExportSection, FunctionSection, ImportSection, Module, StartSection,
TypeSection,
};
pub struct PostCombineStreamPass {
pub vfs_name: String,
pub target_names: Vec<String>,
pub defined_funcs_counts: Vec<u32>,
pub threads: bool,
}
impl PostCombineStreamPass {
pub fn new(
vfs_name: String,
target_names: Vec<String>,
defined_funcs_counts: Vec<u32>,
threads: bool,
) -> Self {
Self {
vfs_name,
target_names,
defined_funcs_counts,
threads,
}
}
}
struct PostCombineRebinder {
func_map: HashMap<u32, u32>,
}
impl Rebind for PostCombineRebinder {
fn function(&self, index: u32) -> u32 {
self.func_map.get(&index).copied().unwrap_or(index)
}
}
#[derive(Default, Debug)]
struct ParsedInfo {
dropped_imports: HashMap<u32, String>,
host_imports: HashMap<u32, String>,
host_import_modules: HashMap<u32, String>,
exported_funcs: HashMap<String, u32>,
mutable_globals: HashMap<u32, i64>, data_segments: Vec<(u32, i32, Vec<u8>)>, pub main_void_funcs: HashSet<u32>,
pub wasi_thread_starts: HashMap<String, u32>,
reset_globals_funcs: HashMap<String, u32>,
wrap_unreachable_targets: HashSet<String>,
set_unreachable_flag_funcs: HashMap<String, u32>,
original_data_count: u32,
import_types: HashMap<u32, u32>,
has_thread_spawn: bool,
thread_spawn_type_idx: Option<u32>,
host_thread_spawn_import: Option<u32>,
host_proc_exit_import: Option<u32>,
start_funcs: ResolvedStartFuncs,
flesh_target_start_exports: HashMap<String, u32>,
normalized_target_start_exports: HashMap<String, u32>,
target_runtime_starts: HashMap<String, u32>,
}
fn resolve_host_export(name: &str, info: &ParsedInfo) -> Option<u32> {
if let Some(&export_orig_idx) = info.exported_funcs.get(name) {
return Some(export_orig_idx);
}
let self_suffix = name.strip_prefix("__wasip1_vfs___self_")?;
if self_suffix == "proc_exit" {
return None;
}
let compact_self_name = format!("__wasip1_vfs_self_{self_suffix}");
if let Some(&export_orig_idx) = info.exported_funcs.get(&compact_self_name) {
return Some(export_orig_idx);
}
None
}
fn is_detector_helper(name: &str) -> bool {
matches!(
name,
"__vfs_deadlock_thread_enter"
| "__vfs_deadlock_thread_exit"
| "__vfs_atomic_wait32"
| "__vfs_atomic_wait64"
| "__vfs_atomic_notify"
| "__vfs_atomic_observe_write"
)
}
fn is_void_detector_helper(name: &str) -> bool {
matches!(
name,
"__vfs_deadlock_thread_enter" | "__vfs_deadlock_thread_exit" | "__vfs_atomic_observe_write"
)
}
fn detector_helper_stub_i32(name: &str) -> Option<i32> {
match name {
"__vfs_atomic_notify" => Some(0),
"__vfs_atomic_wait32" | "__vfs_atomic_wait64" => Some(2),
_ => None,
}
}
fn data_segments_for_memory(
data_segments: &[(u32, i32, Vec<u8>)],
memory_index: u32,
) -> impl Iterator<Item = (usize, i32, &[u8])> {
data_segments
.iter()
.enumerate()
.filter_map(move |(segment_index, (mem_idx, offset, bytes))| {
(*mem_idx == memory_index).then_some((segment_index, *offset, bytes.as_slice()))
})
}
fn should_drop_export(name: &str) -> bool {
if matches!(
name,
"_start"
| "__flesh_vfs_start"
| "__thread_patch"
| "__save_target_memory"
| "__simple_debug_wasip1_vfs_pre_init"
| "simple_debug_wasip1_vfs_pre_init"
| "__wasip1_vfs_wasi_thread_spawn_wrapper"
| "__wasip1_vfs_wasi_thread_spawn___self"
| "__wasip1_vfs_is_root_spawn"
| "wasi_thread_initializer"
| "__wasip1_vfs_thread_initializer"
| "__wasip1_vfs_wasi_thread_start_entry"
) {
return true;
}
if (name.starts_with("__flesh_") && name.ends_with("_start"))
|| (name.starts_with("__wasip1_vfs_") && name.ends_with("__thread_start"))
|| (name.starts_with("__wasip1_virt_layer_") && name.ends_with("_wrap_unreachable"))
{
return true;
}
false
}
fn should_emit_custom_section(name: &str, emitted: &mut HashSet<String>) -> bool {
if name.chars().any(char::is_control) {
return false;
}
if matches!(name, "producers" | "target_features") {
return emitted.insert(name.to_string());
}
true
}
fn wrap_unreachable_proc_exit_target<'a>(name: &'a str, info: &ParsedInfo) -> Option<&'a str> {
let target_name = name
.strip_prefix("__wasip1_vfs_")?
.strip_suffix("_proc_exit")?;
if target_name == "self" || target_name == "_self" {
return None;
}
info.wrap_unreachable_targets
.contains(target_name)
.then_some(target_name)
}
impl StreamPass for PostCombineStreamPass {
fn run(&mut self, input_wasm: &[u8]) -> eyre::Result<Vec<u8>> {
let mut info = ParsedInfo::default();
let mut func_import_count = 0;
let mut global_count = 0;
let mut type_count = 0;
let mut defined_func_count = 0;
let mut memory_count = 0;
let mut memory_import_count = 0;
for payload in wasmparser::Parser::new(0).parse_all(input_wasm) {
match payload? {
wasmparser::Payload::ImportSection(s) => {
for group in s {
for i in group?.into_iter() {
let (_, import) = i?;
if matches!(import.ty, wasmparser::TypeRef::Func(_)) {
if import.module
== "wasip1-vfs:host/virtual-file-system-wasip1-threads-import"
&& import.name == "[static]wasip1-threads.thread-spawn-import"
{
info.host_thread_spawn_import = Some(func_import_count);
}
if import.module
== "wasip1-vfs:host/virtual-file-system-wasip1-core"
&& import.name == "[static]wasip1.proc-exit-import"
{
info.host_proc_exit_import = Some(func_import_count);
}
let is_special = (import.module == "wasip1-vfs"
&& import.name.starts_with("__wasip1_vfs_")
&& (import.name.ends_with("_memory_copy_from")
|| import.name.ends_with("_memory_copy_to")
|| import.name.ends_with("_memory_trap")
|| import.name.ends_with("_reset")
|| import.name.ends_with("__start")
|| import.name.ends_with("__thread_start")
|| import.name.ends_with("__main_void")
|| own_memory_abi::parse_memory_director_export(
import.name,
)
.is_some()
|| import.name.ends_with("_reset_on_thread_once")
|| import.name.ends_with(
own_memory_abi::TARGET_OWN_MEMORY_SIZE_GET_SUFFIX,
)
|| import.name.ends_with(
own_memory_abi::TARGET_OWN_MEMORY_SIZE_SET_SUFFIX,
)
|| import.name.ends_with(
own_memory_abi::TARGET_OWN_MEMORY_SIZE_INIT_SUFFIX,
)
|| import.name.ends_with(
own_memory_abi::TARGET_OWN_MEMORY_SIZE_COMPARE_EXCHANGE_SUFFIX,
)))
|| (import.module == "wasip1-vfs_single_memory"
&& import.name == "__wasip1_vfs_memory_grow_alt")
|| import.module == "wvl_poll"
|| import.module == "wvl_atomic";
if is_special {
info.dropped_imports
.insert(func_import_count, import.name.to_string());
} else if import.module == "__wasip1_vfs-host"
|| import.module == "__wasip1_virt_layer"
|| import.module == "wasip1-vfs"
|| import.module == "wasi_snapshot_preview1"
|| (import.module == "env"
&& import.name == "__wasip1_vfs_wasi_thread_spawn_wrapper")
{
info.host_imports
.insert(func_import_count, import.name.to_string());
info.host_import_modules
.insert(func_import_count, import.module.to_string());
if import.module == "env"
&& import.name == "__wasip1_vfs_wasi_thread_spawn_wrapper"
{
info.has_thread_spawn = true;
if let wasmparser::TypeRef::Func(type_index) = import.ty {
info.thread_spawn_type_idx = Some(type_index);
}
}
}
if let wasmparser::TypeRef::Func(type_index) = import.ty {
info.import_types.insert(func_import_count, type_index);
}
func_import_count += 1;
}
if matches!(import.ty, wasmparser::TypeRef::Global(_)) {
global_count += 1;
}
if matches!(import.ty, wasmparser::TypeRef::Memory(_)) {
memory_import_count += 1;
memory_count += 1;
}
}
}
}
wasmparser::Payload::TypeSection(s) => {
for ty in s {
for _ in ty?.into_types() {
type_count += 1;
}
}
}
wasmparser::Payload::FunctionSection(s) => {
defined_func_count = s.count();
}
wasmparser::Payload::MemorySection(s) => {
memory_count += s.count();
}
wasmparser::Payload::ExportSection(s) => {
for export in s {
let export = export?;
if export.kind == wasmparser::ExternalKind::Func {
info.exported_funcs
.entry(export.name.to_string())
.or_insert(export.index);
match export.name {
"__flesh_vfs_start" => {
info.start_funcs.flesh_vfs_start = Some(export.index)
}
"__thread_patch" => {
info.start_funcs.thread_patch = Some(export.index)
}
"__init_offset_global" => {
info.start_funcs.init_offset_global = Some(export.index)
}
"__save_target_memory" => {
info.start_funcs.save_target_memory = Some(export.index)
}
"simple_debug_wasip1_vfs_pre_init" => {
info.start_funcs.simple_debug_pre_init = Some(export.index)
}
"wasi_thread_initializer" | "__wasip1_vfs_thread_initializer" => {
info.start_funcs.wasi_thread_initializer = Some(export.index)
}
name if name.starts_with("__flesh_")
&& name.ends_with("_start") =>
{
let target_name = name
.strip_prefix("__flesh_")
.unwrap()
.strip_suffix("_start")
.unwrap()
.to_string();
info.flesh_target_start_exports
.entry(target_name)
.or_insert(export.index);
}
name if name.starts_with("__wasip1_vfs_")
&& name.ends_with("__start") =>
{
let target_name = name
.strip_prefix("__wasip1_vfs_")
.unwrap()
.strip_suffix("__start")
.unwrap()
.to_string();
info.normalized_target_start_exports
.insert(target_name, export.index);
}
name if name.starts_with("__wasip1_vfs_")
&& name.ends_with("_wasi_thread_start_anchor") =>
{
let target_name = name
.strip_prefix("__wasip1_vfs_")
.unwrap()
.strip_suffix("_wasi_thread_start_anchor")
.unwrap();
info.wasi_thread_starts
.insert(target_name.to_string(), export.index);
}
name if name.ends_with("__main_void") => {
info.main_void_funcs.insert(export.index);
}
name if name.starts_with("__wasip1_vfs_")
&& name.ends_with("_reset_globals") =>
{
let target = name
.strip_prefix("__wasip1_vfs_")
.unwrap()
.strip_suffix("_reset_globals")
.unwrap();
info.reset_globals_funcs
.insert(target.replace("_", "-"), export.index);
}
name if name.starts_with("__wasip1_virt_layer_")
&& name.ends_with("_wrap_unreachable") =>
{
let target = name
.strip_prefix("__wasip1_virt_layer_")
.unwrap()
.strip_suffix("_wrap_unreachable")
.unwrap();
info.wrap_unreachable_targets.insert(target.to_string());
}
name if name.starts_with("__wasip1_virt_layer_")
&& name.ends_with("_set_unreachable_flag") =>
{
let target = name
.strip_prefix("__wasip1_virt_layer_")
.unwrap()
.strip_suffix("_set_unreachable_flag")
.unwrap();
info.set_unreachable_flag_funcs
.insert(target.to_string(), export.index);
}
_ => {}
}
}
}
}
wasmparser::Payload::GlobalSection(s) => {
for global in s {
let global = global?;
if global.ty.mutable {
let mut reader = global.init_expr.get_operators_reader();
let op = reader.read()?;
match op {
wasmparser::Operator::I32Const { value } => {
info.mutable_globals.insert(global_count, value as i64);
}
wasmparser::Operator::I64Const { value } => {
info.mutable_globals.insert(global_count, value);
}
_ => {}
}
}
global_count += 1;
}
}
wasmparser::Payload::DataSection(s) => {
info.original_data_count = s.count();
for data in s {
let data = data?;
if let wasmparser::DataKind::Active {
memory_index,
offset_expr,
} = data.kind
{
let mut reader = offset_expr.get_operators_reader();
let op = reader.read()?;
if let wasmparser::Operator::I32Const { value } = op {
info.data_segments
.push((memory_index, value, data.data.to_vec()));
}
}
}
}
wasmparser::Payload::DataCountSection { count, .. } => {
info.original_data_count = count;
}
_ => {}
}
}
for (target_name, &flesh_idx) in &info.flesh_target_start_exports {
info.start_funcs
.flesh_target_starts
.insert(target_name.clone(), flesh_idx);
}
for (target_name, &normalized_idx) in &info.normalized_target_start_exports {
info.target_runtime_starts
.insert(target_name.clone(), normalized_idx);
}
for (target_name, &flesh_idx) in &info.flesh_target_start_exports {
info.target_runtime_starts
.entry(target_name.clone())
.or_insert(flesh_idx);
}
let mut module = Module::new();
let mut type_section = TypeSection::new();
let mut import_section = ImportSection::new();
let mut function_section = FunctionSection::new();
let mut memory_section = wasm_encoder::MemorySection::new();
let mut global_section = wasm_encoder::GlobalSection::new();
let mut export_section = ExportSection::new();
let mut code_section = CodeSection::new();
let mut data_count_section = wasm_encoder::DataCountSection { count: 0 };
let mut has_data_count = false;
let mut data_count = 0;
let mut func_map = HashMap::new();
let mut current_new_idx = 0;
let mut dropped_func_original_indices = Vec::new();
let needs_host_proc_exit = info
.host_imports
.values()
.any(|name| name == "__wasip1_vfs___self_proc_exit")
&& info.has_thread_spawn
&& !info.wrap_unreachable_targets.is_empty();
for i in 0..func_import_count {
if info.host_imports.contains_key(&i) {
} else if info.dropped_imports.contains_key(&i) {
dropped_func_original_indices.push(i);
} else {
func_map.insert(i, current_new_idx);
current_new_idx += 1;
}
}
let inject_host_thread_spawn =
info.has_thread_spawn && info.host_thread_spawn_import.is_none();
let mut real_thread_spawn_new_idx = info
.host_thread_spawn_import
.and_then(|idx| func_map.get(&idx).copied());
if info.has_thread_spawn {
if real_thread_spawn_new_idx.is_none() {
real_thread_spawn_new_idx = Some(current_new_idx);
current_new_idx += 1;
}
}
let inject_host_proc_exit = needs_host_proc_exit && info.host_proc_exit_import.is_none();
let mut real_proc_exit_new_idx = info
.host_proc_exit_import
.and_then(|idx| func_map.get(&idx).copied());
if needs_host_proc_exit {
if real_proc_exit_new_idx.is_none() {
real_proc_exit_new_idx = Some(current_new_idx);
current_new_idx += 1;
}
}
for i in 0..defined_func_count {
let orig_idx = func_import_count + i;
func_map.insert(orig_idx, current_new_idx);
current_new_idx += 1;
}
let mut unintentionally_dropped_names = Vec::new();
for (import_idx, name) in &info.host_imports {
if name == "__wasip1_vfs_wasi_thread_spawn_wrapper" {
dropped_func_original_indices.push(*import_idx);
info.dropped_imports.insert(*import_idx, name.clone());
} else if name == "__wasip1_vfs___self_proc_exit" {
dropped_func_original_indices.push(*import_idx);
info.dropped_imports.insert(*import_idx, name.clone());
} else if wrap_unreachable_proc_exit_target(name, &info).is_some() {
dropped_func_original_indices.push(*import_idx);
info.dropped_imports.insert(*import_idx, name.clone());
} else if let Some(export_orig_idx) = resolve_host_export(name, &info) {
let export_new_idx = func_map.get(&export_orig_idx).copied().unwrap();
func_map.insert(*import_idx, export_new_idx);
} else if is_detector_helper(name)
&& info
.host_import_modules
.get(import_idx)
.is_some_and(|module| module == "wasi_snapshot_preview1")
{
dropped_func_original_indices.push(*import_idx);
info.dropped_imports.insert(*import_idx, name.clone());
} else {
dropped_func_original_indices.push(*import_idx);
info.dropped_imports.insert(*import_idx, name.clone());
unintentionally_dropped_names.push(name.as_str());
}
}
crate::abi::is_valid::validate_unresolved_imports(
&unintentionally_dropped_names,
&self.target_names,
)
.wrap_err("Failed to translate Wasm to Component")?;
for orig_idx in &dropped_func_original_indices {
func_map.insert(*orig_idx, current_new_idx);
current_new_idx += 1;
}
let new_start_idx = current_new_idx;
current_new_idx += 1;
let rebinder = PostCombineRebinder {
func_map: func_map.clone(),
};
let mut type_mem_copy = None;
let mut type_void_void = None;
let mut type_void_i32 = None;
let mut type_i32_void = None;
let mut type_i32_i32_void = None;
let mut type_i32_i32 = None;
let mut data_section_opt = None;
let mut is_after_code = false;
let mut custom_sections_after_code: Vec<(String, Vec<u8>)> = Vec::new();
let mut emitted_custom_sections = HashSet::new();
let mut current_defined_func_idx = 0;
for payload in wasmparser::Parser::new(0).parse_all(input_wasm) {
match payload? {
wasmparser::Payload::TypeSection(s) => {
let mut type_count = 0;
for ty in s {
let ty = ty?;
for sub_ty in ty.clone().into_types() {
if let wasmparser::CompositeInnerType::Func(f) =
&sub_ty.composite_type.inner
{
if f.params()
== [
wasmparser::ValType::I32,
wasmparser::ValType::I32,
wasmparser::ValType::I32,
]
&& f.results().is_empty()
{
type_mem_copy = Some(type_count);
} else if f.params().is_empty() && f.results().is_empty() {
type_void_void = Some(type_count);
} else if f.params().is_empty()
&& f.results() == [wasmparser::ValType::I32]
{
type_void_i32 = Some(type_count);
} else if f.params() == [wasmparser::ValType::I32]
&& f.results().is_empty()
{
type_i32_void = Some(type_count);
} else if f.params()
== [wasmparser::ValType::I32, wasmparser::ValType::I32]
&& f.results().is_empty()
{
type_i32_i32_void = Some(type_count);
} else if f.params() == [wasmparser::ValType::I32]
&& f.results() == [wasmparser::ValType::I32]
{
type_i32_i32 = Some(type_count);
}
}
}
if ty.is_explicit_rec_group() {
let rec_types = ty
.into_types()
.map(|sub_ty| {
crate::wasm_stream::translator::translate_sub_type(
&sub_ty, &rebinder,
)
})
.collect::<Vec<_>>();
type_section.ty().rec(rec_types);
} else {
for sub_ty in ty.into_types() {
type_section.ty().subtype(
&crate::wasm_stream::translator::translate_sub_type(
&sub_ty, &rebinder,
),
);
}
}
type_count += 1;
}
if type_mem_copy.is_none() {
type_mem_copy = Some(type_count);
type_section.ty().function(
vec![
wasm_encoder::ValType::I32,
wasm_encoder::ValType::I32,
wasm_encoder::ValType::I32,
],
vec![],
);
type_count += 1;
}
if type_void_void.is_none() {
type_void_void = Some(type_count);
type_section.ty().function(vec![], vec![]);
type_count += 1;
}
if type_void_i32.is_none() {
type_void_i32 = Some(type_count);
type_section
.ty()
.function(vec![], vec![wasm_encoder::ValType::I32]);
type_count += 1;
}
if type_i32_void.is_none() {
type_i32_void = Some(type_count);
type_section
.ty()
.function(vec![wasm_encoder::ValType::I32], vec![]);
type_count += 1;
}
if type_i32_i32_void.is_none() {
type_i32_i32_void = Some(type_count);
type_section.ty().function(
vec![wasm_encoder::ValType::I32, wasm_encoder::ValType::I32],
vec![],
);
type_count += 1;
}
if type_i32_i32.is_none() {
type_i32_i32 = Some(type_count);
type_section.ty().function(
vec![wasm_encoder::ValType::I32],
vec![wasm_encoder::ValType::I32],
);
type_count += 1;
}
module.section(&type_section);
}
wasmparser::Payload::ImportSection(s) => {
let mut idx = 0;
for group in s {
for i in group?.into_iter() {
let (_, import) = i?;
if matches!(import.ty, wasmparser::TypeRef::Func(_)) {
if !info.dropped_imports.contains_key(&idx)
&& !info.host_imports.contains_key(&idx)
{
let entity = match import.ty {
wasmparser::TypeRef::Func(i) => EntityType::Function(i),
_ => unreachable!(),
};
import_section.import(import.module, import.name, entity);
}
idx += 1;
} else {
let entity = match import.ty {
wasmparser::TypeRef::Table(t) => EntityType::Table(
crate::wasm_stream::translator::translate_table_type(
t, &rebinder,
),
),
wasmparser::TypeRef::Memory(t) => EntityType::Memory(
crate::wasm_stream::translator::translate_memory_type(t),
),
wasmparser::TypeRef::Global(g) => EntityType::Global(
crate::wasm_stream::translator::translate_global_type(
g, &rebinder,
),
),
wasmparser::TypeRef::Tag(t) => EntityType::Tag(
crate::wasm_stream::translator::translate_tag_type(t),
),
_ => unimplemented!(),
};
import_section.import(import.module, import.name, entity);
}
}
}
if inject_host_thread_spawn {
import_section.import(
"wasip1-vfs:host/virtual-file-system-wasip1-threads-import",
"[static]wasip1-threads.thread-spawn-import",
wasm_encoder::EntityType::Function(info.thread_spawn_type_idx.unwrap()),
);
}
if inject_host_proc_exit {
import_section.import(
"wasip1-vfs:host/virtual-file-system-wasip1-core",
"[static]wasip1.proc-exit-import",
wasm_encoder::EntityType::Function(type_i32_void.unwrap()),
);
}
if !import_section.is_empty() {
module.section(&import_section);
}
}
wasmparser::Payload::FunctionSection(s) => {
for func in s {
function_section.function(func?);
}
for orig_idx in &dropped_func_original_indices {
let type_index = info.import_types.get(orig_idx).unwrap();
function_section.function(*type_index);
}
function_section.function(type_void_void.unwrap());
module.section(&function_section);
}
wasmparser::Payload::TableSection(s) => {
let mut tables = wasm_encoder::TableSection::new();
for table in s {
tables.table(crate::wasm_stream::translator::translate_table_type(
table?.ty, &rebinder,
));
}
module.section(&tables);
}
wasmparser::Payload::MemorySection(s) => {
for mem in s {
memory_section
.memory(crate::wasm_stream::translator::translate_memory_type(mem?));
}
module.section(&memory_section);
}
wasmparser::Payload::GlobalSection(s) => {
for global in s {
let global = global?;
let mut instrs = Vec::new();
for op in global.init_expr.get_operators_reader() {
let op = op?;
if matches!(op, wasmparser::Operator::End) {
continue;
}
instrs.push(crate::wasm_stream::translator::translate(&op, &rebinder));
}
let init_expr = wasm_encoder::ConstExpr::extended(instrs);
global_section.global(
crate::wasm_stream::translator::translate_global_type(
global.ty, &rebinder,
),
&init_expr,
);
}
module.section(&global_section);
}
wasmparser::Payload::ExportSection(s) => {
for export in s {
let export = export?;
if should_drop_export(export.name) {
continue; }
if export.kind == wasmparser::ExternalKind::Memory {
if export.index != 0 {
continue;
} export_section.export("memory", wasm_encoder::ExportKind::Memory, 0);
continue;
}
let mut actual_export_idx = match export.kind {
wasmparser::ExternalKind::Func => rebinder.function(export.index),
_ => export.index,
};
if export.name == "wasi_thread_start" {
if let Some(&entry_idx) = info
.exported_funcs
.get("__wasip1_vfs_wasi_thread_start_entry")
{
if let Some(&new_entry_idx) = func_map.get(&entry_idx) {
actual_export_idx = new_entry_idx;
}
}
}
export_section.export(
export.name,
match export.kind {
wasmparser::ExternalKind::Func => wasm_encoder::ExportKind::Func,
wasmparser::ExternalKind::Table => wasm_encoder::ExportKind::Table,
wasmparser::ExternalKind::Memory => {
wasm_encoder::ExportKind::Memory
}
wasmparser::ExternalKind::Global => {
wasm_encoder::ExportKind::Global
}
wasmparser::ExternalKind::Tag => wasm_encoder::ExportKind::Tag,
_ => unimplemented!(),
},
actual_export_idx,
);
}
export_section.export("_start", wasm_encoder::ExportKind::Func, new_start_idx);
module.section(&export_section);
if self.threads {
module.section(&StartSection {
function_index: new_start_idx,
});
}
}
wasmparser::Payload::StartSection { .. } => {
}
wasmparser::Payload::ElementSection(s) => {
let mut elements = wasm_encoder::ElementSection::new();
for elem in s {
let elem = elem?;
let mut funcs_vec = Vec::new();
let items = match elem.items {
wasmparser::ElementItems::Functions(f) => {
funcs_vec = f
.into_iter()
.map(|idx| Ok(rebinder.function(idx?)))
.collect::<Result<Vec<u32>, eyre::Error>>()?;
wasm_encoder::Elements::Functions(std::borrow::Cow::Borrowed(
&funcs_vec,
))
}
wasmparser::ElementItems::Expressions(_e, _) => {
unimplemented!(
"ElementItems::Expressions not fully implemented in post_combine rebinder"
);
}
};
match elem.kind {
wasmparser::ElementKind::Passive => {
elements.passive(items);
}
wasmparser::ElementKind::Active {
table_index,
offset_expr,
} => {
let mut instrs = Vec::new();
for op in offset_expr.get_operators_reader() {
let op = op?;
if matches!(op, wasmparser::Operator::End) {
continue;
}
instrs.push(crate::wasm_stream::translator::translate(
&op, &rebinder,
));
}
let offset = wasm_encoder::ConstExpr::extended(instrs);
elements.active(table_index, &offset, items);
}
wasmparser::ElementKind::Declared => {
elements.declared(items);
}
}
}
module.section(&elements);
}
wasmparser::Payload::DataCountSection { count, .. } => {
data_count_section.count = count + info.data_segments.len() as u32;
data_count = count; has_data_count = true;
}
wasmparser::Payload::CodeSectionStart { .. } => {
is_after_code = true;
}
wasmparser::Payload::CodeSectionEntry(body) => {
let func_orig_idx = func_import_count + current_defined_func_idx;
let current_idx_for_mem = current_defined_func_idx;
current_defined_func_idx += 1;
let is_start = info
.start_funcs
.flesh_target_starts
.values()
.chain(info.target_runtime_starts.values())
.any(|&idx| idx == func_orig_idx);
let mut locals = Vec::new();
for local in body.get_locals_reader()? {
let local = local?;
locals.push((
local.0,
crate::wasm_stream::translator::translate_val_type(local.1, &rebinder),
));
}
let mut func = wasm_encoder::Function::new(locals);
let mut mem_idx = 0;
let mut cumulative = 0;
for (i, &count) in self.defined_funcs_counts.iter().enumerate() {
cumulative += count;
if current_idx_for_mem < cumulative {
mem_idx = i as u32;
break;
}
}
let target_thread_spawn_hook = if mem_idx > 0 {
self.target_names
.get((mem_idx - 1) as usize)
.and_then(|target_name| {
info.exported_funcs.get(&format!(
"__wasip1_vfs_wasi_thread_spawn_{}",
own_memory_abi::sanitize_target_name(target_name)
))
})
.and_then(|orig_idx| {
func_map.get(orig_idx).map(|new_idx| (*orig_idx, *new_idx))
})
} else {
None
};
for op in body.get_operators_reader()? {
let op_unwrapped = op?;
let mut skip = false;
if matches!(op_unwrapped, wasmparser::Operator::DataDrop { .. }) {
continue;
}
if let wasmparser::Operator::Call { function_index } = &op_unwrapped {
if is_start && info.main_void_funcs.contains(function_index) {
func.instruction(&wasm_encoder::Instruction::I32Const(0));
skip = true;
}
}
if !skip {
let mut enc_op = if let (
wasmparser::Operator::Call { function_index },
Some((target_hook_orig_idx, target_hook_idx)),
) = (&op_unwrapped, target_thread_spawn_hook)
{
if func_orig_idx != target_hook_orig_idx
&& info.dropped_imports.get(function_index).is_some_and(
|name| name == "__wasip1_vfs_wasi_thread_spawn_wrapper",
)
{
wasm_encoder::Instruction::Call(target_hook_idx)
} else {
crate::wasm_stream::translator::translate(
&op_unwrapped,
&rebinder,
)
}
} else {
crate::wasm_stream::translator::translate(&op_unwrapped, &rebinder)
};
if mem_idx > 0 {
if let Some(mem_info) =
crate::wasm_stream::mem_info::memory_op_info(&op_unwrapped)
{
if mem_info.memory == 0 {
crate::wasm_stream::mem_info::set_memory_index(
&mut enc_op,
mem_idx,
);
}
}
match (&op_unwrapped, &mut enc_op) {
(
wasmparser::Operator::MemorySize { mem: 0, .. }
| wasmparser::Operator::MemoryGrow { mem: 0, .. }
| wasmparser::Operator::MemoryFill { mem: 0 }
| wasmparser::Operator::MemoryDiscard { mem: 0 },
encoded,
) => {
crate::wasm_stream::mem_info::set_memory_index(
encoded, mem_idx,
);
}
(
wasmparser::Operator::MemoryInit { mem: 0, .. },
wasm_encoder::Instruction::MemoryInit { mem, .. },
) => *mem = mem_idx,
(
wasmparser::Operator::MemoryCopy { dst_mem, src_mem },
wasm_encoder::Instruction::MemoryCopy {
dst_mem: encoded_dst_mem,
src_mem: encoded_src_mem,
},
) => {
if *dst_mem == 0 {
*encoded_dst_mem = mem_idx;
}
if *src_mem == 0 {
*encoded_src_mem = mem_idx;
}
}
_ => {}
}
}
func.instruction(&enc_op);
}
}
code_section.function(&func);
}
wasmparser::Payload::DataSection(s) => {
let mut data_section = wasm_encoder::DataSection::new();
let original_data_count = s.count();
for data in s {
let data = data?;
match data.kind {
wasmparser::DataKind::Passive => {
data_section.passive(data.data.iter().copied());
}
wasmparser::DataKind::Active {
memory_index,
offset_expr,
} => {
let mut instrs = Vec::new();
for op in offset_expr.get_operators_reader() {
let op = op?;
if matches!(op, wasmparser::Operator::End) {
continue;
}
instrs.push(crate::wasm_stream::translator::translate(
&op, &rebinder,
));
}
let offset = wasm_encoder::ConstExpr::extended(instrs);
data_section.active(
memory_index,
&offset,
data.data.iter().copied(),
);
}
}
}
for (_, _, bytes) in &info.data_segments {
data_section.passive(bytes.iter().copied());
}
data_count_section.count =
original_data_count + info.data_segments.len() as u32;
has_data_count = true;
data_section_opt = Some(data_section);
}
wasmparser::Payload::CustomSection(s) => {
if !should_emit_custom_section(s.name(), &mut emitted_custom_sections) {
continue;
}
if is_after_code {
custom_sections_after_code.push((s.name().to_string(), s.data().to_vec()));
} else {
module.section(&wasm_encoder::CustomSection {
name: s.name().into(),
data: std::borrow::Cow::Borrowed(s.data()),
});
}
}
_ => {} }
}
for orig_idx in &dropped_func_original_indices {
let name = info.dropped_imports.get(orig_idx).unwrap();
let mut func = wasm_encoder::Function::new(vec![]);
if name == "__wasip1_vfs_wasi_thread_spawn_wrapper" {
let real_thread_spawn_new_idx = real_thread_spawn_new_idx.unwrap();
func.instruction(&wasm_encoder::Instruction::LocalGet(0));
func.instruction(&wasm_encoder::Instruction::Call(real_thread_spawn_new_idx));
func.instruction(&wasm_encoder::Instruction::End);
} else if name == "__wasip1_vfs___self_proc_exit" && needs_host_proc_exit {
func.instruction(&wasm_encoder::Instruction::LocalGet(0));
func.instruction(&wasm_encoder::Instruction::Call(
real_proc_exit_new_idx.unwrap(),
));
func.instruction(&wasm_encoder::Instruction::End);
} else if let Some(target_name) = own_memory_abi::parse_prefixed_target_export(
name,
own_memory_abi::TARGET_MEMORY_COPY_FROM_SUFFIX,
) {
let wasm_mem = self
.target_names
.iter()
.position(|n| own_memory_abi::sanitize_target_name(n) == target_name)
.ok_or_else(|| eyre::eyre!("Target name '{}' not found in provided targets. Ensure the VFS import matches the provided Wasm filename.", target_name))? as u32
+ 1;
func.instruction(&wasm_encoder::Instruction::LocalGet(0));
func.instruction(&wasm_encoder::Instruction::LocalGet(1));
func.instruction(&wasm_encoder::Instruction::LocalGet(2));
func.instruction(&wasm_encoder::Instruction::MemoryCopy {
src_mem: 0,
dst_mem: wasm_mem,
});
func.instruction(&wasm_encoder::Instruction::End);
} else if let Some(target_name) = own_memory_abi::parse_prefixed_target_export(
name,
own_memory_abi::TARGET_MEMORY_COPY_TO_SUFFIX,
) {
let wasm_mem = self
.target_names
.iter()
.position(|n| own_memory_abi::sanitize_target_name(n) == target_name)
.ok_or_else(|| {
eyre::eyre!(
"Target name '{}' not found in provided targets.",
target_name
)
})? as u32
+ 1;
func.instruction(&wasm_encoder::Instruction::LocalGet(0));
func.instruction(&wasm_encoder::Instruction::LocalGet(1));
func.instruction(&wasm_encoder::Instruction::LocalGet(2));
func.instruction(&wasm_encoder::Instruction::MemoryCopy {
src_mem: wasm_mem,
dst_mem: 0,
});
func.instruction(&wasm_encoder::Instruction::End);
} else if let Some(target_name) = own_memory_abi::parse_memory_director_export(name) {
let wasm_mem = self
.target_names
.iter()
.position(|n| own_memory_abi::sanitize_target_name(n) == target_name)
.ok_or_else(|| {
eyre::eyre!(
"Target name '{}' not found in provided targets.",
target_name
)
})? as u32
+ 1;
func.instruction(&wasm_encoder::Instruction::LocalGet(0));
if wasm_mem > 0 {
for i in 0..wasm_mem {
func.instruction(&wasm_encoder::Instruction::MemorySize(i));
if i > 0 {
func.instruction(&wasm_encoder::Instruction::I32Add);
}
}
func.instruction(&wasm_encoder::Instruction::I32Const(65536));
func.instruction(&wasm_encoder::Instruction::I32Mul);
func.instruction(&wasm_encoder::Instruction::I32Add);
}
func.instruction(&wasm_encoder::Instruction::End);
} else if name == "__wasip1_vfs_wasi_thread_start_entry" {
if let Some(&start_idx) = info.exported_funcs.get("wasi_thread_start") {
let start_new_idx = func_map.get(&start_idx).unwrap();
func.instruction(&wasm_encoder::Instruction::LocalGet(0));
func.instruction(&wasm_encoder::Instruction::LocalGet(1));
func.instruction(&wasm_encoder::Instruction::Call(*start_new_idx));
}
func.instruction(&wasm_encoder::Instruction::End);
} else if let Some(target_name) = wrap_unreachable_proc_exit_target(name, &info) {
if let Some(&set_flag_idx) = info.set_unreachable_flag_funcs.get(target_name) {
func.instruction(&wasm_encoder::Instruction::LocalGet(0));
func.instruction(&wasm_encoder::Instruction::Call(
rebinder.function(set_flag_idx),
));
}
func.instruction(&wasm_encoder::Instruction::End);
} else if name.ends_with("_memory_trap") {
func.instruction(&wasm_encoder::Instruction::LocalGet(0));
func.instruction(&wasm_encoder::Instruction::End);
} else if name.ends_with("_memory_grow_alt") {
func.instruction(&wasm_encoder::Instruction::LocalGet(0));
func.instruction(&wasm_encoder::Instruction::MemoryGrow(0));
func.instruction(&wasm_encoder::Instruction::End);
} else if name.ends_with("__thread_start") {
let target_name = name
.strip_prefix("__wasip1_vfs_")
.ok_or_else(|| eyre::eyre!("Failed prefix strip: {}", name))?
.strip_suffix("__thread_start")
.ok_or_else(|| eyre::eyre!("Failed to strip suffix from {}", name))?;
if let Some(&flesh_idx) = info.start_funcs.flesh_target_starts.get(target_name) {
func.instruction(&wasm_encoder::Instruction::Call(
rebinder.function(flesh_idx),
));
}
func.instruction(&wasm_encoder::Instruction::End);
} else if name.ends_with("__start") {
let target_name = name
.strip_prefix("__wasip1_vfs_")
.unwrap()
.strip_suffix("__start")
.unwrap();
if let Some(&orig_start_idx) = info.target_runtime_starts.get(target_name) {
func.instruction(&wasm_encoder::Instruction::Call(
rebinder.function(orig_start_idx),
));
}
func.instruction(&wasm_encoder::Instruction::End);
} else if name.ends_with("__main_void") {
let raw_target_name = name.strip_prefix("__wasip1_vfs_").unwrap();
let target_name = raw_target_name
.strip_suffix("___main_void")
.or_else(|| {
raw_target_name
.strip_suffix("__main_void")
.map(|name| name.strip_suffix('_').unwrap_or(name))
})
.unwrap();
let main_void_name = format!("__wasip1_vfs_{target_name}___main_void");
if let Some(&orig_main_void_idx) = info.exported_funcs.get(&main_void_name) {
func.instruction(&wasm_encoder::Instruction::Call(
rebinder.function(orig_main_void_idx),
));
} else {
if let Some(&orig_start_idx) = info.target_runtime_starts.get(target_name) {
func.instruction(&wasm_encoder::Instruction::Call(
rebinder.function(orig_start_idx),
));
}
func.instruction(&wasm_encoder::Instruction::I32Const(0));
}
func.instruction(&wasm_encoder::Instruction::End);
} else if name == "__wasip1_vfs_reset_on_thread_once" {
func.instruction(&wasm_encoder::Instruction::End);
} else if name == "__wvl_poll_atomic_wait" {
if self.threads {
const MAX_POLL_WAIT_NS: i64 = 100_000_000;
func.instruction(&wasm_encoder::Instruction::LocalGet(0));
func.instruction(&wasm_encoder::Instruction::LocalGet(1));
func.instruction(&wasm_encoder::Instruction::LocalGet(2));
func.instruction(&wasm_encoder::Instruction::I64Const(MAX_POLL_WAIT_NS));
func.instruction(&wasm_encoder::Instruction::LocalGet(2));
func.instruction(&wasm_encoder::Instruction::I64Const(MAX_POLL_WAIT_NS));
func.instruction(&wasm_encoder::Instruction::I64LtU);
func.instruction(&wasm_encoder::Instruction::Select);
func.instruction(&wasm_encoder::Instruction::MemoryAtomicWait32(
wasm_encoder::MemArg {
align: 2,
offset: 0,
memory_index: 0,
},
));
} else {
func.instruction(&wasm_encoder::Instruction::I32Const(2));
}
func.instruction(&wasm_encoder::Instruction::End);
} else if name == "__wvl_atomic_wait32_vfs" {
func.instruction(&wasm_encoder::Instruction::LocalGet(0));
func.instruction(&wasm_encoder::Instruction::LocalGet(1));
func.instruction(&wasm_encoder::Instruction::LocalGet(2));
func.instruction(&wasm_encoder::Instruction::MemoryAtomicWait32(
wasm_encoder::MemArg {
align: 2,
offset: 0,
memory_index: 0,
},
));
func.instruction(&wasm_encoder::Instruction::End);
} else if name == "__wvl_atomic_notify_vfs" {
func.instruction(&wasm_encoder::Instruction::LocalGet(0));
func.instruction(&wasm_encoder::Instruction::LocalGet(1));
func.instruction(&wasm_encoder::Instruction::MemoryAtomicNotify(
wasm_encoder::MemArg {
align: 2,
offset: 0,
memory_index: 0,
},
));
func.instruction(&wasm_encoder::Instruction::End);
} else if name == "__wvl_atomic_cmpxchg32_vfs" {
func.instruction(&wasm_encoder::Instruction::LocalGet(0));
func.instruction(&wasm_encoder::Instruction::LocalGet(1));
func.instruction(&wasm_encoder::Instruction::LocalGet(2));
func.instruction(&wasm_encoder::Instruction::I32AtomicRmwCmpxchg(
wasm_encoder::MemArg {
align: 2,
offset: 0,
memory_index: 0,
},
));
func.instruction(&wasm_encoder::Instruction::End);
} else if name == "__wvl_atomic_store32_vfs" {
func.instruction(&wasm_encoder::Instruction::LocalGet(0));
func.instruction(&wasm_encoder::Instruction::LocalGet(1));
func.instruction(&wasm_encoder::Instruction::I32AtomicStore(
wasm_encoder::MemArg {
align: 2,
offset: 0,
memory_index: 0,
},
));
func.instruction(&wasm_encoder::Instruction::End);
} else if name == "__wvl_atomic_load32_target" || name == "__wvl_atomic_load64_target" {
for i in 0..self.target_names.len() {
let mem_idx = (i + 1) as u32;
func.instruction(&wasm_encoder::Instruction::LocalGet(0));
func.instruction(&wasm_encoder::Instruction::I32Const(i as i32));
func.instruction(&wasm_encoder::Instruction::I32Eq);
func.instruction(&wasm_encoder::Instruction::If(
wasm_encoder::BlockType::Empty,
));
func.instruction(&wasm_encoder::Instruction::LocalGet(1));
if name == "__wvl_atomic_load32_target" {
func.instruction(&wasm_encoder::Instruction::I32AtomicLoad(
wasm_encoder::MemArg {
align: 2,
offset: 0,
memory_index: mem_idx,
},
));
} else {
func.instruction(&wasm_encoder::Instruction::I64AtomicLoad(
wasm_encoder::MemArg {
align: 3,
offset: 0,
memory_index: mem_idx,
},
));
}
func.instruction(&wasm_encoder::Instruction::Return);
func.instruction(&wasm_encoder::Instruction::End);
}
func.instruction(&wasm_encoder::Instruction::Unreachable);
func.instruction(&wasm_encoder::Instruction::End);
} else if name.ends_with("_reset") {
let target_name = name
.strip_prefix("__wasip1_vfs_")
.ok_or_else(|| eyre::eyre!("Failed prefix strip: {}", name))?
.strip_suffix("_reset")
.ok_or_else(|| eyre::eyre!("Failed to strip suffix from {}", name))?;
let wasm_mem = self
.target_names
.iter()
.position(|n| own_memory_abi::sanitize_target_name(n) == target_name)
.ok_or_else(|| {
eyre::eyre!(
"Target name '{}' not found in provided targets.",
target_name
)
})? as u32
+ 1;
if let Some(&reset_atomic_idx) =
info.exported_funcs.get("__vfs_atomic_reset_target")
{
func.instruction(&wasm_encoder::Instruction::I32Const(wasm_mem as i32 - 1));
func.instruction(&wasm_encoder::Instruction::Call(
rebinder.function(reset_atomic_idx),
));
}
if let Some(&reset_globals_idx) = info.reset_globals_funcs.get(target_name) {
func.instruction(&wasm_encoder::Instruction::Call(
rebinder.function(reset_globals_idx),
));
}
let target_data_segments =
data_segments_for_memory(&info.data_segments, wasm_mem).collect::<Vec<_>>();
func.instruction(&wasm_encoder::Instruction::I32Const(0)); func.instruction(&wasm_encoder::Instruction::I32Const(0)); func.instruction(&wasm_encoder::Instruction::MemorySize(wasm_mem)); func.instruction(&wasm_encoder::Instruction::I32Const(65536));
func.instruction(&wasm_encoder::Instruction::I32Mul); func.instruction(&wasm_encoder::Instruction::MemoryFill(wasm_mem));
for (segment_index, offset, bytes) in target_data_segments {
func.instruction(&wasm_encoder::Instruction::I32Const(offset)); func.instruction(&wasm_encoder::Instruction::I32Const(0)); func.instruction(&wasm_encoder::Instruction::I32Const(bytes.len() as i32)); func.instruction(&wasm_encoder::Instruction::MemoryInit {
data_index: info.original_data_count + segment_index as u32,
mem: wasm_mem,
});
}
if let Some(&start_idx) = info.start_funcs.flesh_target_starts.get(target_name) {
func.instruction(&wasm_encoder::Instruction::Call(
rebinder.function(start_idx),
));
}
func.instruction(&wasm_encoder::Instruction::End);
} else if is_void_detector_helper(name) {
func.instruction(&wasm_encoder::Instruction::End);
} else if let Some(value) = detector_helper_stub_i32(name) {
func.instruction(&wasm_encoder::Instruction::I32Const(value));
func.instruction(&wasm_encoder::Instruction::End);
} else {
func.instruction(&wasm_encoder::Instruction::Unreachable);
func.instruction(&wasm_encoder::Instruction::End);
}
code_section.function(&func);
}
let fn_in_starts = FnInStarts::new(&self.target_names);
let start_func =
fn_in_starts.emit_start_body(&info.start_funcs, &self.target_names, |idx| {
rebinder.function(idx)
});
code_section.function(&start_func);
if has_data_count {
module.section(&data_count_section);
}
module.section(&code_section);
if let Some(ds) = data_section_opt {
module.section(&ds);
}
for (name, custom_data) in custom_sections_after_code {
module.section(&wasm_encoder::CustomSection {
name: name.into(),
data: std::borrow::Cow::Borrowed(&custom_data),
});
}
Ok(module.finish())
}
}
#[cfg(test)]
mod tests {
use super::*;
use wasm_encoder::{
CodeSection, DataCountSection, DataSection, ExportKind, ExportSection, Function,
FunctionSection, ImportSection, Instruction, MemorySection, Module, TypeSection,
};
#[test]
fn emits_synthesized_start_section_and_export() -> eyre::Result<()> {
let input = module_with_flesh_vfs_start();
let mut pass = PostCombineStreamPass::new("vfs".to_string(), Vec::new(), vec![1], true);
let output = pass.run(&input)?;
let mut exported_start = None;
let mut section_start = None;
for payload in wasmparser::Parser::new(0).parse_all(&output) {
match payload? {
wasmparser::Payload::ExportSection(exports) => {
for export in exports {
let export = export?;
if export.name == "_start" {
exported_start = Some(export.index);
}
}
}
wasmparser::Payload::StartSection { func, .. } => {
section_start = Some(func);
}
_ => {}
}
}
assert_eq!(exported_start, section_start);
assert_eq!(section_start, Some(1));
Ok(())
}
#[test]
fn synthesized_start_prefers_vfs_thread_initializer_export() -> eyre::Result<()> {
let input = module_with_thread_initializer();
let mut pass = PostCombineStreamPass::new("vfs".to_string(), Vec::new(), vec![3], true);
let output = pass.run(&input)?;
let mut start = None;
let mut calls = Vec::new();
let mut func_idx = 0;
for payload in wasmparser::Parser::new(0).parse_all(&output) {
match payload? {
wasmparser::Payload::StartSection { func, .. } => {
start = Some(func);
}
wasmparser::Payload::CodeSectionEntry(body) => {
if Some(func_idx) == start {
for op in body.get_operators_reader()? {
if let wasmparser::Operator::Call { function_index } = op? {
calls.push(function_index);
}
}
}
func_idx += 1;
}
_ => {}
}
}
assert_eq!(start, Some(3));
assert_eq!(calls, vec![0, 1]);
Ok(())
}
#[test]
fn synthesized_start_uses_flesh_target_start_export() -> eyre::Result<()> {
let input = module_with_target_start_pair();
let mut pass = PostCombineStreamPass::new(
"vfs".to_string(),
vec!["target".to_string()],
vec![3],
true,
);
let output = pass.run(&input)?;
let calls = start_calls(&output)?;
assert_eq!(calls, vec![0, 1]);
Ok(())
}
#[test]
fn reset_data_segments_are_scoped_to_the_target_memory() {
let segments = vec![
(1, 16, vec![1, 2]),
(2, 32, vec![3, 4, 5]),
(1, 48, vec![6]),
];
let selected = data_segments_for_memory(&segments, 2).collect::<Vec<_>>();
assert_eq!(selected, vec![(1, 32, &[3, 4, 5][..])]);
}
#[test]
fn drops_pipeline_exports_but_keeps_late_link_exports() {
assert!(should_drop_export("__flesh_target_name_start"));
assert!(should_drop_export(
"__wasip1_virt_layer_target_name_wrap_unreachable"
));
assert!(!should_drop_export("__wasip1_vfs_target_name_args_get"));
assert!(!should_drop_export(
"__wasip1_vfs_target_name_wasi_thread_start"
));
}
#[test]
fn filters_invalid_and_duplicate_custom_sections() {
let mut emitted = HashSet::new();
assert!(should_emit_custom_section("producers", &mut emitted));
assert!(!should_emit_custom_section("producers", &mut emitted));
assert!(!should_emit_custom_section("\u{8}l", &mut emitted));
assert!(should_emit_custom_section("component-name", &mut emitted));
}
#[test]
fn non_threaded_build_does_not_emit_start_section() -> eyre::Result<()> {
let input = module_with_flesh_vfs_start();
let mut pass = PostCombineStreamPass::new("vfs".to_string(), Vec::new(), vec![1], false);
let output = pass.run(&input)?;
let has_start_section =
wasmparser::Parser::new(0)
.parse_all(&output)
.try_fold(false, |found, payload| {
Ok::<_, wasmparser::BinaryReaderError>(
found || matches!(payload?, wasmparser::Payload::StartSection { .. }),
)
})?;
assert!(!has_start_section);
Ok(())
}
#[test]
fn implements_poll_wait_with_atomic_wait_for_threads() -> eyre::Result<()> {
let input = module_with_poll_wait_import();
let mut pass = PostCombineStreamPass::new("vfs".to_string(), Vec::new(), vec![1], true);
let output = pass.run(&input)?;
let mut found_wait = false;
let mut found_unsigned_clamp = false;
for payload in wasmparser::Parser::new(0).parse_all(&output) {
if let wasmparser::Payload::CodeSectionEntry(body) = payload? {
let ops = body
.get_operators_reader()?
.into_iter()
.collect::<Result<Vec<_>, _>>()?;
for op in &ops {
if matches!(
op,
wasmparser::Operator::MemoryAtomicWait32 {
memarg: wasmparser::MemArg { memory: 0, .. }
}
) {
found_wait = true;
}
}
found_unsigned_clamp |= ops.windows(4).any(|ops| {
matches!(
ops,
[
wasmparser::Operator::LocalGet { local_index: 2 },
wasmparser::Operator::I64Const { value: 100_000_000 },
wasmparser::Operator::I64LtU,
wasmparser::Operator::Select
]
)
});
}
}
assert!(found_wait);
assert!(found_unsigned_clamp);
Ok(())
}
#[test]
fn implements_poll_wait_as_timeout_without_threads() -> eyre::Result<()> {
let input = module_with_poll_wait_import();
let mut pass = PostCombineStreamPass::new("vfs".to_string(), Vec::new(), vec![1], false);
let output = pass.run(&input)?;
let mut found = false;
for payload in wasmparser::Parser::new(0).parse_all(&output) {
if let wasmparser::Payload::CodeSectionEntry(body) = payload? {
let ops = body
.get_operators_reader()?
.into_iter()
.collect::<Result<Vec<_>, _>>()?;
if matches!(
ops.as_slice(),
[
wasmparser::Operator::I32Const { value: 2 },
wasmparser::Operator::End
]
) {
found = true;
}
}
}
assert!(found);
Ok(())
}
#[test]
fn vfs_thread_spawn_wrapper_uses_real_thread_spawn() -> eyre::Result<()> {
let input = module_with_thread_spawn_wrapper_import_and_caller();
let mut pass = PostCombineStreamPass::new(
"vfs".to_string(),
vec!["target".to_string()],
vec![1],
true,
);
let output = pass.run(&input)?;
let caller_calls = function_calls_by_export(&output, "wrapper_import_caller")?;
assert_eq!(caller_calls.len(), 1);
let generated_wrapper = caller_calls[0];
assert_eq!(function_calls(&output, generated_wrapper)?, vec![0]);
Ok(())
}
#[test]
fn target_thread_spawn_import_resolves_to_target_specific_hook() -> eyre::Result<()> {
let input = module_with_target_thread_spawn_import_and_hook();
let mut pass = PostCombineStreamPass::new(
"vfs".to_string(),
vec!["target".to_string()],
vec![3],
true,
);
let output = pass.run(&input)?;
let mut target_hook = None;
let mut caller = None;
for payload in wasmparser::Parser::new(0).parse_all(&output) {
if let wasmparser::Payload::ExportSection(exports) = payload? {
for export in exports {
let export = export?;
if export.name == "__wasip1_vfs_wasi_thread_spawn_target" {
target_hook = Some(export.index);
}
if export.name == "target_spawn_caller" {
caller = Some(export.index);
}
}
}
}
let target_hook = target_hook.expect("target hook export should remain available");
let caller = caller.expect("test caller export should remain available");
assert_eq!(function_calls(&output, caller)?, vec![target_hook]);
Ok(())
}
#[test]
fn target_wasi_thread_start_import_resolves_to_target_export() -> eyre::Result<()> {
let input = module_with_target_wasi_thread_start_import_and_export();
let mut pass = PostCombineStreamPass::new(
"vfs".to_string(),
vec!["target".to_string()],
vec![3],
true,
);
let output = pass.run(&input)?;
let mut target_start = None;
let mut caller = None;
for payload in wasmparser::Parser::new(0).parse_all(&output) {
if let wasmparser::Payload::ExportSection(exports) = payload? {
for export in exports {
let export = export?;
if export.name == "__wasip1_vfs_target_wasi_thread_start" {
target_start = Some(export.index);
}
if export.name == "target_wasi_thread_start_caller" {
caller = Some(export.index);
}
}
}
}
let target_start =
target_start.expect("target wasi_thread_start export should remain available");
let caller = caller.expect("test caller export should remain available");
assert_eq!(function_calls(&output, caller)?, vec![target_start]);
Ok(())
}
#[test]
fn target_defined_generic_thread_spawn_wrapper_call_resolves_to_target_hook() -> eyre::Result<()>
{
let input = module_with_vfs_and_target_generic_thread_spawn_wrapper_callers();
let mut pass = PostCombineStreamPass::new(
"vfs".to_string(),
vec!["target".to_string()],
vec![1, 2],
true,
);
let output = pass.run(&input)?;
let mut target_hook = None;
let mut vfs_caller = None;
let mut target_caller = None;
for payload in wasmparser::Parser::new(0).parse_all(&output) {
if let wasmparser::Payload::ExportSection(exports) = payload? {
for export in exports {
let export = export?;
match export.name {
"vfs_spawn_caller" => vfs_caller = Some(export.index),
"target_spawn_caller" => target_caller = Some(export.index),
"__wasip1_vfs_wasi_thread_spawn_target" => target_hook = Some(export.index),
_ => {}
}
}
}
}
let target_hook = target_hook.expect("target hook export should remain available");
let vfs_caller = vfs_caller.expect("vfs caller export should remain available");
let target_caller = target_caller.expect("target caller export should remain available");
let vfs_calls = function_calls(&output, vfs_caller)?;
let target_hook_calls = function_calls(&output, target_hook)?;
assert_eq!(function_calls(&output, target_caller)?, vec![target_hook]);
assert_eq!(vfs_calls.len(), 1);
assert_ne!(vfs_calls, vec![target_hook]);
assert_eq!(target_hook_calls.len(), 1);
assert_ne!(target_hook_calls, vec![target_hook]);
Ok(())
}
#[test]
fn target_defined_bulk_memory_ops_are_scoped_to_target_memory() -> eyre::Result<()> {
let input = module_with_target_bulk_memory_ops();
let mut pass = PostCombineStreamPass::new(
"vfs".to_string(),
vec!["target".to_string()],
vec![1, 1],
false,
);
let output = pass.run(&input)?;
let ops = function_ops(&output, "target_bulk_ops")?;
assert!(ops.iter().any(|op| matches!(
op,
wasmparser::Operator::MemoryCopy {
dst_mem: 1,
src_mem: 1,
}
)));
assert!(
ops.iter()
.any(|op| matches!(op, wasmparser::Operator::MemoryFill { mem: 1 }))
);
assert!(
ops.iter()
.any(|op| matches!(op, wasmparser::Operator::MemoryInit { mem: 1, .. }))
);
Ok(())
}
#[test]
fn detector_helper_imports_are_intentional_when_unresolved() -> eyre::Result<()> {
let input = module_with_detector_observe_import();
let mut pass = PostCombineStreamPass::new("vfs".to_string(), Vec::new(), vec![1], true);
let output = pass.run(&input)?;
wasmparser::Validator::new().validate_all(&output)?;
assert!(!imports_func(&output, "__vfs_atomic_observe_write")?);
Ok(())
}
#[test]
fn unresolved_detector_notify_stub_returns_zero() -> eyre::Result<()> {
let input = module_with_detector_notify_import();
let mut pass = PostCombineStreamPass::new("vfs".to_string(), Vec::new(), vec![1], true);
let output = pass.run(&input)?;
wasmparser::Validator::new().validate_all(&output)?;
let calls = function_calls_by_export(&output, "detector_notify_caller")?;
let stub_ops = function_ops_by_index(&output, calls[0])?;
assert!(matches!(
stub_ops.as_slice(),
[
wasmparser::Operator::I32Const { value: 0 },
wasmparser::Operator::End,
]
));
Ok(())
}
fn start_calls(output: &[u8]) -> eyre::Result<Vec<u32>> {
let mut start = None;
let mut calls = Vec::new();
let mut func_idx = 0;
for payload in wasmparser::Parser::new(0).parse_all(output) {
match payload? {
wasmparser::Payload::StartSection { func, .. } => {
start = Some(func);
}
wasmparser::Payload::CodeSectionEntry(body) => {
if Some(func_idx) == start {
for op in body.get_operators_reader()? {
if let wasmparser::Operator::Call { function_index } = op? {
calls.push(function_index);
}
}
}
func_idx += 1;
}
_ => {}
}
}
Ok(calls)
}
fn function_ops<'a>(
output: &'a [u8],
export_name: &str,
) -> eyre::Result<Vec<wasmparser::Operator<'a>>> {
let mut imported_funcs = 0;
let mut target_func_idx = None;
let mut defined_func_idx = 0;
for payload in wasmparser::Parser::new(0).parse_all(output) {
match payload? {
wasmparser::Payload::ImportSection(imports) => {
for group in imports {
for import in group?.into_iter() {
let (_, import) = import?;
if matches!(import.ty, wasmparser::TypeRef::Func(_)) {
imported_funcs += 1;
}
}
}
}
wasmparser::Payload::ExportSection(exports) => {
for export in exports {
let export = export?;
if export.name == export_name {
target_func_idx = Some(export.index);
}
}
}
wasmparser::Payload::CodeSectionEntry(body) => {
let absolute_idx = imported_funcs + defined_func_idx;
if Some(absolute_idx) == target_func_idx {
return body
.get_operators_reader()?
.into_iter()
.collect::<Result<Vec<_>, _>>()
.map_err(Into::into);
}
defined_func_idx += 1;
}
_ => {}
}
}
eyre::bail!("exported function {export_name:?} not found")
}
fn function_ops_by_index<'a>(
output: &'a [u8],
target_func_idx: u32,
) -> eyre::Result<Vec<wasmparser::Operator<'a>>> {
let mut imported_funcs = 0;
let mut defined_func_idx = 0;
for payload in wasmparser::Parser::new(0).parse_all(output) {
match payload? {
wasmparser::Payload::ImportSection(imports) => {
for group in imports {
for import in group?.into_iter() {
let (_, import) = import?;
if matches!(import.ty, wasmparser::TypeRef::Func(_)) {
imported_funcs += 1;
}
}
}
}
wasmparser::Payload::CodeSectionEntry(body) => {
let absolute_idx = imported_funcs + defined_func_idx;
if absolute_idx == target_func_idx {
return body
.get_operators_reader()?
.into_iter()
.collect::<Result<Vec<_>, _>>()
.map_err(Into::into);
}
defined_func_idx += 1;
}
_ => {}
}
}
eyre::bail!("function index {target_func_idx} not found")
}
fn function_calls(output: &[u8], target_func_idx: u32) -> eyre::Result<Vec<u32>> {
let mut calls = Vec::new();
let mut imported_funcs = 0;
let mut defined_func_idx = 0;
for payload in wasmparser::Parser::new(0).parse_all(output) {
match payload? {
wasmparser::Payload::ImportSection(imports) => {
for group in imports {
for import in group?.into_iter() {
let (_, import) = import?;
if matches!(import.ty, wasmparser::TypeRef::Func(_)) {
imported_funcs += 1;
}
}
}
}
wasmparser::Payload::CodeSectionEntry(body) => {
let absolute_idx = imported_funcs + defined_func_idx;
if absolute_idx == target_func_idx {
for op in body.get_operators_reader()? {
if let wasmparser::Operator::Call { function_index } = op? {
calls.push(function_index);
}
}
}
defined_func_idx += 1;
}
_ => {}
}
}
Ok(calls)
}
fn function_calls_by_export(output: &[u8], export_name: &str) -> eyre::Result<Vec<u32>> {
let mut target_func_idx = None;
for payload in wasmparser::Parser::new(0).parse_all(output) {
if let wasmparser::Payload::ExportSection(exports) = payload? {
for export in exports {
let export = export?;
if export.name == export_name {
target_func_idx = Some(export.index);
}
}
}
}
let target_func_idx = target_func_idx
.ok_or_else(|| eyre::eyre!("exported function {export_name:?} not found"))?;
function_calls(output, target_func_idx)
}
fn imports_func(output: &[u8], name: &str) -> eyre::Result<bool> {
for payload in wasmparser::Parser::new(0).parse_all(output) {
if let wasmparser::Payload::ImportSection(imports) = payload? {
for group in imports {
for import in group?.into_iter() {
let (_, import) = import?;
if import.name == name && matches!(import.ty, wasmparser::TypeRef::Func(_))
{
return Ok(true);
}
}
}
}
}
Ok(false)
}
fn module_with_flesh_vfs_start() -> Vec<u8> {
let mut module = Module::new();
let mut types = TypeSection::new();
types.ty().function([], []);
module.section(&types);
let mut functions = FunctionSection::new();
functions.function(0);
module.section(&functions);
let mut exports = ExportSection::new();
exports.export("__flesh_vfs_start", ExportKind::Func, 0);
module.section(&exports);
let mut code = CodeSection::new();
let mut func = Function::new([]);
func.instruction(&Instruction::End);
code.function(&func);
module.section(&code);
module.finish()
}
fn module_with_detector_observe_import() -> Vec<u8> {
let mut module = Module::new();
let mut types = TypeSection::new();
types.ty().function(
[
wasm_encoder::ValType::I32,
wasm_encoder::ValType::I32,
wasm_encoder::ValType::I32,
wasm_encoder::ValType::I32,
],
[],
);
types.ty().function([], []);
module.section(&types);
let mut imports = ImportSection::new();
imports.import(
"wasi_snapshot_preview1",
"__vfs_atomic_observe_write",
wasm_encoder::EntityType::Function(0),
);
module.section(&imports);
let mut functions = FunctionSection::new();
functions.function(1);
module.section(&functions);
let mut exports = ExportSection::new();
exports.export("detector_observe_caller", ExportKind::Func, 1);
module.section(&exports);
let mut code = CodeSection::new();
let mut caller = Function::new([]);
caller.instruction(&Instruction::I32Const(0));
caller.instruction(&Instruction::I32Const(1));
caller.instruction(&Instruction::I32Const(4));
caller.instruction(&Instruction::I32Const(4));
caller.instruction(&Instruction::Call(0));
caller.instruction(&Instruction::End);
code.function(&caller);
module.section(&code);
module.finish()
}
fn module_with_detector_notify_import() -> Vec<u8> {
let mut module = Module::new();
let mut types = TypeSection::new();
types.ty().function(
[
wasm_encoder::ValType::I32,
wasm_encoder::ValType::I32,
wasm_encoder::ValType::I32,
],
[wasm_encoder::ValType::I32],
);
types.ty().function([], [wasm_encoder::ValType::I32]);
module.section(&types);
let mut imports = ImportSection::new();
imports.import(
"wasi_snapshot_preview1",
"__vfs_atomic_notify",
wasm_encoder::EntityType::Function(0),
);
module.section(&imports);
let mut functions = FunctionSection::new();
functions.function(1);
module.section(&functions);
let mut exports = ExportSection::new();
exports.export("detector_notify_caller", ExportKind::Func, 1);
module.section(&exports);
let mut code = CodeSection::new();
let mut caller = Function::new([]);
caller.instruction(&Instruction::I32Const(0));
caller.instruction(&Instruction::I32Const(4));
caller.instruction(&Instruction::I32Const(1));
caller.instruction(&Instruction::Call(0));
caller.instruction(&Instruction::End);
code.function(&caller);
module.section(&code);
module.finish()
}
fn module_with_thread_initializer() -> Vec<u8> {
let mut module = Module::new();
let mut types = TypeSection::new();
types.ty().function([], []);
module.section(&types);
let mut functions = FunctionSection::new();
functions.function(0);
functions.function(0);
functions.function(0);
module.section(&functions);
let mut exports = ExportSection::new();
exports.export("__flesh_vfs_start", ExportKind::Func, 0);
exports.export("__wasip1_vfs_thread_initializer", ExportKind::Func, 1);
exports.export("__thread_patch", ExportKind::Func, 2);
module.section(&exports);
let mut code = CodeSection::new();
for _ in 0..3 {
let mut func = Function::new([]);
func.instruction(&Instruction::End);
code.function(&func);
}
module.section(&code);
module.finish()
}
fn module_with_target_start_pair() -> Vec<u8> {
let mut module = Module::new();
let mut types = TypeSection::new();
types.ty().function([], []);
module.section(&types);
let mut functions = FunctionSection::new();
functions.function(0);
functions.function(0);
functions.function(0);
module.section(&functions);
let mut exports = ExportSection::new();
exports.export("__flesh_vfs_start", ExportKind::Func, 0);
exports.export("__wasip1_vfs_target__start", ExportKind::Func, 2);
exports.export("__flesh_target_start", ExportKind::Func, 1);
module.section(&exports);
let mut code = CodeSection::new();
for _ in 0..3 {
let mut func = Function::new([]);
func.instruction(&Instruction::End);
code.function(&func);
}
module.section(&code);
module.finish()
}
fn module_with_thread_spawn_wrapper_import_and_caller() -> Vec<u8> {
let mut module = Module::new();
let mut types = TypeSection::new();
types
.ty()
.function([wasm_encoder::ValType::I32], [wasm_encoder::ValType::I32]);
types.ty().function([], [wasm_encoder::ValType::I32]);
module.section(&types);
let mut imports = ImportSection::new();
imports.import(
"env",
"__wasip1_vfs_wasi_thread_spawn_wrapper",
wasm_encoder::EntityType::Function(0),
);
imports.import(
"wasip1-vfs:host/virtual-file-system-wasip1-threads-import",
"[static]wasip1-threads.thread-spawn-import",
wasm_encoder::EntityType::Function(0),
);
module.section(&imports);
let mut functions = FunctionSection::new();
functions.function(1);
module.section(&functions);
let mut exports = ExportSection::new();
exports.export("wrapper_import_caller", ExportKind::Func, 2);
module.section(&exports);
let mut code = CodeSection::new();
let mut caller = Function::new([]);
caller.instruction(&Instruction::I32Const(1));
caller.instruction(&Instruction::Call(0));
caller.instruction(&Instruction::End);
code.function(&caller);
module.section(&code);
module.finish()
}
fn module_with_target_thread_spawn_import_and_hook() -> Vec<u8> {
let mut module = Module::new();
let mut types = TypeSection::new();
types
.ty()
.function([wasm_encoder::ValType::I32], [wasm_encoder::ValType::I32]);
module.section(&types);
let mut imports = ImportSection::new();
imports.import(
"__wasip1_vfs-host",
"__wasip1_vfs_wasi_thread_spawn_target",
wasm_encoder::EntityType::Function(0),
);
module.section(&imports);
let mut functions = FunctionSection::new();
functions.function(0);
functions.function(0);
module.section(&functions);
let mut exports = ExportSection::new();
exports.export("target_spawn_caller", ExportKind::Func, 1);
exports.export("__wasip1_vfs_wasi_thread_spawn_target", ExportKind::Func, 2);
module.section(&exports);
let mut code = CodeSection::new();
let mut caller = Function::new([]);
caller.instruction(&Instruction::LocalGet(0));
caller.instruction(&Instruction::Call(0));
caller.instruction(&Instruction::End);
code.function(&caller);
let mut target_hook = Function::new([]);
target_hook.instruction(&Instruction::LocalGet(0));
target_hook.instruction(&Instruction::End);
code.function(&target_hook);
module.section(&code);
module.finish()
}
fn module_with_vfs_and_target_generic_thread_spawn_wrapper_callers() -> Vec<u8> {
let mut module = Module::new();
let mut types = TypeSection::new();
types
.ty()
.function([wasm_encoder::ValType::I32], [wasm_encoder::ValType::I32]);
types.ty().function([], [wasm_encoder::ValType::I32]);
module.section(&types);
let mut imports = ImportSection::new();
imports.import(
"env",
"__wasip1_vfs_wasi_thread_spawn_wrapper",
wasm_encoder::EntityType::Function(0),
);
module.section(&imports);
let mut functions = FunctionSection::new();
functions.function(0);
functions.function(0);
functions.function(0);
module.section(&functions);
let mut exports = ExportSection::new();
exports.export("vfs_spawn_caller", ExportKind::Func, 1);
exports.export("target_spawn_caller", ExportKind::Func, 2);
exports.export("__wasip1_vfs_wasi_thread_spawn_target", ExportKind::Func, 3);
module.section(&exports);
let mut code = CodeSection::new();
let mut vfs_caller = Function::new([]);
vfs_caller.instruction(&Instruction::LocalGet(0));
vfs_caller.instruction(&Instruction::Call(0));
vfs_caller.instruction(&Instruction::End);
code.function(&vfs_caller);
let mut target_caller = Function::new([]);
target_caller.instruction(&Instruction::LocalGet(0));
target_caller.instruction(&Instruction::Call(0));
target_caller.instruction(&Instruction::End);
code.function(&target_caller);
let mut target_hook = Function::new([]);
target_hook.instruction(&Instruction::LocalGet(0));
target_hook.instruction(&Instruction::Call(0));
target_hook.instruction(&Instruction::End);
code.function(&target_hook);
module.section(&code);
module.finish()
}
fn module_with_target_wasi_thread_start_import_and_export() -> Vec<u8> {
let mut module = Module::new();
let mut types = TypeSection::new();
types
.ty()
.function([wasm_encoder::ValType::I32, wasm_encoder::ValType::I32], []);
module.section(&types);
let mut imports = ImportSection::new();
imports.import(
"wasip1-vfs",
"__wasip1_vfs_target_wasi_thread_start",
wasm_encoder::EntityType::Function(0),
);
module.section(&imports);
let mut functions = FunctionSection::new();
functions.function(0);
functions.function(0);
module.section(&functions);
let mut exports = ExportSection::new();
exports.export("target_wasi_thread_start_caller", ExportKind::Func, 1);
exports.export("__wasip1_vfs_target_wasi_thread_start", ExportKind::Func, 2);
module.section(&exports);
let mut code = CodeSection::new();
let mut caller = Function::new([]);
caller.instruction(&Instruction::LocalGet(0));
caller.instruction(&Instruction::LocalGet(1));
caller.instruction(&Instruction::Call(0));
caller.instruction(&Instruction::End);
code.function(&caller);
let mut target_start = Function::new([]);
target_start.instruction(&Instruction::End);
code.function(&target_start);
module.section(&code);
module.finish()
}
fn module_with_target_bulk_memory_ops() -> Vec<u8> {
let mut module = Module::new();
let mut types = TypeSection::new();
types.ty().function([], []);
module.section(&types);
let mut functions = FunctionSection::new();
functions.function(0);
functions.function(0);
module.section(&functions);
let mut memories = MemorySection::new();
for _ in 0..2 {
memories.memory(wasm_encoder::MemoryType {
minimum: 1,
maximum: None,
memory64: false,
shared: false,
page_size_log2: None,
});
}
module.section(&memories);
let mut exports = ExportSection::new();
exports.export("target_bulk_ops", ExportKind::Func, 1);
module.section(&exports);
module.section(&DataCountSection { count: 1 });
let mut code = CodeSection::new();
let mut vfs_func = Function::new([]);
vfs_func.instruction(&Instruction::End);
code.function(&vfs_func);
let mut target_func = Function::new([]);
target_func.instruction(&Instruction::I32Const(0));
target_func.instruction(&Instruction::I32Const(4));
target_func.instruction(&Instruction::I32Const(8));
target_func.instruction(&Instruction::MemoryCopy {
dst_mem: 0,
src_mem: 0,
});
target_func.instruction(&Instruction::I32Const(16));
target_func.instruction(&Instruction::I32Const(0));
target_func.instruction(&Instruction::I32Const(4));
target_func.instruction(&Instruction::MemoryFill(0));
target_func.instruction(&Instruction::I32Const(32));
target_func.instruction(&Instruction::I32Const(0));
target_func.instruction(&Instruction::I32Const(4));
target_func.instruction(&Instruction::MemoryInit {
mem: 0,
data_index: 0,
});
target_func.instruction(&Instruction::End);
code.function(&target_func);
module.section(&code);
let mut data = DataSection::new();
data.passive([1, 2, 3, 4]);
module.section(&data);
module.finish()
}
fn module_with_poll_wait_import() -> Vec<u8> {
let mut module = Module::new();
let mut types = TypeSection::new();
types.ty().function(
[
wasm_encoder::ValType::I32,
wasm_encoder::ValType::I32,
wasm_encoder::ValType::I64,
],
[wasm_encoder::ValType::I32],
);
types.ty().function([], []);
module.section(&types);
let mut imports = wasm_encoder::ImportSection::new();
imports.import(
"wvl_poll",
"__wvl_poll_atomic_wait",
wasm_encoder::EntityType::Function(0),
);
module.section(&imports);
let mut functions = FunctionSection::new();
functions.function(1);
module.section(&functions);
let mut memories = wasm_encoder::MemorySection::new();
memories.memory(wasm_encoder::MemoryType {
minimum: 1,
maximum: Some(1),
memory64: false,
shared: true,
page_size_log2: None,
});
module.section(&memories);
let mut exports = ExportSection::new();
exports.export("__flesh_vfs_start", ExportKind::Func, 1);
module.section(&exports);
let mut code = CodeSection::new();
let mut func = Function::new([]);
func.instruction(&Instruction::End);
code.function(&func);
module.section(&code);
module.finish()
}
#[test]
fn reset_body_calls_atomic_reset_target() -> eyre::Result<()> {
let input = module_with_flesh_vfs_start_and_reset();
let mut pass = PostCombineStreamPass::new(
"vfs".to_string(),
vec!["test_target".to_string()],
vec![1],
true,
);
let output = pass.run(&input)?;
let mut atomic_reset_idx = None;
let mut found_call_to_atomic_reset = false;
for payload in wasmparser::Parser::new(0).parse_all(&output) {
match payload? {
wasmparser::Payload::ExportSection(exports) => {
for export in exports {
let export = export?;
if export.name == "__vfs_atomic_reset_target" {
atomic_reset_idx = Some(export.index);
}
}
}
wasmparser::Payload::CodeSectionEntry(body) => {
let ops = body
.get_operators_reader()?
.into_iter()
.collect::<Result<Vec<_>, _>>()?;
for window in ops.windows(2) {
if let (
wasmparser::Operator::I32Const { value: 0 },
wasmparser::Operator::Call { function_index },
) = (&window[0], &window[1])
{
if let Some(ari) = atomic_reset_idx {
if *function_index == ari {
found_call_to_atomic_reset = true;
}
}
}
}
}
_ => {}
}
}
assert!(
found_call_to_atomic_reset,
"A synthesized function (like _reset) should call __vfs_atomic_reset_target"
);
Ok(())
}
fn module_with_flesh_vfs_start_and_reset() -> Vec<u8> {
let mut module = wasm_encoder::Module::new();
let mut types = wasm_encoder::TypeSection::new();
types.ty().function([], []);
types.ty().function([wasm_encoder::ValType::I32], []);
module.section(&types);
let mut imports = wasm_encoder::ImportSection::new();
imports.import(
"wasip1-vfs",
"__wasip1_vfs_test_target_reset",
wasm_encoder::EntityType::Function(0),
);
module.section(&imports);
let mut functions = wasm_encoder::FunctionSection::new();
functions.function(1);
module.section(&functions);
let mut exports = wasm_encoder::ExportSection::new();
exports.export(
"__vfs_atomic_reset_target",
wasm_encoder::ExportKind::Func,
1, );
module.section(&exports);
let mut code = wasm_encoder::CodeSection::new();
let mut atomic_func = wasm_encoder::Function::new([(1, wasm_encoder::ValType::I32)]);
atomic_func.instruction(&wasm_encoder::Instruction::End);
code.function(&atomic_func);
module.section(&code);
module.finish().to_vec()
}
}