Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit e9484c6

Browse files
mrcnskiAnk4n
authored andcommitted
Minor: Update output validity tests (#13190)
* Minor: Update output validity tests Quick follow-up to #13183. Mainly, I wanted to double check that the `test_return_max_memory_offset` test doesn't pass just because the output length is 0. I also: - Organized these tests into a module. - Added a comment explaining why we don't use the `wasm_export_functions` macro. * Update test based on review comment
1 parent 78c93d5 commit e9484c6

File tree

2 files changed

+42
-27
lines changed
  • client/executor

2 files changed

+42
-27
lines changed

client/executor/runtime-test/src/lib.rs

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ use sp_runtime::{
2929
print,
3030
traits::{BlakeTwo256, Hash},
3131
};
32-
#[cfg(not(feature = "std"))]
33-
use sp_runtime_interface::pack_ptr_and_len;
3432

3533
extern "C" {
3634
#[allow(dead_code)]
@@ -344,31 +342,48 @@ sp_core::wasm_export_functions! {
344342
}
345343
}
346344

347-
// Returns a huge len. It should result in an error, and not an allocation.
348-
#[no_mangle]
349-
#[cfg(not(feature = "std"))]
350-
pub extern "C" fn test_return_huge_len(_params: *const u8, _len: usize) -> u64 {
351-
pack_ptr_and_len(0, u32::MAX)
352-
}
345+
// Tests that check output validity. We explicitly return the ptr and len, so we avoid using the
346+
// `wasm_export_functions` macro.
347+
mod output_validity {
348+
#[cfg(not(feature = "std"))]
349+
use super::WASM_PAGE_SIZE;
353350

354-
// Returns an offset right at the edge of the wasm memory boundary. With length 0, it should
355-
// succeed.
356-
#[no_mangle]
357-
#[cfg(not(feature = "std"))]
358-
pub extern "C" fn test_return_max_memory_offset(_params: *const u8, _len: usize) -> u64 {
359-
pack_ptr_and_len((core::arch::wasm32::memory_size(0) * WASM_PAGE_SIZE) as u32, 0)
360-
}
351+
#[cfg(not(feature = "std"))]
352+
use sp_runtime_interface::pack_ptr_and_len;
361353

362-
// Returns an offset right at the edge of the wasm memory boundary. With length 1, it should fail.
363-
#[no_mangle]
364-
#[cfg(not(feature = "std"))]
365-
pub extern "C" fn test_return_max_memory_offset_plus_one(_params: *const u8, _len: usize) -> u64 {
366-
pack_ptr_and_len((core::arch::wasm32::memory_size(0) * WASM_PAGE_SIZE) as u32, 1)
367-
}
354+
// Returns a huge len. It should result in an error, and not an allocation.
355+
#[no_mangle]
356+
#[cfg(not(feature = "std"))]
357+
pub extern "C" fn test_return_huge_len(_params: *const u8, _len: usize) -> u64 {
358+
pack_ptr_and_len(0, u32::MAX)
359+
}
368360

369-
// Returns an output that overflows the u32 range. It should result in an error.
370-
#[no_mangle]
371-
#[cfg(not(feature = "std"))]
372-
pub extern "C" fn test_return_overflow(_params: *const u8, _len: usize) -> u64 {
373-
pack_ptr_and_len(u32::MAX, 1)
361+
// Returns an offset right before the edge of the wasm memory boundary. It should succeed.
362+
#[no_mangle]
363+
#[cfg(not(feature = "std"))]
364+
pub extern "C" fn test_return_max_memory_offset(_params: *const u8, _len: usize) -> u64 {
365+
let output_ptr = (core::arch::wasm32::memory_size(0) * WASM_PAGE_SIZE) as u32 - 1;
366+
let ptr = output_ptr as *mut u8;
367+
unsafe {
368+
ptr.write(u8::MAX);
369+
}
370+
pack_ptr_and_len(output_ptr, 1)
371+
}
372+
373+
// Returns an offset right after the edge of the wasm memory boundary. It should fail.
374+
#[no_mangle]
375+
#[cfg(not(feature = "std"))]
376+
pub extern "C" fn test_return_max_memory_offset_plus_one(
377+
_params: *const u8,
378+
_len: usize,
379+
) -> u64 {
380+
pack_ptr_and_len((core::arch::wasm32::memory_size(0) * WASM_PAGE_SIZE) as u32, 1)
381+
}
382+
383+
// Returns an output that overflows the u32 range. It should result in an error.
384+
#[no_mangle]
385+
#[cfg(not(feature = "std"))]
386+
pub extern "C" fn test_return_overflow(_params: *const u8, _len: usize) -> u64 {
387+
pack_ptr_and_len(u32::MAX, 1)
388+
}
374389
}

client/executor/src/integration_tests/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ fn return_max_memory_offset(wasm_method: WasmExecutionMethod) {
811811

812812
assert_eq!(
813813
call_in_wasm("test_return_max_memory_offset", &[], wasm_method, &mut ext).unwrap(),
814-
().encode()
814+
(u8::MAX).encode()
815815
);
816816
}
817817

0 commit comments

Comments
 (0)