forked from use-ink/ink
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage_never_freed.rs
More file actions
348 lines (331 loc) · 11.7 KB
/
storage_never_freed.rs
File metadata and controls
348 lines (331 loc) · 11.7 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright (C) Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::ink_utils::{
expand_unnamed_consts,
find_contract_impl_id,
find_storage_struct,
};
use clippy_utils::{
diagnostics::span_lint_and_then,
is_lint_allowed,
match_def_path,
match_path,
source::snippet_opt,
};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{
self as hir,
def::{
DefKind,
Res,
},
def_id::{
DefId,
LocalDefId,
},
intravisit::{
walk_body,
walk_expr,
Visitor,
},
Expr,
ExprKind,
ImplItemKind,
ItemId,
ItemKind,
Node,
Path,
QPath,
TyKind,
};
use rustc_lint::{
LateContext,
LateLintPass,
};
use rustc_session::{
declare_lint,
declare_lint_pass,
};
use std::collections::BTreeMap;
declare_lint! {
/// **What it does:**
/// This lint ensures that for every storage field with a collection type that supports adding
/// new elements, there's also an operation for removing elements.
///
/// **Why is this bad?**
/// When a user executes a contract function that writes to storage, the user has to put a
/// deposit down for the amount of storage space used. Whoever frees up that storage at some
/// later point gets the deposit back. Therefore, it is always a good idea to make it possible
/// for users to free up their storage space.
///
/// **Example:**
///
/// In the following example there is a storage field with the `Mapping` type that has an
/// function that inserts new elements:
/// ```rust
/// #[ink(storage)]
/// pub struct Transaction {
/// values: Mapping<AccountId, AccountId>,
/// }
///
/// fn add_value(&mut self, k: &AccountId, v: &AccountId) {
/// // ...
/// self.values.insert(k, v);
/// // ...
/// }
/// ```
///
/// But, ideally, there also should be a function that allows the user to remove elements from
/// the Mapping freeing storage space:
///
/// ```rust
/// fn del_value(&mut self, k: &AccountId) {
/// // ...
/// self.values.remove(k);
/// // ...
/// }
/// ```
pub STORAGE_NEVER_FREED,
Allow,
"storage never freed"
}
declare_lint_pass!(StorageNeverFreed => [STORAGE_NEVER_FREED]);
enum CollectionTy {
Vec,
Map,
}
/// Fields with collection types that should have both insert and remove operations
struct FieldInfo {
pub did: LocalDefId,
pub ty: CollectionTy,
// TODO: replace w/ ids
pub has_insert: bool,
pub has_remove: bool,
}
type FieldName = String;
type FieldsMap = BTreeMap<FieldName, FieldInfo>;
// https://paritytech.github.io/ink/ink_prelude/vec/struct.Vec.html
const VEC_INSERT_OPERATIONS: [&str; 6] = [
"append",
"extend_from_slice",
"extend_from_within",
"insert",
"push",
"push_with_capacity",
];
const VEC_REMOVE_OPERATIONS: [&str; 8] = [
"clear",
"dedup",
"pop",
"remove",
"retain",
"retain_mut",
"swap_remove",
"truncate",
];
// https://paritytech.github.io/ink/ink_storage/struct.Mapping.html
const MAP_INSERT_OPERATIONS: [&str; 1] = ["insert"];
const MAP_REMOVE_OPERATIONS: [&str; 2] = ["remove", "take"];
impl FieldInfo {
pub fn new(did: LocalDefId, ty: CollectionTy) -> Self {
Self {
did,
ty,
has_insert: false,
has_remove: false,
}
}
}
/// Returns `DefId` of a field if it has the `Vec` type
fn find_vec_did(cx: &LateContext, path: &Path) -> Option<DefId> {
if_chain! {
if let Res::Def(DefKind::Struct, def_id) = path.res;
if match_def_path(cx, def_id, &["alloc", "vec", "Vec"]);
then { Some(def_id) } else { None }
}
}
/// Returns `DefId` of a field if it has the `Mapping` type
fn find_map_did(cx: &LateContext, path: &Path) -> Option<DefId> {
if_chain! {
if let Res::Def(DefKind::Struct, def_id) = path.res;
if match_def_path(cx, def_id, &["ink_storage", "lazy", "mapping", "Mapping"]);
then { Some(def_id) } else { None }
}
}
/// Returns vectors of fields that have collection types
fn find_collection_fields(cx: &LateContext, storage_struct_id: ItemId) -> FieldsMap {
let mut result = FieldsMap::new();
let item = cx.tcx.hir().item(storage_struct_id);
if let ItemKind::Struct(var_data, _) = item.kind {
var_data.fields().iter().for_each(|field_def| {
if_chain! {
// Collection fields of the storage are expanded like this:
// vec_field: <Vec<
// AccountId,
// > as ::ink::storage::traits::AutoStorableHint<
// ::ink::storage::traits::ManualKey<993959520u32, ()>,
// >>::Type,
if let TyKind::Path(QPath::Resolved(Some(ty), path)) = field_def.ty.kind;
if match_path(path, &["ink", "storage", "traits", "AutoStorableHint", "Type"]);
if let TyKind::Path(QPath::Resolved(None, path)) = ty.kind;
then {
let field_name = field_def.ident.name.as_str();
// TODO: Inspect type aliases
if let Some(_did) = find_vec_did(cx, path) {
result.insert(field_name.to_string(), FieldInfo::new(field_def.def_id, CollectionTy::Vec));
return;
}
if let Some(_did) = find_map_did(cx, path) {
result.insert(field_name.to_string(), FieldInfo::new(field_def.def_id, CollectionTy::Map));
}
}
}
})
};
result
}
/// Reports the given field defintion
fn report_field(cx: &LateContext, field_info: &FieldInfo) {
if_chain! {
if let Node::Field(field) = cx.tcx.hir().get_by_def_id(field_info.did);
if !is_lint_allowed(cx, STORAGE_NEVER_FREED, field.hir_id);
then {
span_lint_and_then(
cx,
STORAGE_NEVER_FREED,
field.span,
"storage never freed",
|diag| {
let snippet = snippet_opt(cx, field.span).expect("snippet must exist");
diag.span_suggestion(
field.span,
"consider adding operations to remove elements available to the user".to_string(),
snippet,
Applicability::Unspecified,
);
},
)
}
}
}
/// Visitor that collects `insert` and `remove` operations
struct InsertRemoveCollector<'a, 'b, 'tcx> {
cx: &'tcx LateContext<'a>,
fields: &'b mut FieldsMap,
}
impl<'a, 'b, 'tcx> InsertRemoveCollector<'a, 'b, 'tcx> {
fn new(cx: &'tcx LateContext<'a>, fields: &'b mut FieldsMap) -> Self {
Self { cx, fields }
}
/// Finds a field of the supported type in the given expression present with the form
/// `self.field_name`
fn find_field_name(&self, e: &Expr) -> Option<String> {
if_chain! {
if let ExprKind::Field(s, field) = &e.kind;
if let ExprKind::Path(ref path) = s.kind;
let ty = self.cx.qpath_res(path, s.hir_id);
// TODO: check if ty is `self`
then { Some(field.name.as_str().to_string()) } else { None }
}
}
}
impl<'hir> Visitor<'hir> for InsertRemoveCollector<'_, '_, '_> {
fn visit_expr(&mut self, e: &'hir Expr<'hir>) {
match &e.kind {
ExprKind::Assign(lhs, ..) => {
if_chain! {
if let ExprKind::Index(field, _) = lhs.kind;
if let Some(field_name) = self.find_field_name(field);
then {
self.fields
.entry(field_name.to_string())
.and_modify(|field_info| {
field_info.has_insert = true;
});
}
}
}
ExprKind::MethodCall(method_path, receiver, args, _) => {
if let Some(field_name) = self.find_field_name(receiver) {
let method_name = method_path.ident.as_str();
self.fields
.entry(field_name.to_string())
.and_modify(|field_info| {
match field_info.ty {
CollectionTy::Vec
if VEC_INSERT_OPERATIONS.contains(&method_name) =>
{
field_info.has_insert = true;
}
CollectionTy::Vec
if VEC_REMOVE_OPERATIONS.contains(&method_name) =>
{
field_info.has_remove = true;
}
CollectionTy::Map
if MAP_INSERT_OPERATIONS.contains(&method_name) =>
{
field_info.has_insert = true;
}
CollectionTy::Map
if MAP_REMOVE_OPERATIONS.contains(&method_name) =>
{
field_info.has_remove = true;
}
_ => (),
}
});
}
args.iter().for_each(|arg| walk_expr(self, arg));
}
_ => (),
}
walk_expr(self, e);
}
}
impl<'tcx> LateLintPass<'tcx> for StorageNeverFreed {
fn check_mod(
&mut self,
cx: &LateContext<'tcx>,
m: &'tcx hir::Mod<'tcx>,
_: hir::HirId,
) {
if_chain! {
// Find fields of Vec/Mapping type
if let Some(storage_struct_id) = find_storage_struct(cx, m.item_ids);
let mut fields = find_collection_fields(cx, storage_struct_id);
if !fields.is_empty();
// Find all the user-defined functions of the contract
let all_item_ids = expand_unnamed_consts(cx, m.item_ids);
if let Some(contract_impl_id) = find_contract_impl_id(cx, all_item_ids);
let contract_impl = cx.tcx.hir().item(contract_impl_id);
if let ItemKind::Impl(contract_impl) = contract_impl.kind;
then {
contract_impl.items.iter().for_each(|impl_item| {
let impl_item = cx.tcx.hir().impl_item(impl_item.id);
if let ImplItemKind::Fn(_, fn_body_id) = impl_item.kind {
let mut visitor = InsertRemoveCollector::new(cx, &mut fields);
walk_body(&mut visitor, cx.tcx.hir().body(fn_body_id));
}
});
fields.iter().for_each(|(_, field)| {
if field.has_insert && !field.has_remove {
report_field(cx, field)
}
})
}
}
}
}