Skip to content

Commit f2ab415

Browse files
authored
[interpreter] Use explicit bounds checks instead Invalid_argument (#1659)
1 parent e19508a commit f2ab415

File tree

7 files changed

+31
-13
lines changed

7 files changed

+31
-13
lines changed

interpreter/exec/eval_num.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,3 @@ let eval_binop = op I32Op.binop I64Op.binop F32Op.binop F64Op.binop
195195
let eval_testop = op I32Op.testop I64Op.testop F32Op.testop F64Op.testop
196196
let eval_relop = op I32Op.relop I64Op.relop F32Op.relop F64Op.relop
197197
let eval_cvtop = op I32CvtOp.cvtop I64CvtOp.cvtop F32CvtOp.cvtop F64CvtOp.cvtop
198-

interpreter/exec/v128.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ let to_hex_string s =
477477

478478
let of_strings shape ss =
479479
if List.length ss <> num_lanes shape then
480-
raise (Invalid_argument "wrong length");
480+
invalid_arg "wrong length";
481481
let open Bytes in
482482
let b = create bytewidth in
483483
(match shape with

interpreter/runtime/data.ml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
type data = string ref
22
type t = data
33

4+
exception Bounds
5+
46
let alloc bs = ref bs
7+
58
let size seg = I64.of_int_u (String.length !seg)
6-
let load seg i = (!seg).[Int64.to_int i]
9+
10+
let load seg i =
11+
let i' = Int64.to_int i in
12+
if i' < 0 || i' >= String.length !seg then raise Bounds;
13+
!seg.[i']
14+
715
let drop seg = seg := ""

interpreter/runtime/elem.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
type elem = Values.ref_ list ref
22
type t = elem
33

4+
exception Bounds
5+
46
let alloc rs = ref rs
57
let size seg = Lib.List32.length !seg
6-
let load seg i = Lib.List32.nth !seg i
8+
9+
let load seg i =
10+
if i < 0l || i >= Lib.List32.length !seg then raise Bounds;
11+
Lib.List32.nth !seg i
12+
713
let drop seg = seg := []

interpreter/runtime/memory.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ let grow mem delta =
6262
mem.content <- after
6363

6464
let load_byte mem a =
65-
try Array1_64.get mem.content a with Invalid_argument _ -> raise Bounds
65+
if a < 0L || a >= Array1_64.dim mem.content then raise Bounds;
66+
Array1_64.get mem.content a
6667

6768
let store_byte mem a b =
68-
try Array1_64.set mem.content a b with Invalid_argument _ -> raise Bounds
69+
if a < 0L || a >= Array1_64.dim mem.content then raise Bounds;
70+
Array1_64.set mem.content a b
6971

7072
let load_bytes mem a n =
7173
let buf = Buffer.create n in

interpreter/runtime/table.ml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,17 @@ let grow tab delta r =
4747
tab.content <- after
4848

4949
let load tab i =
50-
try Lib.Array32.get tab.content i with Invalid_argument _ -> raise Bounds
50+
if i < 0l || i >= Lib.Array32.length tab.content then raise Bounds;
51+
Lib.Array32.get tab.content i
5152

5253
let store tab i r =
5354
let TableType (lim, t) = tab.ty in
5455
if type_of_ref r <> t then raise Type;
55-
try Lib.Array32.set tab.content i r with Invalid_argument _ -> raise Bounds
56+
if i < 0l || i >= Lib.Array32.length tab.content then raise Bounds;
57+
Lib.Array32.set tab.content i r
5658

5759
let blit tab offset rs =
5860
let data = Array.of_list rs in
59-
try Lib.Array32.blit data 0l tab.content offset (Lib.Array32.length data)
60-
with Invalid_argument _ -> raise Bounds
61+
let len = Lib.Array32.length data in
62+
if offset < 0l || offset > Int32.sub (Lib.Array32.length tab.content) len then raise Bounds;
63+
Lib.Array32.blit data 0l tab.content offset len

interpreter/util/lib.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ module Array32 =
156156
struct
157157
let make n x =
158158
if n < 0l || Int64.of_int32 n > Int64.of_int max_int then
159-
raise (Invalid_argument "Array32.make");
159+
invalid_arg "Array32.make";
160160
Array.make (Int32.to_int n) x
161161

162162
let length a = Int32.of_int (Array.length a)
@@ -179,7 +179,7 @@ struct
179179
struct
180180
let create kind layout n =
181181
if n < 0L || n > Int64.of_int max_int then
182-
raise (Invalid_argument "Bigarray.Array1_64.create");
182+
invalid_arg "Bigarray.Array1_64.create";
183183
Array1.create kind layout (Int64.to_int n)
184184

185185
let dim a = Int64.of_int (Array1.dim a)
@@ -204,7 +204,7 @@ struct
204204
let force o =
205205
match o with
206206
| Some y -> y
207-
| None -> raise (Invalid_argument "Option.force")
207+
| None -> invalid_arg "Option.force"
208208

209209
let map f = function
210210
| Some x -> Some (f x)

0 commit comments

Comments
 (0)