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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright (c) The Libra Core Contributors
// SPDX-License-Identifier: Apache-2.0

//! Defines accessors for compiled modules.

use crate::{
    file_format::{
        AddressPoolIndex, ByteArrayPoolIndex, CompiledModule, CompiledModuleMut, CompiledScript,
        FieldDefinition, FieldDefinitionIndex, FunctionDefinition, FunctionDefinitionIndex,
        FunctionHandle, FunctionHandleIndex, FunctionSignature, FunctionSignatureIndex,
        IdentifierIndex, LocalsSignature, LocalsSignatureIndex, MemberCount, ModuleHandle,
        ModuleHandleIndex, StructDefinition, StructDefinitionIndex, StructHandle,
        StructHandleIndex, TypeSignature, TypeSignatureIndex, UserStringIndex,
    },
    internals::ModuleIndex,
    vm_string::{VMStr, VMString},
};
use solana_libra_types::{
    account_address::AccountAddress,
    byte_array::ByteArray,
    identifier::{IdentStr, Identifier},
    language_storage::ModuleId,
    vm_error::{StatusCode, VMStatus},
};

/// Represents accessors for a compiled module.
///
/// This is a trait to allow working across different wrappers for `CompiledModule`.
pub trait ModuleAccess: Sync {
    /// Returns the `CompiledModule` that will be used for accesses.
    fn as_module(&self) -> &CompiledModule;

    /// Returns the `ModuleHandle` for `self`.
    fn self_handle(&self) -> &ModuleHandle {
        self.module_handle_at(ModuleHandleIndex::new(
            CompiledModule::IMPLEMENTED_MODULE_INDEX,
        ))
    }

    /// Returns the name of the module.
    fn name(&self) -> &IdentStr {
        self.identifier_at(self.self_handle().name)
    }

    /// Returns the address of the module.
    fn address(&self) -> &AccountAddress {
        self.address_at(self.self_handle().address)
    }

    fn module_handle_at(&self, idx: ModuleHandleIndex) -> &ModuleHandle {
        &self.as_module().as_inner().module_handles[idx.into_index()]
    }

    fn struct_handle_at(&self, idx: StructHandleIndex) -> &StructHandle {
        &self.as_module().as_inner().struct_handles[idx.into_index()]
    }

    fn function_handle_at(&self, idx: FunctionHandleIndex) -> &FunctionHandle {
        &self.as_module().as_inner().function_handles[idx.into_index()]
    }

    fn type_signature_at(&self, idx: TypeSignatureIndex) -> &TypeSignature {
        &self.as_module().as_inner().type_signatures[idx.into_index()]
    }

    fn function_signature_at(&self, idx: FunctionSignatureIndex) -> &FunctionSignature {
        &self.as_module().as_inner().function_signatures[idx.into_index()]
    }

    fn locals_signature_at(&self, idx: LocalsSignatureIndex) -> &LocalsSignature {
        &self.as_module().as_inner().locals_signatures[idx.into_index()]
    }

    fn identifier_at(&self, idx: IdentifierIndex) -> &IdentStr {
        &self.as_module().as_inner().identifiers[idx.into_index()]
    }

    fn user_string_at(&self, idx: UserStringIndex) -> &VMStr {
        &self.as_module().as_inner().user_strings[idx.into_index()]
    }

    fn byte_array_at(&self, idx: ByteArrayPoolIndex) -> &ByteArray {
        &self.as_module().as_inner().byte_array_pool[idx.into_index()]
    }

    fn address_at(&self, idx: AddressPoolIndex) -> &AccountAddress {
        &self.as_module().as_inner().address_pool[idx.into_index()]
    }

    fn struct_def_at(&self, idx: StructDefinitionIndex) -> &StructDefinition {
        &self.as_module().as_inner().struct_defs[idx.into_index()]
    }

    fn field_def_at(&self, idx: FieldDefinitionIndex) -> &FieldDefinition {
        &self.as_module().as_inner().field_defs[idx.into_index()]
    }

    fn function_def_at(&self, idx: FunctionDefinitionIndex) -> &FunctionDefinition {
        &self.as_module().as_inner().function_defs[idx.into_index()]
    }

    fn get_field_signature(&self, field_definition_index: FieldDefinitionIndex) -> &TypeSignature {
        let field_definition = self.field_def_at(field_definition_index);
        self.type_signature_at(field_definition.signature)
    }

    // XXX is a partial range required here?
    fn module_handles(&self) -> &[ModuleHandle] {
        &self.as_module().as_inner().module_handles
    }

    fn struct_handles(&self) -> &[StructHandle] {
        &self.as_module().as_inner().struct_handles
    }

    fn function_handles(&self) -> &[FunctionHandle] {
        &self.as_module().as_inner().function_handles
    }

    fn type_signatures(&self) -> &[TypeSignature] {
        &self.as_module().as_inner().type_signatures
    }

    fn function_signatures(&self) -> &[FunctionSignature] {
        &self.as_module().as_inner().function_signatures
    }

    fn locals_signatures(&self) -> &[LocalsSignature] {
        &self.as_module().as_inner().locals_signatures
    }

    fn byte_array_pool(&self) -> &[ByteArray] {
        &self.as_module().as_inner().byte_array_pool
    }

    fn address_pool(&self) -> &[AccountAddress] {
        &self.as_module().as_inner().address_pool
    }

    fn identifiers(&self) -> &[Identifier] {
        &self.as_module().as_inner().identifiers
    }

    fn user_strings(&self) -> &[VMString] {
        &self.as_module().as_inner().user_strings
    }

    fn struct_defs(&self) -> &[StructDefinition] {
        &self.as_module().as_inner().struct_defs
    }

    fn field_defs(&self) -> &[FieldDefinition] {
        &self.as_module().as_inner().field_defs
    }

    fn function_defs(&self) -> &[FunctionDefinition] {
        &self.as_module().as_inner().function_defs
    }

    fn module_id_for_handle(&self, module_handle_idx: &ModuleHandle) -> ModuleId {
        self.as_module().module_id_for_handle(module_handle_idx)
    }

    fn self_id(&self) -> ModuleId {
        self.as_module().self_id()
    }

    fn field_def_range(
        &self,
        field_count: MemberCount,
        first_field: FieldDefinitionIndex,
    ) -> &[FieldDefinition] {
        let first_field = first_field.0 as usize;
        let field_count = field_count as usize;
        // Both `first_field` and `field_count` are `u16` before being converted to usize
        assume!(first_field <= usize::max_value() - field_count);
        let last_field = first_field + field_count;
        &self.as_module().as_inner().field_defs[first_field..last_field]
    }

    fn is_field_in_struct(
        &self,
        field_definition_index: FieldDefinitionIndex,
        struct_handle_index: StructHandleIndex,
    ) -> bool {
        let field_definition = self.field_def_at(field_definition_index);
        struct_handle_index == field_definition.struct_
    }
}

/// Represents accessors for a compiled script.
///
/// This is a trait to allow working across different wrappers for `CompiledScript`.
pub trait ScriptAccess: Sync {
    /// Returns the `CompiledScript` that will be used for accesses.
    fn as_script(&self) -> &CompiledScript;

    fn module_handle_at(&self, idx: ModuleHandleIndex) -> &ModuleHandle {
        &self.as_script().as_inner().module_handles[idx.into_index()]
    }

    fn struct_handle_at(&self, idx: StructHandleIndex) -> &StructHandle {
        &self.as_script().as_inner().struct_handles[idx.into_index()]
    }

    fn function_handle_at(&self, idx: FunctionHandleIndex) -> &FunctionHandle {
        &self.as_script().as_inner().function_handles[idx.into_index()]
    }

    fn type_signature_at(&self, idx: TypeSignatureIndex) -> &TypeSignature {
        &self.as_script().as_inner().type_signatures[idx.into_index()]
    }

    fn function_signature_at(&self, idx: FunctionSignatureIndex) -> &FunctionSignature {
        &self.as_script().as_inner().function_signatures[idx.into_index()]
    }

    fn locals_signature_at(&self, idx: LocalsSignatureIndex) -> &LocalsSignature {
        &self.as_script().as_inner().locals_signatures[idx.into_index()]
    }

    fn identifier_at(&self, idx: IdentifierIndex) -> &IdentStr {
        &self.as_script().as_inner().identifiers[idx.into_index()]
    }

    fn byte_array_at(&self, idx: ByteArrayPoolIndex) -> &ByteArray {
        &self.as_script().as_inner().byte_array_pool[idx.into_index()]
    }

    fn address_at(&self, idx: AddressPoolIndex) -> &AccountAddress {
        &self.as_script().as_inner().address_pool[idx.into_index()]
    }

    fn module_handles(&self) -> &[ModuleHandle] {
        &self.as_script().as_inner().module_handles
    }

    fn struct_handles(&self) -> &[StructHandle] {
        &self.as_script().as_inner().struct_handles
    }

    fn function_handles(&self) -> &[FunctionHandle] {
        &self.as_script().as_inner().function_handles
    }

    fn type_signatures(&self) -> &[TypeSignature] {
        &self.as_script().as_inner().type_signatures
    }

    fn function_signatures(&self) -> &[FunctionSignature] {
        &self.as_script().as_inner().function_signatures
    }

    fn locals_signatures(&self) -> &[LocalsSignature] {
        &self.as_script().as_inner().locals_signatures
    }

    fn byte_array_pool(&self) -> &[ByteArray] {
        &self.as_script().as_inner().byte_array_pool
    }

    fn address_pool(&self) -> &[AccountAddress] {
        &self.as_script().as_inner().address_pool
    }

    fn identifiers(&self) -> &[Identifier] {
        &self.as_script().as_inner().identifiers
    }

    fn main(&self) -> &FunctionDefinition {
        &self.as_script().as_inner().main
    }
}

impl ModuleAccess for CompiledModule {
    fn as_module(&self) -> &CompiledModule {
        self
    }
}

impl ScriptAccess for CompiledScript {
    fn as_script(&self) -> &CompiledScript {
        self
    }
}

impl CompiledModuleMut {
    #[inline]
    pub(crate) fn check_field_range(
        &self,
        field_count: MemberCount,
        first_field: FieldDefinitionIndex,
    ) -> Option<VMStatus> {
        let first_field = first_field.into_index();
        let field_count = field_count as usize;
        // Both first_field and field_count are u16 so this is guaranteed to not overflow.
        // Note that last_field is exclusive, i.e. fields are in the range
        // [first_field, last_field).
        let last_field = first_field + field_count;
        if last_field > self.field_defs.len() {
            let msg = format!(
                "Field definition range [{},{}) out of range for {}",
                first_field,
                last_field,
                self.field_defs.len()
            );
            let status = VMStatus::new(StatusCode::RANGE_OUT_OF_BOUNDS).with_message(msg);
            Some(status)
        } else {
            None
        }
    }
}