Remove unsafe setjmp / longjmp implementation

This commit is contained in:
Syrus
2019-04-09 19:01:51 -07:00
parent 02ed9f0e5f
commit 1ee5e7cde6

View File

@ -1,40 +1,44 @@
use super::process::abort_with_message;
use super::env::get_emscripten_data; use super::env::get_emscripten_data;
use libc::{c_int, c_void}; use libc::{c_int};
use std::cell::UnsafeCell; // use std::cell::UnsafeCell;
use wasmer_runtime_core::vm::Ctx; use wasmer_runtime_core::vm::Ctx;
/// setjmp /// setjmp
pub fn __setjmp(ctx: &mut Ctx, env_addr: u32) -> c_int { pub fn __setjmp(ctx: &mut Ctx, _env_addr: u32) -> c_int {
debug!("emscripten::__setjmp (setjmp)"); debug!("emscripten::__setjmp (setjmp)");
unsafe { abort_with_message(ctx, "missing function: _longjmp");
// Rather than using the env as the holder of the jump buffer pointer, unreachable!()
// we use the environment address to store the index relative to jumps // unsafe {
// so the address of the jump it's outside the wasm memory itself. // // Rather than using the env as the holder of the jump buffer pointer,
let jump_index = emscripten_memory_pointer!(ctx.memory(0), env_addr) as *mut i8; // // we use the environment address to store the index relative to jumps
// We create the jump buffer outside of the wasm memory // // so the address of the jump it's outside the wasm memory itself.
let jump_buf: UnsafeCell<[u32; 27]> = UnsafeCell::new([0; 27]); // let jump_index = emscripten_memory_pointer!(ctx.memory(0), env_addr) as *mut i8;
let jumps = &mut get_emscripten_data(ctx).jumps; // // We create the jump buffer outside of the wasm memory
let result = setjmp(jump_buf.get() as _); // let jump_buf: UnsafeCell<[u32; 27]> = UnsafeCell::new([0; 27]);
// We set the jump index to be the last 3value of jumps // let jumps = &mut get_emscripten_data(ctx).jumps;
*jump_index = jumps.len() as _; // let result = setjmp(jump_buf.get() as _);
// We hold the reference of the jump buffer // // We set the jump index to be the last 3value of jumps
jumps.push(jump_buf); // *jump_index = jumps.len() as _;
result // // We hold the reference of the jump buffer
} // jumps.push(jump_buf);
// result
// }
} }
/// longjmp /// longjmp
#[allow(unreachable_code)] #[allow(unreachable_code)]
pub fn __longjmp(ctx: &mut Ctx, env_addr: u32, val: c_int) { pub fn __longjmp(ctx: &mut Ctx, _env_addr: u32, _val: c_int) {
debug!("emscripten::__longjmp (longmp)"); debug!("emscripten::__longjmp (longmp)");
unsafe { abort_with_message(ctx, "missing function: _longjmp");
// We retrieve the jump index from the env address // unsafe {
let jump_index = emscripten_memory_pointer!(ctx.memory(0), env_addr) as *mut i8; // // We retrieve the jump index from the env address
let jumps = &mut get_emscripten_data(ctx).jumps; // let jump_index = emscripten_memory_pointer!(ctx.memory(0), env_addr) as *mut i8;
// We get the real jump buffer from the jumps vector, using the retrieved index // let jumps = &mut get_emscripten_data(ctx).jumps;
let jump_buf = &jumps[*jump_index as usize]; // // We get the real jump buffer from the jumps vector, using the retrieved index
longjmp(jump_buf.get() as _, val) // let jump_buf = &jumps[*jump_index as usize];
}; // longjmp(jump_buf.get() as _, val)
// };
} }
/// _longjmp /// _longjmp
@ -46,10 +50,10 @@ pub fn _longjmp(ctx: &mut Ctx, env_addr: i32, val: c_int) {
.expect("set_threw is None") .expect("set_threw is None")
.call(env_addr, val) .call(env_addr, val)
.expect("set_threw failed to call"); .expect("set_threw failed to call");
panic!("longjmp"); panic!("longjmp")
} }
extern "C" { // extern "C" {
fn setjmp(env: *mut c_void) -> c_int; // fn setjmp(env: *mut c_void) -> c_int;
fn longjmp(env: *mut c_void, val: c_int) -> !; // fn longjmp(env: *mut c_void, val: c_int) -> !;
} // }