mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-29 16:41:33 +00:00
Hack together enough emscripten stack stuff to make it happy
This commit is contained in:
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -58,6 +58,11 @@ name = "bitflags"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.2.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.25"
|
||||
@ -672,6 +677,7 @@ dependencies = [
|
||||
name = "wasmer"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"cranelift-codegen 0.23.0",
|
||||
"cranelift-entity 0.23.0",
|
||||
"cranelift-native 0.23.0",
|
||||
@ -730,6 +736,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum backtrace 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "89a47830402e9981c5c41223151efcced65a0510c13097c769cede7efb34782a"
|
||||
"checksum backtrace-sys 0.1.24 (registry+https://github.com/rust-lang/crates.io-index)" = "c66d56ac8dabd07f6aacdaf633f4b8262f5b3601a810a0dcddffd5c22c69daa0"
|
||||
"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12"
|
||||
"checksum byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "94f88df23a25417badc922ab0f5716cc1330e87f71ddd9203b3a3ccd9cedf75d"
|
||||
"checksum cc 1.0.25 (registry+https://github.com/rust-lang/crates.io-index)" = "f159dfd43363c4d08055a07703eb7a3406b0dac4d0584d96965a3262db3c9d16"
|
||||
"checksum cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0c4e7bb64a8ebb0d856483e1e682ea3422f883c5f5615a90d51a2c82fe87fdd3"
|
||||
"checksum clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b957d88f4b6a63b9d70d5f454ac8011819c6efa7727858f458ab71c756ce2d3e"
|
||||
|
@ -45,6 +45,7 @@ target-lexicon = "0.2.0"
|
||||
libc = { git = "https://github.com/rust-lang/libc" }
|
||||
nix = "0.11"
|
||||
rayon = "1.0.3"
|
||||
byteorder = "1"
|
||||
|
||||
[build-dependencies]
|
||||
wabt = "0.7.1"
|
||||
|
@ -1,5 +1,7 @@
|
||||
/// NOTE: TODO: These emscripten api implementation only support wasm32 for now because they assume offsets are u32
|
||||
use crate::webassembly::{ImportObject, ImportValue};
|
||||
use crate::webassembly::{ImportObject, ImportValue, LinearMemory};
|
||||
use byteorder::{ByteOrder, LittleEndian};
|
||||
use std::mem;
|
||||
|
||||
// EMSCRIPTEN APIS
|
||||
mod env;
|
||||
@ -17,6 +19,41 @@ mod nullfunc;
|
||||
pub use self::utils::is_emscripten_module;
|
||||
pub use self::storage::{align_memory, static_alloc};
|
||||
|
||||
// TODO: Magic number - how is this calculated?
|
||||
const TOTAL_STACK: u32 = 5242880;
|
||||
// TODO: Magic number stolen from the generated JS - how is this calculated?
|
||||
const DYNAMICTOP_PTR_DIFF: u32 = 1088;
|
||||
|
||||
const STATIC_BUMP: u32 = 215536; // TODO: make this variable
|
||||
|
||||
fn stacktop(static_bump: u32) -> u32 {
|
||||
align_memory(dynamictop_ptr(static_bump) + 4)
|
||||
}
|
||||
|
||||
fn stack_max(static_bump: u32) -> u32 {
|
||||
stacktop(static_bump) + TOTAL_STACK
|
||||
}
|
||||
|
||||
fn dynamic_base(static_bump: u32) -> u32 {
|
||||
align_memory(stack_max(static_bump))
|
||||
}
|
||||
|
||||
fn dynamictop_ptr(static_bump: u32) -> u32 {
|
||||
static_bump + DYNAMICTOP_PTR_DIFF
|
||||
}
|
||||
|
||||
// fn static_alloc(size: usize, static_top: &mut size) -> usize {
|
||||
// let ret = *static_top;
|
||||
// *static_top = (*static_top + size + 15) & (-16 as usize);
|
||||
// ret
|
||||
// }
|
||||
|
||||
pub fn emscripten_set_up_memory(memory: &mut LinearMemory) {
|
||||
let dynamictop_ptr = dynamictop_ptr(STATIC_BUMP) as usize;
|
||||
let mem = &mut memory[dynamictop_ptr..dynamictop_ptr+mem::size_of::<u32>()];
|
||||
LittleEndian::write_u32(mem, dynamic_base(STATIC_BUMP));
|
||||
}
|
||||
|
||||
pub fn generate_emscripten_env<'a, 'b>() -> ImportObject<&'a str, &'b str> {
|
||||
let mut import_object = ImportObject::new();
|
||||
// Global
|
||||
@ -35,6 +72,28 @@ pub fn generate_emscripten_env<'a, 'b>() -> ImportObject<&'a str, &'b str> {
|
||||
"global3",
|
||||
ImportValue::Global(67), // TODO
|
||||
);
|
||||
|
||||
import_object.set(
|
||||
"env",
|
||||
"STACKTOP",
|
||||
ImportValue::Global(stacktop(STATIC_BUMP) as _),
|
||||
);
|
||||
import_object.set(
|
||||
"env",
|
||||
"STACK_MAX",
|
||||
ImportValue::Global(stack_max(STATIC_BUMP) as _),
|
||||
);
|
||||
import_object.set(
|
||||
"env",
|
||||
"DYNAMICTOP_PTR",
|
||||
ImportValue::Global(dynamictop_ptr(STATIC_BUMP) as _),
|
||||
);
|
||||
import_object.set(
|
||||
"env",
|
||||
"tableBase",
|
||||
ImportValue::Global(0),
|
||||
);
|
||||
|
||||
// Print functions
|
||||
import_object.set("env", "printf", ImportValue::Func(io::printf as *const u8));
|
||||
import_object.set(
|
||||
|
@ -1,13 +1,8 @@
|
||||
|
||||
use crate::webassembly::{LinearMemory, Instance};
|
||||
|
||||
pub fn align_memory(size: u32, factor: u32) -> u32 {
|
||||
assert!(factor != 0, "memory cannot be aligned by 0 offset!");
|
||||
if size % factor == 1 {
|
||||
(size) - (size % factor) + (factor)
|
||||
} else {
|
||||
size
|
||||
}
|
||||
pub fn align_memory(ptr: u32) -> u32 {
|
||||
(ptr + 15) & !15
|
||||
}
|
||||
|
||||
// pub fn static_alloc(size: u32, instance: &mut Instance) -> u32 {
|
||||
|
@ -14,6 +14,7 @@ extern crate wasmparser;
|
||||
extern crate target_lexicon;
|
||||
extern crate nix;
|
||||
extern crate rayon;
|
||||
extern crate byteorder;
|
||||
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
|
@ -454,6 +454,7 @@ impl Instance {
|
||||
let to_init = &mut mem_mut[offset..offset + init.data.len()];
|
||||
to_init.copy_from_slice(&init.data);
|
||||
}
|
||||
crate::apis::emscripten::emscripten_set_up_memory(&mut memories[0]);
|
||||
}
|
||||
|
||||
let start_func: Option<FuncIndex> =
|
||||
|
@ -110,11 +110,11 @@ impl LinearMemory {
|
||||
let end = start + size as usize;
|
||||
let slice: &[u8] = &*self;
|
||||
|
||||
// if end <= self.mapped_size() {
|
||||
Some(&slice[start..end])
|
||||
// } else {
|
||||
// None
|
||||
// }
|
||||
if end <= self.current_size() as usize {
|
||||
Some(&slice[start..end])
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user