2019-05-16 17:10:45 +00:00
|
|
|
use wasmer_runtime_core::{
|
|
|
|
codegen::{Event, EventSink, FunctionMiddleware, InternalEvent},
|
|
|
|
module::ModuleInfo,
|
2019-05-23 12:10:17 +00:00
|
|
|
vm::InternalField,
|
2019-05-23 12:10:34 +00:00
|
|
|
wasmparser::{Operator, Type as WpType},
|
2019-05-16 17:10:45 +00:00
|
|
|
};
|
|
|
|
|
2019-05-23 12:10:17 +00:00
|
|
|
static INTERNAL_FIELD: InternalField = InternalField::allocate();
|
|
|
|
|
2019-05-16 17:10:45 +00:00
|
|
|
pub struct Metering {
|
|
|
|
limit: u64,
|
|
|
|
current_block: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Metering {
|
|
|
|
pub fn new(limit: u64) -> Metering {
|
|
|
|
Metering {
|
|
|
|
limit,
|
|
|
|
current_block: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FunctionMiddleware for Metering {
|
|
|
|
type Error = String;
|
|
|
|
fn feed_event<'a, 'b: 'a>(
|
|
|
|
&mut self,
|
|
|
|
op: Event<'a, 'b>,
|
|
|
|
_module_info: &ModuleInfo,
|
|
|
|
sink: &mut EventSink<'a, 'b>,
|
|
|
|
) -> Result<(), Self::Error> {
|
|
|
|
match op {
|
|
|
|
Event::Internal(InternalEvent::FunctionBegin(_)) => {
|
|
|
|
self.current_block = 0;
|
2019-05-16 17:15:05 +00:00
|
|
|
}
|
2019-05-16 17:10:45 +00:00
|
|
|
Event::Wasm(&ref op) | Event::WasmOwned(ref op) => {
|
|
|
|
self.current_block += 1;
|
|
|
|
match *op {
|
|
|
|
Operator::Loop { .. }
|
2019-05-16 17:15:05 +00:00
|
|
|
| Operator::Block { .. }
|
|
|
|
| Operator::End
|
|
|
|
| Operator::If { .. }
|
|
|
|
| Operator::Else
|
|
|
|
| Operator::Unreachable
|
|
|
|
| Operator::Br { .. }
|
|
|
|
| Operator::BrTable { .. }
|
|
|
|
| Operator::BrIf { .. }
|
|
|
|
| Operator::Call { .. }
|
2019-05-16 17:20:09 +00:00
|
|
|
| Operator::CallIndirect { .. }
|
|
|
|
| Operator::Return => {
|
2019-05-23 12:10:34 +00:00
|
|
|
sink.push(Event::Internal(InternalEvent::GetInternal(
|
|
|
|
INTERNAL_FIELD.index() as _,
|
|
|
|
)));
|
2019-05-16 17:15:05 +00:00
|
|
|
sink.push(Event::WasmOwned(Operator::I64Const {
|
|
|
|
value: self.current_block as i64,
|
|
|
|
}));
|
|
|
|
sink.push(Event::WasmOwned(Operator::I64Add));
|
2019-05-23 12:10:34 +00:00
|
|
|
sink.push(Event::Internal(InternalEvent::SetInternal(
|
|
|
|
INTERNAL_FIELD.index() as _,
|
|
|
|
)));
|
2019-05-16 17:15:05 +00:00
|
|
|
self.current_block = 0;
|
|
|
|
}
|
2019-05-16 17:10:45 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
match *op {
|
2019-05-16 17:15:05 +00:00
|
|
|
Operator::Br { .. }
|
|
|
|
| Operator::BrTable { .. }
|
|
|
|
| Operator::BrIf { .. }
|
|
|
|
| Operator::Call { .. }
|
|
|
|
| Operator::CallIndirect { .. } => {
|
2019-05-23 12:10:34 +00:00
|
|
|
sink.push(Event::Internal(InternalEvent::GetInternal(
|
|
|
|
INTERNAL_FIELD.index() as _,
|
|
|
|
)));
|
2019-05-16 17:15:05 +00:00
|
|
|
sink.push(Event::WasmOwned(Operator::I64Const {
|
|
|
|
value: self.limit as i64,
|
|
|
|
}));
|
|
|
|
sink.push(Event::WasmOwned(Operator::I64GeU));
|
|
|
|
sink.push(Event::WasmOwned(Operator::If {
|
|
|
|
ty: WpType::EmptyBlockType,
|
|
|
|
}));
|
|
|
|
sink.push(Event::Internal(InternalEvent::Breakpoint(Box::new(
|
|
|
|
move |ctx| {
|
2019-05-16 17:10:45 +00:00
|
|
|
eprintln!("execution limit reached");
|
|
|
|
unsafe {
|
|
|
|
(ctx.throw)();
|
|
|
|
}
|
2019-05-16 17:15:05 +00:00
|
|
|
},
|
|
|
|
))));
|
|
|
|
sink.push(Event::WasmOwned(Operator::End));
|
|
|
|
}
|
2019-05-16 17:10:45 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
2019-05-16 17:15:05 +00:00
|
|
|
}
|
2019-05-16 17:10:45 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
sink.push(op);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|