@@ -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
3533extern "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}
0 commit comments