Skip to content

Commit

Permalink
fix(compiler): Panic immediately when out of memory (#1450)
Browse files Browse the repository at this point in the history
  • Loading branch information
ospencer committed Oct 27, 2022
1 parent a5ff379 commit 943d47d
Show file tree
Hide file tree
Showing 20 changed files with 206 additions and 253 deletions.
39 changes: 17 additions & 22 deletions compiler/src/codegen/compcore.re
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ let decref_closure_ident = Ident.create_persistent("GRAIN$EXPORT$decRef");

/* Exceptions */
let exception_mod = "GRAIN$MODULE$runtime/exception";
let print_exception_ident = Ident.create_persistent("printException");
let print_exception_closure_ident =
Ident.create_persistent("GRAIN$EXPORT$printException");
let panic_with_exception_ident =
Ident.create_persistent("panicWithException");
let panic_with_exception_closure_ident =
Ident.create_persistent("GRAIN$EXPORT$panicWithException");
let assertion_error_ident = Ident.create_persistent("AssertionError");
let assertion_error_closure_ident =
Ident.create_persistent("GRAIN$EXPORT$AssertionError");
Expand Down Expand Up @@ -109,9 +110,9 @@ let required_global_imports = [
mimp_used: false,
},
{
mimp_id: print_exception_closure_ident,
mimp_id: panic_with_exception_closure_ident,
mimp_mod: exception_mod,
mimp_name: Ident.name(print_exception_closure_ident),
mimp_name: Ident.name(panic_with_exception_closure_ident),
mimp_type: MGlobalImport(Types.Unmanaged(WasmI32), true),
mimp_kind: MImportWasm,
mimp_setup: MSetupNone,
Expand Down Expand Up @@ -190,9 +191,9 @@ let runtime_global_imports =

let required_function_imports = [
{
mimp_id: print_exception_ident,
mimp_id: panic_with_exception_ident,
mimp_mod: exception_mod,
mimp_name: Ident.name(print_exception_ident),
mimp_name: Ident.name(panic_with_exception_ident),
mimp_type:
MFuncImport(
[Types.Unmanaged(WasmI32), Types.Unmanaged(WasmI32)],
Expand Down Expand Up @@ -346,18 +347,21 @@ let get_wasm_imported_name = (~runtime_import=true, mod_, name) => {

let get_grain_imported_name = (mod_, name) => Ident.unique_name(name);

let call_exception_printer = (wasm_mod, env, args) => {
let call_panic_handler = (wasm_mod, env, args) => {
let args = [
Expression.Global_get.make(
wasm_mod,
get_wasm_imported_name(exception_mod, print_exception_closure_ident),
get_wasm_imported_name(
exception_mod,
panic_with_exception_closure_ident,
),
Type.int32,
),
...args,
];
Expression.Call.make(
wasm_mod,
get_wasm_imported_name(exception_mod, print_exception_ident),
get_wasm_imported_name(exception_mod, panic_with_exception_ident),
args,
Type.int32,
);
Expand Down Expand Up @@ -1028,17 +1032,7 @@ let call_error_handler = (wasm_mod, env, err, args) => {
Type.int32,
);
};
Expression.Block.make(
wasm_mod,
gensym_label("call_error_handler"),
[
Expression.Drop.make(
wasm_mod,
call_exception_printer(wasm_mod, env, [err]),
),
Expression.Unreachable.make(wasm_mod),
],
);
call_panic_handler(wasm_mod, env, [err]);
};

let error_if_true = (wasm_mod, env, cond, err, args) =>
Expand Down Expand Up @@ -2367,6 +2361,7 @@ let compile_prim0 = (wasm_mod, env, p0): Expression.t => {
allocate_number_uninitialized(wasm_mod, env, BoxedFloat64)
| AllocateRational =>
allocate_number_uninitialized(wasm_mod, env, BoxedRational)
| Unreachable => Expression.Unreachable.make(wasm_mod)
};
};

Expand Down Expand Up @@ -2486,7 +2481,7 @@ let compile_prim1 = (wasm_mod, env, p1, arg, loc): Expression.t => {
[
Expression.Drop.make(
wasm_mod,
call_exception_printer(wasm_mod, env, [compiled_arg]),
call_panic_handler(wasm_mod, env, [compiled_arg]),
),
Expression.Unreachable.make(wasm_mod),
],
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/codegen/mashtree.re
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ type prim0 =
| AllocateInt64
| AllocateFloat32
| AllocateFloat64
| AllocateRational;
| AllocateRational
| Unreachable;

type prim1 =
Parsetree.prim1 =
Expand Down
1 change: 1 addition & 0 deletions compiler/src/middle_end/analyze_purity.re
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ let rec analyze_comp_expression =
AllocateRational,
) =>
true
| CPrim0(Unreachable) => false
| CPrim1(
AllocateArray | AllocateTuple | AllocateBytes | AllocateString |
NewInt32 |
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/middle_end/anftree.re
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ type prim0 =
| AllocateInt64
| AllocateFloat32
| AllocateFloat64
| AllocateRational;
| AllocateRational
| Unreachable;

type prim1 =
Parsetree.prim1 =
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/middle_end/anftree.rei
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ type prim0 =
| AllocateInt64
| AllocateFloat32
| AllocateFloat64
| AllocateRational;
| AllocateRational
| Unreachable;

type prim1 =
Parsetree.prim1 =
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/parsing/parsetree.re
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ type prim0 =
| AllocateInt64
| AllocateFloat32
| AllocateFloat64
| AllocateRational;
| AllocateRational
| Unreachable;

/** Single-argument operators */
[@deriving (sexp, yojson)]
Expand Down
4 changes: 3 additions & 1 deletion compiler/src/typed/translprim.re
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ let prim_map =
("@ignore", Primitive1(Ignore)),
("@assert", Primitive1(Assert)),
("@throw", Primitive1(Throw)),
("@unreachable", Primitive0(Unreachable)),
("@is", Primitive2(Is)),
("@eq", Primitive2(Eq)),
("@and", Primitive2(And)),
Expand Down Expand Up @@ -1507,7 +1508,8 @@ let transl_prim = (env, desc) => {
| Primitive0(
(
AllocateInt32 | AllocateInt64 | AllocateFloat32 | AllocateFloat64 |
AllocateRational
AllocateRational |
Unreachable
) as p,
) => (
Exp.lambda(~loc, ~attributes=disable_gc, [], Exp.prim0(~loc, p)),
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/typed/typecore.re
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ let prim0_type =
| AllocateInt64
| AllocateFloat32
| AllocateFloat64
| AllocateRational => Builtin_types.type_wasmi32;
| AllocateRational => Builtin_types.type_wasmi32
| Unreachable => newvar(~name="a", ());

let prim1_type =
fun
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/typed/typedtree.re
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ type prim0 =
| AllocateInt64
| AllocateFloat32
| AllocateFloat64
| AllocateRational;
| AllocateRational
| Unreachable;

type prim1 =
Parsetree.prim1 =
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/typed/typedtree.rei
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ type prim0 =
| AllocateInt64
| AllocateFloat32
| AllocateFloat64
| AllocateRational;
| AllocateRational
| Unreachable;

type prim1 =
Parsetree.prim1 =
Expand Down
32 changes: 11 additions & 21 deletions compiler/test/__snapshots__/arrays.0f9e7d37.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ arrays › array_access
(import \"_grainEnv\" \"tbl\" (table $tbl 0 funcref))
(import \"_grainEnv\" \"relocBase\" (global $relocBase_0 i32))
(import \"_grainEnv\" \"moduleRuntimeId\" (global $moduleRuntimeId_0 i32))
(import \"GRAIN$MODULE$runtime/exception\" \"GRAIN$EXPORT$printException\" (global $GRAIN$EXPORT$printException_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/exception\" \"GRAIN$EXPORT$panicWithException\" (global $GRAIN$EXPORT$panicWithException_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/exception\" \"GRAIN$EXPORT$IndexOutOfBounds\" (global $GRAIN$EXPORT$IndexOutOfBounds_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$malloc\" (global $GRAIN$EXPORT$malloc_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$incRef\" (global $GRAIN$EXPORT$incRef_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$decRef\" (global $GRAIN$EXPORT$decRef_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"malloc\" (func $malloc_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $incRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"decRef\" (func $decRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/exception\" \"printException\" (func $printException_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/exception\" \"panicWithException\" (func $panicWithException_0 (param i32 i32) (result i32)))
(global $x_1131 (mut i32) (i32.const 0))
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
(elem $elem (global.get $relocBase_0))
Expand All @@ -31,9 +31,9 @@ arrays › array_access
(local $4 f32)
(local $5 f64)
(return
(block $cleanup_locals.8 (result i32)
(block $cleanup_locals.6 (result i32)
(local.set $0
(block $compile_block.7 (result i32)
(block $compile_block.5 (result i32)
(block $compile_store.3
(global.set $x_1131
(tuple.extract 0
Expand Down Expand Up @@ -76,7 +76,7 @@ arrays › array_access
(block $do_backpatches.2
)
)
(block $MArrayGet.6 (result i32)
(block $MArrayGet.4 (result i32)
(local.set $1
(i32.shr_s
(i32.const 1)
Expand All @@ -97,14 +97,9 @@ arrays › array_access
(local.get $1)
)
(drop
(block $call_error_handler.5
(drop
(call $printException_0
(global.get $GRAIN$EXPORT$printException_0)
(global.get $GRAIN$EXPORT$IndexOutOfBounds_0)
)
)
(unreachable)
(call $panicWithException_0
(global.get $GRAIN$EXPORT$panicWithException_0)
(global.get $GRAIN$EXPORT$IndexOutOfBounds_0)
)
)
)
Expand All @@ -116,14 +111,9 @@ arrays › array_access
(local.get $1)
)
(drop
(block $call_error_handler.4
(drop
(call $printException_0
(global.get $GRAIN$EXPORT$printException_0)
(global.get $GRAIN$EXPORT$IndexOutOfBounds_0)
)
)
(unreachable)
(call $panicWithException_0
(global.get $GRAIN$EXPORT$panicWithException_0)
(global.get $GRAIN$EXPORT$IndexOutOfBounds_0)
)
)
)
Expand Down
32 changes: 11 additions & 21 deletions compiler/test/__snapshots__/arrays.28fcc534.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ arrays › array_access4
(import \"_grainEnv\" \"tbl\" (table $tbl 0 funcref))
(import \"_grainEnv\" \"relocBase\" (global $relocBase_0 i32))
(import \"_grainEnv\" \"moduleRuntimeId\" (global $moduleRuntimeId_0 i32))
(import \"GRAIN$MODULE$runtime/exception\" \"GRAIN$EXPORT$printException\" (global $GRAIN$EXPORT$printException_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/exception\" \"GRAIN$EXPORT$panicWithException\" (global $GRAIN$EXPORT$panicWithException_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/exception\" \"GRAIN$EXPORT$IndexOutOfBounds\" (global $GRAIN$EXPORT$IndexOutOfBounds_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$malloc\" (global $GRAIN$EXPORT$malloc_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$incRef\" (global $GRAIN$EXPORT$incRef_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$decRef\" (global $GRAIN$EXPORT$decRef_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"malloc\" (func $malloc_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"incRef\" (func $incRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"decRef\" (func $decRef_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/exception\" \"printException\" (func $printException_0 (param i32 i32) (result i32)))
(import \"GRAIN$MODULE$runtime/exception\" \"panicWithException\" (func $panicWithException_0 (param i32 i32) (result i32)))
(global $x_1131 (mut i32) (i32.const 0))
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
(elem $elem (global.get $relocBase_0))
Expand All @@ -31,9 +31,9 @@ arrays › array_access4
(local $4 f32)
(local $5 f64)
(return
(block $cleanup_locals.8 (result i32)
(block $cleanup_locals.6 (result i32)
(local.set $0
(block $compile_block.7 (result i32)
(block $compile_block.5 (result i32)
(block $compile_store.3
(global.set $x_1131
(tuple.extract 0
Expand Down Expand Up @@ -76,7 +76,7 @@ arrays › array_access4
(block $do_backpatches.2
)
)
(block $MArrayGet.6 (result i32)
(block $MArrayGet.4 (result i32)
(local.set $1
(i32.shr_s
(i32.const -3)
Expand All @@ -97,14 +97,9 @@ arrays › array_access4
(local.get $1)
)
(drop
(block $call_error_handler.5
(drop
(call $printException_0
(global.get $GRAIN$EXPORT$printException_0)
(global.get $GRAIN$EXPORT$IndexOutOfBounds_0)
)
)
(unreachable)
(call $panicWithException_0
(global.get $GRAIN$EXPORT$panicWithException_0)
(global.get $GRAIN$EXPORT$IndexOutOfBounds_0)
)
)
)
Expand All @@ -116,14 +111,9 @@ arrays › array_access4
(local.get $1)
)
(drop
(block $call_error_handler.4
(drop
(call $printException_0
(global.get $GRAIN$EXPORT$printException_0)
(global.get $GRAIN$EXPORT$IndexOutOfBounds_0)
)
)
(unreachable)
(call $panicWithException_0
(global.get $GRAIN$EXPORT$panicWithException_0)
(global.get $GRAIN$EXPORT$IndexOutOfBounds_0)
)
)
)
Expand Down
Loading

0 comments on commit 943d47d

Please sign in to comment.