tet_core/sandbox.rs
1// This file is part of Tetcore.
2
3// Copyright (C) 2018-2021 Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Definition of a sandbox environment.
19
20use codec::{Encode, Decode};
21use tetcore_std::vec::Vec;
22
23/// Error error that can be returned from host function.
24#[derive(Encode, Decode)]
25#[derive(crate::RuntimeDebug)]
26pub struct HostError;
27
28/// Describes an entity to define or import into the environment.
29#[derive(Clone, PartialEq, Eq, Encode, Decode)]
30#[derive(crate::RuntimeDebug)]
31pub enum ExternEntity {
32 /// Function that is specified by an index in a default table of
33 /// a module that creates the sandbox.
34 #[codec(index = 1)]
35 Function(u32),
36
37 /// Linear memory that is specified by some identifier returned by sandbox
38 /// module upon creation new sandboxed memory.
39 #[codec(index = 2)]
40 Memory(u32),
41}
42
43/// An entry in a environment definition table.
44///
45/// Each entry has a two-level name and description of an entity
46/// being defined.
47#[derive(Clone, PartialEq, Eq, Encode, Decode)]
48#[derive(crate::RuntimeDebug)]
49pub struct Entry {
50 /// Module name of which corresponding entity being defined.
51 pub module_name: Vec<u8>,
52 /// Field name in which corresponding entity being defined.
53 pub field_name: Vec<u8>,
54 /// External entity being defined.
55 pub entity: ExternEntity,
56}
57
58/// Definition of runtime that could be used by sandboxed code.
59#[derive(Clone, PartialEq, Eq, Encode, Decode)]
60#[derive(crate::RuntimeDebug)]
61pub struct EnvironmentDefinition {
62 /// Vector of all entries in the environment definition.
63 pub entries: Vec<Entry>,
64}
65
66/// Constant for specifying no limit when creating a sandboxed
67/// memory instance. For FFI purposes.
68pub const MEM_UNLIMITED: u32 = -1i32 as u32;
69
70/// No error happened.
71///
72/// For FFI purposes.
73pub const ERR_OK: u32 = 0;
74
75/// Validation or instantiation error occurred when creating new
76/// sandboxed module instance.
77///
78/// For FFI purposes.
79pub const ERR_MODULE: u32 = -1i32 as u32;
80
81/// Out-of-bounds access attempted with memory or table.
82///
83/// For FFI purposes.
84pub const ERR_OUT_OF_BOUNDS: u32 = -2i32 as u32;
85
86/// Execution error occurred (typically trap).
87///
88/// For FFI purposes.
89pub const ERR_EXECUTION: u32 = -3i32 as u32;
90
91#[cfg(test)]
92mod tests {
93 use super::*;
94 use std::fmt;
95 use codec::Codec;
96
97 fn roundtrip<S: Codec + PartialEq + fmt::Debug>(s: S) {
98 let encoded = s.encode();
99 assert_eq!(S::decode(&mut &encoded[..]).unwrap(), s);
100 }
101
102 #[test]
103 fn env_def_roundtrip() {
104 roundtrip(EnvironmentDefinition {
105 entries: vec![],
106 });
107
108 roundtrip(EnvironmentDefinition {
109 entries: vec![
110 Entry {
111 module_name: b"kernel"[..].into(),
112 field_name: b"memory"[..].into(),
113 entity: ExternEntity::Memory(1337),
114 },
115 ],
116 });
117
118 roundtrip(EnvironmentDefinition {
119 entries: vec![
120 Entry {
121 module_name: b"env"[..].into(),
122 field_name: b"abort"[..].into(),
123 entity: ExternEntity::Function(228),
124 },
125 ],
126 });
127 }
128}