Skip to content

Commit 6d9d3c0

Browse files
author
Hongbo Zhang
committed
bound access check for string
1 parent 7bc9c01 commit 6d9d3c0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+433
-226
lines changed

jscomp/common/js_config.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ let obj_runtime = "Caml_obj"
232232
let array = "Caml_array"
233233
let format = "Caml_format"
234234
let string = "Caml_string"
235+
let bytes = "Caml_bytes"
235236
let float = "Caml_float"
236237
let hash = "Caml_hash"
237238
let oo = "Caml_oo"

jscomp/common/js_config.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ val parser : string
137137
val obj_runtime : string
138138
val array : string
139139
val format : string
140-
val string : string
140+
val string : string
141+
val bytes : string
141142
val float : string
142143
val curry : string
143144
(* val bigarray : string *)

jscomp/lam.ml

Lines changed: 72 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -337,58 +337,64 @@ let comparison (cmp : Lambda.comparison) a b : bool =
337337
| Clt -> a < b
338338
| Cge -> a >= b
339339

340-
let lift_int i : t =
341-
Lconst (Const_base (Const_int i))
340+
module Lift = struct
341+
let int i : t =
342+
Lconst (Const_base (Const_int i))
342343

343344

344-
let int32 i : t =
345-
Lconst (Const_base (Const_int32 i))
345+
let int32 i : t =
346+
Lconst (Const_base (Const_int32 i))
346347

347-
let lift_bool b = if b then true_ else false_
348+
let bool b = if b then true_ else false_
348349

349-
(* ATTENTION: [float, nativeint] constant propogaton is not done
350-
yet , due to cross platform problem
351-
*)
352-
let lift_float b : t =
353-
Lconst (Const_base (Const_float b))
350+
(* ATTENTION: [float, nativeint] constant propogaton is not done
351+
yet , due to cross platform problem
352+
*)
353+
let float b : t =
354+
Lconst (Const_base (Const_float b))
354355

355-
let lift_nativeint b : t =
356-
Lconst (Const_base (Const_nativeint b))
356+
let nativeint b : t =
357+
Lconst (Const_base (Const_nativeint b))
357358

358-
let lift_int32 b : t =
359-
Lconst (Const_base (Const_int32 b))
359+
let int32 b : t =
360+
Lconst (Const_base (Const_int32 b))
360361

361-
let lift_int64 b : t =
362-
Lconst (Const_base (Const_int64 b))
362+
let int64 b : t =
363+
Lconst (Const_base (Const_int64 b))
364+
let string b : t =
365+
Lconst (Const_base (Const_string (b, None)))
366+
let char b : t =
367+
Lconst (Const_base (Const_char b))
368+
end
363369

364370
let prim ~primitive:(prim : Prim.t) ~args:(ll : t list) : t =
365371
let default () : t = Lprim { primitive = prim ;args = ll } in
366372
match ll with
367373
| [Lconst a] ->
368374
begin match prim, a with
369375
| Pnegint, (Const_base (Const_int a))
370-
-> lift_int (- a)
376+
-> Lift.int (- a)
371377
(* | Pfloatofint, (Const_base (Const_int a)) *)
372-
(* -> lift_float (float_of_int a) *)
378+
(* -> Lift.float (float_of_int a) *)
373379
| Pintoffloat, (Const_base (Const_float a))
374380
->
375-
lift_int (int_of_float (float_of_string a))
376-
(* | Pnegfloat -> lift_float (-. a) *)
377-
(* | Pabsfloat -> lift_float (abs_float a) *)
381+
Lift.int (int_of_float (float_of_string a))
382+
(* | Pnegfloat -> Lift.float (-. a) *)
383+
(* | Pabsfloat -> Lift.float (abs_float a) *)
378384
| Pstringlength, (Const_base (Const_string (a,_)) )
379385
->
380-
lift_int (String.length a)
386+
Lift.int (String.length a)
381387
(* | Pnegbint Pnativeint, (Const_base (Const_nativeint i)) *)
382388
(* -> *)
383-
(* lift_nativeint (Nativeint.neg i) *)
389+
(* Lift.nativeint (Nativeint.neg i) *)
384390
| Pnegbint Pint32, (Const_base (Const_int32 a))
385391
->
386-
lift_int32 (Int32.neg a)
392+
Lift.int32 (Int32.neg a)
387393
| Pnegbint Pint64, (Const_base (Const_int64 a))
388394
->
389-
lift_int64 (Int64.neg a)
395+
Lift.int64 (Int64.neg a)
390396
| Pnot , Const_pointer (a,_)
391-
-> lift_bool (a = 0 )
397+
-> Lift.bool (a = 0 )
392398

393399
| _ -> default ()
394400
end
@@ -397,15 +403,15 @@ let prim ~primitive:(prim : Prim.t) ~args:(ll : t list) : t =
397403
| [Lconst a ; Lconst b] ->
398404
begin match prim, a, b with
399405
| Pbintcomp(_, cmp), Const_base (Const_int32 a), Const_base (Const_int32 b)
400-
-> lift_bool (comparison cmp a b)
406+
-> Lift.bool (comparison cmp a b)
401407
| Pbintcomp(_, cmp), Const_base (Const_int64 a), Const_base (Const_int64 b)
402-
-> lift_bool (comparison cmp a b)
408+
-> Lift.bool (comparison cmp a b)
403409
| Pbintcomp(_, cmp), Const_base (Const_nativeint a), Const_base (Const_nativeint b)
404-
-> lift_bool (comparison cmp a b)
410+
-> Lift.bool (comparison cmp a b)
405411
| Pfloatcomp cmp, Const_base (Const_nativeint a), Const_base (Const_nativeint b)
406-
-> lift_bool (comparison cmp a b)
412+
-> Lift.bool (comparison cmp a b)
407413
| Pintcomp cmp , Const_base (Const_int a), Const_base (Const_int b)
408-
-> lift_bool (comparison cmp a b)
414+
-> Lift.bool (comparison cmp a b)
409415

410416
| (Paddint
411417
| Psubint
@@ -421,7 +427,7 @@ let prim ~primitive:(prim : Prim.t) ~args:(ll : t list) : t =
421427
->
422428
(* WE SHOULD keep it as [int], to preserve types *)
423429
let aa,bb = Int32.of_int a, Int32.of_int b in
424-
let int_ v = lift_int (Int32.to_int v ) in
430+
let int_ v = Lift.int (Int32.to_int v ) in
425431
begin match prim with
426432
| Paddint -> int_ (Int32.add aa bb)
427433
| Psubint -> int_ (Int32.sub aa bb)
@@ -451,22 +457,22 @@ let prim ~primitive:(prim : Prim.t) ~args:(ll : t list) : t =
451457
), Const_base (Const_int32 aa), Const_base (Const_int32 bb)
452458
->
453459
begin match prim with
454-
| Paddbint _ -> lift_int32 (Int32.add aa bb)
455-
| Psubbint _ -> lift_int32 (Int32.sub aa bb)
456-
| Pmulbint _ -> lift_int32 (Int32.mul aa bb)
457-
| Pdivbint _ -> (try lift_int32 (Int32.div aa bb) with _ -> default ())
458-
| Pmodbint _ -> (try lift_int32 (Int32.rem aa bb) with _ -> default ())
459-
| Pandbint _ -> lift_int32 (Int32.logand aa bb)
460-
| Porbint _ -> lift_int32 (Int32.logor aa bb)
461-
| Pxorbint _ -> lift_int32 (Int32.logxor aa bb)
460+
| Paddbint _ -> Lift.int32 (Int32.add aa bb)
461+
| Psubbint _ -> Lift.int32 (Int32.sub aa bb)
462+
| Pmulbint _ -> Lift.int32 (Int32.mul aa bb)
463+
| Pdivbint _ -> (try Lift.int32 (Int32.div aa bb) with _ -> default ())
464+
| Pmodbint _ -> (try Lift.int32 (Int32.rem aa bb) with _ -> default ())
465+
| Pandbint _ -> Lift.int32 (Int32.logand aa bb)
466+
| Porbint _ -> Lift.int32 (Int32.logor aa bb)
467+
| Pxorbint _ -> Lift.int32 (Int32.logxor aa bb)
462468
| _ -> default ()
463469
end
464470
| Plslbint Pint32, Const_base (Const_int32 aa), Const_base (Const_int b)
465-
-> lift_int32 (Int32.shift_left aa b )
471+
-> Lift.int32 (Int32.shift_left aa b )
466472
| Plsrbint Pint32, Const_base (Const_int32 aa), Const_base (Const_int b)
467-
-> lift_int32 (Int32.shift_right_logical aa b )
473+
-> Lift.int32 (Int32.shift_right_logical aa b )
468474
| Pasrbint Pint32, Const_base (Const_int32 aa), Const_base (Const_int b)
469-
-> lift_int32 (Int32.shift_right aa b )
475+
-> Lift.int32 (Int32.shift_right aa b )
470476

471477
| (Paddbint Pint64
472478
| Psubbint Pint64
@@ -479,28 +485,38 @@ let prim ~primitive:(prim : Prim.t) ~args:(ll : t list) : t =
479485
), Const_base (Const_int64 aa), Const_base (Const_int64 bb)
480486
->
481487
begin match prim with
482-
| Paddbint _ -> lift_int64 (Int64.add aa bb)
483-
| Psubbint _ -> lift_int64 (Int64.sub aa bb)
484-
| Pmulbint _ -> lift_int64 (Int64.mul aa bb)
485-
| Pdivbint _ -> (try lift_int64 (Int64.div aa bb) with _ -> default ())
486-
| Pmodbint _ -> (try lift_int64 (Int64.rem aa bb) with _ -> default ())
487-
| Pandbint _ -> lift_int64 (Int64.logand aa bb)
488-
| Porbint _ -> lift_int64 (Int64.logor aa bb)
489-
| Pxorbint _ -> lift_int64 (Int64.logxor aa bb)
488+
| Paddbint _ -> Lift.int64 (Int64.add aa bb)
489+
| Psubbint _ -> Lift.int64 (Int64.sub aa bb)
490+
| Pmulbint _ -> Lift.int64 (Int64.mul aa bb)
491+
| Pdivbint _ -> (try Lift.int64 (Int64.div aa bb) with _ -> default ())
492+
| Pmodbint _ -> (try Lift.int64 (Int64.rem aa bb) with _ -> default ())
493+
| Pandbint _ -> Lift.int64 (Int64.logand aa bb)
494+
| Porbint _ -> Lift.int64 (Int64.logor aa bb)
495+
| Pxorbint _ -> Lift.int64 (Int64.logxor aa bb)
490496
| _ -> default ()
491497
end
492498
| Plslbint Pint64, Const_base (Const_int64 aa), Const_base (Const_int b)
493-
-> lift_int64 (Int64.shift_left aa b )
499+
-> Lift.int64 (Int64.shift_left aa b )
494500
| Plsrbint Pint64, Const_base (Const_int64 aa), Const_base (Const_int b)
495-
-> lift_int64 (Int64.shift_right_logical aa b )
501+
-> Lift.int64 (Int64.shift_right_logical aa b )
496502
| Pasrbint Pint64, Const_base (Const_int64 aa), Const_base (Const_int b)
497-
-> lift_int64 (Int64.shift_right aa b )
503+
-> Lift.int64 (Int64.shift_right aa b )
498504
| Psequand, Const_pointer (a, _), Const_pointer( b, _)
499505
->
500-
lift_bool (a = 1 && b = 1)
506+
Lift.bool (a = 1 && b = 1)
501507
| Psequor, Const_pointer (a, _), Const_pointer( b, _)
502508
->
503-
lift_bool (a = 1 || b = 1)
509+
Lift.bool (a = 1 || b = 1)
510+
| Pstringadd, Const_base(Const_string (a, None)),
511+
Const_base (Const_string (b,None))
512+
->
513+
Lift.string (a ^ b)
514+
| (Pstringrefs | Pstringrefu), Const_base(Const_string(a,None)),
515+
(Const_base(Const_int b)| Const_pointer (b,_))
516+
->
517+
begin try Lift.char (String.get a b)
518+
with _ -> default ()
519+
end
504520
| _ -> default ()
505521
end
506522

jscomp/lam_compile_primitive.ml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,24 +443,41 @@ let translate
443443

444444
| _ -> assert false
445445
end
446-
| Pbytesrefu
447-
| Pbytesrefs ->
446+
| Pbytesrefu ->
448447
begin match args with
449448
| [e;e1] -> Js_of_lam_string.ref_byte e e1
450449
| _ -> assert false
451450
end
452-
451+
452+
| Pbytesrefs ->
453+
begin match args with
454+
| [e ; e1] ->
455+
if !Clflags.fast then
456+
Js_of_lam_string.ref_byte e e1
457+
else E.runtime_call Js_config.bytes "get" args
458+
| _ -> assert false
459+
end
453460
(* For bytes and string, they both return [int] in ocaml
454461
we need tell Pbyteref from Pstringref
455462
1. Pbyteref -> a[i]
456463
2. Pstringref -> a.charCodeAt (a[i] is wrong)
457464
*)
458-
| Pstringrefu
459-
| Pstringrefs ->
465+
| Pstringrefu ->
460466
begin match args with
461467
| [e;e1] -> Js_of_lam_string.ref_string e e1
462468
| _ -> assert false
463469
end
470+
471+
| Pstringrefs ->
472+
begin match args with
473+
| [e;e1] ->
474+
if !Clflags.fast then
475+
Js_of_lam_string.ref_string e e1
476+
else
477+
E.runtime_call Js_config.string "get" args
478+
| _ -> assert false
479+
end
480+
464481
| Pgetglobal i ->
465482
(* TODO -- check args, case by case --
466483
1. include Array --> let include = Array

jscomp/runtime/.runtimedepend

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ caml_array.cmi :
33
caml_backtrace.cmi :
44
caml_basic.cmi : js.cmj
55
caml_builtin_exceptions.cmi :
6+
caml_bytes.cmi :
67
caml_exceptions.cmi : caml_builtin_exceptions.cmi
78
caml_float.cmi :
89
caml_format.cmi :
@@ -37,6 +38,8 @@ caml_basic.cmj : js.cmj caml_basic.cmi
3738
caml_basic.cmx : js.cmx caml_basic.cmi
3839
caml_builtin_exceptions.cmj : caml_builtin_exceptions.cmi
3940
caml_builtin_exceptions.cmx : caml_builtin_exceptions.cmi
41+
caml_bytes.cmj : caml_bytes.cmi
42+
caml_bytes.cmx : caml_bytes.cmi
4043
caml_exceptions.cmj : caml_builtin_exceptions.cmi caml_exceptions.cmi
4144
caml_exceptions.cmx : caml_builtin_exceptions.cmx caml_exceptions.cmi
4245
caml_float.cmj : typed_array.cmj js_float.cmj caml_float.cmi
@@ -119,6 +122,8 @@ caml_basic.cmo : js.cmo caml_basic.cmi
119122
caml_basic.cmj : js.cmj caml_basic.cmi
120123
caml_builtin_exceptions.cmo : caml_builtin_exceptions.cmi
121124
caml_builtin_exceptions.cmj : caml_builtin_exceptions.cmi
125+
caml_bytes.cmo : caml_bytes.cmi
126+
caml_bytes.cmj : caml_bytes.cmi
122127
caml_exceptions.cmo : caml_builtin_exceptions.cmi caml_exceptions.cmi
123128
caml_exceptions.cmj : caml_builtin_exceptions.cmj caml_exceptions.cmi
124129
caml_float.cmo : typed_array.cmo js_float.cmo caml_float.cmi

jscomp/runtime/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ include ../Makefile.shared
22

33
COMPILER=../bin/bsc
44

5-
OTHERS= caml_array caml_string \
5+
OTHERS= caml_array caml_string caml_bytes\
66
caml_obj caml_int64 \
77
caml_exceptions caml_utils caml_sys caml_io\
88
caml_float caml_lexer caml_parser caml_primitive\

jscomp/runtime/caml_bytes.ml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
let get s i =
26+
if i < 0 || i >= Bytes.length s then
27+
raise (Invalid_argument "index out of bounds")
28+
else Bytes.unsafe_get s i

jscomp/runtime/caml_bytes.mli

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
val get : bytes -> int -> char

0 commit comments

Comments
 (0)