1#![cfg_attr(not(feature = "std"), no_std)]
20
21#[cfg(feature = "std")]
23include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
24
25#[cfg(feature = "std")]
27pub fn wasm_binary_unwrap() -> &'static [u8] {
28 WASM_BINARY.expect(
29 "Development wasm binary is not available. Testing is only supported with the flag \
30 disabled.",
31 )
32}
33
34#[cfg(not(feature = "std"))]
35extern crate alloc;
36
37#[cfg(not(feature = "std"))]
38use alloc::{vec, vec::Vec};
39
40#[cfg(not(feature = "std"))]
41use sp_core::{ed25519, sr25519};
42#[cfg(not(feature = "std"))]
43use sp_io::{
44 crypto::{ed25519_verify, sr25519_verify},
45 hashing::{blake2_128, blake2_256, sha2_256, twox_128, twox_256},
46 storage, wasm_tracing,
47};
48#[cfg(not(feature = "std"))]
49use sp_runtime::{
50 print,
51 traits::{BlakeTwo256, Hash},
52};
53
54extern "C" {
55 #[allow(dead_code)]
56 fn missing_external();
57
58 #[allow(dead_code)]
59 fn yet_another_missing_external();
60}
61
62#[cfg(not(feature = "std"))]
63const WASM_PAGE_SIZE: usize = 65536;
65
66#[cfg(not(feature = "std"))]
67static mut MUTABLE_STATIC: u64 = 32;
70
71#[cfg(not(feature = "std"))]
72static mut MUTABLE_STATIC_BSS: u64 = 0;
78
79sp_core::wasm_export_functions! {
80 fn test_calling_missing_external() {
81 unsafe { missing_external() }
82 }
83
84 fn test_calling_yet_another_missing_external() {
85 unsafe { yet_another_missing_external() }
86 }
87
88 fn test_data_in(input: Vec<u8>) -> Vec<u8> {
89 print("set_storage");
90 storage::set(b"input", &input);
91
92 print("storage");
93 let foo = storage::get(b"foo").unwrap();
94
95 print("set_storage");
96 storage::set(b"baz", &foo);
97
98 print("finished!");
99 b"all ok!".to_vec()
100 }
101
102 fn test_clear_prefix(input: Vec<u8>) -> Vec<u8> {
103 storage::clear_prefix(&input, None);
104 b"all ok!".to_vec()
105 }
106
107 fn test_empty_return() {}
108
109 fn test_dirty_plenty_memory(heap_base: u32, heap_pages: u32) {
110 let heap_ptr = heap_base as usize;
118
119 let heap_ptr = round_up_to(heap_ptr, WASM_PAGE_SIZE);
121
122 let heap_ptr = heap_ptr as *mut u8;
124
125 let host_pages = heap_pages as usize * 16;
127 for i in 0..host_pages {
128 unsafe {
129 heap_ptr.add(i * 4096).write(0);
131 }
132 }
133
134 fn round_up_to(n: usize, divisor: usize) -> usize {
135 (n + divisor - 1) / divisor
136 }
137 }
138
139 fn test_allocate_vec(size: u32) -> Vec<u8> {
140 Vec::with_capacity(size as usize)
141 }
142
143 fn test_fp_f32add(a: [u8; 4], b: [u8; 4]) -> [u8; 4] {
144 let a = f32::from_le_bytes(a);
145 let b = f32::from_le_bytes(b);
146 f32::to_le_bytes(a + b)
147 }
148
149 fn test_panic() { panic!("test panic") }
150
151 fn test_conditional_panic(input: Vec<u8>) -> Vec<u8> {
152 if input.len() > 0 {
153 panic!("test panic")
154 }
155
156 input
157 }
158
159 fn test_blake2_256(input: Vec<u8>) -> Vec<u8> {
160 blake2_256(&input).to_vec()
161 }
162
163 fn test_blake2_128(input: Vec<u8>) -> Vec<u8> {
164 blake2_128(&input).to_vec()
165 }
166
167 fn test_sha2_256(input: Vec<u8>) -> Vec<u8> {
168 sha2_256(&input).to_vec()
169 }
170
171 fn test_twox_256(input: Vec<u8>) -> Vec<u8> {
172 twox_256(&input).to_vec()
173 }
174
175 fn test_twox_128(input: Vec<u8>) -> Vec<u8> {
176 twox_128(&input).to_vec()
177 }
178
179 fn test_ed25519_verify(input: Vec<u8>) -> bool {
180 let mut pubkey = [0; 32];
181 let mut sig = [0; 64];
182
183 pubkey.copy_from_slice(&input[0..32]);
184 sig.copy_from_slice(&input[32..96]);
185
186 let msg = b"all ok!";
187 ed25519_verify(&ed25519::Signature::from(sig), &msg[..], &ed25519::Public::from(pubkey))
188 }
189
190 fn test_sr25519_verify(input: Vec<u8>) -> bool {
191 let mut pubkey = [0; 32];
192 let mut sig = [0; 64];
193
194 pubkey.copy_from_slice(&input[0..32]);
195 sig.copy_from_slice(&input[32..96]);
196
197 let msg = b"all ok!";
198 sr25519_verify(&sr25519::Signature::from(sig), &msg[..], &sr25519::Public::from(pubkey))
199 }
200
201 fn test_ordered_trie_root() -> Vec<u8> {
202 BlakeTwo256::ordered_trie_root(
203 vec![
204 b"zero"[..].into(),
205 b"one"[..].into(),
206 b"two"[..].into(),
207 ],
208 sp_core::storage::StateVersion::V1,
209 ).as_ref().to_vec()
210 }
211
212 fn test_offchain_index_set() {
213 sp_io::offchain_index::set(b"k", b"v");
214 }
215
216 fn test_offchain_local_storage() -> bool {
217 let kind = sp_core::offchain::StorageKind::PERSISTENT;
218 assert_eq!(sp_io::offchain::local_storage_get(kind, b"test"), None);
219 sp_io::offchain::local_storage_set(kind, b"test", b"asd");
220 assert_eq!(sp_io::offchain::local_storage_get(kind, b"test"), Some(b"asd".to_vec()));
221
222 let res = sp_io::offchain::local_storage_compare_and_set(
223 kind,
224 b"test",
225 Some(b"asd".to_vec()),
226 b"",
227 );
228 assert_eq!(sp_io::offchain::local_storage_get(kind, b"test"), Some(b"".to_vec()));
229 res
230 }
231
232 fn test_offchain_local_storage_with_none() {
233 let kind = sp_core::offchain::StorageKind::PERSISTENT;
234 assert_eq!(sp_io::offchain::local_storage_get(kind, b"test"), None);
235
236 let res = sp_io::offchain::local_storage_compare_and_set(kind, b"test", None, b"value");
237 assert_eq!(res, true);
238 assert_eq!(sp_io::offchain::local_storage_get(kind, b"test"), Some(b"value".to_vec()));
239 }
240
241 fn test_offchain_http() -> bool {
242 use sp_core::offchain::HttpRequestStatus;
243 let run = || -> Option<()> {
244 let id = sp_io::offchain::http_request_start(
245 "POST",
246 "http://localhost:12345",
247 &[],
248 ).ok()?;
249 sp_io::offchain::http_request_add_header(id, "X-Auth", "test").ok()?;
250 sp_io::offchain::http_request_write_body(id, &[1, 2, 3, 4], None).ok()?;
251 sp_io::offchain::http_request_write_body(id, &[], None).ok()?;
252 let status = sp_io::offchain::http_response_wait(&[id], None);
253 assert!(status == vec![HttpRequestStatus::Finished(200)], "Expected Finished(200) status.");
254 let headers = sp_io::offchain::http_response_headers(id);
255 assert_eq!(headers, vec![(b"X-Auth".to_vec(), b"hello".to_vec())]);
256 let mut buffer = vec![0; 64];
257 let read = sp_io::offchain::http_response_read_body(id, &mut buffer, None).ok()?;
258 assert_eq!(read, 3);
259 assert_eq!(&buffer[0..read as usize], &[1, 2, 3]);
260 let read = sp_io::offchain::http_response_read_body(id, &mut buffer, None).ok()?;
261 assert_eq!(read, 0);
262
263 Some(())
264 };
265
266 run().is_some()
267 }
268
269 fn test_enter_span() -> u64 {
270 wasm_tracing::enter_span(Default::default())
271 }
272
273 fn test_exit_span(span_id: u64) {
274 wasm_tracing::exit(span_id)
275 }
276
277 fn test_nested_spans() {
278 sp_io::init_tracing();
279 let span_id = wasm_tracing::enter_span(Default::default());
280 {
281 sp_io::init_tracing();
282 let span_id = wasm_tracing::enter_span(Default::default());
283 wasm_tracing::exit(span_id);
284 }
285 wasm_tracing::exit(span_id);
286 }
287
288 fn returns_mutable_static() -> u64 {
289 unsafe {
290 MUTABLE_STATIC += 1;
291 MUTABLE_STATIC
292 }
293 }
294
295 fn returns_mutable_static_bss() -> u64 {
296 unsafe {
297 MUTABLE_STATIC_BSS += 1;
298 MUTABLE_STATIC_BSS
299 }
300 }
301
302 fn allocates_huge_stack_array(trap: bool) -> Vec<u8> {
303 let mut data = [0u8; 1024 * 768];
307
308 for (i, v) in data.iter_mut().enumerate() {
318 *v = i as u8; }
320
321 if trap {
322 panic!()
325 }
326
327 data.to_vec()
328 }
329
330 fn check_and_set_in_heap(heap_base: u32, offset: u32) {
335 let test_message = b"Hello invalid heap memory";
336 let ptr = (heap_base + offset) as *mut u8;
337
338 let message_slice = unsafe { alloc::slice::from_raw_parts_mut(ptr, test_message.len()) };
339
340 assert_ne!(test_message, message_slice);
341 message_slice.copy_from_slice(test_message);
342 }
343
344 fn test_return_i8() -> i8 {
345 -66
346 }
347
348 fn test_take_i8(value: i8) {
349 assert_eq!(value, -66);
350 }
351
352 fn allocate_two_gigabyte() -> u32 {
353 let mut data = Vec::new();
354 for _ in 0..205 {
355 data.push(Vec::<u8>::with_capacity(10 * 1024 * 1024));
356 }
357
358 data.iter().map(|d| d.capacity() as u32).sum()
359 }
360
361 fn test_abort_on_panic() {
362 sp_io::panic_handler::abort_on_panic("test_abort_on_panic called");
363 }
364
365 fn test_unreachable_intrinsic() {
366 core::arch::wasm32::unreachable()
367 }
368
369 fn test_return_value() -> u64 {
370 return 1234;
372 }
373}
374
375mod output_validity {
378 #[cfg(not(feature = "std"))]
379 use super::WASM_PAGE_SIZE;
380
381 #[cfg(not(feature = "std"))]
382 use sp_runtime_interface::pack_ptr_and_len;
383
384 #[no_mangle]
386 #[cfg(not(feature = "std"))]
387 pub extern "C" fn test_return_huge_len(_params: *const u8, _len: usize) -> u64 {
388 pack_ptr_and_len(0, u32::MAX)
389 }
390
391 #[no_mangle]
393 #[cfg(not(feature = "std"))]
394 pub extern "C" fn test_return_max_memory_offset(_params: *const u8, _len: usize) -> u64 {
395 let output_ptr = (core::arch::wasm32::memory_size(0) * WASM_PAGE_SIZE) as u32 - 1;
396 let ptr = output_ptr as *mut u8;
397 unsafe {
398 ptr.write(u8::MAX);
399 }
400 pack_ptr_and_len(output_ptr, 1)
401 }
402
403 #[no_mangle]
405 #[cfg(not(feature = "std"))]
406 pub extern "C" fn test_return_max_memory_offset_plus_one(
407 _params: *const u8,
408 _len: usize,
409 ) -> u64 {
410 pack_ptr_and_len((core::arch::wasm32::memory_size(0) * WASM_PAGE_SIZE) as u32, 1)
411 }
412
413 #[no_mangle]
415 #[cfg(not(feature = "std"))]
416 pub extern "C" fn test_return_overflow(_params: *const u8, _len: usize) -> u64 {
417 pack_ptr_and_len(u32::MAX, 1)
418 }
419}