Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bufferoverrun Analysis - small fixes and improvements #1736

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 12 additions & 12 deletions infer/src/bufferoverrun/bounds.ml
Original file line number Diff line number Diff line change
Expand Up @@ -950,25 +950,25 @@ module Bound = struct
let plus_l : weak:bool -> t -> t -> t =
plus_exact ~otherwise:(fun x y ->
match (x, y) with
| MinMax (c1, Plus, Max, d1, _), Linear (c2, x2)
| Linear (c2, x2), MinMax (c1, Plus, Max, d1, _) ->
Linear (Z.(c1 + d1 + c2), x2)
| MinMax (c1, Minus, Min, d1, _), Linear (c2, x2)
| Linear (c2, x2), MinMax (c1, Minus, Min, d1, _) ->
Linear (Z.(c1 - d1 + c2), x2)
| MinMax (c1, Plus, Max, d1, x1), Linear (c2, x2)
| Linear (c2, x2), MinMax (c1, Plus, Max, d1, x1) ->
mk_MinMaxB (Max, (Linear (Z.(c1 + d1 + c2), x2)), (Linear (Z.(c1 + c2), SymLinear.plus (SymLinear.singleton_one x1) x2)))
| MinMax (c1, Minus, Min, d1, x1), Linear (c2, x2)
| Linear (c2, x2), MinMax (c1, Minus, Min, d1, x1) ->
mk_MinMaxB (Max, (Linear (Z.(c1 - d1 + c2), x2)), (Linear (Z.(c1 + c2), SymLinear.plus (SymLinear.singleton_minus_one x1) x2)))
Comment on lines +953 to +958
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While MinMaxB was introduced to express more complex min/max expressions, it is NOT always a better choice than others. By its complex nature, many operations may easily lose precision on MinMaxB bound, for example, during widening it is more likely to have infinity bounds according to my experience. Therefore, if we want to introduce MinMaxB here for the plus operation, we should evaluate overall precision impacts. Let me check if it is fine for our use cases.

| _, _ ->
MInf )


let plus_u : weak:bool -> t -> t -> t =
plus_exact ~otherwise:(fun x y ->
match (x, y) with
| MinMax (c1, Plus, Min, d1, _), Linear (c2, x2)
| Linear (c2, x2), MinMax (c1, Plus, Min, d1, _) ->
Linear (Z.(c1 + d1 + c2), x2)
| MinMax (c1, Minus, Max, d1, _), Linear (c2, x2)
| Linear (c2, x2), MinMax (c1, Minus, Max, d1, _) ->
Linear (Z.(c1 - d1 + c2), x2)
| MinMax (c1, Plus, Min, d1, x1), Linear (c2, x2)
| Linear (c2, x2), MinMax (c1, Plus, Min, d1, x1) ->
mk_MinMaxB (Min, (Linear (Z.(c1 + d1 + c2), x2)), (Linear (Z.(c1 + c2), SymLinear.plus (SymLinear.singleton_one x1) x2)))
| MinMax (c1, Minus, Max, d1, x1), Linear (c2, x2)
| Linear (c2, x2), MinMax (c1, Minus, Max, d1, x1) ->
mk_MinMaxB (Min, (Linear (Z.(c1 - d1 + c2), x2)), (Linear (Z.(c1 + c2), SymLinear.plus (SymLinear.singleton_minus_one x1) x2)))
| _, _ ->
PInf )

Expand Down
4 changes: 4 additions & 0 deletions infer/src/bufferoverrun/bufferOverrunProofObligations.ml
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@ module ArrayAccessCondition = struct
Bound.is_not_infty (ItvPure.ub real_idx) && Bound.le (ItvPure.ub size) (ItvPure.ub real_idx)
then {report_issue_type= Issue IssueType.buffer_overrun_l2; propagate= false}
(* symbolic il >= sl, probably an error *)
else if
Bound.is_infty (ItvPure.ub real_idx) && Bound.is_not_infty (ItvPure.ub size)
then {report_issue_type= Issue IssueType.buffer_overrun_l3; propagate= false}
(* su < iu = +oo, probably an error *)
Comment on lines +350 to +353
Copy link
Contributor

@skcho skcho Feb 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, this error was missed by buffer overrun checker.

a.c:4: error: Buffer Overrun L4
  Offset: [0, +oo] Size: 2.
  2. void foo() {
  3. int a[2];
  4. for(int i=0; a[i]; i++) {}
                  ^
  5. }

For the example case, it reports L4 issue as above. The reason you did not see the issue is that it suppresses the issue types that are likely to be false positive, e.g. offset or size interval includes an infinity bound like above.

let buffer_overrun_l4 =
register ~enabled:false ~id:"BUFFER_OVERRUN_L4" Error BufferOverrunChecker
~user_documentation:"See [BUFFER_OVERRUN_L1](#buffer_overrun_l1)"

Note that ~enabled:false is given when defining the L4 issue type.

To enable all issue types, you can pass --no-filtering option, or --enable-issue-type to enable each of them.

else if
Bound.is_symbolic (ItvPure.lb real_idx) && Bound.le (ItvPure.lb size) (ItvPure.lb real_idx)
then {report_issue_type= Issue IssueType.buffer_overrun_s2; propagate= true}
Expand Down
26 changes: 25 additions & 1 deletion infer/src/bufferoverrun/bufferOverrunUtils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Sem = BufferOverrunSemantics
module Trace = BufferOverrunTrace
module TraceSet = Trace.Set
module TypModels = BufferOverrunTypModels
module BoField = BufferOverrunField

module ModelEnv = struct
type model_env =
Expand Down Expand Up @@ -52,14 +53,37 @@ module Exec = struct
mem


let rec decl_local_loc ({tenv} as model_env) loc typ ~inst_num ~represents_multiple_values
let rec decl_local_struct_fields_loc: ModelEnv.model_env -> Loc.t -> Struct.fields -> Dom.Mem.t * int -> Dom.Mem.t * int =
fun model_env loc fields (mem, inst_num) ->
match fields with
| field :: tl -> (
match field with
| (fn, typ, _) -> (
match typ.desc with
| Tarray {elt= _; length; stride} ->
let child_loc = BoField.Field {prefix= loc; fn; typ = (Some typ)} in
let stride = Option.map ~f:IntLit.to_int_exn stride in
let (mem, inst_num) = decl_local_array model_env child_loc typ ~length ?stride ~inst_num
~represents_multiple_values:false ~dimension:1 mem
in
decl_local_struct_fields_loc model_env loc tl (mem, inst_num)
| _ -> decl_local_struct_fields_loc model_env loc tl (mem, inst_num)))
| [] -> (mem, inst_num)
and decl_local_loc ({tenv} as model_env) loc typ ~inst_num ~represents_multiple_values
~dimension mem =
match typ.Typ.desc with
| Typ.Tarray {elt= typ; length; stride} ->
let stride = Option.map ~f:IntLit.to_int_exn stride in
decl_local_array model_env loc typ ~length ?stride ~inst_num ~represents_multiple_values
~dimension mem
| Typ.Tstruct typname -> (
let mem = (
match Tenv.lookup tenv typname with
| Some {fields} ->
let (mem, _) = decl_local_struct_fields_loc model_env loc fields (mem, 1) in mem
| None -> mem
)
in
Comment on lines +80 to +86
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to pass current dimension and inst_num, to distinguish the allocation sites.

        let mem, inst_num =
          match Tenv.lookup tenv typname with
          | Some {fields} ->
              decl_local_struct_fields_loc model_env loc fields ~inst_num ~dimension mem
          | None ->
              (mem, inst_num)
        in

match TypModels.dispatch tenv typname with
| Some (CArray {element_typ; length}) ->
decl_local_array model_env loc element_typ ~length:(Some length) ~inst_num
Expand Down