Skip to content

Commit

Permalink
Implement non-trapping grow_memory semantics (#308)
Browse files Browse the repository at this point in the history
Addresses #307. Clean up some of the memory error condition handling on the way.
  • Loading branch information
rossberg committed Aug 16, 2016
1 parent c1e7ccd commit 7498edd
Show file tree
Hide file tree
Showing 13 changed files with 1,128 additions and 43 deletions.
2 changes: 1 addition & 1 deletion ml-proto/host/arrange.ml
Expand Up @@ -272,7 +272,7 @@ let table tab =


let memory mem = let memory mem =
let {mlimits = lim} = mem.it in let {mlimits = lim} = mem.it in
Node ("memory " ^ limits int64 lim, []) Node ("memory " ^ limits int32 lim, [])


let segment head dat seg = let segment head dat seg =
let {offset; init} = seg.it in let {offset; init} = seg.it in
Expand Down
2 changes: 1 addition & 1 deletion ml-proto/host/encode.ml
Expand Up @@ -345,7 +345,7 @@ let encode m =
(* Memory section *) (* Memory section *)
let memory mem = let memory mem =
let {mlimits} = mem.it in let {mlimits} = mem.it in
limits vu64 mlimits limits vu32 mlimits


let memory_section memo = let memory_section memo =
section "memory" (opt memory) memo (memo <> None) section "memory" (opt memory) memo (memo <> None)
Expand Down
51 changes: 51 additions & 0 deletions ml-proto/host/encode.ml.rej
@@ -0,0 +1,51 @@
--- host/encode.ml
+++ host/encode.ml
@@ -105,31 +105,30 @@
let rec expr e =
match e.it with
| Nop -> op 0x00
- | Block es -> op 0x01; list expr es; op 0x17
- | Loop es -> op 0x02; list expr es; op 0x17
+ | Block es -> op 0x01; list expr es; op 0x0f
+ | Loop es -> op 0x02; list expr es; op 0x0f
| If (e, es1, es2) ->
expr e; op 0x03; list expr es1;
- if es2 <> [] then op 0x04; list expr es2; op 0x17
+ if es2 <> [] then op 0x04; list expr es2; op 0x0f
| Select (e1, e2, e3) -> expr e1; expr e2; expr e3; op 0x05
| Br (x, eo) -> opt expr eo; op 0x06; arity1 eo; var x
| Br_if (x, eo, e) -> opt expr eo; expr e; op 0x07; arity1 eo; var x
| Br_table (xs, x, eo, e) ->
opt expr eo; expr e; op 0x08; arity1 eo; vec var32 xs; var32 x
-
- | Ast.I32_const c -> op 0x0a; vs32 c.it
- | Ast.I64_const c -> op 0x0b; vs64 c.it
- | Ast.F32_const c -> op 0x0c; f32 c.it
- | Ast.F64_const c -> op 0x0d; f64 c.it
-
- | Ast.Get_local x -> op 0x0e; var x
- | Ast.Set_local (x, e) -> unary e 0x0f; var x
- | Ast.Tee_local (x, e) -> unary e 0x10; var x
-
- | Ast.Call (x, es) -> nary es 0x12; var x
- | Ast.Call_import (x, es) -> nary es 0x1f; var x
- | Ast.Call_indirect (x, e, es) -> expr e; nary es 0x13; var x
- | Ast.Return eo -> nary1 eo 0x14
- | Ast.Unreachable -> op 0x15
+ | Ast.Return eo -> nary1 eo 0x09
+ | Ast.Unreachable -> op 0x0a
+
+ | Ast.I32_const c -> op 0x10; vs32 c.it
+ | Ast.I64_const c -> op 0x11; vs64 c.it
+ | Ast.F32_const c -> op 0x12; f32 c.it
+ | Ast.F64_const c -> op 0x13; f64 c.it
+
+ | Ast.Get_local x -> op 0x14; var x
+ | Ast.Set_local (x, e) -> unary e 0x15; var x
+
+ | Ast.Call (x, es) -> nary es 0x16; var x
+ | Ast.Call_indirect (x, e, es) -> expr e; nary es 0x17; var x
+ | Ast.Call_import (x, es) -> nary es 0x18; var x

| I32_load8_s (o, a, e) -> unary e 0x20; memop o a
| I32_load8_u (o, a, e) -> unary e 0x21; memop o a
24 changes: 18 additions & 6 deletions ml-proto/host/parser.mly
Expand Up @@ -39,6 +39,18 @@ let literal f s =
| Failure msg -> error s.at ("constant out of range: " ^ msg) | Failure msg -> error s.at ("constant out of range: " ^ msg)
| _ -> error s.at "constant out of range" | _ -> error s.at "constant out of range"


let int s at =
try int_of_string s with Failure _ ->
error at "int constant out of range"

let int32 s at =
try I32.of_string s with Failure _ ->
error at "i32 constant out of range"

let int64 s at =
try I64.of_string s with Failure _ ->
error at "i64 constant out of range"



(* Symbolic variables *) (* Symbolic variables *)


Expand Down Expand Up @@ -206,7 +218,7 @@ literal :
; ;


var : var :
| NAT { let at = at () in fun c lookup -> int_of_string $1 @@ at } | NAT { let at = at () in fun c lookup -> int $1 at @@ at }
| VAR { let at = at () in fun c lookup -> lookup c ($1 @@ at) @@ at } | VAR { let at = at () in fun c lookup -> lookup c ($1 @@ at) @@ at }
; ;
var_list : var_list :
Expand Down Expand Up @@ -365,9 +377,9 @@ elem :
; ;
table_limits : table_limits :
| NAT { {min = Int32.of_string $1; max = None} @@ at () } | NAT { {min = int32 $1 (ati 1); max = None} @@ at () }
| NAT NAT | NAT NAT
{ {min = Int32.of_string $1; max = Some (Int32.of_string $2)} @@ at () } { {min = int32 $1 (ati 1); max = Some (int32 $2 (ati 2))} @@ at () }
; ;
table : table :
| LPAR TABLE table_limits elem_type RPAR | LPAR TABLE table_limits elem_type RPAR
Expand All @@ -386,17 +398,17 @@ data :
; ;
memory_limits : memory_limits :
| NAT { {min = Int64.of_string $1; max = None} @@ at () } | NAT { {min = int32 $1 (ati 1); max = None} @@ at () }
| NAT NAT | NAT NAT
{ {min = Int64.of_string $1; max = Some (Int64.of_string $2)} @@ at () } { {min = int32 $1 (ati 1); max = Some (int32 $2 (ati 2))} @@ at () }
; ;
memory : memory :
| LPAR MEMORY memory_limits RPAR | LPAR MEMORY memory_limits RPAR
{ fun c -> {mlimits = $3} @@ at (), [] } { fun c -> {mlimits = $3} @@ at (), [] }
| LPAR MEMORY LPAR DATA text_list RPAR RPAR /* Sugar */ | LPAR MEMORY LPAR DATA text_list RPAR RPAR /* Sugar */
{ let at = at () in { let at = at () in
fun c -> fun c ->
let size = Int64.(div (add (of_int (String.length $5)) 65535L) 65536L) in let size = Int32.(div (add (of_int (String.length $5)) 65535l) 65536l) in
{mlimits = {min = size; max = Some size} @@ at} @@ at, {mlimits = {min = size; max = Some size} @@ at} @@ at,
[{offset = I32_const (0l @@ at) @@ at; init = $5} @@ at] } [{offset = I32_const (0l @@ at) @@ at; init = $5} @@ at] }
; ;
Expand Down

0 comments on commit 7498edd

Please sign in to comment.