Skip to content

Commit 9167d05

Browse files
committed
Make minimal_wasm_module run as actual wasi module
1 parent ff11156 commit 9167d05

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
perf.data
1616
perf.data.old
1717
*.mm_profdata
18+
*.wasm

minimal_wasm_module.rs

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,9 @@ unsafe impl Sync for [u8; 16] {}
6464
#[lang = "freeze"]
6565
unsafe auto trait Freeze {}
6666

67-
unsafe impl<T: ?Sized> Freeze for *const T {}
68-
unsafe impl<T: ?Sized> Freeze for *mut T {}
69-
unsafe impl<T: ?Sized> Freeze for &T {}
70-
unsafe impl<T: ?Sized> Freeze for &mut T {}
71-
7267
#[lang = "structural_peq"]
7368
pub trait StructuralPartialEq {}
7469

75-
#[lang = "structural_teq"]
76-
pub trait StructuralEq {}
77-
7870
#[lang = "not"]
7971
pub trait Not {
8072
type Output;
@@ -185,7 +177,7 @@ pub mod intrinsics {
185177
pub fn min_align_of_val<T: ?::Sized>(val: *const T) -> usize;
186178
pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
187179
pub fn transmute<T, U>(e: T) -> U;
188-
pub fn ctlz_nonzero<T>(x: T) -> T;
180+
pub fn ctlz_nonzero<T>(x: T) -> u32;
189181
#[rustc_safe_intrinsic]
190182
pub fn needs_drop<T: ?::Sized>() -> bool;
191183
#[rustc_safe_intrinsic]
@@ -274,24 +266,67 @@ extern "C" {
274266

275267
#[lang = "panic"]
276268
#[track_caller]
277-
pub fn panic(_msg: &'static str) -> ! {
278-
let ciovec = Ciovec { buf: "Panicking\n" as *const str as *const u8, buf_len: 10 };
269+
pub fn panic(msg: &'static str) -> ! {
270+
let ciovec = Ciovec { buf: "Panicking at " as *const str as *const u8, buf_len: 13 };
279271
unsafe {
280272
fd_write(2, &ciovec, 1, &mut 0);
281273
}
282274
let caller = intrinsics::caller_location();
283275
let ciovec =
284-
Ciovec { buf: caller.file as *const str as *const u8, buf_len: 22 /* FIXME */ };
276+
Ciovec { buf: caller.file as *const str as *const u8, buf_len: 24 /* FIXME */ };
277+
unsafe {
278+
fd_write(2, &ciovec, 1, &mut 0);
279+
}
280+
let ciovec = Ciovec { buf: ": \n" as *const str as *const u8, buf_len: 3 };
281+
unsafe {
282+
fd_write(2, &ciovec, 1, &mut 0);
283+
}
284+
let ciovec = Ciovec { buf: msg as *const str as *const u8, buf_len: 4 /* FIXME */ };
285285
unsafe {
286286
fd_write(2, &ciovec, 1, &mut 0);
287287
}
288-
let ciovec = Ciovec { buf: "\n" as *const str as *const u8, buf_len: 1 /* FIXME */ };
288+
let ciovec = Ciovec { buf: "\n" as *const str as *const u8, buf_len: 1 };
289289
unsafe {
290290
fd_write(2, &ciovec, 1, &mut 0);
291291
}
292292
intrinsics::abort();
293293
}
294294

295+
macro_rules! panic_const {
296+
($($lang:ident = $message:expr,)+) => {
297+
pub mod panic_const {
298+
use super::*;
299+
300+
$(
301+
#[track_caller]
302+
#[lang = stringify!($lang)]
303+
pub fn $lang() -> ! {
304+
panic($message);
305+
}
306+
)+
307+
}
308+
}
309+
}
310+
311+
panic_const! {
312+
panic_const_add_overflow = "attempt to add with overflow",
313+
panic_const_sub_overflow = "attempt to subtract with overflow",
314+
panic_const_mul_overflow = "attempt to multiply with overflow",
315+
panic_const_div_overflow = "attempt to divide with overflow",
316+
panic_const_rem_overflow = "attempt to calculate the remainder with overflow",
317+
panic_const_neg_overflow = "attempt to negate with overflow",
318+
panic_const_shr_overflow = "attempt to shift right with overflow",
319+
panic_const_shl_overflow = "attempt to shift left with overflow",
320+
panic_const_div_by_zero = "attempt to divide by zero",
321+
panic_const_rem_by_zero = "attempt to calculate the remainder with a divisor of zero",
322+
}
323+
324+
#[rustc_builtin_macro]
325+
#[rustc_macro_transparency = "semitransparent"]
326+
pub macro stringify($($t:tt)*) {
327+
/* compiler built-in */
328+
}
329+
295330
#[lang = "panic_location"]
296331
struct Location<'a> {
297332
file: &'a str,
@@ -300,7 +335,7 @@ struct Location<'a> {
300335
}
301336

302337
#[no_mangle]
303-
fn main(foo: u32) -> u32 {
338+
fn _start() {
304339
let ciovec = Ciovec { buf: "foo\n" as *const str as *const u8, buf_len: 4 };
305340
unsafe {
306341
fd_write(2, &ciovec, 1, &mut 0);
@@ -318,10 +353,12 @@ fn main(foo: u32) -> u32 {
318353
}
319354

320355
if argc != 2 {
321-
intrinsics::abort();
356+
//intrinsics::abort();
322357
}
323358

324-
add_1(foo) + add_1_and_2(foo).1 + argc
359+
if add_1(argc) + add_1_and_2(argc).1 + argc != 9 {
360+
panic("oops");
361+
}
325362
}
326363

327364
fn add_1(foo: u32) -> u32 {

src/driver/wasm.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ impl WasmModule {
175175
value: Some(0x1_000_000),
176176
mutable: true,
177177
});
178+
waffle_module.exports.push(waffle::Export {
179+
name: "__stack_pointer".to_owned(),
180+
kind: waffle::ExportKind::Global(stack_pointer),
181+
});
178182
WasmModule {
179183
waffle_module,
180184
declarations: ModuleDeclarations::default(),

0 commit comments

Comments
 (0)