Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions infer/documentation/issues/VECTOR_INVALIDATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,19 @@ void deref_vector_element_after_push_back_bad(std::vector<int>& vec) {
std::cout << *y << "\n"; // bad: y might be invalid
}
```

The same applies to the buffer of a `std::basic_string`: the pointer
returned by `data()` or `c_str()`, an iterator returned by `begin()`,
or the pointer returned by `data()` of a `std::string_view` of the
string, might become invalid after a call to a mutating member
function such as `append()`, `clear()`, `push_back()`, `operator+=`,
`operator=` or `reserve()`, as the standard allows. Unlike for
`std::vector`, this is reported even after a call to `reserve()`.

```cpp
char deref_c_str_after_append_bad(std::string& s) {
const char* p = s.c_str();
s.append(1000, 'x'); // may re-allocate the buffer
return *p; // bad: p might be invalid
}
```
4 changes: 3 additions & 1 deletion infer/src/absint/ProcnameDispatcher.ml
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ let name_cons :
fun m name ->
let {on_templated_name} = m in
let match_fuzzy_name =
let fuzzy_name_regexp = name |> Str.quote |> Printf.sprintf "^%s\\(<.+>\\)?$" |> Str.regexp in
(* the frontend appends the template arguments of function template instantiations to their
names, e.g. [foo<int>], or [foo<>] when they are all empty parameter packs *)
let fuzzy_name_regexp = name |> Str.quote |> Printf.sprintf "^%s\\(<.*>\\)?$" |> Str.regexp in
fun s -> Str.string_match fuzzy_name_regexp s 0
in
let on_qual_name context f qual_name =
Expand Down
41 changes: 41 additions & 0 deletions infer/src/pulse/PulseAbductiveDomain.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2830,6 +2830,47 @@ module AddressAttributes = struct
SafeAttributes.is_hack_constinit_called (CanonValue.canon' astate v) astate
end

let is_read_from_pre_cell astate (v, hist) =
(* the result of an unknown call is a fresh value, but its history carries the cell ids of the
actuals *)
let rec is_unknown_call_result (hist : ValueHistory.t) =
match hist with
| UnknownCall _ ->
true
| Sequence (Call {in_call; _}, hist) ->
is_unknown_call_result in_call || is_unknown_call_result hist
| Sequence (_, hist) | FromCellIds (_, hist) ->
is_unknown_call_result hist
| BinaryOp (_, hist1, hist2) ->
is_unknown_call_result hist1 || is_unknown_call_result hist2
| Multiplex hists ->
List.exists hists ~f:is_unknown_call_result
| Epoch ->
false
in
let v = CanonValue.canon' astate v in
match ValueHistory.get_cell_ids hist with
| Some cell_ids ->
BaseMemory.exists
(fun _ edges ->
BaseMemory.Edges.exists edges ~f:(fun (_, (dest, dest_hist)) ->
Option.exists (ValueHistory.get_cell_id_exn dest_hist) ~f:(fun cell_id ->
ValueHistory.CellId.Set.mem cell_id cell_ids )
&& CanonValue.equal (CanonValue.canon astate dest) v ) )
(astate.pre :> base_domain).heap
&& not (is_unknown_call_result hist)
| None -> (
(* the initial values of object parameters have no cell id, see [add_static_types] *)
match ValueHistory.get_first_event hist with
| Some (ValueHistory.FormalDeclared (pvar, _, _) | Capture {captured_as= pvar; _}) ->
SafeStack.find_opt `Pre (Var.of_pvar pvar) astate
|> Option.bind ~f:(fun vo ->
SafeMemory.find_edge_opt `Pre (ValueOrigin.value vo) Dereference astate )
|> Option.exists ~f:(fun (formal_value, _) -> CanonValue.equal formal_value v)
| _ ->
false )


(* [recurse] is here to enforce that we can only ever make one recursive call when calling into this
function. That's what the code should ensure already as we should only ever need one recursive
call (namely when we are changing the history of a logical var: we should also update the history
Expand Down
3 changes: 3 additions & 0 deletions infer/src/pulse/PulseAbductiveDomain.mli
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ module Memory : sig
[x|->z * y |->z'], with [z'] a fresh abstract value. This is used for abstraction. *)
end

val is_read_from_pre_cell : t -> AbstractValue.t * ValueHistory.t -> bool
(** whether the history of the value names a cell of the pre-condition that holds this value *)

(** Safe version of {!PulseBaseAddressAttributes} *)
module AddressAttributes : sig
val abduce_one : AbstractValue.t -> Attribute.t -> t -> t
Expand Down
1 change: 1 addition & 0 deletions infer/src/pulse/PulseDiagnostic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,7 @@ let invalidation_titles (invalidation : Invalidation.t) =
| GoneOutOfScope _
| OptionalEmpty
| StdVector _
| StdString _
| CppMap _ ->
( "invalidation part of the trace starts here"
, "use-after-lifetime part of the trace starts here" )
Expand Down
78 changes: 77 additions & 1 deletion infer/src/pulse/PulseInvalidation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ type std_vector_function =
| Clear
| Emplace
| EmplaceBack
| Erase
| Insert
| PushBack
| Reserve
| Resize
| ShrinkToFit
[@@deriving compare, equal, yojson_of]

Expand All @@ -27,16 +29,85 @@ let pp_std_vector_function f = function
F.fprintf f "std::vector::emplace"
| EmplaceBack ->
F.fprintf f "std::vector::emplace_back"
| Erase ->
F.fprintf f "std::vector::erase"
| Insert ->
F.fprintf f "std::vector::insert"
| PushBack ->
F.fprintf f "std::vector::push_back"
| Reserve ->
F.fprintf f "std::vector::reserve"
| Resize ->
F.fprintf f "std::vector::resize"
| ShrinkToFit ->
F.fprintf f "std::vector::shrink_to_fit"


type std_string_function =
| Append
| Assign
| Clear
| Erase
| Insert
| OperatorAssign
| OperatorPlusAssign
| PopBack
| PushBack
| Replace
| Reserve
| Resize
| ShrinkToFit
[@@deriving compare, equal, yojson_of]

let all_std_string_functions : std_string_function list =
[ Append
; Assign
; Clear
; Erase
; Insert
; OperatorAssign
; OperatorPlusAssign
; PopBack
; PushBack
; Replace
; Reserve
; Resize
; ShrinkToFit ]


let std_string_method_name = function
| Append ->
"append"
| Assign ->
"assign"
| Clear ->
"clear"
| Erase ->
"erase"
| Insert ->
"insert"
| OperatorAssign ->
"operator="
| OperatorPlusAssign ->
"operator+="
| PopBack ->
"pop_back"
| PushBack ->
"push_back"
| Replace ->
"replace"
| Reserve ->
"reserve"
| Resize ->
"resize"
| ShrinkToFit ->
"shrink_to_fit"


let pp_std_string_function f std_string_f =
F.fprintf f "std::basic_string::%s" (std_string_method_name std_string_f)


type map_type = FollyF14Value | FollyF14Vector | FollyF14Fast
[@@deriving compare, equal, yojson_of]

Expand Down Expand Up @@ -99,6 +170,7 @@ type t =
| GoneOutOfScope of Pvar.t * Typ.t
| OptionalEmpty
| StdVector of std_vector_function
| StdString of std_string_function
| CppMap of map_type * map_function
[@@deriving compare, equal, yojson_of, variants]

Expand Down Expand Up @@ -160,7 +232,7 @@ let issue_type_of_cause ~latent invalidation must_be_valid_reason =
IssueType.use_after_lifetime ~latent
| OptionalEmpty ->
IssueType.optional_empty_access ~latent
| StdVector _ ->
| StdVector _ | StdString _ ->
IssueType.vector_invalidation ~latent
| CppMap _ ->
IssueType.pulse_reference_stability
Expand Down Expand Up @@ -195,6 +267,8 @@ let describe f cause =
F.pp_print_string f "is assigned an empty value"
| StdVector std_vector_f ->
F.fprintf f "was potentially invalidated by `%a`" pp_std_vector_function std_vector_f
| StdString std_string_f ->
F.fprintf f "was potentially invalidated by `%a`" pp_std_string_function std_string_f
| CppMap (map_t, map_f) ->
F.fprintf f "was potentially invalidated by `%a::%a`" pp_map_type map_t pp_map_function map_f

Expand Down Expand Up @@ -227,5 +301,7 @@ let pp f invalidation =
F.fprintf f "FClose(%a)" describe invalidation
| StdVector _ ->
F.fprintf f "StdVector(%a)" describe invalidation
| StdString _ ->
F.fprintf f "StdString(%a)" describe invalidation
| CppMap _ ->
F.fprintf f "CppMap(%a)" describe invalidation
26 changes: 26 additions & 0 deletions infer/src/pulse/PulseInvalidation.mli
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,39 @@ type std_vector_function =
| Clear
| Emplace
| EmplaceBack
| Erase
| Insert
| PushBack
| Reserve
| Resize
| ShrinkToFit
[@@deriving compare, equal, yojson_of]

val pp_std_vector_function : F.formatter -> std_vector_function -> unit

type std_string_function =
| Append
| Assign
| Clear
| Erase
| Insert
| OperatorAssign
| OperatorPlusAssign
| PopBack
| PushBack
| Replace
| Reserve
| Resize
| ShrinkToFit
[@@deriving compare, equal, yojson_of]

val all_std_string_functions : std_string_function list

val std_string_method_name : std_string_function -> string
(** name of the [std::basic_string] member function, e.g. ["push_back"] *)

val pp_std_string_function : F.formatter -> std_string_function -> unit

type map_type = FollyF14Value | FollyF14Vector | FollyF14Fast
[@@deriving compare, equal, yojson_of]

Expand Down Expand Up @@ -53,6 +78,7 @@ type t =
| GoneOutOfScope of Pvar.t * Typ.t
| OptionalEmpty
| StdVector of std_vector_function
| StdString of std_string_function
| CppMap of map_type * map_function
[@@deriving compare, equal, yojson_of]

Expand Down
Loading
Loading