Skip to content

Commit 3c354eb

Browse files
bors[bot]Mark McCaskeyxmclark
committed
Merge #328
328: fix master; add some emscripten calls r=xmclark a=MarkMcCaskey notified by #327 ; that program when compiled now outputs `"RuntimeError: WebAssembly trap occured during runtime: memory out-of-bounds access"` ; which may have been broken by #326 Co-authored-by: Mark McCaskey <mark@wasmer.io> Co-authored-by: Mackenzie Clark <mackenzie.a.z.c@gmail.com>
2 parents d9114e4 + 7cea2c2 commit 3c354eb

File tree

5 files changed

+124
-45
lines changed

5 files changed

+124
-45
lines changed

Cargo.lock

+56-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/emscripten/src/exception.rs

+5
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ pub fn ___cxa_begin_catch(_ctx: &mut Ctx, _exception_object_ptr: u32) -> i32 {
2323
pub fn ___cxa_end_catch(_ctx: &mut Ctx) {
2424
debug!("emscripten::___cxa_end_catch");
2525
}
26+
27+
pub fn ___cxa_uncaught_exception(_ctx: &mut Ctx) -> i32 {
28+
debug!("emscripten::___cxa_uncaught_exception");
29+
-1
30+
}

lib/emscripten/src/lib.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,12 @@ pub fn run_emscripten_instance(
247247
let data_ptr = &mut data as *mut _ as *mut c_void;
248248
instance.context_mut().data = data_ptr;
249249

250+
// ATINIT
251+
// (used by C++)
252+
if let Ok(_func) = instance.dyn_func("globalCtors") {
253+
instance.call("globalCtors", &[])?;
254+
}
255+
250256
if let Ok(_func) = instance.dyn_func("___emscripten_environ_constructor") {
251257
instance.call("___emscripten_environ_constructor", &[])?;
252258
}
@@ -269,7 +275,7 @@ pub fn run_emscripten_instance(
269275
),
270276
};
271277

272-
// TODO atinit and atexit for emscripten
278+
// TODO atexit for emscripten
273279
// println!("{:?}", data);
274280
Ok(())
275281
}
@@ -607,6 +613,7 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
607613
"___cxa_throw" => func!(crate::exception::___cxa_throw),
608614
"___cxa_begin_catch" => func!(crate::exception::___cxa_begin_catch),
609615
"___cxa_end_catch" => func!(crate::exception::___cxa_end_catch),
616+
"___cxa_uncaught_exception" => func!(crate::exception::___cxa_uncaught_exception),
610617

611618
// Time
612619
"_gettimeofday" => func!(crate::time::_gettimeofday),
@@ -619,6 +626,7 @@ pub fn generate_emscripten_env(globals: &mut EmscriptenGlobals) -> ImportObject
619626
"_localtime" => func!(crate::time::_localtime),
620627
"_time" => func!(crate::time::_time),
621628
"_strftime" => func!(crate::time::_strftime),
629+
"_strftime_l" => func!(crate::time::_strftime_l),
622630
"_localtime_r" => func!(crate::time::_localtime_r),
623631
"_gmtime_r" => func!(crate::time::_gmtime_r),
624632
"_mktime" => func!(crate::time::_mktime),

lib/emscripten/src/time.rs

+17
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,20 @@ pub fn _strftime(
310310
);
311311
0
312312
}
313+
314+
/// emscripten: _strftime_l
315+
pub fn _strftime_l(
316+
ctx: &mut Ctx,
317+
s_ptr: c_int,
318+
maxsize: u32,
319+
format_ptr: c_int,
320+
tm_ptr: c_int,
321+
_last: c_int,
322+
) -> i32 {
323+
debug!(
324+
"emscripten::_strftime_l {} {} {} {}",
325+
s_ptr, maxsize, format_ptr, tm_ptr
326+
);
327+
328+
_strftime(ctx, s_ptr, maxsize, format_ptr, tm_ptr)
329+
}

src/bin/wasmer.rs

+37-29
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ use wasmer_runtime_core::{self, backend::CompilerConfig};
1717
#[cfg(feature = "wasi")]
1818
use wasmer_wasi;
1919

20+
// stub module to make conditional compilation happy
21+
#[cfg(not(feature = "wasi"))]
22+
mod wasmer_wasi {
23+
use wasmer_runtime_core::{import::ImportObject, module::Module};
24+
25+
pub fn is_wasi_module(_module: &Module) -> bool {
26+
false
27+
}
28+
29+
pub fn generate_import_object(_args: Vec<Vec<u8>>, _envs: Vec<Vec<u8>>) -> ImportObject {
30+
unimplemented!()
31+
}
32+
}
33+
2034
#[derive(Debug, StructOpt)]
2135
#[structopt(name = "wasmer", about = "Wasm execution runtime.")]
2236
/// The options for the wasmer Command Line Interface
@@ -213,7 +227,6 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
213227
};
214228

215229
// TODO: refactor this
216-
#[cfg(not(feature = "wasi"))]
217230
let (abi, import_object, _em_globals) = if wasmer_emscripten::is_emscripten_module(&module) {
218231
let mut emscripten_globals = wasmer_emscripten::EmscriptenGlobals::new(&module);
219232
(
@@ -222,34 +235,29 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
222235
Some(emscripten_globals), // TODO Em Globals is here to extend, lifetime, find better solution
223236
)
224237
} else {
225-
(
226-
InstanceABI::None,
227-
wasmer_runtime_core::import::ImportObject::new(),
228-
None,
229-
)
230-
};
231-
232-
#[cfg(feature = "wasi")]
233-
let (abi, import_object) = if wasmer_wasi::is_wasi_module(&module) {
234-
(
235-
InstanceABI::WASI,
236-
wasmer_wasi::generate_import_object(
237-
[options.path.to_str().unwrap().to_owned()]
238-
.iter()
239-
.chain(options.args.iter())
240-
.cloned()
241-
.map(|arg| arg.into_bytes())
242-
.collect(),
243-
env::vars()
244-
.map(|(k, v)| format!("{}={}", k, v).into_bytes())
245-
.collect(),
246-
),
247-
)
248-
} else {
249-
(
250-
InstanceABI::None,
251-
wasmer_runtime_core::import::ImportObject::new(),
252-
)
238+
if cfg!(feature = "wasi") && wasmer_wasi::is_wasi_module(&module) {
239+
(
240+
InstanceABI::WASI,
241+
wasmer_wasi::generate_import_object(
242+
[options.path.to_str().unwrap().to_owned()]
243+
.iter()
244+
.chain(options.args.iter())
245+
.cloned()
246+
.map(|arg| arg.into_bytes())
247+
.collect(),
248+
env::vars()
249+
.map(|(k, v)| format!("{}={}", k, v).into_bytes())
250+
.collect(),
251+
),
252+
None,
253+
)
254+
} else {
255+
(
256+
InstanceABI::None,
257+
wasmer_runtime_core::import::ImportObject::new(),
258+
None,
259+
)
260+
}
253261
};
254262

255263
let mut instance = module

0 commit comments

Comments
 (0)