Do not spend time on verifying the signatures before calling Runtime (#8980)

This commit is contained in:
Sergei Shulepov
2021-06-01 16:43:29 +02:00
committed by GitHub
parent 84e402389b
commit bf229f0ba4
@@ -36,75 +36,71 @@ pub enum EntryPointType {
/// Direct call. /// Direct call.
/// ///
/// Call is made by providing only payload reference and length. /// Call is made by providing only payload reference and length.
Direct, Direct {
entrypoint: wasmtime::TypedFunc<(u32, u32), u64>,
},
/// Indirect call. /// Indirect call.
/// ///
/// Call is made by providing payload reference and length, and extra argument /// Call is made by providing payload reference and length, and extra argument
/// for advanced routing (typically extra WASM function pointer). /// for advanced routing.
Wrapped(u32), Wrapped {
/// The extra argument passed to the runtime. It is typically a wasm function pointer.
func: u32,
dispatcher: wasmtime::TypedFunc<(u32, u32, u32), u64>,
},
} }
/// Wasm blob entry point. /// Wasm blob entry point.
pub struct EntryPoint { pub struct EntryPoint {
call_type: EntryPointType, call_type: EntryPointType,
func: wasmtime::Func,
} }
impl EntryPoint { impl EntryPoint {
/// Call this entry point. /// Call this entry point.
pub fn call(&self, data_ptr: Pointer<u8>, data_len: WordSize) -> Result<u64> { pub fn call(&self, data_ptr: Pointer<u8>, data_len: WordSize) -> Result<u64> {
let data_ptr = u32::from(data_ptr) as i32; let data_ptr = u32::from(data_ptr);
let data_len = u32::from(data_len) as i32; let data_len = u32::from(data_len);
(match self.call_type { fn handle_trap(err: wasmtime::Trap) -> Error {
EntryPointType::Direct => { Error::from(format!("Wasm execution trapped: {}", err))
self.func.call(&[ }
wasmtime::Val::I32(data_ptr),
wasmtime::Val::I32(data_len), match self.call_type {
]) EntryPointType::Direct { ref entrypoint } => {
}, entrypoint.call((data_ptr, data_len)).map_err(handle_trap)
EntryPointType::Wrapped(func) => { }
self.func.call(&[ EntryPointType::Wrapped {
wasmtime::Val::I32(func as _), func,
wasmtime::Val::I32(data_ptr), ref dispatcher,
wasmtime::Val::I32(data_len), } => {
]) dispatcher
}, .call((func, data_ptr, data_len))
}) .map_err(handle_trap)
.map(|results| }
// the signature is checked to have i64 return type }
results[0].unwrap_i64() as u64
)
.map_err(|err| Error::from(format!(
"Wasm execution trapped: {}",
err
)))
} }
pub fn direct(func: wasmtime::Func) -> std::result::Result<Self, &'static str> { pub fn direct(func: wasmtime::Func) -> std::result::Result<Self, &'static str> {
use wasmtime::ValType; let entrypoint = func
let entry_point = wasmtime::FuncType::new( .typed::<(u32, u32), u64>()
[ValType::I32, ValType::I32].iter().cloned(), .map_err(|_| "Invalid signature for direct entry point")?
[ValType::I64].iter().cloned(), .clone();
); Ok(Self {
if func.ty() == entry_point { call_type: EntryPointType::Direct { entrypoint },
Ok(Self { func, call_type: EntryPointType::Direct }) })
} else {
Err("Invalid signature for direct entry point")
}
} }
pub fn wrapped(dispatcher: wasmtime::Func, func: u32) -> std::result::Result<Self, &'static str> { pub fn wrapped(
use wasmtime::ValType; dispatcher: wasmtime::Func,
let entry_point = wasmtime::FuncType::new( func: u32,
[ValType::I32, ValType::I32, ValType::I32].iter().cloned(), ) -> std::result::Result<Self, &'static str> {
[ValType::I64].iter().cloned(), let dispatcher = dispatcher
); .typed::<(u32, u32, u32), u64>()
if dispatcher.ty() == entry_point { .map_err(|_| "Invalid signature for wrapped entry point")?
Ok(Self { func: dispatcher, call_type: EntryPointType::Wrapped(func) }) .clone();
} else { Ok(Self {
Err("Invalid signature for wrapped entry point") call_type: EntryPointType::Wrapped { func, dispatcher },
} })
} }
} }