forked from wasmerio/wasmer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metering.rs
310 lines (280 loc) · 10.3 KB
/
metering.rs
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
use wasmer_runtime_core::{
codegen::{Event, EventSink, FunctionMiddleware, InternalEvent::*},
module::ModuleInfo,
vm::{Ctx, InternalField},
wasmparser::{Operator, Type as WpType, TypeOrFuncType as WpTypeOrFuncType},
Instance,
};
static INTERNAL_FIELD_USED: InternalField = InternalField::allocate();
static INTERNAL_FIELD_LIMIT: InternalField = InternalField::allocate();
/// Metering is a compiler middleware that calculates the cost of WebAssembly instructions at
/// compile time and will count the cost of executed instructions at runtime. Within the Metering
/// functionality, this instruction cost is called `points`.
///
/// Each instance has an `exec_limit` which is the maximum number of points which can be used by
/// the instance during a function call. If this limit is exceeded, the function call will trap.
/// Each instance has a `points_used` field which can be used to track points used during a
/// function call and should be set back to zero after a function call.
///
/// Each compiler backend with Metering enabled should produce the same cost used at runtime for
/// the same function calls so we can say that the metering is deterministic.
pub struct Metering {
injections: Vec<Injection>,
current_block_injections: Vec<Injection>,
current_block_cost: u64,
}
impl Metering {
pub fn new() -> Metering {
Metering {
injections: Vec::new(),
current_block_injections: Vec::new(),
current_block_cost: 0,
}
}
fn set_costs<'a, 'b: 'a>(&mut self) {
for inj in &mut self.current_block_injections {
inj.check += self.current_block_cost;
}
// Set add of the last injection
if self.current_block_injections.len() > 0 {
let last_idx = self.current_block_injections.len() - 1;
self.current_block_injections[last_idx] = Injection {
add: self.current_block_cost,
check: 0,
};
}
self.current_block_cost = 0;
}
fn begin<'a, 'b: 'a>(&mut self) {
self.set_costs();
self.current_block_injections
.push(Injection { add: 0, check: 0 });
}
fn end<'a, 'b: 'a>(&mut self) {
self.set_costs();
self.injections.append(&mut self.current_block_injections);
}
fn inject_metering<'a, 'b: 'a>(&self, sink: &mut EventSink<'a, 'b>) {
let prev: Vec<Event> = sink.buffer.drain(..).collect();
let mut inj_idx: usize = 1;
for ev in prev {
match ev {
Event::Internal(FunctionBegin(_)) => {
sink.push(ev);
self.injections[0].inject(sink);
}
Event::Wasm(&ref op) | Event::WasmOwned(ref op) => match *op {
Operator::End
| Operator::If { .. }
| Operator::Else
| Operator::BrIf { .. }
| Operator::Loop { .. }
| Operator::Call { .. }
| Operator::CallIndirect { .. } => {
sink.push(ev);
self.injections[inj_idx].inject(sink);
inj_idx += 1;
}
_ => {
sink.push(ev);
}
},
_ => {
sink.push(ev);
}
}
}
}
/// increment_cost adds 1 to the current_block_cost.
///
/// Later this may be replaced with a cost map for assigning custom unique cost values to
/// specific Operators.
fn increment_cost<'a, 'b: 'a>(&mut self, ev: &Event<'a, 'b>) {
match ev {
Event::Internal(ref iev) => match iev {
FunctionBegin(_) | FunctionEnd | Breakpoint(_) => {
return;
}
_ => {}
},
Event::Wasm(&ref op) | Event::WasmOwned(ref op) => match *op {
Operator::Unreachable | Operator::End | Operator::Else => {
return;
}
_ => {}
},
}
self.current_block_cost += 1;
}
}
#[derive(Copy, Clone, Debug)]
pub struct ExecutionLimitExceededError;
impl FunctionMiddleware for Metering {
type Error = String;
fn feed_event<'a, 'b: 'a>(
&mut self,
ev: Event<'a, 'b>,
_module_info: &ModuleInfo,
sink: &mut EventSink<'a, 'b>,
) -> Result<(), Self::Error> {
// This involves making two passes over an entire function. The first pass counts the costs
// of each code segment. The final pass occurs when Event is FunctionEnd and we actually
// drain the EventSink and repopulate it with metering code injected.
match ev {
Event::Internal(ref iev) => match iev {
FunctionBegin(_) => {
self.injections.clear();
self.current_block_injections.clear();
self.current_block_cost = 0;
sink.push(ev);
self.begin();
return Ok(());
}
FunctionEnd => {
self.end();
self.inject_metering(sink);
sink.push(ev);
return Ok(());
}
_ => {
self.increment_cost(&ev);
sink.push(ev);
return Ok(());
}
},
Event::Wasm(&ref op) | Event::WasmOwned(ref op) => {
self.increment_cost(&ev);
match *op {
Operator::End
| Operator::If { .. }
| Operator::Else
| Operator::Br { .. }
| Operator::BrIf { .. }
| Operator::BrTable { .. }
| Operator::Unreachable
| Operator::Return => {
self.end();
}
_ => {}
}
match *op {
Operator::Loop { .. }
| Operator::End
| Operator::If { .. }
| Operator::Else
| Operator::BrIf { .. }
| Operator::Call { .. }
| Operator::CallIndirect { .. } => {
self.begin();
}
_ => {}
}
}
}
sink.push(ev);
Ok(())
}
}
/// Returns the number of points used by an Instance.
pub fn get_points_used(instance: &Instance) -> u64 {
instance.get_internal(&INTERNAL_FIELD_USED)
}
/// Sets the number of points used by an Instance.
pub fn set_points_used(instance: &mut Instance, value: u64) {
instance.set_internal(&INTERNAL_FIELD_USED, value);
}
/// Returns the number of points used in a Ctx.
pub fn get_points_used_ctx(ctx: &Ctx) -> u64 {
ctx.get_internal(&INTERNAL_FIELD_USED)
}
/// Sets the number of points used in a Ctx.
pub fn set_points_used_ctx(ctx: &mut Ctx, value: u64) {
ctx.set_internal(&INTERNAL_FIELD_USED, value);
}
pub fn set_execution_limit(instance: &mut Instance, limit: u64) {
instance.set_internal(&INTERNAL_FIELD_LIMIT, limit);
}
pub fn set_execution_limit_ctx(ctx: &mut Ctx, limit: u64) {
ctx.set_internal(&INTERNAL_FIELD_LIMIT, limit);
}
pub fn get_execution_limit(instance: &Instance) -> u64 {
instance.get_internal(&INTERNAL_FIELD_LIMIT)
}
pub fn get_execution_limit_ctx(ctx: &Ctx) -> u64 {
ctx.get_internal(&INTERNAL_FIELD_LIMIT)
}
/// Injection is a struct that stores the cost of the subsequent code segment. It injects metering
/// code into the EventSink.
///
/// Code segments may be nested such that multiple segments may begin at different places but all
/// end at the same branching instruction. Thus entering into one code segment guarantees that you
/// will proceed to the nested ones, until the first branching operator is reached. In these cases,
/// the limit check can be done such that we ensure enough gas to complete the entire code segment,
/// including nested parts. However it is important that we only add the cost up to the next
/// metering injection.
///
/// For example, consider the following
///
/// - INJECT METERING CODE (check to if, add cost to next INJECT)
/// | block
/// | ... (non-branching ops)
/// | loop
/// | - INJECT METERING CODE (check to if, add to next INJECT)
/// | | ... (non-branching ops)
/// | | loop
/// | | - INJECT METERING CODE
/// | | | ... (non-branching ops)
/// |____|___|___ if (first branching op)
#[derive(Debug)]
struct Injection {
check: u64,
add: u64,
}
impl Injection {
fn inject<'a, 'b: 'a>(&self, sink: &mut EventSink<'a, 'b>) {
if self.add == 0 {
return;
}
// PUSH USED
sink.push(Event::Internal(GetInternal(
INTERNAL_FIELD_USED.index() as _
)));
// PUSH COST (to next Injection)
sink.push(Event::WasmOwned(Operator::I64Const {
value: self.add as i64,
}));
// USED + COST
sink.push(Event::WasmOwned(Operator::I64Add));
// SAVE USED
sink.push(Event::Internal(SetInternal(
INTERNAL_FIELD_USED.index() as _
)));
// PUSH USED
sink.push(Event::Internal(GetInternal(
INTERNAL_FIELD_USED.index() as _
)));
if self.check > 0 {
// PUSH COST (to next branching op)
sink.push(Event::WasmOwned(Operator::I64Const {
value: self.check as i64,
}));
// USED + COST
sink.push(Event::WasmOwned(Operator::I64Add));
}
// PUSH LIMIT
sink.push(Event::Internal(GetInternal(
INTERNAL_FIELD_LIMIT.index() as _
)));
// IF USED > LIMIT
sink.push(Event::WasmOwned(Operator::I64GtU));
sink.push(Event::WasmOwned(Operator::If {
ty: WpTypeOrFuncType::Type(WpType::EmptyBlockType),
}));
// TRAP! EXECUTION LIMIT EXCEEDED
sink.push(Event::Internal(Breakpoint(Box::new(|_| {
Err(Box::new(ExecutionLimitExceededError))
}))));
// ENDIF
sink.push(Event::WasmOwned(Operator::End));
}
}