Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't emit mutable globals #2391

Merged
merged 1 commit into from
Dec 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions crates/cli-support/src/js/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,17 +560,12 @@ fn instruction(js: &mut JsBuilder, instr: &Instruction, log_error: &mut bool) ->
}

Instruction::Retptr { size } => {
let sp = match js.cx.aux.shadow_stack_pointer {
Some(s) => js.cx.export_name_of(s),
// In theory this shouldn't happen since malloc is included in
// most wasm binaries (and may be gc'd out) and that almost
// always pulls in a stack pointer. We can try to synthesize
// something here later if necessary.
None => bail!("failed to find shadow stack pointer"),
};
js.prelude(&format!("const retptr = wasm.{}.value - {};", sp, size));
js.prelude(&format!("wasm.{}.value = retptr;", sp));
js.finally(&format!("wasm.{}.value += {};", sp, size));
js.cx.inject_stack_pointer_shim()?;
js.prelude(&format!(
"const retptr = wasm.__wbindgen_add_to_stack_pointer(-{});",
size
));
js.finally(&format!("wasm.__wbindgen_add_to_stack_pointer({});", size));
js.stack.push("retptr".to_string());
}

Expand Down
47 changes: 46 additions & 1 deletion crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};
use walrus::{FunctionId, ImportId, MemoryId, Module, TableId};
use walrus::{FunctionId, ImportId, MemoryId, Module, TableId, ValType};

mod binding;

Expand Down Expand Up @@ -53,6 +53,9 @@ pub struct Context<'a> {
/// names.
memory_indices: HashMap<MemoryId, usize>,
table_indices: HashMap<TableId, usize>,

/// A flag to track if the stack pointer setter shim has been injected.
stack_pointer_shim_injected: bool,
}

#[derive(Default)]
Expand Down Expand Up @@ -100,6 +103,7 @@ impl<'a> Context<'a> {
aux,
memory_indices: Default::default(),
table_indices: Default::default(),
stack_pointer_shim_injected: false,
})
}

Expand Down Expand Up @@ -3305,6 +3309,47 @@ impl<'a> Context<'a> {
format!("{}{}", name, cnt)
}
}

fn inject_stack_pointer_shim(&mut self) -> Result<(), Error> {
if self.stack_pointer_shim_injected {
return Ok(());
}
let stack_pointer = match self.aux.shadow_stack_pointer {
Some(s) => s,
// In theory this shouldn't happen since malloc is included in
// most wasm binaries (and may be gc'd out) and that almost
// always pulls in a stack pointer. We can try to synthesize
// something here later if necessary.
None => bail!("failed to find shadow stack pointer"),
};

use walrus::ir::*;

let mut builder =
walrus::FunctionBuilder::new(&mut self.module.types, &[ValType::I32], &[ValType::I32]);
builder.name("__wbindgen_add_to_stack_pointer".to_string());

let mut body = builder.func_body();
let arg = self.module.locals.add(ValType::I32);

// Create a shim function that mutate the stack pointer
// to avoid exporting a mutable global.
body.local_get(arg)
.global_get(stack_pointer)
.binop(BinaryOp::I32Add)
.global_set(stack_pointer)
.global_get(stack_pointer);

let add_to_stack_pointer_func = builder.finish(vec![arg], &mut self.module.funcs);

self.module
.exports
.add("__wbindgen_add_to_stack_pointer", add_to_stack_pointer_func);

self.stack_pointer_shim_injected = true;

Ok(())
}
}

fn check_duplicated_getter_and_setter_names(
Expand Down