sp-virtualization 0.2.0

Spawn a new polkavm instance from within the runtime/pvf
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{DestroyError, ExecBuffer, ExecError, InstantiateError, MemoryError, EXEC_BUFFER_SIZE};
use sp_runtime_interface::{
	pass_by::{
		ConvertAndReturnAs, PassFatPointerAndRead, PassFatPointerAndWrite, PassPointerAndWrite,
	},
	runtime_interface,
};
use strum::EnumCount;

#[cfg(not(substrate_runtime))]
use crate::ExecStatus;

#[derive(EnumCount)]
#[repr(i8)]
pub enum RIInstantiateError {
	InvalidImage = -1,
}

impl From<RIInstantiateError> for i64 {
	fn from(error: RIInstantiateError) -> Self {
		error as i64
	}
}

impl TryFrom<i64> for RIInstantiateError {
	type Error = ();

	fn try_from(value: i64) -> Result<Self, Self::Error> {
		match value {
			-1 => Ok(RIInstantiateError::InvalidImage),
			_ => Err(()),
		}
	}
}

impl From<InstantiateError> for RIInstantiateError {
	fn from(error: InstantiateError) -> Self {
		match error {
			InstantiateError::InvalidImage => RIInstantiateError::InvalidImage,
		}
	}
}

impl From<RIInstantiateError> for InstantiateError {
	fn from(error: RIInstantiateError) -> Self {
		match error {
			RIInstantiateError::InvalidImage => InstantiateError::InvalidImage,
		}
	}
}

#[derive(EnumCount)]
#[repr(i8)]
pub enum RIExecError {
	InvalidInstance = -1,
	InvalidImage = -2,
	OutOfGas = -3,
	Trap = -4,
}

impl From<RIExecError> for i64 {
	fn from(error: RIExecError) -> Self {
		error as i64
	}
}

impl TryFrom<i64> for RIExecError {
	type Error = ();

	fn try_from(value: i64) -> Result<Self, Self::Error> {
		match value {
			-1 => Ok(RIExecError::InvalidInstance),
			-2 => Ok(RIExecError::InvalidImage),
			-3 => Ok(RIExecError::OutOfGas),
			-4 => Ok(RIExecError::Trap),
			_ => Err(()),
		}
	}
}

impl From<RIExecError> for ExecError {
	fn from(error: RIExecError) -> Self {
		match error {
			RIExecError::InvalidInstance => ExecError::InvalidInstance,
			RIExecError::InvalidImage => ExecError::InvalidImage,
			RIExecError::OutOfGas => ExecError::OutOfGas,
			RIExecError::Trap => ExecError::Trap,
		}
	}
}

impl From<ExecError> for RIExecError {
	fn from(error: ExecError) -> Self {
		match error {
			ExecError::InvalidInstance => RIExecError::InvalidInstance,
			ExecError::InvalidImage => RIExecError::InvalidImage,
			ExecError::OutOfGas => RIExecError::OutOfGas,
			ExecError::Trap => RIExecError::Trap,
		}
	}
}

#[derive(EnumCount)]
#[repr(i8)]
pub enum RIDestroyError {
	InvalidInstance = -1,
}

impl From<RIDestroyError> for i64 {
	fn from(error: RIDestroyError) -> Self {
		error as i64
	}
}

impl TryFrom<i64> for RIDestroyError {
	type Error = ();

	fn try_from(value: i64) -> Result<Self, Self::Error> {
		match value {
			-1 => Ok(RIDestroyError::InvalidInstance),
			_ => Err(()),
		}
	}
}

impl From<RIDestroyError> for DestroyError {
	fn from(error: RIDestroyError) -> Self {
		match error {
			RIDestroyError::InvalidInstance => DestroyError::InvalidInstance,
		}
	}
}

impl From<DestroyError> for RIDestroyError {
	fn from(error: DestroyError) -> Self {
		match error {
			DestroyError::InvalidInstance => RIDestroyError::InvalidInstance,
		}
	}
}

#[derive(EnumCount)]
#[repr(i8)]
pub enum RIMemoryError {
	InvalidInstance = -1,
	OutOfBounds = -2,
}

impl From<RIMemoryError> for i64 {
	fn from(error: RIMemoryError) -> Self {
		error as i64
	}
}

impl TryFrom<i64> for RIMemoryError {
	type Error = ();

	fn try_from(value: i64) -> Result<Self, Self::Error> {
		match value {
			-1 => Ok(RIMemoryError::InvalidInstance),
			-2 => Ok(RIMemoryError::OutOfBounds),
			_ => Err(()),
		}
	}
}

impl From<RIMemoryError> for MemoryError {
	fn from(error: RIMemoryError) -> Self {
		match error {
			RIMemoryError::InvalidInstance => MemoryError::InvalidInstance,
			RIMemoryError::OutOfBounds => MemoryError::OutOfBounds,
		}
	}
}

impl From<MemoryError> for RIMemoryError {
	fn from(error: MemoryError) -> Self {
		match error {
			MemoryError::InvalidInstance => RIMemoryError::InvalidInstance,
			MemoryError::OutOfBounds => RIMemoryError::OutOfBounds,
		}
	}
}

// The following code is an excerpt from RFC-145 implementation (still to be adopted)
// ---vvv--- 8< CUT HERE 8< ---vvv---

/// Used to return less-than-64-bit value passed as `i64` through the FFI boundary.
/// Negative values are used to represent error variants.
pub enum RIIntResult<R, E> {
	/// Successful result
	Ok(R),
	/// Error result
	Err(E),
}

impl<R, E, OR, OE> From<Result<OR, OE>> for RIIntResult<R, E>
where
	R: From<OR>,
	E: From<OE>,
{
	fn from(result: Result<OR, OE>) -> Self {
		match result {
			Ok(value) => Self::Ok(value.into()),
			Err(error) => Self::Err(error.into()),
		}
	}
}

impl<R, E, OR, OE> From<RIIntResult<R, E>> for Result<OR, OE>
where
	OR: From<R>,
	OE: From<E>,
{
	fn from(result: RIIntResult<R, E>) -> Self {
		match result {
			RIIntResult::Ok(value) => Ok(value.into()),
			RIIntResult::Err(error) => Err(error.into()),
		}
	}
}

trait IntoI64: Into<i64> {
	const MAX: i64;
}

impl IntoI64 for u8 {
	const MAX: i64 = u8::MAX as i64;
}
impl IntoI64 for u32 {
	const MAX: i64 = u32::MAX as i64;
}

impl<R: Into<i64> + IntoI64, E: Into<i64> + strum::EnumCount> From<RIIntResult<R, E>> for i64 {
	fn from(result: RIIntResult<R, E>) -> Self {
		match result {
			RIIntResult::Ok(value) => value.into(),
			RIIntResult::Err(e) => {
				let error_code: i64 = e.into();
				assert!(
					error_code < 0 && error_code >= -(E::COUNT as i64),
					"Error variant index out of bounds"
				);
				error_code
			},
		}
	}
}

impl<R: TryFrom<i64> + IntoI64, E: TryFrom<i64> + strum::EnumCount> TryFrom<i64>
	for RIIntResult<R, E>
{
	type Error = ();

	fn try_from(value: i64) -> Result<Self, Self::Error> {
		if value >= 0 && value <= R::MAX.into() {
			Ok(RIIntResult::Ok(value.try_into().map_err(|_| ())?))
		} else if value < 0 && value >= -(E::COUNT as i64) {
			Ok(RIIntResult::Err(value.try_into().map_err(|_| ())?))
		} else {
			Err(())
		}
	}
}

pub struct VoidResult;

impl IntoI64 for VoidResult {
	const MAX: i64 = 0;
}

impl From<VoidResult> for u32 {
	fn from(_: VoidResult) -> Self {
		0
	}
}

impl From<u32> for VoidResult {
	fn from(_: u32) -> Self {
		VoidResult
	}
}

impl From<()> for VoidResult {
	fn from(_: ()) -> Self {
		VoidResult
	}
}

impl From<VoidResult> for () {
	fn from(_: VoidResult) -> Self {
		()
	}
}

impl From<VoidResult> for i64 {
	fn from(_: VoidResult) -> Self {
		0
	}
}

impl TryFrom<i64> for VoidResult {
	type Error = ();

	fn try_from(value: i64) -> Result<Self, Self::Error> {
		if value == 0 {
			Ok(VoidResult)
		} else {
			Err(())
		}
	}
}

// ---^^^--- 8< CUT HERE 8< ---^^^---

/// Host functions used to spawn and call into PolkaVM instances.
///
/// Use [`crate::Virt`] instead of these raw host functions. This will also make sure that
/// everything works when running the code in native (test code) as this is a `wasm_only` interface.
///
/// # ⚠️ Unstable — Do Not Use in Production ⚠️
///
/// **This interface is unstable and subject to breaking changes without notice.**
///
/// These host functions are **not available on Polkadot** (or any other production
/// relay/parachain) until the API has been stabilized. If you use them in a production
/// runtime, your runtime **will break** when the API changes.
///
/// Only use for local testing, development, and experimentation on test networks.
/// There is no stability guarantee and no deprecation period.
#[runtime_interface(wasm_only)]
pub trait Virtualization {
	/// See `sp_virtualization::Virt::instantiate`.
	///
	/// Returns the `instance_id` which needs to be passed to reference this instance
	/// when using the other functions of this trait.
	fn instantiate(
		&mut self,
		program: PassFatPointerAndRead<&[u8]>,
	) -> ConvertAndReturnAs<Result<u32, InstantiateError>, RIIntResult<u32, RIInstantiateError>, i64>
	{
		use std::sync::Once;
		static WARN_ONCE: Once = Once::new();
		WARN_ONCE.call_once(|| {
			log::warn!(
				target: crate::LOG_TARGET,
				"Virtualization host functions are UNSTABLE and subject to breaking changes. \
				They are NOT available on Polkadot and using them in production will cause breakage. \
				Only use for testing and experimentation.",
			);
		});
		self.virtualization()
			.instantiate(program)
			.expect("instantiation failed")
			.map(|id| id.0)
			.map_err(|err| TryFrom::try_from(err).expect("Invalid error"))
	}

	/// Start execution of a function on the given instance.
	///
	/// Returns [`ExecStatus::Finished`] or [`ExecStatus::Syscall`] as `u8`.
	/// When a syscall occurs, the syscall arguments are written into the
	/// `exec_buffer` via [`PassPointerAndWrite`].
	fn execute(
		&mut self,
		instance_id: u32,
		function: PassFatPointerAndRead<&str>,
		gas_left: i64,
		exec_buffer: PassPointerAndWrite<&mut ExecBuffer, { EXEC_BUFFER_SIZE }>,
	) -> ConvertAndReturnAs<Result<u8, ExecError>, RIIntResult<u8, RIExecError>, i64> {
		let instance_id = sp_wasm_interface::InstanceId(instance_id);
		self.virtualization()
			.run(instance_id, gas_left, sp_wasm_interface::ExecAction::Execute(function))
			.expect("execution failed")
			.map(|outcome| {
				*exec_buffer = ExecBuffer::from_outcome(&outcome);
				ExecStatus::from_outcome(&outcome).into()
			})
			.map_err(|err| TryFrom::try_from(err).expect("Invalid error"))
	}

	/// Resume execution after a syscall.
	///
	/// Returns [`ExecStatus::Finished`] or [`ExecStatus::Syscall`] as `u8`.
	/// When a syscall occurs, the syscall arguments are written into the
	/// `exec_buffer` via [`PassPointerAndWrite`].
	fn resume(
		&mut self,
		instance_id: u32,
		gas_left: i64,
		return_value: u64,
		exec_buffer: PassPointerAndWrite<&mut ExecBuffer, { EXEC_BUFFER_SIZE }>,
	) -> ConvertAndReturnAs<Result<u8, ExecError>, RIIntResult<u8, RIExecError>, i64> {
		let instance_id = sp_wasm_interface::InstanceId(instance_id);
		self.virtualization()
			.run(instance_id, gas_left, sp_wasm_interface::ExecAction::Resume(return_value))
			.expect("resume failed")
			.map(|outcome| {
				*exec_buffer = ExecBuffer::from_outcome(&outcome);
				ExecStatus::from_outcome(&outcome).into()
			})
			.map_err(|err| TryFrom::try_from(err).expect("Invalid error"))
	}

	/// Destroy this instance.
	///
	/// Any attempt accessing an instance after destruction will yield the `InvalidInstance` error.
	fn destroy(
		&mut self,
		instance_id: u32,
	) -> ConvertAndReturnAs<Result<(), DestroyError>, RIIntResult<VoidResult, RIDestroyError>, i64>
	{
		let instance_id = sp_wasm_interface::InstanceId(instance_id);
		self.virtualization()
			.destroy(instance_id)
			.expect("memory access error")
			.map_err(|err| TryFrom::try_from(err).expect("Invalid error"))
	}

	/// See `sp_virtualization::Memory::read`.
	fn read_memory(
		&mut self,
		instance_id: u32,
		offset: u32,
		dest: PassFatPointerAndWrite<&mut [u8]>,
	) -> ConvertAndReturnAs<Result<(), MemoryError>, RIIntResult<VoidResult, RIMemoryError>, i64> {
		let instance_id = sp_wasm_interface::InstanceId(instance_id);
		self.virtualization()
			.read_memory(instance_id, offset, dest)
			.expect("memory access error")
			.map_err(|err| TryFrom::try_from(err).expect("Invalid error"))
	}

	/// See `sp_virtualization::Memory::write`.
	fn write_memory(
		&mut self,
		instance_id: u32,
		offset: u32,
		src: PassFatPointerAndRead<&[u8]>,
	) -> ConvertAndReturnAs<Result<(), MemoryError>, RIIntResult<VoidResult, RIMemoryError>, i64> {
		let instance_id = sp_wasm_interface::InstanceId(instance_id);
		self.virtualization()
			.write_memory(instance_id, offset, src)
			.expect("memory access error")
			.map_err(|err| TryFrom::try_from(err).expect("Invalid error"))
	}
}