Fix wasm build (#102)

* Reserve pages for heap at initialization time

Since it is freshly allocated memory it won't collide with stack, data or whatever else.

* Compile all wasm files with LTO.

Modules compiled without LTO contain undesired imports and outright invalid (e.g. type mismatches inside the wasm).
This commit is contained in:
Sergey Pepyakin
2018-03-21 10:44:14 +03:00
committed by Gav Wood
parent ad552cba4b
commit 96fb93b09c
13 changed files with 25 additions and 8 deletions
+1
View File
@@ -28,6 +28,7 @@ std = [
[profile.release] [profile.release]
panic = "abort" panic = "abort"
lto = true
[workspace] [workspace]
members = [] members = []
Binary file not shown.
@@ -29,6 +29,7 @@ std = [
[profile.release] [profile.release]
panic = "abort" panic = "abort"
lto = true
[workspace] [workspace]
members = [] members = []
@@ -35,10 +35,23 @@ struct Heap {
} }
impl Heap { impl Heap {
fn new() -> Self { fn new(memory: &MemoryInstance) -> Result<Self> {
Heap { const HEAP_SIZE_IN_PAGES: u32 = 8;
end: 262144, const PAGE_SIZE_IN_BYTES: u32 = 65536;
let prev_page_count = memory.grow(HEAP_SIZE_IN_PAGES).map_err(
|_: ::parity_wasm::interpreter::Error<DummyUserError>| Error::from(ErrorKind::Runtime),
)?;
if prev_page_count == 0xFFFFFFFF {
// Wasm vm refuses to mount the specified amount of new pages. This
// could mean that wasm binary specifies memory limit and we are trying
// to allocate beyond that limit.
return Err(ErrorKind::Runtime.into());
} }
let allocated_area_start = prev_page_count * PAGE_SIZE_IN_BYTES;
Ok(Heap {
end: allocated_area_start as u32,
})
} }
fn allocate(&mut self, size: u32) -> u32 { fn allocate(&mut self, size: u32) -> u32 {
let r = self.end; let r = self.end;
@@ -57,13 +70,13 @@ struct FunctionExecutor<'e, E: Externalities + 'e> {
} }
impl<'e, E: Externalities> FunctionExecutor<'e, E> { impl<'e, E: Externalities> FunctionExecutor<'e, E> {
fn new(m: &Arc<MemoryInstance>, e: &'e mut E) -> Self { fn new(m: &Arc<MemoryInstance>, e: &'e mut E) -> Result<Self> {
FunctionExecutor { Ok(FunctionExecutor {
heap: Heap::new(), heap: Heap::new(&*m)?,
memory: Arc::clone(m), memory: Arc::clone(m),
ext: e, ext: e,
hash_lookup: HashMap::new(), hash_lookup: HashMap::new(),
} })
} }
} }
@@ -317,7 +330,7 @@ impl CodeExecutor for WasmExecutor {
let module = program.add_module_by_sigs("test", module, map!["env" => FunctionExecutor::<E>::SIGNATURES]).expect("runtime signatures always provided; qed"); let module = program.add_module_by_sigs("test", module, map!["env" => FunctionExecutor::<E>::SIGNATURES]).expect("runtime signatures always provided; qed");
let memory = module.memory(ItemIndex::Internal(0)).expect("all modules compiled with rustc include memory segments; qed"); let memory = module.memory(ItemIndex::Internal(0)).expect("all modules compiled with rustc include memory segments; qed");
let mut fec = FunctionExecutor::new(&memory, ext); let mut fec = FunctionExecutor::new(&memory, ext)?;
let size = data.len() as u32; let size = data.len() as u32;
let offset = fec.heap.allocate(size); let offset = fec.heap.allocate(size);
@@ -11,6 +11,7 @@ substrate-runtime-io = { path = "../../runtime-io", version = "0.1", default_fea
[profile.release] [profile.release]
panic = "abort" panic = "abort"
lto = true
[workspace] [workspace]
members = [] members = []
@@ -31,6 +31,7 @@ crate-type = ["cdylib"]
[profile.release] [profile.release]
panic = "abort" panic = "abort"
lto = true
[workspace] [workspace]
members = [] members = []