Add InternalCtx.

This commit is contained in:
losfair 2019-03-21 08:39:06 +08:00
parent 395161acdc
commit cb3846ff6a
3 changed files with 76 additions and 48 deletions

View File

@ -405,7 +405,12 @@ impl ProtectedCaller for X64ExecutionContext {
msg: "only one linear memory is supported".into(), msg: "only one linear memory is supported".into(),
}); });
} }
unsafe { ((**(*_vmctx).memories).base, (**(*_vmctx).memories).bound) } unsafe {
(
(**(*_vmctx).internal.memories).base,
(**(*_vmctx).internal.memories).bound,
)
}
} else if _module.info.imported_memories.len() > 0 { } else if _module.info.imported_memories.len() > 0 {
if _module.info.memories.len() != 0 || _module.info.imported_memories.len() != 1 { if _module.info.memories.len() != 0 || _module.info.imported_memories.len() != 1 {
return Err(RuntimeError::Trap { return Err(RuntimeError::Trap {
@ -414,8 +419,8 @@ impl ProtectedCaller for X64ExecutionContext {
} }
unsafe { unsafe {
( (
(**(*_vmctx).imported_memories).base, (**(*_vmctx).internal.imported_memories).base,
(**(*_vmctx).imported_memories).bound, (**(*_vmctx).internal.imported_memories).bound,
) )
} }
} else { } else {
@ -1394,7 +1399,7 @@ impl X64FunctionCode {
} }
dynasm!( dynasm!(
assembler assembler
; mov r15, r14 => vm::Ctx.memories ; mov r15, r14 => vm::InternalCtx.memories
); );
} else if info.imported_memories.len() > 0 { } else if info.imported_memories.len() > 0 {
if info.memories.len() != 0 || info.imported_memories.len() != 1 { if info.memories.len() != 0 || info.imported_memories.len() != 1 {
@ -1404,7 +1409,7 @@ impl X64FunctionCode {
} }
dynasm!( dynasm!(
assembler assembler
; mov r15, r14 => vm::Ctx.imported_memories ; mov r15, r14 => vm::InternalCtx.imported_memories
); );
} else { } else {
return Ok(()); return Ok(());
@ -1477,7 +1482,7 @@ impl X64FunctionCode {
} }
dynasm!( dynasm!(
assembler assembler
; mov rcx, r9 => vm::Ctx.memories ; mov rcx, r9 => vm::InternalCtx.memories
); );
true true
} else if info.imported_memories.len() > 0 { } else if info.imported_memories.len() > 0 {
@ -1488,7 +1493,7 @@ impl X64FunctionCode {
} }
dynasm!( dynasm!(
assembler assembler
; mov rcx, r9 => vm::Ctx.imported_memories ; mov rcx, r9 => vm::InternalCtx.imported_memories
); );
true true
} else { } else {
@ -2101,7 +2106,7 @@ impl FunctionCodeGenerator for X64FunctionCode {
if global_index < module_info.imported_globals.len() { if global_index < module_info.imported_globals.len() {
dynasm!( dynasm!(
assembler assembler
; mov rax, r14 => vm::Ctx.imported_globals ; mov rax, r14 => vm::InternalCtx.imported_globals
); );
} else { } else {
global_index -= module_info.imported_globals.len(); global_index -= module_info.imported_globals.len();
@ -2112,7 +2117,7 @@ impl FunctionCodeGenerator for X64FunctionCode {
} }
dynasm!( dynasm!(
assembler assembler
; mov rax, r14 => vm::Ctx.globals ; mov rax, r14 => vm::InternalCtx.globals
); );
} }
@ -2139,7 +2144,7 @@ impl FunctionCodeGenerator for X64FunctionCode {
dynasm!( dynasm!(
assembler assembler
; push rbx ; push rbx
; mov rbx, r14 => vm::Ctx.imported_globals ; mov rbx, r14 => vm::InternalCtx.imported_globals
); );
} else { } else {
global_index -= module_info.imported_globals.len(); global_index -= module_info.imported_globals.len();
@ -2151,7 +2156,7 @@ impl FunctionCodeGenerator for X64FunctionCode {
dynasm!( dynasm!(
assembler assembler
; push rbx ; push rbx
; mov rbx, r14 => vm::Ctx.globals ; mov rbx, r14 => vm::InternalCtx.globals
); );
} }
@ -5129,7 +5134,7 @@ unsafe extern "C" fn invoke_import(
_memory_base: *mut u8, _memory_base: *mut u8,
) -> u64 { ) -> u64 {
let vmctx: &mut vm::Ctx = &mut *vmctx; let vmctx: &mut vm::Ctx = &mut *vmctx;
let import = (*vmctx.imported_funcs.offset(import_id as isize)).func; let import = (*vmctx.internal.imported_funcs.offset(import_id as isize)).func;
CONSTRUCT_STACK_AND_CALL_NATIVE(stack_top, stack_base, vmctx, import) CONSTRUCT_STACK_AND_CALL_NATIVE(stack_top, stack_base, vmctx, import)
} }
@ -5155,15 +5160,18 @@ unsafe extern "C" fn call_indirect(
assert!(stack_top as usize <= stack_base as usize); assert!(stack_top as usize <= stack_base as usize);
let table: &LocalTable = match local_or_import { let table: &LocalTable = match local_or_import {
CallIndirectLocalOrImport::Local => &*(*(*vmctx).tables), CallIndirectLocalOrImport::Local => &*(*(*vmctx).internal.tables),
CallIndirectLocalOrImport::Import => &*(*(*vmctx).imported_tables), CallIndirectLocalOrImport::Import => &*(*(*vmctx).internal.imported_tables),
}; };
if elem_index >= table.count as usize { if elem_index >= table.count as usize {
eprintln!("element index out of bounds"); eprintln!("element index out of bounds");
protect_unix::trigger_trap(); protect_unix::trigger_trap();
} }
let anyfunc = &*(table.base as *mut vm::Anyfunc).offset(elem_index as isize); let anyfunc = &*(table.base as *mut vm::Anyfunc).offset(elem_index as isize);
let dynamic_sigindex = *(*vmctx).dynamic_sigindices.offset(sig_index as isize); let dynamic_sigindex = *(*vmctx)
.internal
.dynamic_sigindices
.offset(sig_index as isize);
if anyfunc.func.is_null() { if anyfunc.func.is_null() {
eprintln!("null anyfunc"); eprintln!("null anyfunc");

View File

@ -13,6 +13,25 @@ use std::{ffi::c_void, mem, ptr};
#[derive(Debug)] #[derive(Debug)]
#[repr(C)] #[repr(C)]
pub struct Ctx { pub struct Ctx {
// `internal` must be the first field of `Ctx`.
pub internal: InternalCtx,
pub(crate) local_functions: *const *const Func,
local_backing: *mut LocalBacking,
import_backing: *mut ImportBacking,
module: *const ModuleInner,
pub data: *mut c_void,
pub data_finalizer: Option<extern "C" fn(data: *mut c_void)>,
}
/// The internal context of the currently running WebAssembly instance.
///
///
#[derive(Debug)]
#[repr(C)]
pub struct InternalCtx {
/// A pointer to an array of locally-defined memories, indexed by `MemoryIndex`. /// A pointer to an array of locally-defined memories, indexed by `MemoryIndex`.
pub memories: *mut *mut LocalMemory, pub memories: *mut *mut LocalMemory,
@ -39,15 +58,6 @@ pub struct Ctx {
/// signature id. This is used to allow call-indirect to other /// signature id. This is used to allow call-indirect to other
/// modules safely. /// modules safely.
pub dynamic_sigindices: *const SigId, pub dynamic_sigindices: *const SigId,
pub(crate) local_functions: *const *const Func,
local_backing: *mut LocalBacking,
import_backing: *mut ImportBacking,
module: *const ModuleInner,
pub data: *mut c_void,
pub data_finalizer: Option<extern "C" fn(data: *mut c_void)>,
} }
impl Ctx { impl Ctx {
@ -58,6 +68,7 @@ impl Ctx {
module: &ModuleInner, module: &ModuleInner,
) -> Self { ) -> Self {
Self { Self {
internal: InternalCtx {
memories: local_backing.vm_memories.as_mut_ptr(), memories: local_backing.vm_memories.as_mut_ptr(),
tables: local_backing.vm_tables.as_mut_ptr(), tables: local_backing.vm_tables.as_mut_ptr(),
globals: local_backing.vm_globals.as_mut_ptr(), globals: local_backing.vm_globals.as_mut_ptr(),
@ -68,6 +79,7 @@ impl Ctx {
imported_funcs: import_backing.vm_functions.as_mut_ptr(), imported_funcs: import_backing.vm_functions.as_mut_ptr(),
dynamic_sigindices: local_backing.dynamic_sigindices.as_ptr(), dynamic_sigindices: local_backing.dynamic_sigindices.as_ptr(),
},
local_functions: local_backing.local_functions.as_ptr(), local_functions: local_backing.local_functions.as_ptr(),
local_backing, local_backing,
@ -88,6 +100,7 @@ impl Ctx {
data_finalizer: extern "C" fn(*mut c_void), data_finalizer: extern "C" fn(*mut c_void),
) -> Self { ) -> Self {
Self { Self {
internal: InternalCtx {
memories: local_backing.vm_memories.as_mut_ptr(), memories: local_backing.vm_memories.as_mut_ptr(),
tables: local_backing.vm_tables.as_mut_ptr(), tables: local_backing.vm_tables.as_mut_ptr(),
globals: local_backing.vm_globals.as_mut_ptr(), globals: local_backing.vm_globals.as_mut_ptr(),
@ -98,6 +111,7 @@ impl Ctx {
imported_funcs: import_backing.vm_functions.as_mut_ptr(), imported_funcs: import_backing.vm_functions.as_mut_ptr(),
dynamic_sigindices: local_backing.dynamic_sigindices.as_ptr(), dynamic_sigindices: local_backing.dynamic_sigindices.as_ptr(),
},
local_functions: local_backing.local_functions.as_ptr(), local_functions: local_backing.local_functions.as_ptr(),
local_backing, local_backing,

View File

@ -17,7 +17,7 @@ pub unsafe extern "C" fn local_static_memory_grow(
memory_index: LocalMemoryIndex, memory_index: LocalMemoryIndex,
delta: Pages, delta: Pages,
) -> i32 { ) -> i32 {
let local_memory = *ctx.memories.add(memory_index.index()); let local_memory = *ctx.internal.memories.add(memory_index.index());
let memory = (*local_memory).memory as *mut StaticMemory; let memory = (*local_memory).memory as *mut StaticMemory;
match (*memory).grow(delta, &mut *local_memory) { match (*memory).grow(delta, &mut *local_memory) {
@ -30,7 +30,7 @@ pub unsafe extern "C" fn local_static_memory_size(
ctx: &vm::Ctx, ctx: &vm::Ctx,
memory_index: LocalMemoryIndex, memory_index: LocalMemoryIndex,
) -> Pages { ) -> Pages {
let local_memory = *ctx.memories.add(memory_index.index()); let local_memory = *ctx.internal.memories.add(memory_index.index());
let memory = (*local_memory).memory as *mut StaticMemory; let memory = (*local_memory).memory as *mut StaticMemory;
(*memory).size() (*memory).size()
@ -41,7 +41,7 @@ pub unsafe extern "C" fn local_dynamic_memory_grow(
memory_index: LocalMemoryIndex, memory_index: LocalMemoryIndex,
delta: Pages, delta: Pages,
) -> i32 { ) -> i32 {
let local_memory = *ctx.memories.add(memory_index.index()); let local_memory = *ctx.internal.memories.add(memory_index.index());
let memory = (*local_memory).memory as *mut DynamicMemory; let memory = (*local_memory).memory as *mut DynamicMemory;
match (*memory).grow(delta, &mut *local_memory) { match (*memory).grow(delta, &mut *local_memory) {
@ -54,7 +54,7 @@ pub unsafe extern "C" fn local_dynamic_memory_size(
ctx: &vm::Ctx, ctx: &vm::Ctx,
memory_index: LocalMemoryIndex, memory_index: LocalMemoryIndex,
) -> Pages { ) -> Pages {
let local_memory = *ctx.memories.add(memory_index.index()); let local_memory = *ctx.internal.memories.add(memory_index.index());
let memory = (*local_memory).memory as *mut DynamicMemory; let memory = (*local_memory).memory as *mut DynamicMemory;
(*memory).size() (*memory).size()
@ -69,7 +69,10 @@ pub unsafe extern "C" fn imported_static_memory_grow(
import_memory_index: ImportedMemoryIndex, import_memory_index: ImportedMemoryIndex,
delta: Pages, delta: Pages,
) -> i32 { ) -> i32 {
let local_memory = *ctx.imported_memories.add(import_memory_index.index()); let local_memory = *ctx
.internal
.imported_memories
.add(import_memory_index.index());
let memory = (*local_memory).memory as *mut StaticMemory; let memory = (*local_memory).memory as *mut StaticMemory;
match (*memory).grow(delta, &mut *local_memory) { match (*memory).grow(delta, &mut *local_memory) {
@ -82,7 +85,10 @@ pub unsafe extern "C" fn imported_static_memory_size(
ctx: &vm::Ctx, ctx: &vm::Ctx,
import_memory_index: ImportedMemoryIndex, import_memory_index: ImportedMemoryIndex,
) -> Pages { ) -> Pages {
let local_memory = *ctx.imported_memories.add(import_memory_index.index()); let local_memory = *ctx
.internal
.imported_memories
.add(import_memory_index.index());
let memory = (*local_memory).memory as *mut StaticMemory; let memory = (*local_memory).memory as *mut StaticMemory;
(*memory).size() (*memory).size()
@ -93,7 +99,7 @@ pub unsafe extern "C" fn imported_dynamic_memory_grow(
memory_index: ImportedMemoryIndex, memory_index: ImportedMemoryIndex,
delta: Pages, delta: Pages,
) -> i32 { ) -> i32 {
let local_memory = *ctx.imported_memories.add(memory_index.index()); let local_memory = *ctx.internal.imported_memories.add(memory_index.index());
let memory = (*local_memory).memory as *mut DynamicMemory; let memory = (*local_memory).memory as *mut DynamicMemory;
match (*memory).grow(delta, &mut *local_memory) { match (*memory).grow(delta, &mut *local_memory) {
@ -106,7 +112,7 @@ pub unsafe extern "C" fn imported_dynamic_memory_size(
ctx: &vm::Ctx, ctx: &vm::Ctx,
memory_index: ImportedMemoryIndex, memory_index: ImportedMemoryIndex,
) -> Pages { ) -> Pages {
let local_memory = *ctx.imported_memories.add(memory_index.index()); let local_memory = *ctx.internal.imported_memories.add(memory_index.index());
let memory = (*local_memory).memory as *mut DynamicMemory; let memory = (*local_memory).memory as *mut DynamicMemory;
(*memory).size() (*memory).size()