Files
wasmer/lib/emscripten/src/io/unix.rs

64 lines
2.1 KiB
Rust
Raw Normal View History

2019-06-13 14:05:57 +02:00
use super::super::env::call_malloc;
2019-06-13 20:46:05 +02:00
use super::super::utils::copy_cstr_into_wasm;
use libc::{chroot as _chroot, getpwuid as _getpwuid, printf as _printf};
use std::mem;
2018-11-13 19:19:23 -08:00
use wasmer_runtime_core::vm::Ctx;
2018-11-13 19:19:23 -08:00
2018-11-20 20:11:58 +01:00
/// putchar
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 putchar(_ctx: &mut Ctx, chr: i32) {
2019-01-30 20:03:54 -06:00
unsafe { libc::putchar(chr) };
}
2018-11-20 20:11:58 +01:00
/// printf
pub fn printf(ctx: &mut Ctx, memory_offset: i32, extra: i32) -> i32 {
debug!("emscripten::printf {}, {}", memory_offset, extra);
unsafe {
let addr = emscripten_memory_pointer!(ctx.memory(0), memory_offset) as _;
_printf(addr, extra)
}
2018-11-13 19:19:23 -08:00
}
2019-03-20 16:46:42 -07:00
/// chroot
pub fn chroot(ctx: &mut Ctx, name_ptr: i32) -> i32 {
debug!("emscripten::chroot");
2019-03-20 16:46:42 -07:00
let name = emscripten_memory_pointer!(ctx.memory(0), name_ptr) as *const i8;
unsafe { _chroot(name) }
}
2019-03-25 11:58:44 -07:00
/// getpwuid
2019-06-13 21:53:37 +02:00
#[allow(clippy::cast_ptr_alignment)]
2019-06-13 14:05:57 +02:00
pub fn getpwuid(ctx: &mut Ctx, uid: i32) -> i32 {
debug!("emscripten::getpwuid {}", uid);
#[repr(C)]
struct GuestPasswd {
pw_name: u32,
pw_passwd: u32,
pw_uid: u32,
pw_gid: u32,
pw_gecos: u32,
pw_dir: u32,
pw_shell: u32,
}
2019-06-13 21:53:37 +02:00
2019-06-13 14:05:57 +02:00
unsafe {
let passwd = &*_getpwuid(uid as _);
let passwd_struct_offset = call_malloc(ctx, mem::size_of::<GuestPasswd>() as _);
let passwd_struct_ptr =
emscripten_memory_pointer!(ctx.memory(0), passwd_struct_offset) as *mut GuestPasswd;
2019-06-13 21:53:37 +02:00
assert_eq!(passwd_struct_ptr as usize % std::cmp::min(std::mem::size_of::<usize>(), std::mem::align_of::<usize>()), 0);
2019-06-13 14:05:57 +02:00
(*passwd_struct_ptr).pw_name = copy_cstr_into_wasm(ctx, passwd.pw_name);
(*passwd_struct_ptr).pw_passwd = copy_cstr_into_wasm(ctx, passwd.pw_passwd);
(*passwd_struct_ptr).pw_gecos = copy_cstr_into_wasm(ctx, passwd.pw_gecos);
(*passwd_struct_ptr).pw_dir = copy_cstr_into_wasm(ctx, passwd.pw_dir);
(*passwd_struct_ptr).pw_shell = copy_cstr_into_wasm(ctx, passwd.pw_shell);
(*passwd_struct_ptr).pw_uid = passwd.pw_uid;
(*passwd_struct_ptr).pw_gid = passwd.pw_gid;
passwd_struct_offset as _
}
// unsafe { _getpwuid(uid as _) as _}
// 0
2019-03-25 11:58:44 -07:00
}