Add the internals field and necessary structures for metering.

This commit is contained in:
losfair 2019-05-17 01:10:21 +08:00
parent 4e5ac24517
commit 6aa87a0bbf
6 changed files with 46 additions and 4 deletions

View File

@ -472,6 +472,7 @@ impl FunctionCodeGenerator<CodegenError> for LLVMFunctionCodeGenerator {
fn feed_event(&mut self, event: Event, module_info: &ModuleInfo) -> Result<(), CodegenError> {
let op = match event {
Event::Wasm(x) => x,
Event::WasmOwned(ref x) => x,
Event::Internal(_x) => {
return Ok(());
}

View File

@ -163,6 +163,7 @@ impl Intrinsics {
let stack_lower_bound_ty = i8_ty;
let memory_base_ty = i8_ty;
let memory_bound_ty = void_ty;
let internals_ty = void_ty;
let local_function_ty = i8_ptr_ty;
let anyfunc_ty = context.struct_type(
@ -218,6 +219,9 @@ impl Intrinsics {
memory_bound_ty
.ptr_type(AddressSpace::Generic)
.as_basic_type_enum(),
internals_ty
.ptr_type(AddressSpace::Generic)
.as_basic_type_enum(),
local_function_ty
.ptr_type(AddressSpace::Generic)
.as_basic_type_enum(),

View File

@ -15,7 +15,20 @@ use crate::{
},
vm,
};
use std::slice;
use std::{
slice,
fmt::Debug,
};
pub const INTERNALS_SIZE: usize = 256;
pub(crate) struct Internals(pub(crate) [u64; INTERNALS_SIZE]);
impl Debug for Internals {
fn fmt(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(formatter, "Internals({:?})", &self.0[..])
}
}
/// The `LocalBacking` "owns" the memory used by all the local resources of an Instance.
/// That is, local memories, tables, and globals (as well as some additional
@ -40,6 +53,8 @@ pub struct LocalBacking {
/// as well) are subject to change.
pub(crate) dynamic_sigindices: BoxedMap<SigIndex, vm::SigId>,
pub(crate) local_functions: BoxedMap<LocalFuncIndex, *const vm::Func>,
pub(crate) internals: Internals,
}
impl LocalBacking {
@ -66,6 +81,8 @@ impl LocalBacking {
dynamic_sigindices,
local_functions,
internals: Internals([0; 256]),
}
}

View File

@ -17,6 +17,7 @@ use wasmparser::{Operator, Type as WpType};
pub enum Event<'a, 'b> {
Internal(InternalEvent),
Wasm(&'b Operator<'a>),
WasmOwned(Operator<'a>),
}
pub enum InternalEvent {
@ -39,7 +40,9 @@ impl fmt::Debug for InternalEvent {
}
}
pub struct BkptInfo {}
pub struct BkptInfo {
pub throw: unsafe extern "C" fn () -> !,
}
pub trait ModuleCodeGenerator<FCG: FunctionCodeGenerator<E>, RM: RunnableModule, E: Debug> {
/// Creates a new module code generator.

View File

@ -49,6 +49,8 @@ pub use self::module::Module;
pub use self::typed_func::Func;
use std::sync::Arc;
pub use wasmparser;
use self::cache::{Artifact, Error as CacheError};
pub mod prelude {

View File

@ -1,4 +1,4 @@
pub use crate::backing::{ImportBacking, LocalBacking};
pub use crate::backing::{ImportBacking, LocalBacking, INTERNALS_SIZE};
use crate::{
memory::{Memory, MemoryType},
module::{ModuleInfo, ModuleInner},
@ -92,6 +92,8 @@ pub struct InternalCtx {
pub memory_base: *mut u8,
pub memory_bound: usize,
pub internals: *mut [u64; INTERNALS_SIZE], // TODO: Make this dynamic?
}
#[repr(C)]
@ -200,6 +202,8 @@ impl Ctx {
memory_base: mem_base,
memory_bound: mem_bound,
internals: &mut local_backing.internals.0,
},
local_functions: local_backing.local_functions.as_ptr(),
@ -249,6 +253,8 @@ impl Ctx {
memory_base: mem_base,
memory_bound: mem_bound,
internals: &mut local_backing.internals.0,
},
local_functions: local_backing.local_functions.as_ptr(),
@ -356,9 +362,13 @@ impl Ctx {
11 * (mem::size_of::<usize>() as u8)
}
pub fn offset_local_functions() -> u8 {
pub fn offset_internals() -> u8 {
12 * (mem::size_of::<usize>() as u8)
}
pub fn offset_local_functions() -> u8 {
13 * (mem::size_of::<usize>() as u8)
}
}
enum InnerFunc {}
@ -572,6 +582,11 @@ mod vm_offset_tests {
offset_of!(InternalCtx => memory_bound).get_byte_offset(),
);
assert_eq!(
Ctx::offset_internals() as usize,
offset_of!(InternalCtx => internals).get_byte_offset(),
);
assert_eq!(
Ctx::offset_local_functions() as usize,
offset_of!(Ctx => local_functions).get_byte_offset(),