| 1 |
//! Support for deduplicating module imports when turning them into a component. |
| 2 |
//! |
| 3 |
//! Core wasm allows for duplicate imports of the same name/field in a core wasm |
| 4 |
//! module. This is not allowed in the component model, however, meaning that |
| 5 |
//! such a core wasm module cannot be inserted into a component as-is. This |
| 6 |
//! module is tasked with tackling this problem. |
| 7 |
//! |
| 8 |
//! The general idea of this module is to rewrite imports in-place to a |
| 9 |
//! different and unique name. The original name is then recorded in |
| 10 |
//! [`ModuleImportMap`] which is then plumbed through to the location that |
| 11 |
//! classifies all imports. The classification then uses the original names for |
| 12 |
//! what an import should be resolved to but records it under the unique names |
| 13 |
//! generated here. |
| 14 |
|
| 15 |
use anyhow::Result; |
| 16 |
use std::borrow::Cow; |
| 17 |
use std::collections::HashMap; |
| 18 |
use std::fmt::Write; |
| 19 |
use wasm_encoder::reencode::{Reencode, RoundtripReencoder}; |
| 20 |
use wasm_encoder::{ImportSection, RawSection}; |
| 21 |
use wasmparser::{Parser, Payload::*}; |
| 22 |
|
| 23 |
/// A map of current names (possibly new) to original names, if any. |
| 24 |
#[derive(Default)] |
| 25 |
pub struct ModuleImportMap { |
| 26 |
/// A two-level map for what names map to what. |
| 27 |
/// |
| 28 |
/// * The first level of the map is the "module" field, or the first string, |
| 29 |
/// in a wasm import. |
| 30 |
/// * The second level of the map is the "name" field, or the second string, |
| 31 |
/// in a wasm import. |
| 32 |
/// * The value of the second level is what this name was originally found |
| 33 |
/// under in the original module. `None` means "same as the hash map key" |
| 34 |
/// while `Some` means "it's this new name". |
| 35 |
/// |
| 36 |
/// This map is built during `ModuleImportMap::new` and serves double-duty |
| 37 |
/// to actually track duplicate imports. |
| 38 |
renamed_to_original: HashMap<String, HashMap<String, Option<String>>>, |
| 39 |
} |
| 40 |
|
| 41 |
impl ModuleImportMap { |
| 42 |
/// Creates a new [`ModuleImportMap`] tracking duplicate imports, if any, |
| 43 |
/// from the `wasm` provided. |
| 44 |
/// |
| 45 |
/// Upon success a new wasm binary (possibly the same as the original) is |
| 46 |
/// returned which is the new source of truth for this module. If duplicate |
| 47 |
/// imports were found then a [`ModuleImportMap`] is returned, otherwise |
| 48 |
/// `None` is returned. |
| 49 |
/// |
| 50 |
/// # Errors |
| 51 |
/// |
| 52 |
/// Returns an error if `wasm` could not be parsed as a wasm module. |
| 53 |
pub fn new<'a>(wasm: Cow<'a, [u8]>) -> Result<(Cow<'a, [u8]>, Option<ModuleImportMap>)> { |
| 54 |
let mut module = wasm_encoder::Module::new(); |
| 55 |
let mut ret = ModuleImportMap::default(); |
| 56 |
let mut found_duplicate_imports = false; |
| 57 |
|
| 58 |
for payload in Parser::new(0).parse_all(&wasm) { |
| 59 |
let payload = payload?; |
| 60 |
match &payload { |
| 61 |
Version { encoding, .. } if *encoding == wasmparser::Encoding::Component => { |
| 62 |
// if this is a component let someone else deal with the |
| 63 |
// error, we'll punt that up the stack. |
| 64 |
assert!(!found_duplicate_imports); |
| 65 |
break; |
| 66 |
} |
| 67 |
|
| 68 |
// This is the section we're interested in. Go over each import |
| 69 |
// and delegate to `ModuleImportMap::push_import` for figuring |
| 70 |
// out what to do with this import. At the end put the new |
| 71 |
// import section in the `module` we're building. |
| 72 |
ImportSection(i) => { |
| 73 |
let mut new_import_section = ImportSection::new(); |
| 74 |
for import in i.clone() { |
| 75 |
found_duplicate_imports = ret |
| 76 |
.push_import(&mut new_import_section, import?)? |
| 77 |
|| found_duplicate_imports; |
| 78 |
} |
| 79 |
module.section(&new_import_section); |
| 80 |
} |
| 81 |
|
| 82 |
// All other sections get plumbed through as-is. This ensures we |
| 83 |
// don't tamper with binary offsets anywhere in the module |
| 84 |
// except the import section, for example. |
| 85 |
_ => { |
| 86 |
if let Some((id, range)) = payload.as_section() { |
| 87 |
module.section(&RawSection { |
| 88 |
id, |
| 89 |
data: &wasm[range], |
| 90 |
}); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if found_duplicate_imports { |
| 97 |
Ok((module.finish().into(), Some(ret))) |
| 98 |
} else { |
| 99 |
Ok((wasm, None)) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/// Returns `Ok(true)` if this is a duplicate import, or `Ok(false)` if it's |
| 104 |
/// a unique import for the first time. |
| 105 |
fn push_import( |
| 106 |
&mut self, |
| 107 |
new_import_section: &mut ImportSection, |
| 108 |
import: wasmparser::Import<'_>, |
| 109 |
) -> Result<bool> { |
| 110 |
let module_map = self |
| 111 |
.renamed_to_original |
| 112 |
.entry(import.module.to_string()) |
| 113 |
.or_insert(HashMap::new()); |
| 114 |
|
| 115 |
// If this import hasn't yet been seen, then great! Record that it is |
| 116 |
// using its original name and encode it with its original name. |
| 117 |
if !module_map.contains_key(import.name) { |
| 118 |
let prev = module_map.insert(import.name.to_string(), None); |
| 119 |
assert!(prev.is_none()); |
| 120 |
RoundtripReencoder.parse_import(new_import_section, import)?; |
| 121 |
return Ok(false); |
| 122 |
} |
| 123 |
|
| 124 |
// FIXME: this is technically O(n^2), but it's also only applicable when |
| 125 |
// a module has lots of imports, and surely that won't happen often... |
| 126 |
// right? ... right? |
| 127 |
// |
| 128 |
// If this isn't fixed by the time someone reads this and is angry about |
| 129 |
// O(n^2), sorry. |
| 130 |
// |
| 131 |
// Otherwise the purpose of this loop is to determine a unique name for |
| 132 |
// the new import, something that hasn't previously been seen before. |
| 133 |
let mut new_name = import.name.to_string(); |
| 134 |
for i in 2.. { |
| 135 |
new_name.truncate(import.name.len()); |
| 136 |
write!(new_name, " [v{i}]").unwrap(); |
| 137 |
if !module_map.contains_key(&new_name) { |
| 138 |
break; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// Now that `new_name` is unique, record the import in the new import |
| 143 |
// section. |
| 144 |
let mut new_import = import; |
| 145 |
new_import.name = &new_name; |
| 146 |
RoundtripReencoder.parse_import(new_import_section, new_import)?; |
| 147 |
|
| 148 |
// Also record that `new_name` was originally known as `import.name` |
| 149 |
// for later lookup in `original_names` below. |
| 150 |
let prev = module_map.insert(new_name, Some(import.name.to_string())); |
| 151 |
assert!(prev.is_none()); |
| 152 |
Ok(true) |
| 153 |
} |
| 154 |
|
| 155 |
/// Returns the original `name` that `import` should use, if any. |
| 156 |
/// |
| 157 |
/// If `None` is returned then `import.name` should be used. |
| 158 |
pub fn original_name(&self, import: &wasmparser::Import<'_>) -> Option<&str> { |
| 159 |
self.renamed_to_original |
| 160 |
.get(import.module)? |
| 161 |
.get(import.name)? |
| 162 |
.as_deref() |
| 163 |
} |
| 164 |
} |