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
39 changes: 37 additions & 2 deletions infer/documentation/issues/VECTOR_INVALIDATION.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
An address pointing into a C++ `std::vector` might have become
invalid. This can happen when an address is taken into a vector, then
An address pointing into a C++ `std::vector`, or into another container
of the standard library (see below), might have become invalid. This
can happen when an address is taken into a vector, then
the vector is mutated in a way that might invalidate the address, for
example by adding elements to the vector, which might trigger a
re-allocation of the entire vector contents (thereby invalidating the
Expand All @@ -17,3 +18,37 @@ 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
}
```

This issue is also reported for other containers of the standard library
(`std::deque`, `std::list`, `std::map`, `std::set`, `std::unordered_map`,
etc.) when an iterator or a reference to an element is used after an operation
that invalidates it, for instance after the element was removed with `erase()`
or `clear()`, or after an insertion into a `std::deque`, which invalidates all
its iterators. Dereferencing the `end()` iterator of a container is reported
too.

```cpp
void erase_in_loop_bad(std::map<int, int>& map) {
for (auto it = map.begin(); it != map.end(); ++it) {
if (it->second == 0) {
map.erase(it); // bad: `it` is invalid after this, so `++it` is too
}
}
}
```
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
3 changes: 3 additions & 0 deletions infer/src/pulse/PulseCallOperations.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ let matches_iter =
[ "std::__detail::_Node_iterator"
; "std::__wrap_iter"
; "std::_Rb_tree_iterator"
; "std::back_insert_iterator"
; "std::front_insert_iterator"
; "std::insert_iterator"
; "__gnu_cxx::__normal_iterator" ]


Expand Down
4 changes: 3 additions & 1 deletion infer/src/pulse/PulseDiagnostic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,9 @@ let invalidation_titles (invalidation : Invalidation.t) =
| GoneOutOfScope _
| OptionalEmpty
| StdVector _
| CppMap _ ->
| StdString _
| CppMap _
| StdContainer _ ->
( "invalidation part of the trace starts here"
, "use-after-lifetime part of the trace starts here" )

Expand Down
150 changes: 149 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,151 @@ 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 std_container =
| Deque
| List
| Map
| Multimap
| Multiset
| Set
| UnorderedMap
| UnorderedMultimap
| UnorderedMultiset
| UnorderedSet
[@@deriving compare, equal, yojson_of]

type std_container_function =
| Clear
| Emplace
| EmplaceBack
| EmplaceFront
| Erase
| Insert
| PushBack
| PushFront
[@@deriving compare, equal, yojson_of]

let pp_std_container f = function
| Deque ->
F.fprintf f "std::deque"
| List ->
F.fprintf f "std::list"
| Map ->
F.fprintf f "std::map"
| Multimap ->
F.fprintf f "std::multimap"
| Multiset ->
F.fprintf f "std::multiset"
| Set ->
F.fprintf f "std::set"
| UnorderedMap ->
F.fprintf f "std::unordered_map"
| UnorderedMultimap ->
F.fprintf f "std::unordered_multimap"
| UnorderedMultiset ->
F.fprintf f "std::unordered_multiset"
| UnorderedSet ->
F.fprintf f "std::unordered_set"


let pp_std_container_function f = function
| Clear ->
F.fprintf f "clear"
| Emplace ->
F.fprintf f "emplace"
| EmplaceBack ->
F.fprintf f "emplace_back"
| EmplaceFront ->
F.fprintf f "emplace_front"
| Erase ->
F.fprintf f "erase"
| Insert ->
F.fprintf f "insert"
| PushBack ->
F.fprintf f "push_back"
| PushFront ->
F.fprintf f "push_front"


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

Expand Down Expand Up @@ -99,7 +236,9 @@ 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
| StdContainer of std_container * std_container_function
[@@deriving compare, equal, yojson_of, variants]

let is_same_type invalidation1 invalidation2 =
Expand Down Expand Up @@ -160,7 +299,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 _ | StdContainer _ ->
IssueType.vector_invalidation ~latent
| CppMap _ ->
IssueType.pulse_reference_stability
Expand Down Expand Up @@ -195,8 +334,13 @@ 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
| StdContainer (container, container_f) ->
F.fprintf f "was invalidated by `%a::%a`" pp_std_container container pp_std_container_function
container_f


let suggest cause =
Expand Down Expand Up @@ -227,5 +371,9 @@ 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
| StdContainer _ ->
F.fprintf f "StdContainer(%a)" describe invalidation
Loading
Loading