2019-01-11 05:37:59 +00:00
|
|
|
use super::env;
|
2019-01-24 05:49:34 +00:00
|
|
|
use super::env::get_emscripten_data;
|
2019-05-06 21:05:04 +00:00
|
|
|
use crate::storage::align_memory;
|
2018-11-27 04:29:26 +00:00
|
|
|
use libc::stat;
|
2018-12-09 07:02:26 +00:00
|
|
|
use std::ffi::CStr;
|
2019-01-24 05:49:34 +00:00
|
|
|
use std::mem::size_of;
|
2018-11-26 21:15:49 +00:00
|
|
|
use std::os::raw::c_char;
|
2019-05-25 01:00:07 +00:00
|
|
|
use std::path::PathBuf;
|
2018-11-27 04:28:13 +00:00
|
|
|
use std::slice;
|
2019-02-07 02:57:11 +00:00
|
|
|
use wasmer_runtime_core::memory::Memory;
|
2019-01-26 01:55:33 +00:00
|
|
|
use wasmer_runtime_core::{
|
2019-01-26 02:12:36 +00:00
|
|
|
module::Module,
|
|
|
|
structures::TypedIndex,
|
|
|
|
types::{ImportedMemoryIndex, ImportedTableIndex},
|
2019-01-30 05:08:03 +00:00
|
|
|
units::Pages,
|
2019-01-26 02:12:36 +00:00
|
|
|
vm::Ctx,
|
2019-01-26 01:55:33 +00:00
|
|
|
};
|
2019-01-24 23:30:13 +00:00
|
|
|
|
2018-11-21 04:51:11 +00:00
|
|
|
/// We check if a provided module is an Emscripten generated one
|
2019-01-18 18:54:16 +00:00
|
|
|
pub fn is_emscripten_module(module: &Module) -> bool {
|
2019-02-21 00:41:41 +00:00
|
|
|
for (_, import_name) in &module.info().imported_functions {
|
2019-02-07 00:26:45 +00:00
|
|
|
let namespace = module
|
2019-02-21 00:41:41 +00:00
|
|
|
.info()
|
2019-02-07 00:26:45 +00:00
|
|
|
.namespace_table
|
|
|
|
.get(import_name.namespace_index);
|
2019-02-21 00:41:41 +00:00
|
|
|
let field = module.info().name_table.get(import_name.name_index);
|
2019-09-23 01:23:22 +00:00
|
|
|
if (field == "_emscripten_memcpy_big"
|
|
|
|
|| field == "emscripten_memcpy_big"
|
|
|
|
|| field == "__map_file")
|
2019-07-07 00:46:48 +00:00
|
|
|
&& namespace == "env"
|
|
|
|
{
|
2019-01-18 06:18:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
2018-11-21 04:51:11 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 15:12:46 +00:00
|
|
|
pub fn get_emscripten_table_size(module: &Module) -> Result<(u32, Option<u32>), String> {
|
|
|
|
if module.info().imported_tables.len() == 0 {
|
|
|
|
return Err("Emscripten requires at least one imported table".to_string());
|
|
|
|
}
|
2019-02-21 00:41:41 +00:00
|
|
|
let (_, table) = &module.info().imported_tables[ImportedTableIndex::new(0)];
|
2019-08-16 15:12:46 +00:00
|
|
|
Ok((table.minimum, table.maximum))
|
2019-01-26 01:55:33 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 15:18:51 +00:00
|
|
|
pub fn get_emscripten_memory_size(module: &Module) -> Result<(Pages, Option<Pages>, bool), String> {
|
2019-08-16 15:57:36 +00:00
|
|
|
if module.info().imported_memories.len() == 0 {
|
2019-08-16 15:18:51 +00:00
|
|
|
return Err("Emscripten requires at least one imported memory".to_string());
|
|
|
|
}
|
2019-02-21 00:41:41 +00:00
|
|
|
let (_, memory) = &module.info().imported_memories[ImportedMemoryIndex::new(0)];
|
2019-08-16 15:18:51 +00:00
|
|
|
Ok((memory.minimum, memory.maximum, memory.shared))
|
2019-01-26 02:12:36 +00:00
|
|
|
}
|
|
|
|
|
2019-05-06 21:05:04 +00:00
|
|
|
/// Reads values written by `-s EMIT_EMSCRIPTEN_METADATA=1`
|
|
|
|
/// Assumes values start from the end in this order:
|
|
|
|
/// Last export: Dynamic Base
|
|
|
|
/// Second-to-Last export: Dynamic top pointer
|
2019-08-19 14:45:14 +00:00
|
|
|
pub fn get_emscripten_metadata(module: &Module) -> Result<Option<(u32, u32)>, String> {
|
|
|
|
let max_idx = match module.info().globals.iter().map(|(k, _)| k).max() {
|
|
|
|
Some(x) => x,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
|
|
|
|
let snd_max_idx = match module
|
2019-05-06 21:05:04 +00:00
|
|
|
.info()
|
|
|
|
.globals
|
|
|
|
.iter()
|
|
|
|
.map(|(k, _)| k)
|
2019-08-19 14:45:14 +00:00
|
|
|
.filter(|k| *k != max_idx)
|
|
|
|
.max()
|
|
|
|
{
|
|
|
|
Some(x) => x,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
2019-05-06 21:05:04 +00:00
|
|
|
|
|
|
|
use wasmer_runtime_core::types::{GlobalInit, Initializer::Const, Value::I32};
|
|
|
|
if let (
|
|
|
|
GlobalInit {
|
|
|
|
init: Const(I32(dynamic_base)),
|
|
|
|
..
|
|
|
|
},
|
|
|
|
GlobalInit {
|
|
|
|
init: Const(I32(dynamictop_ptr)),
|
|
|
|
..
|
|
|
|
},
|
|
|
|
) = (
|
2019-08-19 14:45:14 +00:00
|
|
|
&module.info().globals[max_idx],
|
|
|
|
&module.info().globals[snd_max_idx],
|
2019-05-06 21:05:04 +00:00
|
|
|
) {
|
2019-08-19 14:45:14 +00:00
|
|
|
let dynamic_base = (*dynamic_base as u32).checked_sub(32).ok_or(format!(
|
|
|
|
"emscripten unexpected dynamic_base {}",
|
|
|
|
*dynamic_base as u32
|
|
|
|
))?;
|
|
|
|
let dynamictop_ptr = (*dynamictop_ptr as u32).checked_sub(32).ok_or(format!(
|
|
|
|
"emscripten unexpected dynamictop_ptr {}",
|
|
|
|
*dynamictop_ptr as u32
|
|
|
|
))?;
|
|
|
|
Ok(Some((
|
|
|
|
align_memory(dynamic_base),
|
|
|
|
align_memory(dynamictop_ptr),
|
|
|
|
)))
|
2019-05-06 21:05:04 +00:00
|
|
|
} else {
|
2019-08-19 14:45:14 +00:00
|
|
|
Ok(None)
|
2019-05-06 21:05:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-09 21:58:05 +00:00
|
|
|
pub unsafe fn write_to_buf(ctx: &mut Ctx, string: *const c_char, buf: u32, max: u32) -> u32 {
|
2019-01-24 21:04:12 +00:00
|
|
|
let buf_addr = emscripten_memory_pointer!(ctx.memory(0), buf) as *mut c_char;
|
2018-12-11 17:06:20 +00:00
|
|
|
|
2018-12-14 01:58:08 +00:00
|
|
|
for i in 0..max {
|
|
|
|
*buf_addr.add(i as _) = *string.add(i as _);
|
2018-12-11 17:06:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
|
2018-12-13 23:09:07 +00:00
|
|
|
/// This function expects nullbyte to be appended.
|
2019-01-24 05:49:34 +00:00
|
|
|
pub unsafe fn copy_cstr_into_wasm(ctx: &mut Ctx, cstr: *const c_char) -> u32 {
|
2018-12-07 03:08:17 +00:00
|
|
|
let s = CStr::from_ptr(cstr).to_str().unwrap();
|
|
|
|
let cstr_len = s.len();
|
2019-02-09 21:58:05 +00:00
|
|
|
let space_offset = env::call_malloc(ctx, (cstr_len as u32) + 1);
|
2019-02-07 02:57:11 +00:00
|
|
|
let raw_memory = emscripten_memory_pointer!(ctx.memory(0), space_offset) as *mut c_char;
|
2018-12-07 03:08:17 +00:00
|
|
|
let slice = slice::from_raw_parts_mut(raw_memory, cstr_len);
|
|
|
|
|
|
|
|
for (byte, loc) in s.bytes().zip(slice.iter_mut()) {
|
2019-02-07 02:57:11 +00:00
|
|
|
*loc = byte as _;
|
2018-12-07 03:08:17 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 13:50:35 +00:00
|
|
|
// TODO: Appending null byte won't work, because there is CStr::from_ptr(cstr)
|
|
|
|
// at the top that crashes when there is no null byte
|
2018-12-07 03:08:17 +00:00
|
|
|
*raw_memory.add(cstr_len) = 0;
|
|
|
|
|
|
|
|
space_offset
|
|
|
|
}
|
|
|
|
|
2019-02-09 21:58:05 +00:00
|
|
|
pub unsafe fn allocate_on_stack<'a, T: Copy>(ctx: &'a mut Ctx, count: u32) -> (u32, &'a mut [T]) {
|
2019-02-02 23:28:50 +00:00
|
|
|
let offset = get_emscripten_data(ctx)
|
|
|
|
.stack_alloc
|
2019-07-07 00:15:35 +00:00
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
2019-02-02 23:28:50 +00:00
|
|
|
.call(count * (size_of::<T>() as u32))
|
|
|
|
.unwrap();
|
2019-01-24 21:04:12 +00:00
|
|
|
let addr = emscripten_memory_pointer!(ctx.memory(0), offset) as *mut T;
|
2019-01-24 05:49:34 +00:00
|
|
|
let slice = slice::from_raw_parts_mut(addr, count as usize);
|
|
|
|
|
|
|
|
(offset, slice)
|
2018-12-07 03:40:44 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 21:58:05 +00:00
|
|
|
pub unsafe fn allocate_cstr_on_stack<'a>(ctx: &'a mut Ctx, s: &str) -> (u32, &'a [u8]) {
|
|
|
|
let (offset, slice) = allocate_on_stack(ctx, (s.len() + 1) as u32);
|
2018-12-07 03:40:44 +00:00
|
|
|
|
|
|
|
use std::iter;
|
|
|
|
for (byte, loc) in s.bytes().chain(iter::once(0)).zip(slice.iter_mut()) {
|
|
|
|
*loc = byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
(offset, slice)
|
|
|
|
}
|
|
|
|
|
2019-03-15 21:10:17 +00:00
|
|
|
#[cfg(not(target_os = "windows"))]
|
2019-01-24 05:49:34 +00:00
|
|
|
pub unsafe fn copy_terminated_array_of_cstrs(_ctx: &mut Ctx, cstrs: *mut *mut c_char) -> u32 {
|
2019-03-12 21:00:33 +00:00
|
|
|
let _total_num = {
|
2018-11-26 21:15:49 +00:00
|
|
|
let mut ptr = cstrs;
|
|
|
|
let mut counter = 0;
|
|
|
|
while !(*ptr).is_null() {
|
|
|
|
counter += 1;
|
|
|
|
ptr = ptr.add(1);
|
|
|
|
}
|
|
|
|
counter
|
|
|
|
};
|
2018-11-27 04:29:26 +00:00
|
|
|
debug!(
|
|
|
|
"emscripten::copy_terminated_array_of_cstrs::total_num: {}",
|
2019-03-12 21:00:33 +00:00
|
|
|
_total_num
|
2018-11-27 04:29:26 +00:00
|
|
|
);
|
2018-11-26 21:15:49 +00:00
|
|
|
0
|
|
|
|
}
|
|
|
|
|
2018-12-31 02:39:03 +00:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct GuestStat {
|
2019-01-04 02:39:07 +00:00
|
|
|
st_dev: u32,
|
2018-12-31 17:31:23 +00:00
|
|
|
__st_dev_padding: u32,
|
|
|
|
__st_ino_truncated: u32,
|
2018-12-31 02:39:03 +00:00
|
|
|
st_mode: u32,
|
|
|
|
st_nlink: u32,
|
|
|
|
st_uid: u32,
|
|
|
|
st_gid: u32,
|
2018-12-31 17:31:23 +00:00
|
|
|
st_rdev: u32,
|
|
|
|
__st_rdev_padding: u32,
|
2018-12-31 02:39:03 +00:00
|
|
|
st_size: u32,
|
|
|
|
st_blksize: u32,
|
|
|
|
st_blocks: u32,
|
|
|
|
st_atime: u64,
|
|
|
|
st_mtime: u64,
|
|
|
|
st_ctime: u64,
|
2019-04-08 18:26:25 +00:00
|
|
|
st_ino: u32,
|
2018-12-31 02:39:03 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 06:33:46 +00:00
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
2019-01-24 05:49:34 +00:00
|
|
|
pub unsafe fn copy_stat_into_wasm(ctx: &mut Ctx, buf: u32, stat: &stat) {
|
2019-01-24 21:04:12 +00:00
|
|
|
let stat_ptr = emscripten_memory_pointer!(ctx.memory(0), buf) as *mut GuestStat;
|
2018-12-31 02:39:03 +00:00
|
|
|
(*stat_ptr).st_dev = stat.st_dev as _;
|
2018-12-31 17:31:23 +00:00
|
|
|
(*stat_ptr).__st_dev_padding = 0;
|
|
|
|
(*stat_ptr).__st_ino_truncated = stat.st_ino as _;
|
2018-12-31 02:39:03 +00:00
|
|
|
(*stat_ptr).st_mode = stat.st_mode as _;
|
|
|
|
(*stat_ptr).st_nlink = stat.st_nlink as _;
|
|
|
|
(*stat_ptr).st_uid = stat.st_uid as _;
|
|
|
|
(*stat_ptr).st_gid = stat.st_gid as _;
|
|
|
|
(*stat_ptr).st_rdev = stat.st_rdev as _;
|
2018-12-31 17:31:23 +00:00
|
|
|
(*stat_ptr).__st_rdev_padding = 0;
|
2018-12-31 02:39:03 +00:00
|
|
|
(*stat_ptr).st_size = stat.st_size as _;
|
2018-12-31 15:15:24 +00:00
|
|
|
(*stat_ptr).st_blksize = 4096;
|
2018-12-31 02:39:03 +00:00
|
|
|
#[cfg(not(target_os = "windows"))]
|
|
|
|
{
|
|
|
|
(*stat_ptr).st_blocks = stat.st_blocks as _;
|
|
|
|
}
|
2018-12-31 15:15:24 +00:00
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
{
|
|
|
|
(*stat_ptr).st_blocks = 0;
|
|
|
|
}
|
2018-12-31 02:39:03 +00:00
|
|
|
(*stat_ptr).st_atime = stat.st_atime as _;
|
|
|
|
(*stat_ptr).st_mtime = stat.st_mtime as _;
|
|
|
|
(*stat_ptr).st_ctime = stat.st_ctime as _;
|
2018-12-31 17:31:23 +00:00
|
|
|
(*stat_ptr).st_ino = stat.st_ino as _;
|
2018-11-27 03:48:19 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 21:00:33 +00:00
|
|
|
#[allow(dead_code)] // it's used in `env/windows/mod.rs`.
|
2019-02-07 02:57:11 +00:00
|
|
|
pub fn read_string_from_wasm(memory: &Memory, offset: u32) -> String {
|
2019-02-07 18:51:19 +00:00
|
|
|
let v: Vec<u8> = memory.view()[(offset as usize)..]
|
2019-02-07 02:57:11 +00:00
|
|
|
.iter()
|
2019-02-07 18:51:19 +00:00
|
|
|
.map(|cell| cell.get())
|
|
|
|
.take_while(|&byte| byte != 0)
|
|
|
|
.collect();
|
|
|
|
String::from_utf8_lossy(&v).to_owned().to_string()
|
2019-02-07 02:57:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 01:00:07 +00:00
|
|
|
/// This function trys to find an entry in mapdir
|
|
|
|
/// translating paths into their correct value
|
|
|
|
pub fn get_cstr_path(ctx: &mut Ctx, path: *const i8) -> Option<std::ffi::CString> {
|
2019-05-29 21:20:52 +00:00
|
|
|
use std::collections::VecDeque;
|
|
|
|
|
2019-08-20 23:17:43 +00:00
|
|
|
let path_str =
|
|
|
|
unsafe { std::ffi::CStr::from_ptr(path as *const _).to_str().unwrap() }.to_string();
|
2019-05-29 21:20:52 +00:00
|
|
|
let data = get_emscripten_data(ctx);
|
|
|
|
let path = PathBuf::from(path_str);
|
|
|
|
let mut prefix_added = false;
|
|
|
|
let mut components = path.components().collect::<VecDeque<_>>();
|
|
|
|
// TODO(mark): handle absolute/non-canonical/non-relative paths too (this
|
|
|
|
// functionality should be shared among the abis)
|
|
|
|
if components.len() == 1 {
|
|
|
|
components.push_front(std::path::Component::CurDir);
|
|
|
|
prefix_added = true;
|
|
|
|
}
|
|
|
|
let mut cumulative_path = PathBuf::new();
|
|
|
|
for c in components.into_iter() {
|
|
|
|
cumulative_path.push(c);
|
|
|
|
if let Some(val) = data
|
|
|
|
.mapped_dirs
|
|
|
|
.get(&cumulative_path.to_string_lossy().to_string())
|
|
|
|
{
|
|
|
|
let rest_of_path = if !prefix_added {
|
|
|
|
path.strip_prefix(cumulative_path).ok()?
|
|
|
|
} else {
|
|
|
|
&path
|
|
|
|
};
|
|
|
|
let rebased_path = val.join(rest_of_path);
|
|
|
|
return std::ffi::CString::new(rebased_path.to_string_lossy().as_bytes()).ok();
|
|
|
|
}
|
2019-05-25 01:00:07 +00:00
|
|
|
}
|
2019-05-29 21:20:52 +00:00
|
|
|
None
|
2019-05-25 01:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// gets the current directory
|
|
|
|
/// handles mapdir logic
|
|
|
|
pub fn get_current_directory(ctx: &mut Ctx) -> Option<PathBuf> {
|
|
|
|
if let Some(val) = get_emscripten_data(ctx).mapped_dirs.get(".") {
|
|
|
|
return Some(val.clone());
|
|
|
|
}
|
|
|
|
std::env::current_dir()
|
|
|
|
.map(|cwd| {
|
|
|
|
if let Some(val) = get_emscripten_data(ctx)
|
|
|
|
.mapped_dirs
|
|
|
|
.get(&cwd.to_string_lossy().to_string())
|
|
|
|
{
|
|
|
|
val.clone()
|
|
|
|
} else {
|
|
|
|
cwd
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.ok()
|
|
|
|
}
|