tp: extract IncrementalState to fix CustomState UAF (#5593)

Fixes the use-after-free flagged in
`PacketSequenceStateGeneration::CustomState`.

## The bug

Previously, each `CustomState` held a raw `generation_` back-pointer
that was re-pointed every time a new generation was created via
`OnNewTracePacketDefaults` (the `set_generation(this)` loop in the
multi-arg ctor). The `TraceSorter` can hold a `RefPtr` to an older
Generation `G1` while a newer `G2` — to which `G1`'s shared CustomState
had been re-pointed — is dropped after `SEQ_INCREMENTAL_STATE_CLEARED`.
`G1` stays alive (and so does the CustomState), but its `generation_`
then dangles to freed memory; any subsequent lookup through the
CustomState dereferences the freed `G2`.

## The fix

Refactor the generation/CustomState relationship so that lifetime is
correct **by construction**:

- New `IncrementalState : RefCounted`. Owns the
per-incremental-state-interval data: `interned_data_`, the array of
`CustomState`s (now `unique_ptr` rather than `RefPtr`), and the
persistent thread descriptor. A new `IncrementalState` is constructed
only on `SEQ_INCREMENTAL_STATE_CLEARED`.

- `PacketSequenceStateGeneration` becomes a thin
per-`trace_packet_defaults` snapshot: holds `RefPtr<IncrementalState>`
(shared with sibling generations within the same interval), the defaults
blob, and the validity flag. All interned-data / custom-state /
thread-descriptor accessors are forwarders to the `IncrementalState`.

- `CustomState` is no longer `RefCounted` (it's owned uniquely by its
`IncrementalState`). Its back-pointer is `IncrementalState*` and is set
exactly once at lazy-allocation time inside
`IncrementalState::GetCustomState<T>`. Because the `IncrementalState`
owns the `CustomState`, the pointer is stable for the entire life of the
CustomState — UAF impossible.

- `OnNewTracePacketDefaults` no longer copies the `InternedFieldMap` or
re-points CustomStates; it constructs a new Generation that shares the
same `RefPtr<IncrementalState>`. This removes a per-defaults-change copy
that could be O(map-size) in the hot path.

- `OnPacketLoss` walks the IncrementalState's CustomState array and
clears any slot whose `ClearOnPacketLoss()` opted in (TES today), then
returns a new Generation referencing the same `IncrementalState` with
the validity bit cleared.

---

**Stack:**
- #5588 — tp: stop mutating PacketSequenceStateGeneration on packet loss
- #5590 — tp: split TrackEventSequenceState into descriptor + delta
state
- **#5593 — tp: extract IncrementalState to fix CustomState UAF** (this
PR)
19 files changed
tree: 704bdd632f01aff0b10210897746f8cf099201b6
  1. .github/
  2. bazel/
  3. build_overrides/
  4. buildtools/
  5. contrib/
  6. docs/
  7. examples/
  8. gn/
  9. include/
  10. infra/
  11. protos/
  12. python/
  13. sdk/
  14. src/
  15. test/
  16. third_party/
  17. tools/
  18. ui/
  19. .bazelignore
  20. .bazelrc
  21. .bazelversion
  22. .clang-format
  23. .clang-tidy
  24. .git-blame-ignore-revs
  25. .gitallowed
  26. .gitattributes
  27. .gitignore
  28. .gn
  29. .style.yapf
  30. Android.bp
  31. Android.bp.extras
  32. BUILD
  33. BUILD.extras
  34. BUILD.gn
  35. CHANGELOG
  36. CONTRIBUTORS.txt
  37. DIR_METADATA
  38. heapprofd.rc
  39. LICENSE
  40. meson.build
  41. METADATA
  42. MODULE.bazel
  43. MODULE.bazel.lock
  44. MODULE_LICENSE_APACHE2
  45. OWNERS
  46. OWNERS.github
  47. perfetto.rc
  48. perfetto_flags.aconfig
  49. PerfettoIntegrationTests.xml
  50. persistent_cfg.pbtxt
  51. README.chromium
  52. README.md
  53. TEST_MAPPING
  54. traced_perf.rc
  55. WORKSPACE
README.md

Perfetto - System profiling, app tracing and trace analysis

Perfetto is an open-source suite of SDKs, daemons and tools which use tracing to help developers understand the behaviour of complex systems and root-cause functional and performance issues on client and embedded systems.

It is a production-grade tool that is the default tracing system for the Android operating system and the Chromium browser.

Core Components

Perfetto is not a single tool, but a collection of components that work together:

  • High-performance tracing daemons: For capturing tracing information from many processes on a single machine into a unified trace file.
  • Low-overhead tracing SDK: A C++17 library for direct userspace-to-userspace tracing of timings and state changes in your application.
  • Extensive OS-level probes: For capturing system-wide context on Android and Linux (e.g. scheduling states, CPU frequencies, memory profiling, callstack sampling).
  • Browser-based UI: A powerful, fully local UI for visualizing and exploring large, multi-GB traces on a timeline. It works in all major browsers, requires no installation, and can open traces from other tools.
  • SQL-based analysis library: A powerful engine that allows you to programmatically query traces using SQL to automate analysis and extract custom metrics.

Why Use Perfetto?

Perfetto was designed to be a versatile and powerful tracing system for a wide range of use cases.

  • For Android App & Platform Developers: Debug and root-cause functional and performance issues like slow startups, dropped frames (jank), animation glitches, low memory kills, and ANRs. Profile both Java/Kotlin and native C++ memory usage with heap dumps and profiles.
  • For C/C++ Developers (Linux, macOS, Windows): Use the Tracing SDK to instrument your application with custom trace points to understand its execution flow, find performance bottlenecks, and debug complex behavior. On Linux, you can also perform detailed CPU and native heap profiling.
  • For Linux Kernel & System Developers: Get deep insights into kernel behavior. Perfetto acts as an efficient userspace daemon for ftrace, allowing you to visualize scheduling, syscalls, interrupts, and custom kernel tracepoints on a timeline.
  • For Chromium Developers: Perfetto is the tracing backend for chrome://tracing. Use it to debug and root-cause issues in the browser, V8, and Blink.
  • For Performance Engineers & SREs: Analyze and visualize a wide range of profiling and tracing formats, not just Perfetto's. Use the powerful SQL interface to programmatically analyze traces from tools like Linux perf, macOS Instruments, Chrome JSON traces, and more.

Getting Started

We‘ve designed our documentation to guide you to the right information as quickly as possible, whether you’re a newcomer to performance analysis or an experienced developer.

  1. New to tracing? If you're unfamiliar with concepts like tracing and profiling, start here:

  2. Ready to dive in? Our “Getting Started” guide is the main entry point for all users. It will help you find the right tutorials and documentation for your specific needs:

  3. Want the full overview? For a comprehensive look at what Perfetto is, why it's useful, and who uses it, see our main documentation page:

Debian Distribution

For users interested in the Debian distribution of Perfetto, the official source of truth and packaging efforts are maintained at Debian Perfetto Salsa Repository

Community & Support

Have questions? Need help?

We follow Google's Open Source Community Guidelines.