blob: 57c92841c4412b3dd6ba1b2780263dbace69c959 [file] [log] [blame]
Mitch Phillipsf3968e82020-01-31 19:57:04 -08001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <platform/bionic/malloc.h>
Mitch Phillips0083b0f2020-02-13 17:37:11 -080030#include <private/bionic_arc4random.h>
Mitch Phillipsf3968e82020-01-31 19:57:04 -080031#include <private/bionic_malloc_dispatch.h>
32#include <stddef.h>
33#include <stdint.h>
34#include <stdio.h>
35#include <string.h>
36#include <sys/types.h>
37
38#include "bionic/gwp_asan_wrappers.h"
39#include "gwp_asan/guarded_pool_allocator.h"
40#include "gwp_asan/options.h"
41#include "gwp_asan/random.h"
42#include "malloc_common.h"
43
44#ifndef LIBC_STATIC
45#include "bionic/malloc_common_dynamic.h"
46#endif // LIBC_STATIC
47
48static gwp_asan::GuardedPoolAllocator GuardedAlloc;
49static const MallocDispatch* prev_dispatch;
50
51using Options = gwp_asan::options::Options;
52
53// ============================================================================
54// Implementation of gFunctions.
55// ============================================================================
56
57// This function handles initialisation as asked for by MallocInitImpl. This
58// should always be called in a single-threaded context.
59bool gwp_asan_initialize(const MallocDispatch* dispatch, bool*, const char*) {
60 prev_dispatch = dispatch;
61
62 Options Opts;
63 Opts.Enabled = true;
64 Opts.MaxSimultaneousAllocations = 32;
65 Opts.SampleRate = 2500;
66 Opts.InstallSignalHandlers = false;
67 Opts.InstallForkHandlers = true;
68
69 GuardedAlloc.init(Opts);
Mitch Phillips4d62ffc2020-02-18 17:13:52 -080070 // TODO(b/149790891): The log line below causes ART tests to fail as they're
71 // not expecting any output. Disable the output for now.
72 // info_log("GWP-ASan has been enabled.");
Mitch Phillipsf3968e82020-01-31 19:57:04 -080073 return true;
74}
75
76void gwp_asan_finalize() {
77}
78
79void gwp_asan_get_malloc_leak_info(uint8_t**, size_t*, size_t*, size_t*, size_t*) {
80}
81
82void gwp_asan_free_malloc_leak_info(uint8_t*) {
83}
84
85ssize_t gwp_asan_malloc_backtrace(void*, uintptr_t*, size_t) {
86 // TODO(mitchp): GWP-ASan might be able to return the backtrace for the
87 // provided address.
88 return -1;
89}
90
91bool gwp_asan_write_malloc_leak_info(FILE*) {
92 return false;
93}
94
95void* gwp_asan_gfunctions[] = {
96 (void*)gwp_asan_initialize, (void*)gwp_asan_finalize,
97 (void*)gwp_asan_get_malloc_leak_info, (void*)gwp_asan_free_malloc_leak_info,
98 (void*)gwp_asan_malloc_backtrace, (void*)gwp_asan_write_malloc_leak_info,
99};
100
101// ============================================================================
102// Implementation of GWP-ASan malloc wrappers.
103// ============================================================================
104
105void* gwp_asan_calloc(size_t n_elements, size_t elem_size) {
106 if (__predict_false(GuardedAlloc.shouldSample())) {
107 size_t bytes;
108 if (!__builtin_mul_overflow(n_elements, elem_size, &bytes)) {
109 if (void* result = GuardedAlloc.allocate(bytes)) {
110 return result;
111 }
112 }
113 }
114 return prev_dispatch->calloc(n_elements, elem_size);
115}
116
117void gwp_asan_free(void* mem) {
118 if (__predict_false(GuardedAlloc.pointerIsMine(mem))) {
119 GuardedAlloc.deallocate(mem);
120 return;
121 }
122 prev_dispatch->free(mem);
123}
124
125void* gwp_asan_malloc(size_t bytes) {
126 if (__predict_false(GuardedAlloc.shouldSample())) {
127 if (void* result = GuardedAlloc.allocate(bytes)) {
128 return result;
129 }
130 }
131 return prev_dispatch->malloc(bytes);
132}
133
134size_t gwp_asan_malloc_usable_size(const void* mem) {
135 if (__predict_false(GuardedAlloc.pointerIsMine(mem))) {
136 return GuardedAlloc.getSize(mem);
137 }
138 return prev_dispatch->malloc_usable_size(mem);
139}
140
141void* gwp_asan_realloc(void* old_mem, size_t bytes) {
142 if (__predict_false(GuardedAlloc.pointerIsMine(old_mem))) {
143 size_t old_size = GuardedAlloc.getSize(old_mem);
144 void* new_ptr = gwp_asan_malloc(bytes);
145 if (new_ptr) memcpy(new_ptr, old_mem, (bytes < old_size) ? bytes : old_size);
146 GuardedAlloc.deallocate(old_mem);
147 return new_ptr;
148 }
149 return prev_dispatch->realloc(old_mem, bytes);
150}
151
152int gwp_asan_malloc_iterate(uintptr_t base, size_t size,
153 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
154 if (__predict_false(GuardedAlloc.pointerIsMine(reinterpret_cast<void*>(base)))) {
155 // TODO(mitchp): GPA::iterate() returns void, but should return int.
156 // TODO(mitchp): GPA::iterate() should take uintptr_t, not void*.
157 GuardedAlloc.iterate(reinterpret_cast<void*>(base), size, callback, arg);
158 return 0;
159 }
160 return prev_dispatch->malloc_iterate(base, size, callback, arg);
161}
162
163void gwp_asan_malloc_disable() {
164 GuardedAlloc.disable();
165 prev_dispatch->malloc_disable();
166}
167
168void gwp_asan_malloc_enable() {
169 GuardedAlloc.enable();
170 prev_dispatch->malloc_enable();
171}
172
173static const MallocDispatch gwp_asan_dispatch __attribute__((unused)) = {
174 gwp_asan_calloc,
175 gwp_asan_free,
176 Malloc(mallinfo),
177 gwp_asan_malloc,
178 gwp_asan_malloc_usable_size,
179 Malloc(memalign),
180 Malloc(posix_memalign),
181#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
182 Malloc(pvalloc),
183#endif
184 gwp_asan_realloc,
185#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
186 Malloc(valloc),
187#endif
188 gwp_asan_malloc_iterate,
189 gwp_asan_malloc_disable,
190 gwp_asan_malloc_enable,
191 Malloc(mallopt),
192 Malloc(aligned_alloc),
193 Malloc(malloc_info),
194};
195
Mitch Phillips0083b0f2020-02-13 17:37:11 -0800196// The probability (1 / kProcessSampleRate) that a process will be ranodmly
197// selected for sampling. kProcessSampleRate should always be a power of two to
198// avoid modulo bias.
199static constexpr uint8_t kProcessSampleRate = 128;
200
Mitch Phillipsf3968e82020-01-31 19:57:04 -0800201bool ShouldGwpAsanSampleProcess() {
Mitch Phillips0083b0f2020-02-13 17:37:11 -0800202 uint8_t random_number;
203 __libc_safe_arc4random_buf(&random_number, sizeof(random_number));
204 return random_number % kProcessSampleRate == 0;
Mitch Phillipsf3968e82020-01-31 19:57:04 -0800205}
206
Mitch Phillipsbba80dc2020-02-11 14:42:14 -0800207bool MaybeInitGwpAsanFromLibc(libc_globals* globals) {
Mitch Phillipsf3968e82020-01-31 19:57:04 -0800208 // Never initialize the Zygote here. A Zygote chosen for sampling would also
209 // have all of its children sampled. Instead, the Zygote child will choose
210 // whether it samples or not just after the Zygote forks. For
211 // libc_scudo-preloaded executables (like mediaswcodec), the program name
212 // might not be available yet. The zygote never uses dynamic libc_scudo.
213 const char* progname = getprogname();
214 if (progname && strncmp(progname, "app_process", 11) == 0) {
215 return false;
216 }
Mitch Phillipsbba80dc2020-02-11 14:42:14 -0800217 return MaybeInitGwpAsan(globals);
Mitch Phillipsf3968e82020-01-31 19:57:04 -0800218}
219
220static bool GwpAsanInitialized = false;
221
222// Maybe initializes GWP-ASan. Called by android_mallopt() and libc's
223// initialisation. This should always be called in a single-threaded context.
Mitch Phillipsbba80dc2020-02-11 14:42:14 -0800224bool MaybeInitGwpAsan(libc_globals* globals, bool force_init) {
Mitch Phillipsf3968e82020-01-31 19:57:04 -0800225 if (GwpAsanInitialized) {
226 error_log("GWP-ASan was already initialized for this process.");
227 return false;
228 }
229
230 // If the caller hasn't forced GWP-ASan on, check whether we should sample
231 // this process.
232 if (!force_init && !ShouldGwpAsanSampleProcess()) {
233 return false;
234 }
235
236 // GWP-ASan is compatible with heapprofd/malloc_debug/malloc_hooks iff
237 // GWP-ASan was installed first. If one of these other libraries was already
238 // installed, we don't enable GWP-ASan. These libraries are normally enabled
239 // in libc_init after GWP-ASan, but if the new process is a zygote child and
240 // trying to initialize GWP-ASan through mallopt(), one of these libraries may
241 // be installed. It may be possible to change this in future by modifying the
242 // internal dispatch pointers of these libraries at this point in time, but
243 // given that they're all debug-only, we don't really mind for now.
244 if (GetDefaultDispatchTable() != nullptr) {
245 // Something else is installed.
246 return false;
247 }
248
249 // GWP-ASan's initialization is always called in a single-threaded context, so
250 // we can initialize lock-free.
Mitch Phillipsbba80dc2020-02-11 14:42:14 -0800251 // Set GWP-ASan as the malloc dispatch table.
252 globals->malloc_dispatch_table = gwp_asan_dispatch;
253 atomic_store(&globals->default_dispatch_table, &gwp_asan_dispatch);
Mitch Phillipsf3968e82020-01-31 19:57:04 -0800254
Mitch Phillipsbba80dc2020-02-11 14:42:14 -0800255 // If malloc_limit isn't installed, we can skip the default_dispatch_table
256 // lookup.
257 if (GetDispatchTable() == nullptr) {
258 atomic_store(&globals->current_dispatch_table, &gwp_asan_dispatch);
259 }
Mitch Phillipsf3968e82020-01-31 19:57:04 -0800260
261#ifndef LIBC_STATIC
262 SetGlobalFunctions(gwp_asan_gfunctions);
263#endif // LIBC_STATIC
264
265 GwpAsanInitialized = true;
266
267 gwp_asan_initialize(NativeAllocatorDispatch(), nullptr, nullptr);
268
269 return true;
270}
Mitch Phillipsc03856c2020-02-13 16:41:14 -0800271
272bool DispatchIsGwpAsan(const MallocDispatch* dispatch) {
273 return dispatch == &gwp_asan_dispatch;
274}