Skip to content

Commit a20b0d1

Browse files
authored
[cleanup] Various fixes after script removal (#17142)
## Description - Renamed various `view` related items - Removed `Location::Script` and removed related code (which was always unreachable) - Moved FunctionView to the bytecode verifier ## Test plan - CI --- ## Release notes Check each box that your changes affect. If none of the boxes relate to your changes, release notes aren't required. For each box you select, include information after the relevant heading that describes the impact of your changes that a user might notice and any actions they must take to implement updates. - [ ] Protocol: - [ ] Nodes (Validators and Full nodes): - [ ] Indexer: - [ ] JSON-RPC: - [ ] GraphQL: - [ ] CLI: - [ ] Rust SDK:
1 parent 8430b5f commit a20b0d1

File tree

65 files changed

+1013
-892
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1013
-892
lines changed

crates/move-binary-format/src/binary_views.rs

Lines changed: 0 additions & 73 deletions
This file was deleted.

crates/move-binary-format/src/check_bounds.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ impl<'a> BoundsChecker<'a> {
135135
}
136136

137137
fn check_function_defs(&mut self) -> PartialVMResult<()> {
138-
let view = self.module;
139-
for (function_def_idx, function_def) in view.function_defs().iter().enumerate() {
138+
for (function_def_idx, function_def) in self.module.function_defs().iter().enumerate() {
140139
self.check_function_def(function_def_idx, function_def)?
141140
}
142141
Ok(())

crates/move-binary-format/src/errors.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ pub type PartialVMResult<T> = ::std::result::Result<T, PartialVMError>;
1919
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
2020
pub enum Location {
2121
Undefined,
22-
Script,
2322
Module(ModuleId),
2423
}
2524

@@ -317,7 +316,6 @@ impl fmt::Display for Location {
317316
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
318317
match self {
319318
Location::Undefined => write!(f, "UNDEFINED"),
320-
Location::Script => write!(f, "Script"),
321319
Location::Module(id) => write!(f, "Module {:?}", id),
322320
}
323321
}

crates/move-binary-format/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use std::fmt;
88

99
pub mod binary_config;
10-
pub mod binary_views;
1110
pub mod check_bounds;
1211
pub mod compatibility;
1312
#[macro_use]

crates/move-bytecode-source-map/src/mapping.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<'a> SourceMapping<'a> {
3636
}
3737
}
3838

39-
pub fn new_from_view(bytecode: &'a CompiledModule, default_loc: Loc) -> Result<Self> {
39+
pub fn new_without_source_map(bytecode: &'a CompiledModule, default_loc: Loc) -> Result<Self> {
4040
Ok(Self::new(
4141
SourceMap::dummy_from_view(bytecode, default_loc)?,
4242
bytecode,

crates/move-bytecode-source-map/tests/dummies.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use move_ir_types::location::Spanned;
99
#[test]
1010
fn test_empty_module() {
1111
let module = empty_module();
12-
let view = &module;
1312
let location = Spanned::unsafe_no_loc(()).loc;
14-
SourceMapping::new_from_view(view, location)
13+
SourceMapping::new_without_source_map(&module, location)
1514
.expect("unable to build source mapping for empty script");
1615
}

crates/move-bytecode-utils/src/lib.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,18 @@ impl<'a> Modules<'a> {
9898
}
9999

100100
pub fn resolve_struct(
101-
view: &CompiledModule,
101+
module: &CompiledModule,
102102
sidx: StructHandleIndex,
103103
) -> (&AccountAddress, &IdentStr, &IdentStr) {
104-
let shandle = view.struct_handle_at(sidx);
105-
let mhandle = view.module_handle_at(shandle.module);
106-
let address = view.address_identifier_at(mhandle.address);
107-
let module_name = view.identifier_at(mhandle.name);
108-
let struct_name = view.identifier_at(shandle.name);
104+
let shandle = module.struct_handle_at(sidx);
105+
let mhandle = module.module_handle_at(shandle.module);
106+
let address = module.address_identifier_at(mhandle.address);
107+
let module_name = module.identifier_at(mhandle.name);
108+
let struct_name = module.identifier_at(shandle.name);
109109
(address, module_name, struct_name)
110110
}
111111

112-
pub fn format_signature_token(view: &CompiledModule, t: &SignatureToken) -> String {
112+
pub fn format_signature_token(module: &CompiledModule, t: &SignatureToken) -> String {
113113
match t {
114114
SignatureToken::Bool => "bool".to_string(),
115115
SignatureToken::U8 => "u8".to_string(),
@@ -121,28 +121,28 @@ pub fn format_signature_token(view: &CompiledModule, t: &SignatureToken) -> Stri
121121
SignatureToken::Address => "address".to_string(),
122122
SignatureToken::Signer => "signer".to_string(),
123123
SignatureToken::Vector(inner) => {
124-
format!("vector<{}>", format_signature_token(view, inner))
124+
format!("vector<{}>", format_signature_token(module, inner))
125125
}
126-
SignatureToken::Reference(inner) => format!("&{}", format_signature_token(view, inner)),
126+
SignatureToken::Reference(inner) => format!("&{}", format_signature_token(module, inner)),
127127
SignatureToken::MutableReference(inner) => {
128-
format!("&mut {}", format_signature_token(view, inner))
128+
format!("&mut {}", format_signature_token(module, inner))
129129
}
130130
SignatureToken::TypeParameter(i) => format!("T{}", i),
131131

132-
SignatureToken::Struct(idx) => format_signature_token_struct(view, *idx, &[]),
132+
SignatureToken::Struct(idx) => format_signature_token_struct(module, *idx, &[]),
133133
SignatureToken::StructInstantiation(struct_inst) => {
134134
let (idx, ty_args) = &**struct_inst;
135-
format_signature_token_struct(view, *idx, ty_args)
135+
format_signature_token_struct(module, *idx, ty_args)
136136
}
137137
}
138138
}
139139

140140
pub fn format_signature_token_struct(
141-
view: &CompiledModule,
141+
module: &CompiledModule,
142142
sidx: StructHandleIndex,
143143
ty_args: &[SignatureToken],
144144
) -> String {
145-
let (address, module_name, struct_name) = resolve_struct(view, sidx);
145+
let (address, module_name, struct_name) = resolve_struct(module, sidx);
146146
let s;
147147
let ty_args_string = if ty_args.is_empty() {
148148
""
@@ -151,7 +151,7 @@ pub fn format_signature_token_struct(
151151
"<{}>",
152152
ty_args
153153
.iter()
154-
.map(|t| format_signature_token(view, t))
154+
.map(|t| format_signature_token(module, t))
155155
.collect::<Vec<_>>()
156156
.join(", ")
157157
);

crates/move-bytecode-verifier/src/absint.rs

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,29 @@
33
// SPDX-License-Identifier: Apache-2.0
44

55
use move_binary_format::{
6-
binary_views::FunctionView,
7-
control_flow_graph::{BlockId, ControlFlowGraph},
6+
control_flow_graph::{BlockId, ControlFlowGraph, VMControlFlowGraph},
87
errors::PartialVMResult,
9-
file_format::{Bytecode, CodeOffset},
8+
file_format::{
9+
AbilitySet, Bytecode, CodeOffset, CodeUnit, FunctionDefinitionIndex, FunctionHandle,
10+
Signature,
11+
},
12+
CompiledModule,
1013
};
1114
use move_bytecode_verifier_meter::{Meter, Scope};
1215
use std::collections::BTreeMap;
1316

17+
/// A `FunctionContext` holds all the information needed by the verifier for `FunctionDefinition`.`
18+
/// A control flow graph is built for a function when the `FunctionContext` is created.
19+
pub struct FunctionContext<'a> {
20+
index: Option<FunctionDefinitionIndex>,
21+
code: &'a CodeUnit,
22+
parameters: &'a Signature,
23+
return_: &'a Signature,
24+
locals: &'a Signature,
25+
type_parameters: &'a [AbilitySet],
26+
cfg: VMControlFlowGraph,
27+
}
28+
1429
/// Trait for finite-height abstract domains. Infinite height domains would require a more complex
1530
/// trait with widening and a partial order.
1631
pub trait AbstractDomain: Clone + Sized {
@@ -71,16 +86,16 @@ pub trait TransferFunctions {
7186
}
7287

7388
pub trait AbstractInterpreter: TransferFunctions {
74-
/// Analyze procedure local@function_view starting from pre-state local@initial_state.
89+
/// Analyze procedure local@function_context starting from pre-state local@initial_state.
7590
fn analyze_function(
7691
&mut self,
7792
initial_state: Self::State,
78-
function_view: &FunctionView,
93+
function_context: &FunctionContext,
7994
meter: &mut (impl Meter + ?Sized),
8095
) -> PartialVMResult<()> {
8196
meter.add(Scope::Function, ANALYZE_FUNCTION_BASE_COST)?;
8297
let mut inv_map = InvariantMap::new();
83-
let entry_block_id = function_view.cfg().entry_block_id();
98+
let entry_block_id = function_context.cfg().entry_block_id();
8499
let mut next_block = Some(entry_block_id);
85100
inv_map.insert(entry_block_id, BlockInvariant { pre: initial_state });
86101

@@ -90,19 +105,19 @@ pub trait AbstractInterpreter: TransferFunctions {
90105
None => {
91106
// This can only happen when all predecessors have errors,
92107
// so skip the block and move on to the next one
93-
next_block = function_view.cfg().next_block(block_id);
108+
next_block = function_context.cfg().next_block(block_id);
94109
continue;
95110
}
96111
};
97112

98113
let pre_state = &block_invariant.pre;
99114
// Note: this will stop analysis after the first error occurs, to avoid the risk of
100115
// subsequent crashes
101-
let post_state = self.execute_block(block_id, pre_state, function_view, meter)?;
116+
let post_state = self.execute_block(block_id, pre_state, function_context, meter)?;
102117

103-
let mut next_block_candidate = function_view.cfg().next_block(block_id);
118+
let mut next_block_candidate = function_context.cfg().next_block(block_id);
104119
// propagate postcondition of this block to successor blocks
105-
for successor_block_id in function_view.cfg().successors(block_id) {
120+
for successor_block_id in function_context.cfg().successors(block_id) {
106121
meter.add(Scope::Function, PER_SUCCESSOR_COST)?;
107122
match inv_map.get_mut(successor_block_id) {
108123
Some(next_block_invariant) => {
@@ -118,7 +133,7 @@ pub trait AbstractInterpreter: TransferFunctions {
118133
JoinResult::Changed => {
119134
// If the cur->successor is a back edge, jump back to the beginning
120135
// of the loop, instead of the normal next block
121-
if function_view
136+
if function_context
122137
.cfg()
123138
.is_back_edge(block_id, *successor_block_id)
124139
{
@@ -150,16 +165,64 @@ pub trait AbstractInterpreter: TransferFunctions {
150165
&mut self,
151166
block_id: BlockId,
152167
pre_state: &Self::State,
153-
function_view: &FunctionView,
168+
function_context: &FunctionContext,
154169
meter: &mut (impl Meter + ?Sized),
155170
) -> PartialVMResult<Self::State> {
156171
meter.add(Scope::Function, EXECUTE_BLOCK_BASE_COST)?;
157172
let mut state_acc = pre_state.clone();
158-
let block_end = function_view.cfg().block_end(block_id);
159-
for offset in function_view.cfg().instr_indexes(block_id) {
160-
let instr = &function_view.code().code[offset as usize];
173+
let block_end = function_context.cfg().block_end(block_id);
174+
for offset in function_context.cfg().instr_indexes(block_id) {
175+
let instr = &function_context.code().code[offset as usize];
161176
self.execute(&mut state_acc, instr, offset, block_end, meter)?
162177
}
163178
Ok(state_acc)
164179
}
165180
}
181+
182+
impl<'a> FunctionContext<'a> {
183+
// Creates a `FunctionContext` for a module function.
184+
pub fn new(
185+
module: &'a CompiledModule,
186+
index: FunctionDefinitionIndex,
187+
code: &'a CodeUnit,
188+
function_handle: &'a FunctionHandle,
189+
) -> Self {
190+
Self {
191+
index: Some(index),
192+
code,
193+
parameters: module.signature_at(function_handle.parameters),
194+
return_: module.signature_at(function_handle.return_),
195+
locals: module.signature_at(code.locals),
196+
type_parameters: &function_handle.type_parameters,
197+
cfg: VMControlFlowGraph::new(&code.code),
198+
}
199+
}
200+
201+
pub fn index(&self) -> Option<FunctionDefinitionIndex> {
202+
self.index
203+
}
204+
205+
pub fn code(&self) -> &CodeUnit {
206+
self.code
207+
}
208+
209+
pub fn parameters(&self) -> &Signature {
210+
self.parameters
211+
}
212+
213+
pub fn return_(&self) -> &Signature {
214+
self.return_
215+
}
216+
217+
pub fn locals(&self) -> &Signature {
218+
self.locals
219+
}
220+
221+
pub fn type_parameters(&self) -> &[AbilitySet] {
222+
self.type_parameters
223+
}
224+
225+
pub fn cfg(&self) -> &VMControlFlowGraph {
226+
&self.cfg
227+
}
228+
}

0 commit comments

Comments
 (0)