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
use crate::{
access::ModuleAccess,
file_format::{
AddressPoolIndex, FunctionSignature, IdentifierIndex, ModuleHandle, ModuleHandleIndex,
SignatureToken, StructHandle, StructHandleIndex,
},
};
use solana_libra_types::{
account_address::AccountAddress,
identifier::Identifier,
vm_error::{StatusCode, VMStatus},
};
use std::collections::BTreeMap;
pub struct Resolver {
address_map: BTreeMap<AccountAddress, AddressPoolIndex>,
identifier_map: BTreeMap<Identifier, IdentifierIndex>,
module_handle_map: BTreeMap<ModuleHandle, ModuleHandleIndex>,
struct_handle_map: BTreeMap<StructHandle, StructHandleIndex>,
}
impl Resolver {
pub fn new(module: &impl ModuleAccess) -> Self {
let mut address_map = BTreeMap::new();
for (idx, address) in module.address_pool().iter().enumerate() {
address_map.insert(address.clone(), AddressPoolIndex(idx as u16));
}
let mut identifier_map = BTreeMap::new();
for (idx, name) in module.identifiers().iter().enumerate() {
identifier_map.insert(name.clone(), IdentifierIndex(idx as u16));
}
let mut module_handle_map = BTreeMap::new();
for (idx, module_hadndle) in module.module_handles().iter().enumerate() {
module_handle_map.insert(module_hadndle.clone(), ModuleHandleIndex(idx as u16));
}
let mut struct_handle_map = BTreeMap::new();
for (idx, struct_handle) in module.struct_handles().iter().enumerate() {
struct_handle_map.insert(struct_handle.clone(), StructHandleIndex(idx as u16));
}
Self {
address_map,
identifier_map,
module_handle_map,
struct_handle_map,
}
}
pub fn import_signature_token(
&self,
dependency: &impl ModuleAccess,
sig_token: &SignatureToken,
) -> Result<SignatureToken, VMStatus> {
match sig_token {
SignatureToken::Bool
| SignatureToken::U64
| SignatureToken::String
| SignatureToken::ByteArray
| SignatureToken::Address
| SignatureToken::TypeParameter(_) => Ok(sig_token.clone()),
SignatureToken::Struct(sh_idx, types) => {
let struct_handle = dependency.struct_handle_at(*sh_idx);
let defining_module_handle = dependency.module_handle_at(struct_handle.module);
let defining_module_address = dependency.address_at(defining_module_handle.address);
let defining_module_name = dependency.identifier_at(defining_module_handle.name);
let local_module_handle = ModuleHandle {
address: *self
.address_map
.get(defining_module_address)
.ok_or_else(|| VMStatus::new(StatusCode::TYPE_RESOLUTION_FAILURE))?,
name: *self
.identifier_map
.get(defining_module_name)
.ok_or_else(|| VMStatus::new(StatusCode::TYPE_RESOLUTION_FAILURE))?,
};
let struct_name = dependency.identifier_at(struct_handle.name);
let local_struct_handle = StructHandle {
module: *self
.module_handle_map
.get(&local_module_handle)
.ok_or_else(|| VMStatus::new(StatusCode::TYPE_RESOLUTION_FAILURE))?,
name: *self
.identifier_map
.get(struct_name)
.ok_or_else(|| VMStatus::new(StatusCode::TYPE_RESOLUTION_FAILURE))?,
is_nominal_resource: struct_handle.is_nominal_resource,
type_formals: struct_handle.type_formals.clone(),
};
Ok(SignatureToken::Struct(
*self
.struct_handle_map
.get(&local_struct_handle)
.ok_or_else(|| VMStatus::new(StatusCode::TYPE_RESOLUTION_FAILURE))?,
types
.iter()
.map(|t| self.import_signature_token(dependency, &t))
.collect::<Result<Vec<_>, VMStatus>>()?,
))
}
SignatureToken::Reference(sub_sig_token) => Ok(SignatureToken::Reference(Box::new(
self.import_signature_token(dependency, sub_sig_token)?,
))),
SignatureToken::MutableReference(sub_sig_token) => {
Ok(SignatureToken::MutableReference(Box::new(
self.import_signature_token(dependency, sub_sig_token)?,
)))
}
}
}
pub fn import_function_signature(
&self,
dependency: &impl ModuleAccess,
func_sig: &FunctionSignature,
) -> Result<FunctionSignature, VMStatus> {
let mut return_types = Vec::<SignatureToken>::new();
let mut arg_types = Vec::<SignatureToken>::new();
for e in &func_sig.return_types {
return_types.push(self.import_signature_token(dependency, e)?);
}
for e in &func_sig.arg_types {
arg_types.push(self.import_signature_token(dependency, e)?);
}
Ok(FunctionSignature {
return_types,
arg_types,
type_formals: func_sig.type_formals.clone(),
})
}
}