mirror of
https://github.com/fluencelabs/wasmer
synced 2024-12-12 22:05:33 +00:00
Table now working properly
This commit is contained in:
parent
037f76e3b1
commit
2a118930c2
@ -1,21 +1,21 @@
|
||||
use core::ops::{Index, IndexMut};
|
||||
use core::ptr::NonNull;
|
||||
use std::ops::{Index, IndexMut};
|
||||
use std::ptr::NonNull;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
#[repr(transparent)]
|
||||
pub struct UncheckedSlice<T> {
|
||||
ptr: NonNull<T>,
|
||||
pub ptr: NonNull<T>,
|
||||
}
|
||||
|
||||
impl<T> UncheckedSlice<T> {
|
||||
#[inline]
|
||||
unsafe fn get_unchecked(&self, index: usize) -> &T {
|
||||
pub unsafe fn get_unchecked(&self, index: usize) -> &T {
|
||||
let ptr = self.ptr.as_ptr();
|
||||
&*ptr.add(index)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T {
|
||||
pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T {
|
||||
let ptr = self.ptr.as_ptr();
|
||||
&mut *(ptr.add(index) as *mut _)
|
||||
}
|
||||
|
@ -1,5 +1,32 @@
|
||||
use crate::webassembly::ImportObject;
|
||||
use libc::putchar;
|
||||
use crate::webassembly::{ImportObject, VmCtx};
|
||||
// use libc::putchar;
|
||||
|
||||
extern fn putchar(a: *const u8, context: *const u8) {
|
||||
println!("PUT CHAAAR original pointer {:?}", context);
|
||||
let vmctx: &VmCtx = unsafe { &*(context as *const VmCtx) };
|
||||
println!("PUT CHAAAR {}", vmctx.test);
|
||||
println!("PUT CHAAAR pointer {:p}", vmctx);
|
||||
let x = vmctx as *const _;
|
||||
let x_tables = vmctx.tables.as_ptr();
|
||||
let tables_ptr_1 = (&vmctx.tables) as *const _;
|
||||
let tables_ptr_2 = unsafe { (&vmctx.tables.get_unchecked(0)) as *const _ };
|
||||
let tables_ptr_3 = &vmctx.tables as *const _ ;
|
||||
let tables_ptr_4 = &vmctx.tables as *const _ ;
|
||||
// let tables: &Vec<Vec<usize>> = unsafe { &*(tables_ptr_4 as *const Vec<Vec<usize>>) };
|
||||
let x_tables_serial: &Vec<*const usize> = unsafe { &*(tables_ptr_1 as *const Vec<*const usize>) };
|
||||
// let tables: &Vec<> = vmctx.tables as &Vec<Vec<usize>>;
|
||||
println!("PUT CHAAAR pointer {:?}", x);
|
||||
println!("PUT CHAAAR pointer 1 {:p}", &vmctx.tables);
|
||||
println!("PUT CHAAAR pointer 2 {:p}", tables_ptr_1);
|
||||
println!("PUT CHAAAR pointer 3 (0) {:p}", tables_ptr_2);
|
||||
println!("PUT CHAAAR pointer 3 (0) {:p}", tables_ptr_3);
|
||||
// println!("PUT CHAAAR pointer 4 (0) {:p} {:?}", tables_ptr_4, tables);
|
||||
// println!("PUT CHAAAR pointer 4 (0) {:p} {:?}", &vmctx.tables, vmctx.tables);
|
||||
// println!("PUT CHAAAR pointer 4 (0) {:p} {:?}", &vmctx.tables[0], vmctx.tables[0]);
|
||||
println!("PUT CHAAAR pointer {:?} {:?}", x_tables, x_tables_serial);
|
||||
let x_tables = vmctx.tables.as_ptr();
|
||||
println!("PUT CHAAAR pointer {:?}", x_tables);
|
||||
}
|
||||
|
||||
pub fn generate_libc_env<'a, 'b>() -> ImportObject<&'a str, &'b str> {
|
||||
let mut import_object = ImportObject::new();
|
||||
@ -12,6 +39,7 @@ mod tests {
|
||||
use super::generate_libc_env;
|
||||
use crate::webassembly::{
|
||||
instantiate, ErrorKind, Export, ImportObject, Instance, Module, ResultObject,
|
||||
VmCtx
|
||||
};
|
||||
use libc::putchar;
|
||||
|
||||
@ -21,12 +49,21 @@ mod tests {
|
||||
let import_object = generate_libc_env();
|
||||
let result_object = instantiate(wasm_bytes, import_object).expect("Not compiled properly");
|
||||
let module = result_object.module;
|
||||
let instance = result_object.instance;
|
||||
let mut instance = result_object.instance;
|
||||
let func_index = match module.info.exports.get("main") {
|
||||
Some(&Export::Function(index)) => index,
|
||||
_ => panic!("Function not found"),
|
||||
};
|
||||
let main: fn() = get_instance_function!(instance, func_index);
|
||||
main();
|
||||
let main: fn(&VmCtx) = get_instance_function!(instance, func_index);
|
||||
let mainn_func_index = match module.info.exports.get("mainn") {
|
||||
Some(&Export::Function(index)) => index,
|
||||
_ => panic!("Function not found"),
|
||||
};
|
||||
let mainn: fn(&VmCtx) = get_instance_function!(instance, mainn_func_index);
|
||||
let context = instance.generate_context();
|
||||
main(&context);
|
||||
println!("---------MAINNN NOW---------");
|
||||
// let context = instance.generate_context();
|
||||
mainn(&context);
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,34 @@
|
||||
(module
|
||||
(type $FUNCSIG$iii (func (param i32 i32) (result i32)))
|
||||
(type $FUNCSIG$ii (func (param i32) (result i32)))
|
||||
(import "env" "putchar" (func $putchar (param i32) (result i32)))
|
||||
(table 0 anyfunc)
|
||||
(elem (i32.const 0) $multiply)
|
||||
(table 1 1 anyfunc)
|
||||
(memory $0 1)
|
||||
(export "memory" (memory $0))
|
||||
(export "main" (func $main))
|
||||
(func $main (; 1 ;) (result i32)
|
||||
(export "mainn" (func $mainn))
|
||||
(func $dispatch (; 0 ;) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
|
||||
(call_indirect (type $FUNCSIG$iii)
|
||||
(get_local $1)
|
||||
(get_local $2)
|
||||
(get_local $0)
|
||||
)
|
||||
)
|
||||
(func $multiply (; 1 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32)
|
||||
(i32.mul
|
||||
(get_local $1)
|
||||
(get_local $0)
|
||||
)
|
||||
)
|
||||
(func $mainn (; 2 ;) (result i32)
|
||||
(call $dispatch
|
||||
(i32.const 0)
|
||||
(i32.const 20)
|
||||
(i32.const 30)
|
||||
)
|
||||
)
|
||||
(func $main (; 3 ;) (result i32)
|
||||
(drop
|
||||
(call $putchar
|
||||
(i32.const 97)
|
||||
|
28
src/main.rs
28
src/main.rs
@ -81,8 +81,34 @@ fn execute_wasm(wasm_path: PathBuf) -> Result<(), String> {
|
||||
_ => panic!("Main function not found"),
|
||||
});
|
||||
let main: fn(&webassembly::VmCtx) = get_instance_function!(instance, func_index);
|
||||
let context = instance.generate_context();
|
||||
let mainn_func_index = match module.info.exports.get("mainn") {
|
||||
Some(&webassembly::Export::Function(index)) => index,
|
||||
_ => panic!("Mainn function not found"),
|
||||
};
|
||||
let mainn: fn(*const u8) -> i32 = get_instance_function!(instance, mainn_func_index);
|
||||
let context = &instance.generate_context();
|
||||
// println!("Context ptr {:p}", context);
|
||||
// println!("Context ptr {:?}", &context as *const _);
|
||||
// println!("Context ptr {:?}", &context as *const _);
|
||||
// println!("Memories ptr {:?}", context.memories.as_ptr());
|
||||
// println!("Tables ptr {:?}", context.tables.as_ptr());
|
||||
// println!("Tables ptr {:?}", context.tables.as_ptr());
|
||||
// println!("Tables ptr {:?}", &context.tables as *const _);
|
||||
// println!("Tables ptr {:?}", &context.tables as *const _);
|
||||
// println!("User data ptr {:?}", &context.user_data as *const _);
|
||||
// println!("Globals ptr {:?}", &context.globals as *const _);
|
||||
// println!("Memories ptr {:?}", &context.memories as *const _);
|
||||
// println!("Tables ptr {:?}", &context.tables as *const _);
|
||||
// unsafe {
|
||||
// println!("Tables 0 ptr {:p}", &context.tables.get_unchecked(0));
|
||||
// println!("Tables 0 ptr {:p}", &context.tables.get_unchecked(0).get(0));
|
||||
// }
|
||||
let table_ptr = &context.tables as *const _;
|
||||
// let table: &Ta
|
||||
main(&context);
|
||||
println!("-------------NOW MAINN----------");
|
||||
let res = mainn(context.tables.as_ptr() as *const u8);
|
||||
println!("RESULT {:?}", res);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -73,12 +73,13 @@ fn get_function_addr(
|
||||
#[repr(C)]
|
||||
pub struct VmCtx<'phantom> {
|
||||
pub user_data: UserData,
|
||||
globals: UncheckedSlice<u8>,
|
||||
memories: UncheckedSlice<UncheckedSlice<u8>>,
|
||||
tables: UncheckedSlice<BoundedSlice<usize>>,
|
||||
pub globals: UncheckedSlice<u8>,
|
||||
pub memories: UncheckedSlice<UncheckedSlice<u8>>,
|
||||
pub tables: UncheckedSlice<BoundedSlice<usize>>,
|
||||
pub test: String,
|
||||
// globals: Vec<u8>,
|
||||
// memories: Vec<Vec<u8>>,
|
||||
// pub tables: UncheckedSlice<BoundedSlice<usize>>,
|
||||
// pub tables: Vec<Vec<usize>>,
|
||||
phantom: PhantomData<&'phantom ()>,
|
||||
}
|
||||
|
||||
@ -510,6 +511,8 @@ impl Instance {
|
||||
|
||||
let tables: Vec<BoundedSlice<usize>> = self.tables.iter().map(|table| table[..].into()).collect();
|
||||
|
||||
println!("GENERATING CONTEXT {:?}", self.tables);
|
||||
|
||||
let globals: UncheckedSlice<u8> = self.globals[..].into();
|
||||
|
||||
// assert!(memories.len() >= 1, "modules must have at least one memory");
|
||||
@ -524,6 +527,7 @@ impl Instance {
|
||||
// process,
|
||||
instance: instance,
|
||||
},
|
||||
test: "TEST".to_string(),
|
||||
phantom: PhantomData,
|
||||
};
|
||||
data
|
||||
@ -569,20 +573,18 @@ impl Clone for Instance {
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn grow_memory(size: u32, memory_index: u32, vmctx: &VmCtx) -> i32 {
|
||||
extern "C" fn grow_memory(size: u32, memory_index: u32, vmctx: &mut VmCtx) -> i32 {
|
||||
|
||||
return 0;
|
||||
// unimplemented!();
|
||||
// let instance = &vmctx
|
||||
// .data()
|
||||
// .user_data
|
||||
// .instance;
|
||||
// let instance = &vmctx.user_data.instance;
|
||||
|
||||
// let mut memory = &mut instance.memories[memory_index as usize];
|
||||
// let mut memory = instance.memories[memory_index as usize];
|
||||
|
||||
// if let Some(old_size) = memory.grow(size) {
|
||||
// old_size as i32
|
||||
// } else {
|
||||
-1
|
||||
// -1
|
||||
// }
|
||||
|
||||
// unsafe {
|
||||
|
@ -409,72 +409,73 @@ impl<'environment> FuncEnvironmentTrait for FuncEnvironment<'environment> {
|
||||
fn make_table(&mut self, func: &mut ir::Function, table_index: TableIndex) -> ir::Table {
|
||||
// OLD
|
||||
// Create a table whose base address is stored at `vmctx+0`.
|
||||
// let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
|
||||
// let base_gv = func.create_global_value(ir::GlobalValueData::Load {
|
||||
// base: vmctx,
|
||||
// offset: Offset32::new(0),
|
||||
// global_type: self.pointer_type(),
|
||||
// });
|
||||
// let bound_gv = func.create_global_value(ir::GlobalValueData::Load {
|
||||
// base: vmctx,
|
||||
// offset: Offset32::new(0),
|
||||
// global_type: I32,
|
||||
// });
|
||||
|
||||
// func.create_table(ir::TableData {
|
||||
// base_gv,
|
||||
// min_size: Imm64::new(0),
|
||||
// bound_gv,
|
||||
// element_size: Imm64::new(i64::from(self.pointer_bytes()) * 2),
|
||||
// index_type: I32,
|
||||
// })
|
||||
|
||||
let ptr_size = self.ptr_size();
|
||||
|
||||
let base = self.mod_info.tables_base.unwrap_or_else(|| {
|
||||
let tables_offset = self.ptr_size() as i32 * -1;
|
||||
let new_base = func.create_global_value(ir::GlobalValueData::VMContext {});
|
||||
// {
|
||||
// offset: tables_offset.into(),
|
||||
// });
|
||||
// self.mod_info.globals_base = Some(new_base);
|
||||
new_base
|
||||
let vmctx = func.create_global_value(ir::GlobalValueData::VMContext);
|
||||
let base_gv = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: Offset32::new(0),
|
||||
global_type: self.pointer_type(),
|
||||
});
|
||||
|
||||
let table_data_offset = (table_index as usize * ptr_size * 2) as i32;
|
||||
|
||||
let new_table_addr_addr = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base,
|
||||
offset: table_data_offset.into(),
|
||||
global_type: self.pointer_type(), // Might be I32
|
||||
});
|
||||
let new_table_addr = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: new_table_addr_addr,
|
||||
offset: 0.into(),
|
||||
global_type: self.pointer_type(), // Might be I32
|
||||
});
|
||||
|
||||
let new_table_bounds_addr = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base,
|
||||
offset: (table_data_offset + ptr_size as i32).into(),
|
||||
global_type: self.pointer_type(), // Might be I32
|
||||
});
|
||||
let new_table_bounds = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: new_table_bounds_addr,
|
||||
offset: 0.into(),
|
||||
global_type: I32, // Might be self.pointer_type()
|
||||
let bound_gv = func.create_global_value(ir::GlobalValueData::Load {
|
||||
base: vmctx,
|
||||
offset: Offset32::new(0),
|
||||
global_type: I64,
|
||||
});
|
||||
|
||||
let table = func.create_table(ir::TableData {
|
||||
base_gv: new_table_addr,
|
||||
base_gv: base_gv,
|
||||
min_size: Imm64::new(0),
|
||||
// min_size: (self.mod_info.tables[table_index].size as i64).into(),
|
||||
bound_gv: new_table_bounds,
|
||||
element_size: (ptr_size as i64).into(),
|
||||
index_type: I32,
|
||||
bound_gv,
|
||||
element_size: Imm64::new(i64::from(self.pointer_bytes()) * 2),
|
||||
index_type: I64,
|
||||
});
|
||||
|
||||
println!("FUNC {:?}", func);
|
||||
table
|
||||
// let ptr_size = self.ptr_size();
|
||||
|
||||
// let base = self.mod_info.tables_base.unwrap_or_else(|| {
|
||||
// let tables_offset = self.ptr_size() as i32 * -1;
|
||||
// let new_base = func.create_global_value(ir::GlobalValueData::VMContext {});
|
||||
// // {
|
||||
// // offset: tables_offset.into(),
|
||||
// // });
|
||||
// // self.mod_info.globals_base = Some(new_base);
|
||||
// new_base
|
||||
// });
|
||||
|
||||
// let table_data_offset = (table_index as usize * ptr_size * 2) as i32;
|
||||
|
||||
// let new_table_addr_addr = func.create_global_value(ir::GlobalValueData::Load {
|
||||
// base,
|
||||
// offset: table_data_offset.into(),
|
||||
// global_type: self.pointer_type(), // Might be I32
|
||||
// });
|
||||
// let new_table_addr = func.create_global_value(ir::GlobalValueData::Load {
|
||||
// base: new_table_addr_addr,
|
||||
// offset: 0.into(),
|
||||
// global_type: self.pointer_type(), // Might be I32
|
||||
// });
|
||||
|
||||
// let new_table_bounds_addr = func.create_global_value(ir::GlobalValueData::Load {
|
||||
// base,
|
||||
// offset: (table_data_offset + ptr_size as i32).into(),
|
||||
// global_type: self.pointer_type(), // Might be I32
|
||||
// });
|
||||
// let new_table_bounds = func.create_global_value(ir::GlobalValueData::Load {
|
||||
// base: new_table_bounds_addr,
|
||||
// offset: 0.into(),
|
||||
// global_type: I32, // Might be self.pointer_type()
|
||||
// });
|
||||
|
||||
// let table = func.create_table(ir::TableData {
|
||||
// base_gv: new_table_addr,
|
||||
// min_size: Imm64::new(0),
|
||||
// // min_size: (self.mod_info.tables[table_index].size as i64).into(),
|
||||
// bound_gv: new_table_bounds,
|
||||
// element_size: (ptr_size as i64).into(),
|
||||
// index_type: I32,
|
||||
// });
|
||||
|
||||
// table
|
||||
}
|
||||
|
||||
fn make_indirect_sig(&mut self, func: &mut ir::Function, index: SignatureIndex) -> ir::SigRef {
|
||||
@ -501,7 +502,7 @@ impl<'environment> FuncEnvironmentTrait for FuncEnvironment<'environment> {
|
||||
&mut self,
|
||||
mut pos: FuncCursor,
|
||||
_table_index: TableIndex,
|
||||
_table: ir::Table,
|
||||
table: ir::Table,
|
||||
_sig_index: SignatureIndex,
|
||||
sig_ref: ir::SigRef,
|
||||
callee: ir::Value,
|
||||
@ -518,15 +519,23 @@ impl<'environment> FuncEnvironmentTrait for FuncEnvironment<'environment> {
|
||||
// TODO: Generate bounds checking code.
|
||||
let ptr = self.pointer_type();
|
||||
let callee_offset = if ptr == I32 {
|
||||
pos.ins().imul_imm(callee, 4)
|
||||
// pos.ins().imul_imm(callee, 4)
|
||||
callee
|
||||
} else {
|
||||
let ext = pos.ins().uextend(I64, callee);
|
||||
pos.ins().imul_imm(ext, 4)
|
||||
ext
|
||||
// pos.ins().imul_imm(ext, 4)
|
||||
};
|
||||
let entry_addr = pos.ins().table_addr(
|
||||
self.pointer_type(),
|
||||
table,
|
||||
callee_offset,
|
||||
0,
|
||||
);
|
||||
let mut mflags = ir::MemFlags::new();
|
||||
mflags.set_notrap();
|
||||
mflags.set_aligned();
|
||||
let func_ptr = pos.ins().load(ptr, mflags, callee_offset, 0);
|
||||
let func_ptr = pos.ins().load(ptr, mflags, entry_addr, 0);
|
||||
|
||||
// Build a value list for the indirect call instruction containing the callee, call_args,
|
||||
// and the vmctx parameter.
|
||||
@ -535,10 +544,14 @@ impl<'environment> FuncEnvironmentTrait for FuncEnvironment<'environment> {
|
||||
args.extend(call_args.iter().cloned(), &mut pos.func.dfg.value_lists);
|
||||
args.push(vmctx, &mut pos.func.dfg.value_lists);
|
||||
|
||||
Ok(pos
|
||||
let inst = pos
|
||||
.ins()
|
||||
.CallIndirect(ir::Opcode::CallIndirect, INVALID, sig_ref, args)
|
||||
.0)
|
||||
.0;
|
||||
|
||||
println!("FUNC {:?}", pos.func);
|
||||
|
||||
Ok(inst)
|
||||
}
|
||||
|
||||
fn translate_call(
|
||||
|
Loading…
Reference in New Issue
Block a user