From 6aa87a0bbf2e6f4e02f75e5d96e0d46345c2973d Mon Sep 17 00:00:00 2001 From: losfair Date: Fri, 17 May 2019 01:10:21 +0800 Subject: [PATCH] Add the `internals` field and necessary structures for metering. --- lib/llvm-backend/src/code.rs | 1 + lib/llvm-backend/src/intrinsics.rs | 4 ++++ lib/runtime-core/src/backing.rs | 19 ++++++++++++++++++- lib/runtime-core/src/codegen.rs | 5 ++++- lib/runtime-core/src/lib.rs | 2 ++ lib/runtime-core/src/vm.rs | 19 +++++++++++++++++-- 6 files changed, 46 insertions(+), 4 deletions(-) diff --git a/lib/llvm-backend/src/code.rs b/lib/llvm-backend/src/code.rs index 8514208ad..aecf6fd74 100644 --- a/lib/llvm-backend/src/code.rs +++ b/lib/llvm-backend/src/code.rs @@ -472,6 +472,7 @@ impl FunctionCodeGenerator 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(()); } diff --git a/lib/llvm-backend/src/intrinsics.rs b/lib/llvm-backend/src/intrinsics.rs index ebee5a388..2198821ed 100644 --- a/lib/llvm-backend/src/intrinsics.rs +++ b/lib/llvm-backend/src/intrinsics.rs @@ -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(), diff --git a/lib/runtime-core/src/backing.rs b/lib/runtime-core/src/backing.rs index 821ff379f..52aec386e 100644 --- a/lib/runtime-core/src/backing.rs +++ b/lib/runtime-core/src/backing.rs @@ -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, pub(crate) local_functions: BoxedMap, + + pub(crate) internals: Internals, } impl LocalBacking { @@ -66,6 +81,8 @@ impl LocalBacking { dynamic_sigindices, local_functions, + + internals: Internals([0; 256]), } } diff --git a/lib/runtime-core/src/codegen.rs b/lib/runtime-core/src/codegen.rs index 1d9f30f1a..9ae20d8c9 100644 --- a/lib/runtime-core/src/codegen.rs +++ b/lib/runtime-core/src/codegen.rs @@ -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, RM: RunnableModule, E: Debug> { /// Creates a new module code generator. diff --git a/lib/runtime-core/src/lib.rs b/lib/runtime-core/src/lib.rs index 20708c17a..871e5875b 100644 --- a/lib/runtime-core/src/lib.rs +++ b/lib/runtime-core/src/lib.rs @@ -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 { diff --git a/lib/runtime-core/src/vm.rs b/lib/runtime-core/src/vm.rs index 323e40c21..b772cc9b4 100644 --- a/lib/runtime-core/src/vm.rs +++ b/lib/runtime-core/src/vm.rs @@ -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::() as u8) } - pub fn offset_local_functions() -> u8 { + pub fn offset_internals() -> u8 { 12 * (mem::size_of::() as u8) } + + pub fn offset_local_functions() -> u8 { + 13 * (mem::size_of::() 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(),