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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

//! This module implements a checker for verifying properties about the acquires list on function
//! definitions. Function definitions must annotate the global resources (declared in that module)
//! accesssed by `BorrowGlobal`, `MoveFrom`, and any transitive function calls
//! The list of acquired resources (stored in `FunctionDefinition`'s `acquires_global_resources`
//! field) must have:
//! - No duplicate resources (checked by `check_duplication`)
//! - No missing resources (any resource acquired must be present)
//! - No additional resources (no extraneous resources not actually acquired)

use move_binary_format::{
    access::ModuleAccess,
    errors::{PartialVMError, PartialVMResult},
    file_format::{
        Bytecode, CodeOffset, CompiledModule, FunctionDefinition, FunctionDefinitionIndex,
        FunctionHandle, FunctionHandleIndex, StructDefinitionIndex,
    },
};
use move_core_types::vm_status::StatusCode;
use std::collections::{BTreeSet, HashMap};

pub(crate) struct AcquiresVerifier<'a> {
    module: &'a CompiledModule,
    current_function: FunctionDefinitionIndex,
    annotated_acquires: BTreeSet<StructDefinitionIndex>,
    actual_acquires: BTreeSet<StructDefinitionIndex>,
    handle_to_def: HashMap<FunctionHandleIndex, &'a FunctionDefinition>,
}

impl<'a> AcquiresVerifier<'a> {
    pub(crate) fn verify(
        module: &'a CompiledModule,
        index: FunctionDefinitionIndex,
        function_definition: &'a FunctionDefinition,
    ) -> PartialVMResult<()> {
        let annotated_acquires = function_definition
            .acquires_global_resources
            .iter()
            .cloned()
            .collect();
        let mut handle_to_def = HashMap::new();
        for func_def in module.function_defs() {
            handle_to_def.insert(func_def.function, func_def);
        }
        let mut verifier = Self {
            module,
            current_function: index,
            annotated_acquires,
            actual_acquires: BTreeSet::new(),
            handle_to_def,
        };

        for (offset, instruction) in function_definition
            .code
            .as_ref()
            .unwrap()
            .code
            .iter()
            .enumerate()
        {
            verifier.verify_instruction(instruction, offset as CodeOffset)?
        }

        for annotation in verifier.annotated_acquires {
            if !verifier.actual_acquires.contains(&annotation) {
                return Err(PartialVMError::new(
                    StatusCode::EXTRANEOUS_ACQUIRES_ANNOTATION,
                ));
            }

            let struct_def = module.struct_defs().get(annotation.0 as usize).unwrap();
            let struct_handle = module.struct_handle_at(struct_def.struct_handle);
            if !struct_handle.abilities.has_key() {
                return Err(PartialVMError::new(StatusCode::INVALID_ACQUIRES_ANNOTATION));
            }
        }

        Ok(())
    }

    fn verify_instruction(
        &mut self,
        instruction: &Bytecode,
        offset: CodeOffset,
    ) -> PartialVMResult<()> {
        match instruction {
            Bytecode::Call(idx) => self.call_acquire(*idx, offset),
            Bytecode::CallGeneric(idx) => {
                let fi = self.module.function_instantiation_at(*idx);
                self.call_acquire(fi.handle, offset)
            }
            Bytecode::MoveFrom(idx)
            | Bytecode::MutBorrowGlobal(idx)
            | Bytecode::ImmBorrowGlobal(idx) => self.struct_acquire(*idx, offset),
            Bytecode::MoveFromGeneric(idx)
            | Bytecode::MutBorrowGlobalGeneric(idx)
            | Bytecode::ImmBorrowGlobalGeneric(idx) => {
                let si = self.module.struct_instantiation_at(*idx);
                self.struct_acquire(si.def, offset)
            }
            _ => Ok(()),
        }
    }

    fn call_acquire(
        &mut self,
        fh_idx: FunctionHandleIndex,
        offset: CodeOffset,
    ) -> PartialVMResult<()> {
        let function_handle = self.module.function_handle_at(fh_idx);
        let mut function_acquired_resources =
            self.function_acquired_resources(function_handle, fh_idx);
        for acquired_resource in &function_acquired_resources {
            if !self.annotated_acquires.contains(acquired_resource) {
                return Err(self.error(StatusCode::MISSING_ACQUIRES_ANNOTATION, offset));
            }
        }
        self.actual_acquires
            .append(&mut function_acquired_resources);
        Ok(())
    }

    fn struct_acquire(
        &mut self,
        sd_idx: StructDefinitionIndex,
        offset: CodeOffset,
    ) -> PartialVMResult<()> {
        if self.annotated_acquires.contains(&sd_idx) {
            self.actual_acquires.insert(sd_idx);
            Ok(())
        } else {
            Err(self.error(StatusCode::MISSING_ACQUIRES_ANNOTATION, offset))
        }
    }

    fn function_acquired_resources(
        &self,
        function_handle: &FunctionHandle,
        fh_idx: FunctionHandleIndex,
    ) -> BTreeSet<StructDefinitionIndex> {
        if function_handle.module != self.module.self_handle_idx() {
            return BTreeSet::new();
        }
        match self.handle_to_def.get(&fh_idx) {
            Some(func_def) => func_def.acquires_global_resources.iter().cloned().collect(),
            None => BTreeSet::new(),
        }
    }

    fn error(&self, status: StatusCode, offset: CodeOffset) -> PartialVMError {
        PartialVMError::new(status).at_code_offset(self.current_function, offset)
    }
}