Skip to content

Commit 447b6e9

Browse files
committed
CN VIP: Improve err msg for live alloc bound-check
1 parent a330424 commit 447b6e9

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

backend/cn/lib/check.ml

+30-11
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ let check_alloc_bounds loc ~ptr ub_unspec =
450450
let@ model = model () in
451451
let ub = CF.Undefined.(UB_CERB004_unspecified ub_unspec) in
452452
fail (fun ctxt ->
453-
{ loc; msg = Alloc_out_of_bounds { constr; ptr; ub; ctxt; model } }))
453+
{ loc; msg = Alloc_out_of_bounds { constr; term = ptr; ub; ctxt; model } }))
454454
else
455455
return ()
456456

@@ -473,7 +473,7 @@ let check_both_eq_alloc loc arg1 arg2 ub =
473473
| `True -> return ()
474474

475475

476-
let check_live_alloc_bounds reason loc arg ub constr =
476+
let check_live_alloc_bounds reason loc arg ub term constr =
477477
let@ base_size = RI.Special.get_live_alloc reason loc arg in
478478
let here = Locations.other __FUNCTION__ in
479479
let base, size = Alloc.History.get_base_size base_size here in
@@ -484,7 +484,8 @@ let check_live_alloc_bounds reason loc arg ub constr =
484484
| `True -> return ()
485485
| `False ->
486486
let@ model = model () in
487-
fail (fun ctxt -> { loc; msg = Undefined_behaviour { ub; ctxt; model } }))
487+
fail (fun ctxt ->
488+
{ loc; msg = Alloc_out_of_bounds { term; constr; ub; ctxt; model } }))
488489
else
489490
return ()
490491

@@ -1382,7 +1383,13 @@ let rec check_expr labels (e : BT.t Mu.expr) (k : IT.t -> unit m) : unit m =
13821383
check_pexpr pe2 (fun arg2 ->
13831384
let@ () = check_both_eq_alloc loc arg1 arg2 ub in
13841385
let@ () =
1385-
check_live_alloc_bounds `Ptr_cmp loc arg1 ub (both_in_bounds arg1 arg2)
1386+
check_live_alloc_bounds
1387+
`Ptr_cmp
1388+
loc
1389+
arg1
1390+
ub
1391+
(IT.tuple_ [ arg1; arg2 ] here)
1392+
(both_in_bounds arg1 arg2)
13861393
in
13871394
k (op (arg1, arg2))))
13881395
in
@@ -1411,7 +1418,13 @@ let rec check_expr labels (e : BT.t Mu.expr) (k : IT.t -> unit m) : unit m =
14111418
let ub_unspec = CF.Undefined.UB_unspec_pointer_sub in
14121419
let ub = CF.Undefined.(UB_CERB004_unspecified ub_unspec) in
14131420
let@ () =
1414-
check_live_alloc_bounds `Ptr_diff loc arg1 ub (both_in_bounds arg1 arg2)
1421+
check_live_alloc_bounds
1422+
`Ptr_diff
1423+
loc
1424+
arg1
1425+
ub
1426+
(IT.tuple_ [ arg1; arg2 ] here)
1427+
(both_in_bounds arg1 arg2)
14151428
in
14161429
let ptr_diff_bt = Memory.bt_of_sct (Integer Ptrdiff_t) in
14171430
let value =
@@ -1510,11 +1523,17 @@ let rec check_expr labels (e : BT.t Mu.expr) (k : IT.t -> unit m) : unit m =
15101523
let@ () = check_has_alloc_id loc vt1 ub_unspec in
15111524
let here = Locations.other __FUNCTION__ in
15121525
let@ () =
1513-
check_live_alloc_bounds `ISO_array_shift loc vt1 ub (fun ~base ~size ->
1514-
let addr = addr_ result here in
1515-
let lower = le_ (base, addr) here in
1516-
let upper = le_ (addr, add_ (base, size) here) here in
1517-
and_ [ lower; upper ] here)
1526+
check_live_alloc_bounds
1527+
`ISO_array_shift
1528+
loc
1529+
vt1
1530+
ub
1531+
result
1532+
(fun ~base ~size ->
1533+
let addr = addr_ result here in
1534+
let lower = le_ (base, addr) here in
1535+
let upper = le_ (addr, add_ (base, size) here) here in
1536+
and_ [ lower; upper ] here)
15181537
in
15191538
k result))
15201539
| PtrMemberShift (_tag_sym, _memb_ident, _pe) ->
@@ -1535,7 +1554,7 @@ let rec check_expr labels (e : BT.t Mu.expr) (k : IT.t -> unit m) : unit m =
15351554
let@ () = check_has_alloc_id loc vt2 ub_unspec in
15361555
let ub = CF.Undefined.(UB_CERB004_unspecified ub_unspec) in
15371556
let@ () =
1538-
check_live_alloc_bounds `Copy_alloc_id loc vt2 ub (fun ~base ~size ->
1557+
check_live_alloc_bounds `Copy_alloc_id loc vt2 ub vt1 (fun ~base ~size ->
15391558
let addr = vt1 in
15401559
let lower = le_ (base, addr) here in
15411560
let upper = le_ (addr, add_ (base, size) here) here in

backend/cn/lib/typeErrors.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ type message =
205205
model : Solver.model_with_q
206206
}
207207
| Alloc_out_of_bounds of
208-
{ ptr : IT.t;
208+
{ term : IT.t;
209209
constr : IT.t;
210210
ub : CF.Undefined.undefined_behaviour;
211211
ctxt : Context.t * log;
@@ -508,8 +508,8 @@ let pp_message te =
508508
| None -> !^(CF.Undefined.ub_short_string ub)
509509
in
510510
{ short; descr = Some descr; state = Some state }
511-
| Alloc_out_of_bounds { constr; ptr; ub; ctxt; model } ->
512-
let short = !^"Pointer " ^^ bquotes (IT.pp ptr) ^^ !^" out of bounds" in
511+
| Alloc_out_of_bounds { constr; term; ub; ctxt; model } ->
512+
let short = bquotes (IT.pp term) ^^ !^" out of bounds" in
513513
let state =
514514
trace ctxt model Explain.{ no_ex with unproven_constraint = Some (LC.T constr) }
515515
in

0 commit comments

Comments
 (0)