mod.rs 11.2 KB
Newer Older
Shawn Tabrizi's avatar
Shawn Tabrizi committed
1
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.

//! WASM re-execution of a parachain candidate.
//! In the context of relay-chain candidate evaluation, there are some additional
//! steps to ensure that the provided input parameters are correct.
//! Assuming the parameters are correct, this module provides a wrapper around
//! a WASM VM for re-execution of a parachain candidate.

23
use std::any::{TypeId, Any};
24
use crate::primitives::{ValidationParams, ValidationResult, UpwardMessage};
25
use codec::{Decode, Encode};
26
use sp_core::storage::ChildInfo;
27
28
use sp_core::traits::CallInWasm;
use sp_wasm_interface::HostFunctions as _;
29
use sp_externalities::Extensions;
30

31
#[cfg(not(any(target_os = "android", target_os = "unknown")))]
32
pub use validation_host::{run_worker, ValidationPool, EXECUTION_TIMEOUT_SEC};
33
34

mod validation_host;
35
36
37
38

// maximum memory in bytes
const MAX_RUNTIME_MEM: usize = 1024 * 1024 * 1024; // 1 GiB
const MAX_CODE_MEM: usize = 16 * 1024 * 1024; // 16 MiB
39

40
41
42
43
44
sp_externalities::decl_extension! {
	/// The extension that is registered at the `Externalities` when validating a parachain state
	/// transition.
	pub(crate) struct ParachainExt(Box<dyn Externalities>);
}
45

46
47
48
49
impl ParachainExt {
	pub fn new<T: Externalities + 'static>(ext: T) -> Self {
		Self(Box::new(ext))
	}
50
}
51

52
53
/// A stub validation-pool defined when compiling for Android or WASM.
#[cfg(any(target_os = "android", target_os = "unknown"))]
54
55
56
57
58
#[derive(Clone)]
pub struct ValidationPool {
	_inner: (), // private field means not publicly-instantiable
}

59
#[cfg(any(target_os = "android", target_os = "unknown"))]
60
61
62
63
64
65
66
impl ValidationPool {
	/// Create a new `ValidationPool`.
	pub fn new() -> Self {
		ValidationPool { _inner: () }
	}
}

67
68
69
70
71
72
/// A stub function defined when compiling for Android or WASM.
#[cfg(any(target_os = "android", target_os = "unknown"))]
pub fn run_worker(_: &str) -> Result<(), String> {
	Err("Cannot run validation worker on this platform".to_string())
}

73
/// WASM code execution mode.
74
75
///
/// > Note: When compiling for WASM, the `Remote` variants are not available.
76
pub enum ExecutionMode<'a> {
77
78
79
	/// Execute in-process. The execution can not be interrupted or aborted.
	Local,
	/// Remote execution in a spawned process.
80
	Remote(&'a ValidationPool),
81
	/// Remote execution in a spawned test runner.
82
	RemoteTest(&'a ValidationPool),
83
84
}

85
86
87
/// Error type for the wasm executor
#[derive(Debug, derive_more::Display, derive_more::From)]
pub enum Error {
88
89
90
	/// Wasm executor error.
	#[display(fmt = "WASM executor error: {:?}", _0)]
	WasmExecutor(sc_executor::error::Error),
91
92
	/// Call data is too large.
	#[display(fmt = "Validation parameters are {} bytes, max allowed is {}", _0, MAX_RUNTIME_MEM)]
93
	#[from(ignore)]
94
	ParamsTooLarge(usize),
95
96
97
	/// Code size it too large.
	#[display(fmt = "WASM code is {} bytes, max allowed is {}", _0, MAX_CODE_MEM)]
	CodeTooLarge(usize),
98
99
100
	/// Bad return data or type.
	#[display(fmt = "Validation function returned invalid data.")]
	BadReturn,
101
102
103
104
105
	#[display(fmt = "Validation function timeout.")]
	Timeout,
	#[display(fmt = "IO error: {}", _0)]
	Io(std::io::Error),
	#[display(fmt = "System error: {}", _0)]
106
	System(Box<dyn std::error::Error + Send>),
107
108
	#[display(fmt = "WASM worker error: {}", _0)]
	External(String),
109
	#[display(fmt = "Shared memory error: {}", _0)]
110
	#[cfg(not(any(target_os = "android", target_os = "unknown")))]
111
	SharedMem(shared_memory::SharedMemError),
112
113
114
115
116
}

impl std::error::Error for Error {
	fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
		match self {
117
			Error::WasmExecutor(ref err) => Some(err),
118
119
			Error::Io(ref err) => Some(err),
			Error::System(ref err) => Some(&**err),
120
			#[cfg(not(any(target_os = "android", target_os = "unknown")))]
121
			Error::SharedMem(ref err) => Some(err),
122
			_ => None,
123
124
125
126
		}
	}
}

127
/// Externalities for parachain validation.
128
pub trait Externalities: Send {
129
	/// Called when a message is to be posted to the parachain's relay chain.
130
	fn post_upward_message(&mut self, message: UpwardMessage) -> Result<(), String>;
131
132
}

133
134
135
136
137
138
139
/// Validate a candidate under the given validation code.
///
/// This will fail if the validation code is not a proper parachain validation module.
pub fn validate_candidate<E: Externalities + 'static>(
	validation_code: &[u8],
	params: ValidationParams,
	ext: E,
140
	options: ExecutionMode<'_>,
141
142
143
144
145
) -> Result<ValidationResult, Error> {
	match options {
		ExecutionMode::Local => {
			validate_candidate_internal(validation_code, &params.encode(), ext)
		},
146
		#[cfg(not(any(target_os = "android", target_os = "unknown")))]
147
148
		ExecutionMode::Remote(pool) => {
			pool.validate_candidate(validation_code, params, ext, false)
149
		},
150
		#[cfg(not(any(target_os = "android", target_os = "unknown")))]
151
152
		ExecutionMode::RemoteTest(pool) => {
			pool.validate_candidate(validation_code, params, ext, true)
153
		},
154
		#[cfg(any(target_os = "android", target_os = "unknown"))]
155
		ExecutionMode::Remote(pool) =>
156
157
158
			Err(Error::System(Box::<dyn std::error::Error + Send + Sync>::from(
				"Remote validator not available".to_string()
			) as Box<_>)),
159
		#[cfg(any(target_os = "android", target_os = "unknown"))]
160
		ExecutionMode::RemoteTest(pool) =>
161
162
163
			Err(Error::System(Box::<dyn std::error::Error + Send + Sync>::from(
				"Remote validator not available".to_string()
			) as Box<_>)),
164
165
166
	}
}

167
/// The host functions provided by the wasm executor to the parachain wasm blob.
168
169
170
171
type HostFunctions = (
	sp_io::SubstrateHostFunctions,
	crate::wasm_api::parachain::HostFunctions,
);
172
173
174
175
176
177
178
179
180

/// Validate a candidate under the given validation code.
///
/// This will fail if the validation code is not a proper parachain validation module.
pub fn validate_candidate_internal<E: Externalities + 'static>(
	validation_code: &[u8],
	encoded_call_data: &[u8],
	externalities: E,
) -> Result<ValidationResult, Error> {
181
182
183
184
185
	let mut extensions = Extensions::new();
	extensions.register(ParachainExt::new(externalities));
	extensions.register(sp_core::traits::TaskExecutorExt(sp_core::tasks::executor()));

	let mut ext = ValidationExternalities(extensions);
186

187
	let executor = sc_executor::WasmExecutor::new(
188
189
		sc_executor::WasmExecutionMethod::Interpreted,
		// TODO: Make sure we don't use more than 1GB: https://github.com/paritytech/polkadot/issues/699
190
191
		Some(1024),
		HostFunctions::host_functions(),
Gavin Wood's avatar
Gavin Wood committed
192
		8
193
194
195
	);
	let res = executor.call_in_wasm(
		validation_code,
Gavin Wood's avatar
Gavin Wood committed
196
		None,
197
198
199
		"validate_block",
		encoded_call_data,
		&mut ext,
200
		sp_core::traits::MissingHostFunctions::Allow,
201
202
203
	)?;

	ValidationResult::decode(&mut &res[..]).map_err(|_| Error::BadReturn.into())
204
205
}

206
207
/// The validation externalities that will panic on any storage related access. They just provide
/// access to the parachain extension.
208
struct ValidationExternalities(Extensions);
209

210
211
212
impl sp_externalities::Externalities for ValidationExternalities {
	fn storage(&self, _: &[u8]) -> Option<Vec<u8>> {
		panic!("storage: unsupported feature for parachain validation")
213
214
	}

215
216
	fn storage_hash(&self, _: &[u8]) -> Option<Vec<u8>> {
		panic!("storage_hash: unsupported feature for parachain validation")
217
218
	}

219
	fn child_storage_hash(&self, _: &ChildInfo, _: &[u8]) -> Option<Vec<u8>> {
220
221
		panic!("child_storage_hash: unsupported feature for parachain validation")
	}
222

223
	fn child_storage(&self, _: &ChildInfo, _: &[u8]) -> Option<Vec<u8>> {
224
225
		panic!("child_storage: unsupported feature for parachain validation")
	}
226

227
	fn kill_child_storage(&mut self, _: &ChildInfo) {
228
229
		panic!("kill_child_storage: unsupported feature for parachain validation")
	}
230

231
232
233
	fn clear_prefix(&mut self, _: &[u8]) {
		panic!("clear_prefix: unsupported feature for parachain validation")
	}
234

235
	fn clear_child_prefix(&mut self, _: &ChildInfo, _: &[u8]) {
236
		panic!("clear_child_prefix: unsupported feature for parachain validation")
237
238
	}

239
240
	fn place_storage(&mut self, _: Vec<u8>, _: Option<Vec<u8>>) {
		panic!("place_storage: unsupported feature for parachain validation")
241
242
	}

243
	fn place_child_storage(&mut self, _: &ChildInfo, _: Vec<u8>, _: Option<Vec<u8>>) {
244
		panic!("place_child_storage: unsupported feature for parachain validation")
245
	}
246

247
248
249
	fn chain_id(&self) -> u64 {
		panic!("chain_id: unsupported feature for parachain validation")
	}
250

251
252
253
	fn storage_root(&mut self) -> Vec<u8> {
		panic!("storage_root: unsupported feature for parachain validation")
	}
254

255
	fn child_storage_root(&mut self, _: &ChildInfo) -> Vec<u8> {
256
257
		panic!("child_storage_root: unsupported feature for parachain validation")
	}
258

259
260
261
	fn storage_changes_root(&mut self, _: &[u8]) -> Result<Option<Vec<u8>>, ()> {
		panic!("storage_changes_root: unsupported feature for parachain validation")
	}
262

263
	fn next_child_storage_key(&self, _: &ChildInfo, _: &[u8]) -> Option<Vec<u8>> {
264
265
266
267
268
		panic!("next_child_storage_key: unsupported feature for parachain validation")
	}

	fn next_storage_key(&self, _: &[u8]) -> Option<Vec<u8>> {
		panic!("next_storage_key: unsupported feature for parachain validation")
Nikolay Volf's avatar
Nikolay Volf committed
269
270
271
272
273
274
275
276
	}

	fn storage_append(
		&mut self,
		_key: Vec<u8>,
		_value: Vec<u8>,
	) {
		panic!("storage_append: unsupported feature for parachain validation")
277
278
279
280
281
282
283
284
285
286
287
288
	}

	fn storage_start_transaction(&mut self) {
		panic!("storage_start_transaction: unsupported feature for parachain validation")
	}

	fn storage_rollback_transaction(&mut self) -> Result<(), ()> {
		panic!("storage_rollback_transaction: unsupported feature for parachain validation")
	}

	fn storage_commit_transaction(&mut self) -> Result<(), ()> {
		panic!("storage_commit_transaction: unsupported feature for parachain validation")
289
	}
Gavin Wood's avatar
Gavin Wood committed
290
291
292
293
294
295
296
297

	fn wipe(&mut self) {
		panic!("wipe: unsupported feature for parachain validation")
	}

	fn commit(&mut self) {
		panic!("commit: unsupported feature for parachain validation")
	}
298

299
300
301
302
303
304
305
306
307
308
309
310
	fn read_write_count(&self) -> (u32, u32, u32, u32) {
		panic!("read_write_count: unsupported feature for parachain validation")
	}

	fn reset_read_write_count(&mut self) {
		panic!("reset_read_write_count: unsupported feature for parachain validation")
	}

	fn set_whitelist(&mut self, _: Vec<Vec<u8>>) {
		panic!("set_whitelist: unsupported feature for parachain validation")
	}

311
312
313
	fn set_offchain_storage(&mut self, _: &[u8], _: std::option::Option<&[u8]>) {
		panic!("set_offchain_storage: unsupported feature for parachain validation")
	}
314
315
316
317
}

impl sp_externalities::ExtensionStore for ValidationExternalities {
	fn extension_by_type_id(&mut self, type_id: TypeId) -> Option<&mut dyn Any> {
318
		self.0.get_mut(type_id)
319
	}
Nikolay Volf's avatar
Nikolay Volf committed
320
321
322

	fn register_extension_with_type_id(
		&mut self,
323
324
		type_id: TypeId,
		extension: Box<dyn sp_externalities::Extension>,
Nikolay Volf's avatar
Nikolay Volf committed
325
	) -> Result<(), sp_externalities::Error> {
326
		self.0.register_with_type_id(type_id, extension)
Nikolay Volf's avatar
Nikolay Volf committed
327
328
329
330
	}

	fn deregister_extension_by_type_id(
		&mut self,
331
		type_id: TypeId,
Nikolay Volf's avatar
Nikolay Volf committed
332
	) -> Result<(), sp_externalities::Error> {
333
334
335
336
		match self.0.deregister(type_id) {
			Some(_) => Ok(()),
			None => Err(sp_externalities::Error::ExtensionIsNotRegistered(type_id))
		}
Nikolay Volf's avatar
Nikolay Volf committed
337
	}
338
}