1use crate::error::{Error, WasmError};
25
26use codec::Decode;
27use parking_lot::Mutex;
28use sc_executor_common::{
29 runtime_blob::RuntimeBlob,
30 wasm_runtime::{HeapAllocStrategy, WasmInstance, WasmModule},
31};
32use schnellru::{ByLength, LruMap};
33use sp_core::traits::{Externalities, FetchRuntimeCode, RuntimeCode};
34use sp_version::RuntimeVersion;
35use sp_wasm_interface::HostFunctions;
36
37use std::{
38 panic::AssertUnwindSafe,
39 path::{Path, PathBuf},
40 sync::Arc,
41};
42
43#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
45pub enum WasmExecutionMethod {
46 Compiled {
48 instantiation_strategy: sc_executor_wasmtime::InstantiationStrategy,
50 },
51}
52
53impl Default for WasmExecutionMethod {
54 fn default() -> Self {
55 Self::Compiled {
56 instantiation_strategy: sc_executor_wasmtime::InstantiationStrategy::PoolingCopyOnWrite,
57 }
58 }
59}
60
61#[derive(Debug, PartialEq, Eq, Hash, Clone)]
62struct VersionedRuntimeId {
63 code_hash: Vec<u8>,
65 wasm_method: WasmExecutionMethod,
67 heap_alloc_strategy: HeapAllocStrategy,
69}
70
71struct VersionedRuntime {
73 module: Box<dyn WasmModule>,
75 version: Option<RuntimeVersion>,
77
78 instances: Vec<Mutex<Option<Box<dyn WasmInstance>>>>,
82}
83
84impl VersionedRuntime {
85 fn with_instance<R, F>(&self, ext: &mut dyn Externalities, f: F) -> Result<R, Error>
87 where
88 F: FnOnce(
89 &dyn WasmModule,
90 &mut dyn WasmInstance,
91 Option<&RuntimeVersion>,
92 &mut dyn Externalities,
93 ) -> Result<R, Error>,
94 {
95 let instance = self
97 .instances
98 .iter()
99 .enumerate()
100 .find_map(|(index, i)| i.try_lock().map(|i| (index, i)));
101
102 match instance {
103 Some((index, mut locked)) => {
104 let (mut instance, new_inst) = locked
105 .take()
106 .map(|r| Ok((r, false)))
107 .unwrap_or_else(|| self.module.new_instance().map(|i| (i, true)))?;
108
109 let result = f(&*self.module, &mut *instance, self.version.as_ref(), ext);
110 if let Err(e) = &result {
111 if new_inst {
112 tracing::warn!(
113 target: "wasm-runtime",
114 error = %e,
115 "Fresh runtime instance failed",
116 )
117 } else {
118 tracing::warn!(
119 target: "wasm-runtime",
120 error = %e,
121 "Evicting failed runtime instance",
122 );
123 }
124 } else {
125 *locked = Some(instance);
126
127 if new_inst {
128 tracing::debug!(
129 target: "wasm-runtime",
130 "Allocated WASM instance {}/{}",
131 index + 1,
132 self.instances.len(),
133 );
134 }
135 }
136
137 result
138 },
139 None => {
140 tracing::warn!(target: "wasm-runtime", "Ran out of free WASM instances");
141
142 let mut instance = self.module.new_instance()?;
144
145 f(&*self.module, &mut *instance, self.version.as_ref(), ext)
146 },
147 }
148 }
149}
150
151pub struct RuntimeCache {
163 runtimes: Mutex<LruMap<VersionedRuntimeId, Arc<VersionedRuntime>>>,
167 max_runtime_instances: usize,
169 cache_path: Option<PathBuf>,
170}
171
172impl RuntimeCache {
173 pub fn new(
184 max_runtime_instances: usize,
185 cache_path: Option<PathBuf>,
186 runtime_cache_size: u8,
187 ) -> RuntimeCache {
188 let cap = ByLength::new(runtime_cache_size.max(1) as u32);
189 RuntimeCache { runtimes: Mutex::new(LruMap::new(cap)), max_runtime_instances, cache_path }
190 }
191
192 pub fn with_instance<'c, H, R, F>(
219 &self,
220 runtime_code: &'c RuntimeCode<'c>,
221 ext: &mut dyn Externalities,
222 wasm_method: WasmExecutionMethod,
223 heap_alloc_strategy: HeapAllocStrategy,
224 allow_missing_func_imports: bool,
225 f: F,
226 ) -> Result<Result<R, Error>, Error>
227 where
228 H: HostFunctions,
229 F: FnOnce(
230 &dyn WasmModule,
231 &mut dyn WasmInstance,
232 Option<&RuntimeVersion>,
233 &mut dyn Externalities,
234 ) -> Result<R, Error>,
235 {
236 let code_hash = &runtime_code.hash;
237
238 let versioned_runtime_id =
239 VersionedRuntimeId { code_hash: code_hash.clone(), heap_alloc_strategy, wasm_method };
240
241 let mut runtimes = self.runtimes.lock(); let versioned_runtime = if let Some(versioned_runtime) = runtimes.get(&versioned_runtime_id)
243 {
244 versioned_runtime.clone()
245 } else {
246 let code = runtime_code.fetch_runtime_code().ok_or(WasmError::CodeNotFound)?;
247
248 let time = std::time::Instant::now();
249
250 let result = create_versioned_wasm_runtime::<H>(
251 &code,
252 ext,
253 wasm_method,
254 heap_alloc_strategy,
255 allow_missing_func_imports,
256 self.max_runtime_instances,
257 self.cache_path.as_deref(),
258 );
259
260 match result {
261 Ok(ref result) => {
262 tracing::debug!(
263 target: "wasm-runtime",
264 "Prepared new runtime version {:?} in {} ms.",
265 result.version,
266 time.elapsed().as_millis(),
267 );
268 },
269 Err(ref err) => {
270 tracing::warn!(target: "wasm-runtime", error = ?err, "Cannot create a runtime");
271 },
272 }
273
274 let versioned_runtime = Arc::new(result?);
275
276 runtimes.insert(versioned_runtime_id, versioned_runtime.clone());
278
279 versioned_runtime
280 };
281
282 drop(runtimes);
284
285 Ok(versioned_runtime.with_instance(ext, f))
286 }
287}
288
289pub fn create_wasm_runtime_with_code<H>(
291 wasm_method: WasmExecutionMethod,
292 heap_alloc_strategy: HeapAllocStrategy,
293 blob: RuntimeBlob,
294 allow_missing_func_imports: bool,
295 cache_path: Option<&Path>,
296) -> Result<Box<dyn WasmModule>, WasmError>
297where
298 H: HostFunctions,
299{
300 if let Some(blob) = blob.as_polkavm_blob() {
301 return sc_executor_polkavm::create_runtime::<H>(blob);
302 }
303
304 match wasm_method {
305 WasmExecutionMethod::Compiled { instantiation_strategy } =>
306 sc_executor_wasmtime::create_runtime::<H>(
307 blob,
308 sc_executor_wasmtime::Config {
309 allow_missing_func_imports,
310 cache_path: cache_path.map(ToOwned::to_owned),
311 semantics: sc_executor_wasmtime::Semantics {
312 heap_alloc_strategy,
313 instantiation_strategy,
314 deterministic_stack_limit: None,
315 canonicalize_nans: false,
316 parallel_compilation: true,
317 wasm_multi_value: false,
318 wasm_bulk_memory: false,
319 wasm_reference_types: false,
320 wasm_simd: false,
321 },
322 },
323 )
324 .map(|runtime| -> Box<dyn WasmModule> { Box::new(runtime) }),
325 }
326}
327
328fn decode_version(mut version: &[u8]) -> Result<RuntimeVersion, WasmError> {
329 Decode::decode(&mut version).map_err(|_| {
330 WasmError::Instantiation(
331 "failed to decode \"Core_version\" result using old runtime version".into(),
332 )
333 })
334}
335
336fn decode_runtime_apis(apis: &[u8]) -> Result<Vec<([u8; 8], u32)>, WasmError> {
337 use sp_api::RUNTIME_API_INFO_SIZE;
338
339 apis.chunks(RUNTIME_API_INFO_SIZE)
340 .map(|chunk| {
341 <[u8; RUNTIME_API_INFO_SIZE]>::try_from(chunk)
344 .map(sp_api::deserialize_runtime_api_info)
345 .map_err(|_| WasmError::Other("a clipped runtime api info declaration".to_owned()))
346 })
347 .collect::<Result<Vec<_>, WasmError>>()
348}
349
350pub fn read_embedded_version(blob: &RuntimeBlob) -> Result<Option<RuntimeVersion>, WasmError> {
356 if let Some(mut version_section) = blob.custom_section_contents("runtime_version") {
357 let apis = blob
358 .custom_section_contents("runtime_apis")
359 .map(decode_runtime_apis)
360 .transpose()?
361 .map(Into::into);
362
363 let core_version = apis.as_ref().and_then(sp_version::core_version_from_apis);
364 let mut decoded_version = sp_version::RuntimeVersion::decode_with_version_hint(
369 &mut version_section,
370 core_version,
371 )
372 .map_err(|_| WasmError::Instantiation("failed to decode version section".into()))?;
373
374 if let Some(apis) = apis {
375 decoded_version.apis = apis;
376 }
377
378 Ok(Some(decoded_version))
379 } else {
380 Ok(None)
381 }
382}
383
384fn create_versioned_wasm_runtime<H>(
385 code: &[u8],
386 ext: &mut dyn Externalities,
387 wasm_method: WasmExecutionMethod,
388 heap_alloc_strategy: HeapAllocStrategy,
389 allow_missing_func_imports: bool,
390 max_instances: usize,
391 cache_path: Option<&Path>,
392) -> Result<VersionedRuntime, WasmError>
393where
394 H: HostFunctions,
395{
396 let blob = sc_executor_common::runtime_blob::RuntimeBlob::uncompress_if_needed(code)?;
399
400 let mut version = read_embedded_version(&blob)?;
404
405 let runtime = create_wasm_runtime_with_code::<H>(
406 wasm_method,
407 heap_alloc_strategy,
408 blob,
409 allow_missing_func_imports,
410 cache_path,
411 )?;
412
413 if version.is_none() {
416 let version_result = {
418 let mut ext = AssertUnwindSafe(ext);
421
422 let runtime = AssertUnwindSafe(runtime.as_ref());
425 crate::executor::with_externalities_safe(&mut **ext, move || {
426 runtime.new_instance()?.call("Core_version".into(), &[])
427 })
428 .map_err(|_| WasmError::Instantiation("panic in call to get runtime version".into()))?
429 };
430
431 if let Ok(version_buf) = version_result {
432 version = Some(decode_version(&version_buf)?)
433 }
434 }
435
436 let mut instances = Vec::with_capacity(max_instances);
437 instances.resize_with(max_instances, || Mutex::new(None));
438
439 Ok(VersionedRuntime { module: runtime, version, instances })
440}
441
442#[cfg(test)]
443mod tests {
444 use super::*;
445 use codec::Encode;
446 use sp_api::{Core, RuntimeApiInfo};
447 use sp_runtime::RuntimeString;
448 use sp_version::{create_apis_vec, RuntimeVersion};
449 use sp_wasm_interface::HostFunctions;
450 use substrate_test_runtime::Block;
451
452 #[derive(Encode)]
453 pub struct OldRuntimeVersion {
454 pub spec_name: RuntimeString,
455 pub impl_name: RuntimeString,
456 pub authoring_version: u32,
457 pub spec_version: u32,
458 pub impl_version: u32,
459 pub apis: sp_version::ApisVec,
460 }
461
462 #[test]
463 fn host_functions_are_equal() {
464 let host_functions = sp_io::SubstrateHostFunctions::host_functions();
465
466 let equal = &host_functions[..] == &host_functions[..];
467 assert!(equal, "Host functions are not equal");
468 }
469
470 #[test]
471 fn old_runtime_version_decodes() {
472 let old_runtime_version = OldRuntimeVersion {
473 spec_name: "test".into(),
474 impl_name: "test".into(),
475 authoring_version: 1,
476 spec_version: 1,
477 impl_version: 1,
478 apis: create_apis_vec!([(<dyn Core::<Block>>::ID, 1)]),
479 };
480
481 let version = decode_version(&old_runtime_version.encode()).unwrap();
482 assert_eq!(1, version.transaction_version);
483 assert_eq!(0, version.system_version);
484 }
485
486 #[test]
487 fn old_runtime_version_decodes_fails_with_version_3() {
488 let old_runtime_version = OldRuntimeVersion {
489 spec_name: "test".into(),
490 impl_name: "test".into(),
491 authoring_version: 1,
492 spec_version: 1,
493 impl_version: 1,
494 apis: create_apis_vec!([(<dyn Core::<Block>>::ID, 3)]),
495 };
496
497 decode_version(&old_runtime_version.encode()).unwrap_err();
498 }
499
500 #[test]
501 fn new_runtime_version_decodes() {
502 let old_runtime_version = RuntimeVersion {
503 spec_name: "test".into(),
504 impl_name: "test".into(),
505 authoring_version: 1,
506 spec_version: 1,
507 impl_version: 1,
508 apis: create_apis_vec!([(<dyn Core::<Block>>::ID, 3)]),
509 transaction_version: 3,
510 system_version: 4,
511 };
512
513 let version = decode_version(&old_runtime_version.encode()).unwrap();
514 assert_eq!(3, version.transaction_version);
515 assert_eq!(0, version.system_version);
516
517 let old_runtime_version = RuntimeVersion {
518 spec_name: "test".into(),
519 impl_name: "test".into(),
520 authoring_version: 1,
521 spec_version: 1,
522 impl_version: 1,
523 apis: create_apis_vec!([(<dyn Core::<Block>>::ID, 4)]),
524 transaction_version: 3,
525 system_version: 4,
526 };
527
528 let version = decode_version(&old_runtime_version.encode()).unwrap();
529 assert_eq!(3, version.transaction_version);
530 assert_eq!(4, version.system_version);
531 }
532
533 #[test]
534 fn embed_runtime_version_works() {
535 let wasm = sp_maybe_compressed_blob::decompress(
536 substrate_test_runtime::wasm_binary_unwrap(),
537 sp_maybe_compressed_blob::CODE_BLOB_BOMB_LIMIT,
538 )
539 .expect("Decompressing works");
540 let runtime_version = RuntimeVersion {
541 spec_name: "test_replace".into(),
542 impl_name: "test_replace".into(),
543 authoring_version: 100,
544 spec_version: 100,
545 impl_version: 100,
546 apis: create_apis_vec!([(<dyn Core::<Block>>::ID, 4)]),
547 transaction_version: 100,
548 system_version: 1,
549 };
550
551 let embedded = sp_version::embed::embed_runtime_version(&wasm, runtime_version.clone())
552 .expect("Embedding works");
553
554 let blob = RuntimeBlob::new(&embedded).expect("Embedded blob is valid");
555 let read_version = read_embedded_version(&blob)
556 .ok()
557 .flatten()
558 .expect("Reading embedded version works");
559
560 assert_eq!(runtime_version, read_version);
561 }
562}