-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathinstruction_consistency.rs
More file actions
259 lines (240 loc) · 11.2 KB
/
instruction_consistency.rs
File metadata and controls
259 lines (240 loc) · 11.2 KB
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
// Copyright (c) The Diem Core Contributors
// Copyright (c) The Move Contributors
// SPDX-License-Identifier: Apache-2.0
//! This module defines the transfer functions for verifying consistency of each bytecode
//! instruction, in particular, for the bytecode instructions that come in both generic and
//! non-generic flavors. It also checks constraints on instructions like VecPack/VecUnpack.
use move_binary_format::{
access::ModuleAccess,
binary_views::BinaryIndexedView,
errors::{Location, PartialVMError, PartialVMResult, VMResult},
file_format::{
Bytecode, CodeOffset, CodeUnit, CompiledModule, CompiledScript, FieldHandleIndex,
FunctionDefinitionIndex, FunctionHandleIndex, StructDefinitionIndex,
StructVariantHandleIndex, TableIndex, VariantFieldHandleIndex,
},
};
use move_core_types::{function::ClosureMask, vm_status::StatusCode};
pub struct InstructionConsistency<'a> {
resolver: BinaryIndexedView<'a>,
current_function: Option<FunctionDefinitionIndex>,
}
impl<'a> InstructionConsistency<'a> {
pub fn verify_module(module: &'a CompiledModule) -> VMResult<()> {
Self::verify_module_impl(module).map_err(|e| e.finish(Location::Module(module.self_id())))
}
fn verify_module_impl(module: &'a CompiledModule) -> PartialVMResult<()> {
let resolver = BinaryIndexedView::Module(module);
for (idx, func_def) in module.function_defs().iter().enumerate() {
match &func_def.code {
None => (),
Some(code) => {
let checker = Self {
resolver,
current_function: Some(FunctionDefinitionIndex(idx as TableIndex)),
};
checker.check_instructions(code)?
},
}
}
Ok(())
}
pub fn verify_script(module: &'a CompiledScript) -> VMResult<()> {
Self::verify_script_impl(module).map_err(|e| e.finish(Location::Script))
}
pub fn verify_script_impl(script: &'a CompiledScript) -> PartialVMResult<()> {
let checker = Self {
resolver: BinaryIndexedView::Script(script),
current_function: None,
};
checker.check_instructions(&script.code)
}
fn check_instructions(&self, code: &CodeUnit) -> PartialVMResult<()> {
for (offset, instr) in code.code.iter().enumerate() {
use Bytecode::*;
match instr {
MutBorrowField(field_handle_index) | ImmBorrowField(field_handle_index) => {
self.check_field_op(offset, *field_handle_index, /* generic */ false)?;
},
MutBorrowFieldGeneric(field_inst_index)
| ImmBorrowFieldGeneric(field_inst_index) => {
let field_inst = self.resolver.field_instantiation_at(*field_inst_index)?;
self.check_field_op(offset, field_inst.handle, /* generic */ true)?;
},
MutBorrowVariantField(field_handle_index)
| ImmBorrowVariantField(field_handle_index) => {
self.check_variant_field_op(
offset,
*field_handle_index,
/* generic */ false,
)?;
},
MutBorrowVariantFieldGeneric(field_inst_index)
| ImmBorrowVariantFieldGeneric(field_inst_index) => {
let field_inst = self
.resolver
.variant_field_instantiation_at(*field_inst_index)?;
self.check_variant_field_op(
offset,
field_inst.handle,
/* generic */ true,
)?;
},
Call(idx) => {
self.check_function_op(offset, *idx, /* generic */ false)?;
},
CallGeneric(idx) => {
let func_inst = self.resolver.function_instantiation_at(*idx);
self.check_function_op(offset, func_inst.handle, /* generic */ true)?;
},
PackClosure(idx, mask) => {
self.check_function_op(offset, *idx, /* generic */ false)?;
self.check_closure_mask(offset, *idx, *mask)?
},
PackClosureGeneric(idx, mask) => {
let func_inst = self.resolver.function_instantiation_at(*idx);
self.check_function_op(offset, func_inst.handle, /* generic */ true)?;
self.check_closure_mask(offset, func_inst.handle, *mask)?
},
Pack(idx) | Unpack(idx) => {
self.check_struct_op(offset, *idx, /* generic */ false)?;
},
PackGeneric(idx) | UnpackGeneric(idx) => {
let struct_inst = self.resolver.struct_instantiation_at(*idx)?;
self.check_struct_op(offset, struct_inst.def, /* generic */ true)?;
},
PackVariant(idx) | UnpackVariant(idx) | TestVariant(idx) => {
self.check_variant_op(offset, *idx, /* generic */ false)?;
},
PackVariantGeneric(idx) | UnpackVariantGeneric(idx) | TestVariantGeneric(idx) => {
let struct_inst = self.resolver.struct_variant_instantiation_at(*idx)?;
self.check_variant_op(offset, struct_inst.handle, /* generic */ true)?;
},
MutBorrowGlobal(idx) | ImmBorrowGlobal(idx) => {
self.check_struct_op(offset, *idx, /* generic */ false)?;
},
MutBorrowGlobalGeneric(idx) | ImmBorrowGlobalGeneric(idx) => {
let struct_inst = self.resolver.struct_instantiation_at(*idx)?;
self.check_struct_op(offset, struct_inst.def, /* generic */ true)?;
},
Exists(idx) | MoveFrom(idx) | MoveTo(idx) => {
self.check_struct_op(offset, *idx, /* generic */ false)?;
},
ExistsGeneric(idx) | MoveFromGeneric(idx) | MoveToGeneric(idx) => {
let struct_inst = self.resolver.struct_instantiation_at(*idx)?;
self.check_struct_op(offset, struct_inst.def, /* generic */ true)?;
},
VecPack(_, num) | VecUnpack(_, num) => {
if *num > u16::MAX as u64 {
return Err(PartialVMError::new(StatusCode::CONSTRAINT_NOT_SATISFIED)
.at_code_offset(self.current_function(), offset as CodeOffset)
.with_message("VecPack/VecUnpack argument out of range".to_string()));
}
},
// List out the other options explicitly so there's a compile error if a new
// bytecode gets added.
CallClosure(_) | FreezeRef | Pop | Ret | Branch(_) | BrTrue(_) | BrFalse(_)
| LdU8(_) | LdU16(_) | LdU32(_) | LdU64(_) | LdU128(_) | LdU256(_) | LdConst(_)
| CastU8 | CastU16 | CastU32 | CastU64 | CastU128 | CastU256 | LdTrue | LdFalse
| ReadRef | WriteRef | Add | Sub | Mul | Mod | Div | Negate | BitOr | BitAnd
| Xor | Shl | Shr | Or | And | Not | Eq | Neq | Lt | Gt | Le | Ge | CopyLoc(_)
| MoveLoc(_) | StLoc(_) | MutBorrowLoc(_) | ImmBorrowLoc(_) | VecLen(_)
| VecImmBorrow(_) | VecMutBorrow(_) | VecPushBack(_) | VecPopBack(_)
| VecSwap(_) | Abort | AbortMsg | Nop | LdI8(_) | LdI16(_) | LdI32(_)
| LdI64(_) | LdI128(_) | LdI256(_) | CastI8 | CastI16 | CastI32 | CastI64
| CastI128 | CastI256 => (),
}
}
Ok(())
}
//
// Helpers for instructions that come in a generic and non generic form.
// Verifies the generic form uses a generic member and the non generic form
// a non generic one.
//
fn check_field_op(
&self,
offset: usize,
field_handle_index: FieldHandleIndex,
generic: bool,
) -> PartialVMResult<()> {
let field_handle = self.resolver.field_handle_at(field_handle_index)?;
self.check_struct_op(offset, field_handle.owner, generic)
}
fn check_variant_field_op(
&self,
offset: usize,
field_handle_index: VariantFieldHandleIndex,
generic: bool,
) -> PartialVMResult<()> {
let field_handle = self.resolver.variant_field_handle_at(field_handle_index)?;
self.check_struct_op(offset, field_handle.struct_index, generic)
}
fn current_function(&self) -> FunctionDefinitionIndex {
self.current_function.unwrap_or(FunctionDefinitionIndex(0))
}
fn check_struct_op(
&self,
offset: usize,
struct_def_index: StructDefinitionIndex,
generic: bool,
) -> PartialVMResult<()> {
let struct_def = self.resolver.struct_def_at(struct_def_index)?;
let struct_handle = self.resolver.struct_handle_at(struct_def.struct_handle);
if struct_handle.type_parameters.is_empty() == generic {
return Err(
PartialVMError::new(StatusCode::GENERIC_MEMBER_OPCODE_MISMATCH)
.at_code_offset(self.current_function(), offset as CodeOffset),
);
}
Ok(())
}
fn check_variant_op(
&self,
offset: usize,
idx: StructVariantHandleIndex,
generic: bool,
) -> PartialVMResult<()> {
let variant_handle = self.resolver.struct_variant_handle_at(idx)?;
let struct_def = self.resolver.struct_def_at(variant_handle.struct_index)?;
let struct_handle = self.resolver.struct_handle_at(struct_def.struct_handle);
if struct_handle.type_parameters.is_empty() == generic {
return Err(
PartialVMError::new(StatusCode::GENERIC_MEMBER_OPCODE_MISMATCH)
.at_code_offset(self.current_function(), offset as CodeOffset),
);
}
Ok(())
}
fn check_function_op(
&self,
offset: usize,
func_handle_index: FunctionHandleIndex,
generic: bool,
) -> PartialVMResult<()> {
let function_handle = self.resolver.function_handle_at(func_handle_index);
if function_handle.type_parameters.is_empty() == generic {
return Err(
PartialVMError::new(StatusCode::GENERIC_MEMBER_OPCODE_MISMATCH)
.at_code_offset(self.current_function(), offset as CodeOffset),
);
}
Ok(())
}
fn check_closure_mask(
&self,
offset: usize,
func_handle_index: FunctionHandleIndex,
mask: ClosureMask,
) -> PartialVMResult<()> {
let function_handle = self.resolver.function_handle_at(func_handle_index);
let signature = self.resolver.signature_at(function_handle.parameters);
if let Some(max) = mask.max_captured() {
if max >= signature.len() {
return Err(PartialVMError::new(StatusCode::INVALID_CLOSURE_MASK)
.at_code_offset(self.current_function(), offset as CodeOffset));
}
}
Ok(())
}
}