From ef205fb60f61071e30599de7e8b5119dc8f325b9 Mon Sep 17 00:00:00 2001 From: Steve Akinyemi Date: Tue, 18 Dec 2018 13:44:15 +0100 Subject: [PATCH] Fix segfault --- src/apis/emscripten/env.rs | 34 +++++++++++--------------------- src/apis/emscripten/exception.rs | 3 ++- src/apis/host/posix/env.rs | 8 ++++---- 3 files changed, 18 insertions(+), 27 deletions(-) diff --git a/src/apis/emscripten/env.rs b/src/apis/emscripten/env.rs index 7826b3020..5acd64612 100644 --- a/src/apis/emscripten/env.rs +++ b/src/apis/emscripten/env.rs @@ -1,6 +1,6 @@ use super::super::host; /// NOTE: These syscalls only support wasm_32 for now because they take u32 offset -use libc::{c_int, getgrnam as libc_getgrnam, getpwnam as libc_getpwnam, c_long, sysconf}; +use libc::{c_int, getgrnam as libc_getgrnam, getpwnam as libc_getpwnam, c_long, sysconf, getenv}; use std::ffi::CStr; use std::mem; use std::os::raw::c_char; @@ -8,29 +8,19 @@ use std::os::raw::c_char; use super::utils::{copy_cstr_into_wasm, copy_terminated_array_of_cstrs}; use crate::webassembly::Instance; -#[no_mangle] +// #[no_mangle] /// emscripten: _getenv // (name: *const char) -> *const c_char; pub extern "C" fn _getenv(name: c_int, instance: &mut Instance) -> u32 { - debug!("emscripten::_getenv {}", name); - let name = unsafe { - let name_addr = instance.memory_offset_addr(0, name as usize) as *const c_char; - CStr::from_ptr(name_addr).to_str().unwrap() - }; - match host::get_env(name, instance) { - Ok(env_value) => { - // Append null byte - let env_value = env_value + "\0"; - let env_value_ptr = env_value.as_ptr() as *mut c_char; - let res = unsafe { copy_cstr_into_wasm(instance, env_value_ptr) }; - // Test - let c_str = instance.memory_offset_addr(0, res as _) as *mut i8; - use std::ffi::CStr; - debug!("#### cstr = {:?}", unsafe { CStr::from_ptr(c_str) }); - debug!("res = {}", res); - res - } - Err(_) => 0, - } + debug!("emscripten::_getenv"); + + let name_addr = instance.memory_offset_addr(0, name as usize) as *const c_char; + + debug!("=> name({:?})", unsafe { CStr::from_ptr(name_addr) }); + + let c_str = unsafe { getenv(name_addr) }; + if c_str.is_null() { return 0; } + + unsafe { copy_cstr_into_wasm(instance, c_str) } } pub extern "C" fn _getpwnam(name_ptr: c_int, instance: &mut Instance) -> c_int { diff --git a/src/apis/emscripten/exception.rs b/src/apis/emscripten/exception.rs index dc45454d5..65d6e2e6b 100644 --- a/src/apis/emscripten/exception.rs +++ b/src/apis/emscripten/exception.rs @@ -1,4 +1,5 @@ use crate::webassembly::Instance; +use super::process::_abort; /// emscripten: ___cxa_allocate_exception pub extern "C" fn ___cxa_allocate_exception(size: u32, instance: &mut Instance) -> u32 { @@ -10,5 +11,5 @@ pub extern "C" fn ___cxa_allocate_exception(size: u32, instance: &mut Instance) /// TODO: We don't have support for exceptions yet pub extern "C" fn ___cxa_throw(ptr: u32, ty: u32, destructor: u32, instance: &mut Instance) { debug!("emscripten::___cxa_throw"); - debug!("unimplmeneted yet!"); + _abort(); } diff --git a/src/apis/host/posix/env.rs b/src/apis/host/posix/env.rs index b0fff8174..5943e589b 100644 --- a/src/apis/host/posix/env.rs +++ b/src/apis/host/posix/env.rs @@ -1,7 +1,7 @@ use crate::webassembly::Instance; use std::env; -pub extern "C" fn get_env(name: &str, _instance: &mut Instance) -> Result { - debug!("host::get_env({:?})", name); - env::var(name) -} +// pub extern "C" fn get_env(name: &str, _instance: &mut Instance) -> Result { +// debug!("host::get_env({:?})", name); +// env::var(name) +// }