Repository navigation
[DRAFT] [pulse] invalidate the caller's vector elements when a callee reallocates - #2204
Closed
VladimirMakaev wants to merge 5 commits into
Closed
VladimirMakaev wants to merge 5 commits into
VladimirMakaev wants to merge 5 commits into
Conversation
VladimirMakaev
force-pushed
the
pulse-vector-callee-invalidation
branch
3 times, most recently
from
October 5, 2026 23:09
7e5fd52 to
eed56c3
Compare
Pulse tied only `operator[]` and `at()` to the backing array of a `std::vector`, did not model
`erase()`, `resize()` or range `assign`/`insert`, and did not match `name<>` calls such as
`emplace_back()`, so uses of invalidated elements went unreported.
```cpp
int front_then_push_back(std::vector<int>& vec) {
int& first = vec.front();
vec.push_back(42);
return first; // Infer missed: vector invalidation
}
int erase_then_deref(std::vector<int>& vec) {
int* elt = &vec[1];
vec.erase(vec.begin());
return *elt; // Infer missed: vector invalidation
}
```
Model `front()`, `back()`, `data()` and the `emplace_back()` result; make `erase()`, growing
`resize()` and range `assign`/`insert` invalidate and update the size; model iterator `operator=`
and make `++`/`--` move only the stepped iterator, so `it = v.erase(it)` works and `--it; ++it`
from `end()` is still caught. Matching `name<>` in `ProcnameDispatcher` affects the models of
every checker; no other name newly matches.
Limitations: `erase()` invalidates every element, even before the erased position, and `resize()`
invalidates unless the vector provably does not grow (constructors such as `std::vector<int> v(n)`
leave the size unknown).
## Test plan
New tests in `cpp/pulse/vector.cpp`, `cpp/pulse/vector_iterator.cpp`,
`cpp/pulse/reference_stability.cpp` and `cpp/pulse-17/std_vector.cpp`, with `FP_`/`FN_` tests for
the limitations. All codetoanalyze tests pass; the new tests give the same results with libstdc++.
Pulse treated `std::find`, `std::lower_bound`, `std::next` and similar functions as unknown calls,
so the iterators they return were not tied to their vector and later reallocations went
unreported. Iterator arithmetic also kept the `end()` marking.
```cpp
int find_then_push_back(std::vector<int>& vec) {
auto it = std::find(vec.begin(), vec.end(), 3);
if (it == vec.end()) {
return 0;
}
vec.push_back(1);
return *it; // Infer missed: vector invalidation
}
int last(std::vector<int>& vec) {
return vec.empty() ? 0 : *(vec.end() - 1); // false positive: end() dereference
}
```
Model the algorithms that return a position without reordering the range as returning an unknown
position in the range of their first argument, propagating taint as unknown calls did. Model `+`,
`-`, `+=`, `-=`, `std::next`, `std::prev` and `std::advance` as shifting the position,
`operator->` like `operator*`, and `cbegin()`/`cend()`.
Limitations:
- these models have no "not found" case, so an unchecked `*std::find(...)` is not reported
- only `std::vector` and `std::string` iterators are matched, not reverse iterators, raw pointers
or other containers
- summaries of loops reading `it->x` now keep the elements read, as with `(*it).x`, which made
some of yaml-cpp's iterator summaries 4-7x larger
## Test plan
New tests in `cpp/pulse/std_algorithm.cpp`, `cpp/pulse/vector_iterator.cpp` and
`cpp/pulse/taint/strings.cpp`. The C, C++, Java, Kotlin and SIL codetoanalyze tests pass.
Since e1f88f2 (August 2024), callers passing null to a function that checks `p == NULL` and then dereferences `p` anyway get no NULLPTR_DEREFERENCE: the access fails on `ComparedToNullInThisProcedure` before `MustBeValid` is abduced. That commit deleted the caller tests showing this (`call_test_after_dereference_bad`, `no_early_return_no_zero_manifest_bad`). Java is hit hardest: bytecode spells every null check as `== null`. ```c #include <stddef.h> struct node { int data; }; void log_message(const char* msg); int get_data(struct node* n) { if (n == NULL) { log_message("null node"); } return n->data; } int caller(void) { return get_data(NULL); // Infer missed: null dereference } ``` When an access fails on `ComparedToNullInThisProcedure` or `ConstantDereference` and the pointer's history shows it was read from a pre-condition cell holding it, abduce `MustBeValid` as successful accesses do. All null values of a path share one canonical value: the check keeps a null local, or the result of an unknown call given a null argument, from blaming the caller. Limitations: with the opt-in COMPARED_TO_NULL_AND_DEREFERENCED, one bug yields two reports (callee and caller). ## Test plan New `c/pulse/compared_to_null.c`, with callers not blamed for a null local or unknown-call result (direct or through a wrapper), and an `FP_` test for an unknown noreturn function in the null branch. Callers restored in `cpp/pulse/nullptr.cpp` and `objc/pulse/null_deref/return_npe_test.m`, added in `java/pulse/NullPointerExceptions.java`. All suites pass; the ObjC line comes from that file alone.
…ng buffer
Pulse invalidates the `data()` buffer of a destroyed `std::basic_string`, but `c_str()`,
mutators and `std::string_view` were not tied to it, so dangling pointers were missed:
```cpp
std::string make_string();
char c_str_then_append_bad(std::string& s) {
const char* p = s.c_str();
s.append(1000, 'x');
return *p; // Infer missed: use of an invalidated buffer
}
char view_of_temporary_string_bad() {
std::string_view sv = make_string();
return *sv.data(); // Infer missed: use after delete
}
```
`c_str()` is now modelled like `data()`. Mutators such as `append`, `erase`, `clear` and
`operator=` invalidate the buffer (VECTOR_INVALIDATION, even after `reserve()`) and return the
receiver when they return `basic_string&`; views and string iterators point into the buffer. As
`operator=` now records the write, a PULSE_CONST_REFABLE false positive on assigned by-value
strings goes away.
Limitations: a by-value string whose `c_str()` is only used in `(void)p` loses its
PULSE_CONST_REFABLE, as with `data()`; `swap`, `getline`, `cbegin()`/`end()` iterators and
buffer pointer arithmetic are not tracked.
## Test plan
New tests in `cpp/pulse/basic_string.cpp`, `cpp/pulse/const_refable.cpp`,
`cpp/pulse/taint/strings.cpp` and `cpp/pulse-17/string_view.cpp`, with `FN_` tests for the
limitations. Copies of mutator results in `taint/strings.cpp` are now reported as
PULSE_UNNECESSARY_COPY_INTERMEDIATE, like the existing `operator+=` one. The codetoanalyze tests
pass.
…ates
When a callee reallocated a `std::vector`, the caller's pointers and references to its elements
stayed valid unless the callee had accessed those elements itself.
```cpp
void add(std::vector<int>& vec) { vec.push_back(42); }
int deref_after_add(std::vector<int>& vec) {
int* elt = &vec[1];
add(vec);
return *elt; // Infer missed: vector invalidation
}
```
When applying a summary, extend the callee's invalidation of the array (by `push_back`, `erase`,
`clear`, ...) to the elements the caller knows, except for a `push_back` or `resize` after the
caller's `reserve`, as intraprocedurally. The copy constructor now copies the elements one level
deep, so growing a copy leaves the original's elements valid; moves still share them.
Limitations:
- Only vector elements are covered, not fields of elements or of objects freed by a callee.
- As intraprocedurally, `erase` at the end or a shrinking `resize` invalidates every element.
- A by-value lambda capture (`[vec]`) still shares the elements.
## Test plan
New tests in `cpp/pulse/vector.cpp` and an FN_ test in `c/pulse/interprocedural.c`. All
codetoanalyze tests pass.
VladimirMakaev
force-pushed
the
pulse-vector-callee-invalidation
branch
from
October 7, 2026 15:49
eed56c3 to
e54d6ce
Compare
Contributor
Author
|
Will reopen once current PR queue is cleared |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack: #2181 → #2209 → #2194 → #2204, also on #2169 (review only the top commit; lands after #2194)
When a callee reallocated a
std::vector, the caller's pointers and references to its elementsstayed valid unless the callee had accessed those elements itself.
When applying a summary, extend the callee's invalidation of the array (by
push_back,erase,clear, ...) to the elements the caller knows, except for apush_backorresizeafter thecaller's
reserve, as intraprocedurally. The copy constructor now copies the elements one leveldeep, so growing a copy leaves the original's elements valid; moves still share them.
Limitations:
eraseat the end or a shrinkingresizeinvalidates every element.[vec]) still shares the elements.Test plan
New tests in
cpp/pulse/vector.cppand an FN_ test inc/pulse/interprocedural.c. Allcodetoanalyze tests pass.