Skip to content

Commit

Permalink
Use BTreeMap for NonstandardWitSection.adapters for determinism
Browse files Browse the repository at this point in the history
In the `externref` code, we call `export_xform` while iterating
over `adapters` with `iter_mut`. Calling `export_xform` can result
in shims, with the id depending on the order in which we iterate
over the exports.

To make the final .wasm output deterministic, I've changed this to
use a `BTreeMap`, so that the iteration order doesn't depend on
the hashmap `RandomState`
  • Loading branch information
Aaron1011 committed Feb 22, 2024
1 parent 00ab174 commit d6e66a9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2406,7 +2406,7 @@ impl<'a> Context<'a> {

pub fn generate(&mut self) -> Result<(), Error> {
self.prestore_global_import_identifiers()?;
for (id, adapter) in crate::sorted_iter(&self.wit.adapters) {
for (id, adapter) in self.wit.adapters.iter() {
let instrs = match &adapter.kind {
AdapterKind::Import { .. } => continue,
AdapterKind::Local { instructions } => instructions,
Expand Down
5 changes: 3 additions & 2 deletions crates/cli-support/src/wit/standard.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::descriptor::VectorKind;
use crate::wit::{AuxImport, WasmBindgenAux};
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::collections::{BTreeMap, HashSet};
use walrus::{FunctionId, ImportId, TypedCustomSectionId};

#[derive(Default, Debug)]
pub struct NonstandardWitSection {
/// A list of adapter functions, keyed by their id.
pub adapters: HashMap<AdapterId, Adapter>,
// This is a `BTreeMap` so that we can use `iter_mut` with a deterministic order
pub adapters: BTreeMap<AdapterId, Adapter>,

/// A list of pairs for adapter functions that implement core wasm imports.
pub implements: Vec<(ImportId, FunctionId, AdapterId)>,
Expand Down

0 comments on commit d6e66a9

Please sign in to comment.