Fix potential huge allocation as a result of validate_block output (#13183)

* Fix potential huge allocation as a result of `validate_block` output

* Address review comments; add more tests

* Update client/executor/wasmtime/src/runtime.rs

* Remove unnecessary comments

Co-authored-by: Bastian Köcher <git@kchr.de>
This commit is contained in:
Marcin S
2023-01-20 11:19:30 +01:00
committed by GitHub
parent 57b1de3f47
commit e8288005f7
6 changed files with 125 additions and 4 deletions
@@ -29,6 +29,8 @@ use sp_runtime::{
print,
traits::{BlakeTwo256, Hash},
};
#[cfg(not(feature = "std"))]
use sp_runtime_interface::pack_ptr_and_len;
extern "C" {
#[allow(dead_code)]
@@ -38,6 +40,10 @@ extern "C" {
fn yet_another_missing_external();
}
#[cfg(not(feature = "std"))]
/// The size of a WASM page in bytes.
const WASM_PAGE_SIZE: usize = 65536;
#[cfg(not(feature = "std"))]
/// Mutable static variables should be always observed to have
/// the initialized value at the start of a runtime call.
@@ -92,7 +98,7 @@ sp_core::wasm_export_functions! {
let heap_ptr = heap_base as usize;
// Find the next wasm page boundary.
let heap_ptr = round_up_to(heap_ptr, 65536);
let heap_ptr = round_up_to(heap_ptr, WASM_PAGE_SIZE);
// Make it an actual pointer
let heap_ptr = heap_ptr as *mut u8;
@@ -337,3 +343,32 @@ sp_core::wasm_export_functions! {
return 1234;
}
}
// Returns a huge len. It should result in an error, and not an allocation.
#[no_mangle]
#[cfg(not(feature = "std"))]
pub extern "C" fn test_return_huge_len(_params: *const u8, _len: usize) -> u64 {
pack_ptr_and_len(0, u32::MAX)
}
// Returns an offset right at the edge of the wasm memory boundary. With length 0, it should
// succeed.
#[no_mangle]
#[cfg(not(feature = "std"))]
pub extern "C" fn test_return_max_memory_offset(_params: *const u8, _len: usize) -> u64 {
pack_ptr_and_len((core::arch::wasm32::memory_size(0) * WASM_PAGE_SIZE) as u32, 0)
}
// Returns an offset right at the edge of the wasm memory boundary. With length 1, it should fail.
#[no_mangle]
#[cfg(not(feature = "std"))]
pub extern "C" fn test_return_max_memory_offset_plus_one(_params: *const u8, _len: usize) -> u64 {
pack_ptr_and_len((core::arch::wasm32::memory_size(0) * WASM_PAGE_SIZE) as u32, 1)
}
// Returns an output that overflows the u32 range. It should result in an error.
#[no_mangle]
#[cfg(not(feature = "std"))]
pub extern "C" fn test_return_overflow(_params: *const u8, _len: usize) -> u64 {
pack_ptr_and_len(u32::MAX, 1)
}