mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-31 14:31:02 +00:00
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:
committed by
Gav Wood
parent
ad552cba4b
commit
96fb93b09c
@@ -28,6 +28,7 @@ std = [
|
|||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
panic = "abort"
|
panic = "abort"
|
||||||
|
lto = true
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
members = []
|
members = []
|
||||||
|
|||||||
BIN
Binary file not shown.
Regular → Executable
BIN
Binary file not shown.
@@ -29,6 +29,7 @@ std = [
|
|||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
panic = "abort"
|
panic = "abort"
|
||||||
|
lto = true
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
members = []
|
members = []
|
||||||
|
|||||||
BIN
Binary file not shown.
Regular → Executable
BIN
Binary file not shown.
@@ -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 = []
|
||||||
|
|||||||
BIN
Binary file not shown.
Regular → Executable
BIN
Binary file not shown.
@@ -31,6 +31,7 @@ crate-type = ["cdylib"]
|
|||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
panic = "abort"
|
panic = "abort"
|
||||||
|
lto = true
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
members = []
|
members = []
|
||||||
|
|||||||
BIN
Binary file not shown.
Regular → Executable
BIN
Binary file not shown.
Reference in New Issue
Block a user