Files
wasmer/lib/emscripten/src/env/mod.rs

200 lines
5.3 KiB
Rust
Raw Normal View History

#[cfg(unix)]
mod unix;
#[cfg(windows)]
mod windows;
#[cfg(unix)]
pub use self::unix::*;
#[cfg(windows)]
pub use self::windows::*;
2019-05-24 12:15:52 -07:00
use libc::c_char;
use crate::{allocate_on_stack, EmscriptenData};
use std::os::raw::c_int;
use wasmer_runtime_core::{
memory::ptr::{Array, WasmPtr},
types::ValueType,
vm::Ctx,
};
pub fn call_malloc(ctx: &mut Ctx, size: u32) -> u32 {
2019-07-06 17:46:48 -07:00
get_emscripten_data(ctx)
.malloc
.as_ref()
.unwrap()
.call(size)
.unwrap()
}
pub fn call_malloc_with_cast<T: Copy, Ty>(ctx: &mut Ctx, size: u32) -> WasmPtr<T, Ty> {
2019-07-07 17:10:55 -07:00
WasmPtr::new(call_malloc(ctx, size))
}
pub fn call_memalign(ctx: &mut Ctx, alignment: u32, size: u32) -> u32 {
if let Some(memalign) = &get_emscripten_data(ctx).memalign {
memalign.call(alignment, size).unwrap()
} else {
panic!("Memalign is set to None");
}
}
pub fn call_memset(ctx: &mut Ctx, pointer: u32, value: u32, size: u32) -> u32 {
get_emscripten_data(ctx)
.memset
.as_ref()
.unwrap()
.call(pointer, value, size)
.unwrap()
}
pub(crate) fn get_emscripten_data(ctx: &mut Ctx) -> &mut EmscriptenData {
unsafe { &mut *(ctx.data as *mut EmscriptenData) }
}
pub fn _getpagesize(_ctx: &mut Ctx) -> u32 {
debug!("emscripten::_getpagesize");
16384
}
pub fn _times(ctx: &mut Ctx, buffer: u32) -> u32 {
if buffer != 0 {
call_memset(ctx, buffer, 0, 16);
}
0
}
#[allow(clippy::cast_ptr_alignment)]
pub fn ___build_environment(ctx: &mut Ctx, environ: c_int) {
debug!("emscripten::___build_environment {}", environ);
const MAX_ENV_VALUES: u32 = 64;
const TOTAL_ENV_SIZE: u32 = 1024;
let environment = emscripten_memory_pointer!(ctx.memory(0), environ) as *mut c_int;
2019-05-03 17:34:57 -07:00
let (mut pool_offset, env_ptr, mut pool_ptr) = unsafe {
let (pool_offset, _pool_slice): (u32, &mut [u8]) =
allocate_on_stack(ctx, TOTAL_ENV_SIZE as u32);
let (env_offset, _env_slice): (u32, &mut [u8]) =
allocate_on_stack(ctx, (MAX_ENV_VALUES * 4) as u32);
let env_ptr = emscripten_memory_pointer!(ctx.memory(0), env_offset) as *mut c_int;
2019-05-03 17:34:57 -07:00
let pool_ptr = emscripten_memory_pointer!(ctx.memory(0), pool_offset) as *mut u8;
*env_ptr = pool_offset as i32;
*environment = env_offset as i32;
2019-05-03 17:34:57 -07:00
(pool_offset, env_ptr, pool_ptr)
};
2019-05-03 17:34:57 -07:00
// *env_ptr = 0;
let default_vars = vec![
["USER", "web_user"],
["LOGNAME", "web_user"],
["PATH", "/"],
["PWD", "/"],
["HOME", "/home/web_user"],
["LANG", "C.UTF-8"],
["_", "thisProgram"],
];
let mut strings = vec![];
let mut total_size = 0;
for [key, val] in &default_vars {
let line = key.to_string() + "=" + val;
total_size += line.len();
strings.push(line);
}
if total_size as u32 > TOTAL_ENV_SIZE {
panic!("Environment size exceeded TOTAL_ENV_SIZE!");
}
unsafe {
for (i, s) in strings.iter().enumerate() {
for (j, c) in s.chars().enumerate() {
debug_assert!(c < u8::max_value() as char);
*pool_ptr.add(j) = c as u8;
}
*env_ptr.add(i * 4) = pool_offset as i32;
pool_offset += s.len() as u32 + 1;
pool_ptr = pool_ptr.add(s.len() + 1);
}
*env_ptr.add(strings.len() * 4) = 0;
}
}
fix(emscripten) Various warning fixes and cleanups (#266) * fix(emscripten) Remove unused imports. This patch removes unused imports reported by `rustc` as warnings. * fix(emscripten) Allow unreachable patterns in `_clock_gettime`. The compiler thinks `CLOCK_MONOTONIC_COARSE` is unreachable, which is not always the case. Add an attribute to allow unreachable patterns to remove the warning. * fix(emscripten) Rename unused variables. This patch renames various unused variables by appending an underscore to them. * fix(emscripten) Declare `table` as immutable. The `table` variable in `EmscriptenGlobals::new` was declared as mutable, but it's never mutated. * fix(emscripten) Remove an unnecessary `unsafe` block. * fix(emscripten) Remove duplicate definition of `SO_NOSIGPIPE`. The `SO_NOSIGPIPE` constant is defined in `syscalls/mod.rs` and `syscalls/unix.rs`. It's never used in the first case. We can safely remove it in this file, and keep it in `unix.rs`. * fix(emscripten) `read_string_from_wasm` is used only on Windows. Mark `read_string_from_wasm` as possible deadcode, since it's used only on Windows. * fix(emscripten) Remove `DYNAMICTOP_PTR_DIFF`, `stacktop`, `stack_max`, `dynamic_base` and `dynamic_ptr`. Four functions and one constant are used together but never used inside or outside this file. They are deadcode. * fix(emscripten) Remove `infinity` and `nan` fields of `EmscriptenGlobalsData`. Those fields are never used. * fix(emscripten) Allow non snake case in `emscripten_target.rs`. Many functions in this file don't follow the snake case style for Rust function names. The reason is that we want the names to match the emscripten symbol names; even if a mapping is done in `lib.rs`, it's easier to get the same names. * fix(emscripten) Rename `STATIC_TOP` to `static_top`. This variable is not a constant.
2019-03-12 22:00:33 +01:00
pub fn ___assert_fail(_ctx: &mut Ctx, _a: c_int, _b: c_int, _c: c_int, _d: c_int) {
debug!("emscripten::___assert_fail {} {} {} {}", _a, _b, _c, _d);
// TODO: Implement like emscripten expects regarding memory/page size
// TODO raise an error
}
2019-05-24 12:15:52 -07:00
pub fn _pathconf(ctx: &mut Ctx, path_addr: c_int, name: c_int) -> c_int {
debug!(
"emscripten::_pathconf {} {} - UNIMPLEMENTED",
path_addr, name
);
let _path = emscripten_memory_pointer!(ctx.memory(0), path_addr) as *const c_char;
match name {
0 => 32000,
1 | 2 | 3 => 255,
4 | 5 | 16 | 17 | 18 => 4096,
6 | 7 | 20 => 1,
8 => 0,
9 | 10 | 11 | 12 | 14 | 15 | 19 => -1,
13 => 64,
_ => {
// ___setErrNo(22);
-1
}
}
}
pub fn _fpathconf(_ctx: &mut Ctx, _fildes: c_int, name: c_int) -> c_int {
debug!("emscripten::_fpathconf {} {}", _fildes, name);
match name {
0 => 32000,
1 | 2 | 3 => 255,
4 | 5 | 16 | 17 | 18 => 4096,
6 | 7 | 20 => 1,
8 => 0,
9 | 10 | 11 | 12 | 14 | 15 | 19 => -1,
13 => 64,
_ => {
// ___setErrNo(22);
-1
}
}
}
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct EmAddrInfo {
// int
pub ai_flags: i32,
// int
pub ai_family: i32,
// int
pub ai_socktype: i32,
// int
pub ai_protocol: i32,
// socklen_t
pub ai_addrlen: u32,
// struct sockaddr*
pub ai_addr: WasmPtr<EmSockAddr>,
// char*
pub ai_canonname: WasmPtr<c_char, Array>,
// struct addrinfo*
pub ai_next: WasmPtr<EmAddrInfo>,
}
unsafe impl ValueType for EmAddrInfo {}
// NOTE: from looking at emscripten JS, this should be a union
// TODO: review this, highly likely to have bugs
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct EmSockAddr {
pub sa_family: i16,
pub sa_data: [c_char; 14],
}
unsafe impl ValueType for EmSockAddr {}