Skip to content

Commit 10b4a08

Browse files
authored
Merge branch 'master' into improve-exception-logging
2 parents c3c415f + 89b92e3 commit 10b4a08

File tree

14 files changed

+156
-48
lines changed

14 files changed

+156
-48
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All PRs to the Wasmer repository must add to this file.
55
Blocks of changes will separated by version increments.
66

77
## **[Unreleased]**
8+
- [#422](https://github.com/wasmerio/wasmer/pull/422) Improved Emscripten functions to run optipng and pngquant compiled to wasm
89
- [#409](https://github.com/wasmerio/wasmer/pull/409) Improved Emscripten functions to run JavascriptCore compiled to wasm
910
- [#399](https://github.com/wasmerio/wasmer/pull/399) Add example of using a plugin extended from WASI
1011
- [#397](https://github.com/wasmerio/wasmer/pull/397) Fix WASI fs abstraction to work on Windows

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ rustc_version = "0.2.3"
4343

4444
[features]
4545
default = ["fast-tests", "wasi"]
46-
debug = ["wasmer-clif-backend/debug", "wasmer-runtime-core/debug"]
46+
debug = ["wasmer-runtime-core/debug"]
47+
extra-debug = ["wasmer-clif-backend/debug", "wasmer-runtime-core/debug"]
4748
# This feature will allow cargo test to run much faster
4849
fast-tests = []
4950
"backend:llvm" = ["wasmer-llvm-backend"]

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,8 @@ production-release:
8484
debug-release:
8585
cargo build --release --features "debug"
8686

87+
extra-debug-release:
88+
cargo build --release --features "extra-debug"
89+
8790
publish-release:
8891
ghr -t ${GITHUB_TOKEN} -u ${CIRCLE_PROJECT_USERNAME} -r ${CIRCLE_PROJECT_REPONAME} -c ${CIRCLE_SHA1} -delete ${VERSION} ./artifacts/

examples/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# WebAssembly Examples
2+
3+
In this directory you can find a set of different examples that can run on the Wasmer WebAssembly runtime:
4+
5+
* Cowsay (WASI ABI) [[source-code](https://github.com/wapm-packages/cowsay)] [[wapm-package](https://wapm.io/package/cowsay)]
6+
* Nginx (Emscripten ABI) [[source-code](https://github.com/wapm-packages/nginx)] [[wapm-package](https://wapm.io/package/nginx)]
7+
* Lua (Emscripten ABI) [[source-code](https://github.com/wapm-packages/lua)] [[wapm-package](https://wapm.io/package/lua)]
8+
* PHP (Emscripten ABI) [[source-code](https://github.com/wapm-packages/php)] [[wapm-package](https://wapm.io/package/php)]
9+
* SQLite (Emscripten ABI) [[source-code](https://github.com/wapm-packages/sqlite)] [[wapm-package](https://wapm.io/package/sqlite)]

lib/emscripten/src/emscripten_target.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub fn _dladdr(_ctx: &mut Ctx, _a: i32, _b: i32) -> i32 {
5555
0
5656
}
5757
pub fn _pthread_attr_init(_ctx: &mut Ctx, _a: i32) -> i32 {
58-
debug!("emscripten::_pthread_attr_init");
58+
debug!("emscripten::_pthread_attr_init({})", _a);
5959
0
6060
}
6161
pub fn _pthread_attr_destroy(_ctx: &mut Ctx, _a: i32) -> i32 {
@@ -68,7 +68,10 @@ pub fn _pthread_attr_getstack(
6868
_stacksize: i32,
6969
_other: i32,
7070
) -> i32 {
71-
debug!("emscripten::_pthread_attr_getstack");
71+
debug!(
72+
"emscripten::_pthread_attr_getstack({}, {}, {})",
73+
_stackaddr, _stacksize, _other
74+
);
7275
// TODO: Translate from Emscripten
7376
// HEAP32[stackaddr >> 2] = STACK_BASE;
7477
// HEAP32[stacksize >> 2] = TOTAL_STACK;
@@ -87,7 +90,7 @@ pub fn _pthread_getspecific(_ctx: &mut Ctx, _a: i32) -> i32 {
8790
0
8891
}
8992
pub fn _pthread_getattr_np(_ctx: &mut Ctx, _thread: i32, _attr: i32) -> i32 {
90-
debug!("emscripten::_pthread_getattr_np");
93+
debug!("emscripten::_pthread_getattr_np({}, {})", _thread, _attr);
9194
0
9295
}
9396
pub fn _pthread_setspecific(_ctx: &mut Ctx, _a: i32, _b: i32) -> i32 {
@@ -732,6 +735,10 @@ pub fn invoke_vj(ctx: &mut Ctx, index: i32, a1: i32, a2: i32) {
732735
panic!("dyn_call_vj is set to None");
733736
}
734737
}
738+
pub fn invoke_vjji(ctx: &mut Ctx, index: i32, a1: i32, a2: i32, a3: i32, a4: i32, a5: i32) {
739+
debug!("emscripten::invoke_vjji");
740+
invoke_no_return!(ctx, dyn_call_vjji, index, a1, a2, a3, a4, a5)
741+
}
735742
pub fn invoke_vij(ctx: &mut Ctx, index: i32, a1: i32, a2: i32, a3: i32) {
736743
debug!("emscripten::invoke_vij");
737744
if let Some(dyn_call_vij) = &get_emscripten_data(ctx).dyn_call_vij {

lib/emscripten/src/env/mod.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,51 @@ pub fn ___build_environment(ctx: &mut Ctx, environ: c_int) {
6060
const MAX_ENV_VALUES: u32 = 64;
6161
const TOTAL_ENV_SIZE: u32 = 1024;
6262
let environment = emscripten_memory_pointer!(ctx.memory(0), environ) as *mut c_int;
63-
unsafe {
63+
let (mut pool_offset, env_ptr, mut pool_ptr) = unsafe {
6464
let (pool_offset, _pool_slice): (u32, &mut [u8]) =
6565
allocate_on_stack(ctx, TOTAL_ENV_SIZE as u32);
6666
let (env_offset, _env_slice): (u32, &mut [u8]) =
6767
allocate_on_stack(ctx, (MAX_ENV_VALUES * 4) as u32);
6868
let env_ptr = emscripten_memory_pointer!(ctx.memory(0), env_offset) as *mut c_int;
69-
let mut _pool_ptr = emscripten_memory_pointer!(ctx.memory(0), pool_offset) as *mut c_int;
69+
let pool_ptr = emscripten_memory_pointer!(ctx.memory(0), pool_offset) as *mut u8;
7070
*env_ptr = pool_offset as i32;
7171
*environment = env_offset as i32;
7272

73-
// *env_ptr = 0;
73+
(pool_offset, env_ptr, pool_ptr)
7474
};
75-
// unsafe {
76-
// *env_ptr = 0;
77-
// };
75+
76+
// *env_ptr = 0;
77+
let default_vars = vec![
78+
["USER", "web_user"],
79+
["LOGNAME", "web_user"],
80+
["PATH", "/"],
81+
["PWD", "/"],
82+
["HOME", "/home/web_user"],
83+
["LANG", "C.UTF-8"],
84+
["_", "thisProgram"],
85+
];
86+
let mut strings = vec![];
87+
let mut total_size = 0;
88+
for [key, val] in &default_vars {
89+
let line = key.to_string() + "=" + val;
90+
total_size += line.len();
91+
strings.push(line);
92+
}
93+
if total_size as u32 > TOTAL_ENV_SIZE {
94+
panic!("Environment size exceeded TOTAL_ENV_SIZE!");
95+
}
96+
unsafe {
97+
for (i, s) in strings.iter().enumerate() {
98+
for (j, c) in s.chars().enumerate() {
99+
debug_assert!(c < u8::max_value() as char);
100+
*pool_ptr.add(j) = c as u8;
101+
}
102+
*env_ptr.add(i * 4) = pool_offset as i32;
103+
pool_offset += s.len() as u32 + 1;
104+
pool_ptr = pool_ptr.add(s.len() + 1);
105+
}
106+
*env_ptr.add(strings.len() * 4) = 0;
107+
}
78108
}
79109

80110
pub fn ___assert_fail(_ctx: &mut Ctx, _a: c_int, _b: c_int, _c: c_int, _d: c_int) {

lib/emscripten/src/jmp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use wasmer_runtime_core::vm::Ctx;
77
/// setjmp
88
pub fn __setjmp(ctx: &mut Ctx, _env_addr: u32) -> c_int {
99
debug!("emscripten::__setjmp (setjmp)");
10-
abort_with_message(ctx, "missing function: _longjmp");
10+
abort_with_message(ctx, "missing function: _setjmp");
1111
unreachable!()
1212
// unsafe {
1313
// // Rather than using the env as the holder of the jump buffer pointer,

lib/emscripten/src/lib.rs

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ pub struct EmscriptenData<'a> {
127127
pub dyn_call_viijiii: Option<Func<'a, (i32, i32, i32, i32, i32, i32, i32, i32)>>,
128128
pub dyn_call_viijj: Option<Func<'a, (i32, i32, i32, i32, i32, i32, i32)>>,
129129
pub dyn_call_vj: Option<Func<'a, (i32, i32, i32)>>,
130+
pub dyn_call_vjji: Option<Func<'a, (i32, i32, i32, i32, i32, i32)>>,
130131
pub dyn_call_vij: Option<Func<'a, (i32, i32, i32, i32)>>,
131132
pub dyn_call_viji: Option<Func<'a, (i32, i32, i32, i32, i32)>>,
132133
pub dyn_call_vijiii: Option<Func<'a, (i32, i32, i32, i32, i32, i32, i32)>>,
@@ -146,11 +147,7 @@ impl<'a> EmscriptenData<'a> {
146147
pub fn new(instance: &'a mut Instance) -> EmscriptenData<'a> {
147148
let malloc = instance.func("_malloc").unwrap();
148149
let free = instance.func("_free").unwrap();
149-
let memalign = if let Ok(func) = instance.func("_memalign") {
150-
Some(func)
151-
} else {
152-
None
153-
};
150+
let memalign = instance.func("_memalign").ok();
154151
let memset = instance.func("_memset").unwrap();
155152
let stack_alloc = instance.func("stackAlloc").unwrap();
156153

@@ -198,6 +195,7 @@ impl<'a> EmscriptenData<'a> {
198195
let dyn_call_viijiii = instance.func("dynCall_viijiii").ok();
199196
let dyn_call_viijj = instance.func("dynCall_viijj").ok();
200197
let dyn_call_vj = instance.func("dynCall_vj").ok();
198+
let dyn_call_vjji = instance.func("dynCall_vjji").ok();
201199
let dyn_call_vij = instance.func("dynCall_vij").ok();
202200
let dyn_call_viji = instance.func("dynCall_viji").ok();
203201
let dyn_call_vijiii = instance.func("dynCall_vijiii").ok();
@@ -261,6 +259,7 @@ impl<'a> EmscriptenData<'a> {
261259
dyn_call_viijiii,
262260
dyn_call_viijj,
263261
dyn_call_vj,
262+
dyn_call_vjji,
264263
dyn_call_vij,
265264
dyn_call_viji,
266265
dyn_call_vijiii,
@@ -282,6 +281,7 @@ pub fn run_emscripten_instance(
282281
instance: &mut Instance,
283282
path: &str,
284283
args: Vec<&str>,
284+
entrypoint: Option<String>,
285285
) -> CallResult<()> {
286286
let mut data = EmscriptenData::new(instance);
287287
let data_ptr = &mut data as *mut _ as *mut c_void;
@@ -299,45 +299,54 @@ pub fn run_emscripten_instance(
299299

300300
// println!("running emscripten instance");
301301

302-
let main_func = instance.dyn_func("_main")?;
303-
let num_params = main_func.signature().params().len();
304-
let _result = match num_params {
305-
2 => {
306-
let (argc, argv) = store_module_arguments(instance.context_mut(), path, args);
307-
instance.call("_main", &[Value::I32(argc as i32), Value::I32(argv as i32)])?;
308-
}
309-
0 => {
310-
instance.call("_main", &[])?;
311-
}
312-
_ => panic!(
313-
"The emscripten main function has received an incorrect number of params {}",
314-
num_params
315-
),
316-
};
302+
if let Some(ep) = entrypoint {
303+
debug!("Running entry point: {}", &ep);
304+
let ep_fn = instance.dyn_func(&ep)?;
305+
let arg = unsafe { allocate_cstr_on_stack(instance.context_mut(), args[0]).0 };
306+
//let (argc, argv) = store_module_arguments(instance.context_mut(), args);
307+
instance.call(&ep, &[Value::I32(arg as i32)])?;
308+
} else {
309+
let main_func = instance.dyn_func("_main")?;
310+
let num_params = main_func.signature().params().len();
311+
let _result = match num_params {
312+
2 => {
313+
let mut new_args = vec![path];
314+
new_args.extend(args);
315+
let (argc, argv) = store_module_arguments(instance.context_mut(), new_args);
316+
instance.call("_main", &[Value::I32(argc as i32), Value::I32(argv as i32)])?;
317+
}
318+
0 => {
319+
instance.call("_main", &[])?;
320+
}
321+
_ => panic!(
322+
"The emscripten main function has received an incorrect number of params {}",
323+
num_params
324+
),
325+
};
326+
}
317327

318328
// TODO atexit for emscripten
319329
// println!("{:?}", data);
320330
Ok(())
321331
}
322332

323-
fn store_module_arguments(ctx: &mut Ctx, path: &str, args: Vec<&str>) -> (u32, u32) {
333+
fn store_module_arguments(ctx: &mut Ctx, args: Vec<&str>) -> (u32, u32) {
324334
let argc = args.len() + 1;
325335

326336
let mut args_slice = vec![0; argc];
327-
args_slice[0] = unsafe { allocate_cstr_on_stack(ctx, path).0 };
328-
for (slot, arg) in args_slice[1..argc].iter_mut().zip(args.iter()) {
337+
for (slot, arg) in args_slice[0..argc].iter_mut().zip(args.iter()) {
329338
*slot = unsafe { allocate_cstr_on_stack(ctx, &arg).0 };
330339
}
331340

332341
let (argv_offset, argv_slice): (_, &mut [u32]) =
333-
unsafe { allocate_on_stack(ctx, ((argc + 1) * 4) as u32) };
342+
unsafe { allocate_on_stack(ctx, ((argc) * 4) as u32) };
334343
assert!(!argv_slice.is_empty());
335344
for (slot, arg) in argv_slice[0..argc].iter_mut().zip(args_slice.iter()) {
336345
*slot = *arg
337346
}
338347
argv_slice[argc] = 0;
339348

340-
(argc as u32, argv_offset)
349+
(argc as u32 - 1, argv_offset)
341350
}
342351

343352
pub fn emscripten_set_up_memory(memory: &Memory, globals: &EmscriptenGlobalsData) {
@@ -597,6 +606,7 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
597606
"___syscall272" => func!(crate::syscalls::___syscall272),
598607
"___syscall295" => func!(crate::syscalls::___syscall295),
599608
"___syscall300" => func!(crate::syscalls::___syscall300),
609+
"___syscall320" => func!(crate::syscalls::___syscall320),
600610
"___syscall324" => func!(crate::syscalls::___syscall324),
601611
"___syscall330" => func!(crate::syscalls::___syscall330),
602612
"___syscall334" => func!(crate::syscalls::___syscall334),
@@ -718,6 +728,7 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
718728
"invoke_v" => func!(crate::emscripten_target::invoke_v),
719729
"invoke_vi" => func!(crate::emscripten_target::invoke_vi),
720730
"invoke_vj" => func!(crate::emscripten_target::invoke_vj),
731+
"invoke_vjji" => func!(crate::emscripten_target::invoke_vjji),
721732
"invoke_vii" => func!(crate::emscripten_target::invoke_vii),
722733
"invoke_viii" => func!(crate::emscripten_target::invoke_viii),
723734
"invoke_viiii" => func!(crate::emscripten_target::invoke_viiii),

lib/emscripten/src/memory.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ pub fn _emscripten_memcpy_big(ctx: &mut Ctx, dest: u32, src: u32, len: u32) -> u
2121

2222
/// emscripten: _emscripten_get_heap_size
2323
pub fn _emscripten_get_heap_size(ctx: &mut Ctx) -> u32 {
24-
debug!("emscripten::_emscripten_get_heap_size",);
25-
ctx.memory(0).size().bytes().0 as u32
24+
debug!("emscripten::_emscripten_get_heap_size");
25+
let result = ctx.memory(0).size().bytes().0 as u32;
26+
debug!("=> {}", result);
27+
28+
result
2629
}
2730

2831
// From emscripten implementation

lib/emscripten/src/process.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub fn _raise(_ctx: &mut Ctx, _one: i32) -> i32 {
8282
}
8383

8484
pub fn _sem_init(_ctx: &mut Ctx, _one: i32, _two: i32, _three: i32) -> i32 {
85-
debug!("emscripten::_sem_init");
85+
debug!("emscripten::_sem_init: {}, {}, {}", _one, _two, _three);
8686
0
8787
}
8888

0 commit comments

Comments
 (0)