blob: d2f6571fc953e6d2a2fd04d47e3768069a327f3c [file] [log] [blame] [view]
Vlad Tsyrklevich08bc05252018-12-04 06:58:541# GWP-ASan
2
3GWP-ASan is a debug tool intended to detect heap memory errors in the wild. It
Vlad Tsyrklevichf9c90652018-12-28 21:15:034samples allocations to a debug allocator, similar to ElectricFence or Page Heap,
Vlad Tsyrklevich6e6402a2019-01-22 07:50:205causing memory errors to crash and report additional debugging context about
6the error.
Vlad Tsyrklevich08bc05252018-12-04 06:58:547
Vlad Tsyrklevichee4629b2019-10-24 20:07:068It is also known by its recursive backronym, GWP-ASan Will Provide Allocation
9Sanity.
10
Vlad Tsyrklevich08bc05252018-12-04 06:58:5411## Allocator
12
13The GuardedPageAllocator returns allocations on pages buffered on both sides by
14guard pages. The allocations are either left- or right-aligned to detect buffer
15overflows and underflows. When an allocation is freed, the page is marked
16inaccessible so use-after-frees cause an exception (until that page is reused
17for another allocation.)
18
19The allocator saves stack traces on every allocation and deallocation to
20preserve debug context if that allocation results in a memory error.
21
Vlad Tsyrklevichdc1a9a5e82018-12-18 18:04:0122The allocator implements a quarantine mechanism by allocating virtual memory for
Vlad Tsyrklevich4a2e4d202019-04-25 00:22:4323more allocations than the total number of physical pages it can return at any
24given time. The difference forms a rudimentary quarantine.
25
26Because pages are re-used for allocations, it's possible that a long-lived
27use-after-free will cause a crash long after the original allocation has been
28replaced. In order to decrease the likelihood of incorrect stack traces being
29reported, we allocate a lot of virtual memory but don't store metadata for every
30allocation. That way though we may not be able to report the metadata for an old
31allocation, we will not report incorrect stack traces.
Vlad Tsyrklevichdc1a9a5e82018-12-18 18:04:0132
Vlad Tsyrklevich08bc05252018-12-04 06:58:5433## Crash handler
34
35The allocator is designed so that memory errors with GWP-ASan allocations
36intentionally trigger invalid access exceptions. A hook in the crashpad crash
37handler process inspects crashes, determines if they are GWP-ASan exceptions,
38and adds additional debug information to the crash minidump if so.
39
40The crash handler hook determines if the exception was related to GWP-ASan by
41reading the allocator internals and seeing if the exception address was within
42the bounds of the allocator region. If it is, the crash handler hook extracts
43debug information about that allocation, such as thread IDs and stack traces
44for allocation (and deallocation, if relevant) and writes it to the crash dump.
45
46The crash handler runs with elevated privileges so parsing information from a
47lesser-privileged process is security sensitive. The GWP-ASan hook is specially
48structured to minimize the amount of allocator logic it relies on and to
49validate the allocator internals before reasoning about them.
50
51## Status
52
Vlad Tsyrklevichee4629b2019-10-24 20:07:0653GWP-ASan is implemented for malloc and PartitionAlloc. It is enabled by default
54on Windows and macOS. The allocator parameters can be manually modified by using
55an invocation like the following:
Vlad Tsyrklevich08bc05252018-12-04 06:58:5456
57```shell
58chrome --enable-features="GwpAsanMalloc<Study" \
59 --force-fieldtrials=Study/Group1 \
Vlad Tsyrklevich4a2e4d202019-04-25 00:22:4360 --force-fieldtrial-params=Study.Group1:MaxAllocations/128/MaxMetadata/255/TotalPages/4096/AllocationSamplingFrequency/1000/ProcessSamplingProbability/1.0
Vlad Tsyrklevich08bc05252018-12-04 06:58:5461```
62
Vlad Tsyrklevich04d86642019-05-21 00:22:5063GWP-ASan is tuned more aggressively in canary/dev, to increase the likelihood we
64catch newly introduced bugs, and for specific processes depending on the
65particular allocator.
Vlad Tsyrklevich4a2e4d202019-04-25 00:22:4366
Vlad Tsyrklevich6e6402a2019-01-22 07:50:2067A [hotlist of bugs discovered by by GWP-ASan](https://bugs.chromium.org/p/chromium/issues/list?can=1&q=Hotlist%3DGWP-ASan)
Vlad Tsyrklevich4a2e4d202019-04-25 00:22:4368exists, though GWP-ASan crashes are filed without external visibility by
69default.
Vlad Tsyrklevich6e6402a2019-01-22 07:50:2070
Vlad Tsyrklevich04d86642019-05-21 00:22:5071## Limitations
72
73- GWP-ASan is configured with a small fixed-size amount of memory, so
74 long-lived allocations can quickly deplete the page pool and lead the
75 allocator to run out of memory. Depending on the sampling frequency and
76 distribution of allocation lifetimes this may lead to only allocations early
77 in the process lifetime being sampled.
78- Allocations over a page in size are not sampled.
79- The allocator skips zero-size allocations. Zero-size allocations on some
80 platforms return valid pointers and may be subject to lifetime and bounds
81 issues.
82- GWP-ASan does not intercept allocations for Oilpan or the v8 GC.
83- GWP-ASan does not hook PDFium's fork of PartitionAlloc.
84- Right-aligned allocations to catch overflows are not perfectly right-aligned,
85 so small out-of-bounds accesses may be missed.
Vlad Tsyrklevichee4629b2019-10-24 20:07:0686- GWP-ASan does not sample some early allocations that occur before field trial
87 initialization.
88- Depending on the platform, GWP-ASan may or may not hook malloc allocations
89 that occur in code not linked directly against Chrome.
Vlad Tsyrklevich04d86642019-05-21 00:22:5090
Vlad Tsyrklevich08bc05252018-12-04 06:58:5491## Testing
92
93There is [not yet](https://crbug.com/910751) a way to intentionally trigger a
94GWP-ASan exception.
95
96There is [not yet](https://crbug.com/910749) a way to inspect GWP-ASan data in
Vlad Tsyrklevich4a2e4d202019-04-25 00:22:4397a minidump (crash report) without access to Google's crash service.