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(),
});
}
unsafe { ((**(*_vmctx).memories).base, (**(*_vmctx).memories).bound) }
unsafe {
(
(**(*_vmctx).internal.memories).base,
(**(*_vmctx).internal.memories).bound,
)
}
} else if _module.info.imported_memories.len() > 0 {
if _module.info.memories.len() != 0 || _module.info.imported_memories.len() != 1 {
return Err(RuntimeError::Trap {
@ -414,8 +419,8 @@ impl ProtectedCaller for X64ExecutionContext {
}
unsafe {
(
(**(*_vmctx).imported_memories).base,
(**(*_vmctx).imported_memories).bound,
(**(*_vmctx).internal.imported_memories).base,
(**(*_vmctx).internal.imported_memories).bound,
)
}
} else {
@ -1394,7 +1399,7 @@ impl X64FunctionCode {
}
dynasm!(
assembler
; mov r15, r14 => vm::Ctx.memories
; mov r15, r14 => vm::InternalCtx.memories
);
} else if info.imported_memories.len() > 0 {
if info.memories.len() != 0 || info.imported_memories.len() != 1 {
@ -1404,7 +1409,7 @@ impl X64FunctionCode {
}
dynasm!(
assembler
; mov r15, r14 => vm::Ctx.imported_memories
; mov r15, r14 => vm::InternalCtx.imported_memories
);
} else {
return Ok(());
@ -1477,7 +1482,7 @@ impl X64FunctionCode {
}
dynasm!(
assembler
; mov rcx, r9 => vm::Ctx.memories
; mov rcx, r9 => vm::InternalCtx.memories
);
true
} else if info.imported_memories.len() > 0 {
@ -1488,7 +1493,7 @@ impl X64FunctionCode {
}
dynasm!(
assembler
; mov rcx, r9 => vm::Ctx.imported_memories
; mov rcx, r9 => vm::InternalCtx.imported_memories
);
true
} else {
@ -2101,7 +2106,7 @@ impl FunctionCodeGenerator for X64FunctionCode {
if global_index < module_info.imported_globals.len() {
dynasm!(
assembler
; mov rax, r14 => vm::Ctx.imported_globals
; mov rax, r14 => vm::InternalCtx.imported_globals
);
} else {
global_index -= module_info.imported_globals.len();
@ -2112,7 +2117,7 @@ impl FunctionCodeGenerator for X64FunctionCode {
}
dynasm!(
assembler
; mov rax, r14 => vm::Ctx.globals
; mov rax, r14 => vm::InternalCtx.globals
);
}
@ -2139,7 +2144,7 @@ impl FunctionCodeGenerator for X64FunctionCode {
dynasm!(
assembler
; push rbx
; mov rbx, r14 => vm::Ctx.imported_globals
; mov rbx, r14 => vm::InternalCtx.imported_globals
);
} else {
global_index -= module_info.imported_globals.len();
@ -2151,7 +2156,7 @@ impl FunctionCodeGenerator for X64FunctionCode {
dynasm!(
assembler
; 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,
) -> u64 {
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)
}
@ -5155,15 +5160,18 @@ unsafe extern "C" fn call_indirect(
assert!(stack_top as usize <= stack_base as usize);
let table: &LocalTable = match local_or_import {
CallIndirectLocalOrImport::Local => &*(*(*vmctx).tables),
CallIndirectLocalOrImport::Import => &*(*(*vmctx).imported_tables),
CallIndirectLocalOrImport::Local => &*(*(*vmctx).internal.tables),
CallIndirectLocalOrImport::Import => &*(*(*vmctx).internal.imported_tables),
};
if elem_index >= table.count as usize {
eprintln!("element index out of bounds");
protect_unix::trigger_trap();
}
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() {
eprintln!("null anyfunc");

View File

@ -13,6 +13,25 @@ use std::{ffi::c_void, mem, ptr};
#[derive(Debug)]
#[repr(C)]
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`.
pub memories: *mut *mut LocalMemory,
@ -39,15 +58,6 @@ pub struct Ctx {
/// signature id. This is used to allow call-indirect to other
/// modules safely.
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 {
@ -58,6 +68,7 @@ impl Ctx {
module: &ModuleInner,
) -> Self {
Self {
internal: InternalCtx {
memories: local_backing.vm_memories.as_mut_ptr(),
tables: local_backing.vm_tables.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(),
dynamic_sigindices: local_backing.dynamic_sigindices.as_ptr(),
},
local_functions: local_backing.local_functions.as_ptr(),
local_backing,
@ -88,6 +100,7 @@ impl Ctx {
data_finalizer: extern "C" fn(*mut c_void),
) -> Self {
Self {
internal: InternalCtx {
memories: local_backing.vm_memories.as_mut_ptr(),
tables: local_backing.vm_tables.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(),
dynamic_sigindices: local_backing.dynamic_sigindices.as_ptr(),
},
local_functions: local_backing.local_functions.as_ptr(),
local_backing,

View File

@ -17,7 +17,7 @@ pub unsafe extern "C" fn local_static_memory_grow(
memory_index: LocalMemoryIndex,
delta: Pages,
) -> 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;
match (*memory).grow(delta, &mut *local_memory) {
@ -30,7 +30,7 @@ pub unsafe extern "C" fn local_static_memory_size(
ctx: &vm::Ctx,
memory_index: LocalMemoryIndex,
) -> 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;
(*memory).size()
@ -41,7 +41,7 @@ pub unsafe extern "C" fn local_dynamic_memory_grow(
memory_index: LocalMemoryIndex,
delta: Pages,
) -> 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;
match (*memory).grow(delta, &mut *local_memory) {
@ -54,7 +54,7 @@ pub unsafe extern "C" fn local_dynamic_memory_size(
ctx: &vm::Ctx,
memory_index: LocalMemoryIndex,
) -> 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;
(*memory).size()
@ -69,7 +69,10 @@ pub unsafe extern "C" fn imported_static_memory_grow(
import_memory_index: ImportedMemoryIndex,
delta: Pages,
) -> 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;
match (*memory).grow(delta, &mut *local_memory) {
@ -82,7 +85,10 @@ pub unsafe extern "C" fn imported_static_memory_size(
ctx: &vm::Ctx,
import_memory_index: ImportedMemoryIndex,
) -> 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;
(*memory).size()
@ -93,7 +99,7 @@ pub unsafe extern "C" fn imported_dynamic_memory_grow(
memory_index: ImportedMemoryIndex,
delta: Pages,
) -> 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;
match (*memory).grow(delta, &mut *local_memory) {
@ -106,7 +112,7 @@ pub unsafe extern "C" fn imported_dynamic_memory_size(
ctx: &vm::Ctx,
memory_index: ImportedMemoryIndex,
) -> 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;
(*memory).size()