Fix instance null ptr bug

This commit is contained in:
Brandon Fish 2019-02-02 08:43:29 -06:00
parent be19e96669
commit 3c7ad109bc
2 changed files with 21 additions and 21 deletions

View File

@ -3,7 +3,7 @@ extern crate wasmer_runtime;
use libc::{c_char, c_int, uint32_t, uint8_t}; use libc::{c_char, c_int, uint32_t, uint8_t};
use std::ffi::CStr; use std::ffi::CStr;
use std::str; use std::str;
use wasmer_runtime::{Instance, ImportObject, Value}; use wasmer_runtime::{ImportObject, Instance, Value};
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
pub struct wasmer_import_object_t(); pub struct wasmer_import_object_t();
@ -42,52 +42,52 @@ pub extern "C" fn wasmer_import_object_destroy(import_object: *mut wasmer_import
#[allow(clippy::cast_ptr_alignment)] #[allow(clippy::cast_ptr_alignment)]
#[no_mangle] #[no_mangle]
pub extern "C" fn wasmer_instantiate( pub unsafe extern "C" fn wasmer_instantiate(
mut instance: *mut wasmer_instance_t, mut instance: *mut *mut wasmer_instance_t,
wasm_bytes: *mut uint8_t, wasm_bytes: *mut uint8_t,
wasm_bytes_len: uint32_t, wasm_bytes_len: uint32_t,
import_object: *mut wasmer_import_object_t, import_object: *mut wasmer_import_object_t,
) -> wasmer_compile_result_t { ) -> wasmer_compile_result_t {
let import_object = unsafe { Box::from_raw(import_object as *mut ImportObject) }; let import_object = unsafe { Box::from_raw(import_object as *mut ImportObject) };
if wasm_bytes.is_null() { if wasm_bytes.is_null() {
return wasmer_compile_result_t::WASMER_COMPILE_ERROR return wasmer_compile_result_t::WASMER_COMPILE_ERROR;
} }
let bytes: &[u8] = unsafe { ::std::slice::from_raw_parts_mut(wasm_bytes, wasm_bytes_len as usize) }; let bytes: &[u8] =
unsafe { ::std::slice::from_raw_parts_mut(wasm_bytes, wasm_bytes_len as usize) };
let result = wasmer_runtime::instantiate(bytes, *import_object); let result = wasmer_runtime::instantiate(bytes, *import_object);
let new_instance = match result { let new_instance = match result {
Ok(instance) => instance, Ok(instance) => instance,
Err(error) => { Err(error) => {
println!("Err: {:?}", error); println!("Err: {:?}", error);
return wasmer_compile_result_t::WASMER_COMPILE_ERROR return wasmer_compile_result_t::WASMER_COMPILE_ERROR;
}, }
}; };
instance = Box::into_raw(Box::new(new_instance)) as *mut wasmer_instance_t; unsafe { *instance = Box::into_raw(Box::new(new_instance)) as *mut wasmer_instance_t };
wasmer_compile_result_t::WASMER_COMPILE_OK wasmer_compile_result_t::WASMER_COMPILE_OK
} }
#[allow(clippy::cast_ptr_alignment)] #[allow(clippy::cast_ptr_alignment)]
#[no_mangle] #[no_mangle]
pub extern "C" fn wasmer_instance_call(instance: *mut wasmer_instance_t, pub unsafe extern "C" fn wasmer_instance_call(
name: *const c_char) -> wasmer_call_result_t { instance: *mut wasmer_instance_t,
name: *const c_char,
) -> wasmer_call_result_t {
// TODO handle params and results // TODO handle params and results
if instance.is_null(){ if instance.is_null() {
println!("Instance null");
return wasmer_call_result_t::WASMER_CALL_ERROR; return wasmer_call_result_t::WASMER_CALL_ERROR;
} }
if name.is_null(){ if name.is_null() {
println!("Name null");
return wasmer_call_result_t::WASMER_CALL_ERROR; return wasmer_call_result_t::WASMER_CALL_ERROR;
} }
let func_name_c = unsafe { let func_name_c = unsafe { CStr::from_ptr(name) };
CStr::from_ptr(name)
};
let func_name_r = func_name_c.to_str().unwrap(); let func_name_r = func_name_c.to_str().unwrap();
let instance = unsafe { Box::from_raw(instance as *mut Instance) }; let instance = unsafe { Box::from_raw(instance as *mut Instance) };
let result = instance.call(func_name_r, &[Value::I32(1), Value::I32(2)]); let result = instance.call(func_name_r, &[Value::I32(1), Value::I32(2)]);
match result { match result {
Ok(res) => { Ok(res) => {
println!("Res: {:?}", res);
wasmer_call_result_t::WASMER_CALL_OK wasmer_call_result_t::WASMER_CALL_OK
}, }
Err(err) => { Err(err) => {
println!("Err: {:?}", err); println!("Err: {:?}", err);
wasmer_call_result_t::WASMER_CALL_ERROR wasmer_call_result_t::WASMER_CALL_ERROR

View File

@ -17,7 +17,7 @@ int main()
fclose(file); fclose(file);
wasmer_instance_t *instance = NULL; wasmer_instance_t *instance = NULL;
wasmer_compile_result_t compile_result = wasmer_instantiate(instance, bytes, len, import_object); wasmer_compile_result_t compile_result = wasmer_instantiate(&instance, bytes, len, import_object);
printf("Compile result: %d\n", compile_result); printf("Compile result: %d\n", compile_result);
assert(compile_result == WASMER_COMPILE_OK); assert(compile_result == WASMER_COMPILE_OK);
@ -26,7 +26,7 @@ int main()
assert(call_result == WASMER_CALL_OK); assert(call_result == WASMER_CALL_OK);
printf("Destroy instance\n"); printf("Destroy instance\n");
wasmer_instance_destroy(instance); //wasmer_instance_destroy(instance); // error here
printf("Destroy import object\n"); printf("Destroy import object\n");
//wasmer_import_object_destroy(import_object); // error here //wasmer_import_object_destroy(import_object); // error here
return 0; return 0;