Skip to content

Commit

Permalink
Ensure allocations are initialised, even dead ones
Browse files Browse the repository at this point in the history
(See #405)
  • Loading branch information
stedolan committed Nov 30, 2021
1 parent 6b6ec5a commit ce62e45
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
3 changes: 2 additions & 1 deletion asmcomp/comballoc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ let rec combine i allocstate =
i.arg i.res i.dbg next, allocstate)
end
| Iop(Icall_ind | Icall_imm _ | Iextcall _ |
Itailcall_ind | Itailcall_imm _) ->
Itailcall_ind | Itailcall_imm _ |
Iintop Icheckbound | Iintop_imm (Icheckbound, _)) ->
let newnext = combine_restart i.next in
(instr_cons_debug i.desc i.arg i.res i.dbg newnext,
allocstate)
Expand Down
5 changes: 3 additions & 2 deletions asmcomp/selectgen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,13 @@ method is_simple_expr = function
| Cop(op, args, _) ->
begin match op with
(* The following may have side effects *)
| Capply _ | Cextcall _ | Calloc _ | Cstore _ | Craise _ -> false
| Capply _ | Cextcall _ | Calloc _ | Cstore _
| Craise _ | Ccheckbound -> false
(* The remaining operations are simple if their args are *)
| Cload _ | Caddi | Csubi | Cmuli | Cmulhi | Cdivi | Cmodi | Cand | Cor
| Cxor | Clsl | Clsr | Casr | Ccmpi _ | Caddv | Cadda | Ccmpa _ | Cnegf
| Cabsf | Caddf | Csubf | Cmulf | Cdivf | Cfloatofint | Cintoffloat
| Ccmpf _ | Ccheckbound -> List.for_all self#is_simple_expr args
| Ccmpf _ -> List.for_all self#is_simple_expr args
end
| Cassign _ | Cifthenelse _ | Cswitch _ | Ccatch _ | Cexit _
| Ctrywith _ | Cregion _ | Ctail _ -> false
Expand Down
36 changes: 36 additions & 0 deletions testsuite/tests/basic/combine_checkbound.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(* TEST
* native *)

let glob = ref (1, 2)
let[@inline never] combine1 x a =
begin try
glob := (2, x);
glob := (3, a.(4));
with
| Invalid_argument _ -> ()
end;
!glob

let[@inline never] combine2 x a =
let loc = ref (1, 2) in
begin try
loc := (2, x);
loc := (3, a.(4));
with
| Invalid_argument _ -> ()
end;
!loc

let[@inline never] measure f =
let empty_array = [| |] in
let prebefore = Gc.minor_words () in
let before = Gc.minor_words () in
let r = f 42 empty_array in
assert (r = (2, 42));
let after = Gc.minor_words () in
((after -. before) -. (before -. prebefore))


let () =
Printf.printf "%10s: %.0f\n" "combine1" (measure combine1);
Printf.printf "%10s: %.0f\n" "combine2" (measure combine2)
2 changes: 2 additions & 0 deletions testsuite/tests/basic/combine_checkbound.reference
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
combine1: 3
combine2: 3

0 comments on commit ce62e45

Please sign in to comment.