blob: da85cf526b0e277956d9c76f0353b5f2e9f5e549 [file] [log] [blame]
Ryan Savitskiecc37e32018-12-14 15:57:21 +00001/*
2 * Copyright (C) 2018 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#pragma once
30
Elliott Hughes446b4dd2021-01-14 13:34:20 -080031#include <malloc.h>
Ryan Savitskif77928d2019-01-23 18:39:35 +000032#include <stdbool.h>
Peter Collingbourne1e110fb2020-01-09 10:48:22 -080033#include <stdint.h>
Ryan Savitskif77928d2019-01-23 18:39:35 +000034
Christopher Ferris30659fd2019-04-15 19:01:08 -070035// Structures for android_mallopt.
zijunzhaof0fb4182023-06-13 01:19:37 +000036#pragma clang diagnostic push
37#pragma clang diagnostic ignored "-Wnullability-completeness"
Christopher Ferris30659fd2019-04-15 19:01:08 -070038typedef struct {
39 // Pointer to the buffer allocated by a call to M_GET_MALLOC_LEAK_INFO.
40 uint8_t* buffer;
41 // The size of the "info" buffer.
42 size_t overall_size;
43 // The size of a single entry.
44 size_t info_size;
45 // The sum of all allocations that have been tracked. Does not include
46 // any heap overhead.
47 size_t total_memory;
48 // The maximum number of backtrace entries.
49 size_t backtrace_size;
50} android_mallopt_leak_info_t;
zijunzhaof0fb4182023-06-13 01:19:37 +000051#pragma clang diagnostic pop
Ryan Savitskiecc37e32018-12-14 15:57:21 +000052// Opcodes for android_mallopt.
53
Ryan Savitskif77928d2019-01-23 18:39:35 +000054enum {
Florian Mayerdb59b892018-11-27 17:06:54 +000055 // Marks the calling process as a profileable zygote child, possibly
56 // initializing profiling infrastructure.
Ryan Savitskif77928d2019-01-23 18:39:35 +000057 M_INIT_ZYGOTE_CHILD_PROFILING = 1,
58#define M_INIT_ZYGOTE_CHILD_PROFILING M_INIT_ZYGOTE_CHILD_PROFILING
Florian Mayerdb59b892018-11-27 17:06:54 +000059 M_RESET_HOOKS = 2,
60#define M_RESET_HOOKS M_RESET_HOOKS
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080061 // Set an upper bound on the total size in bytes of all allocations made
62 // using the memory allocation APIs.
63 // arg = size_t*
64 // arg_size = sizeof(size_t)
65 M_SET_ALLOCATION_LIMIT_BYTES = 3,
66#define M_SET_ALLOCATION_LIMIT_BYTES M_SET_ALLOCATION_LIMIT_BYTES
Christopher Ferris8189e772019-04-09 16:37:23 -070067 // Called after the zygote forks to indicate this is a child.
68 M_SET_ZYGOTE_CHILD = 4,
69#define M_SET_ZYGOTE_CHILD M_SET_ZYGOTE_CHILD
Christopher Ferris30659fd2019-04-15 19:01:08 -070070
71 // Options to dump backtraces of allocations. These options only
72 // work when malloc debug has been enabled.
73
74 // Writes the backtrace information of all current allocations to a file.
75 // NOTE: arg_size has to be sizeof(FILE*) because FILE is an opaque type.
76 // arg = FILE*
77 // arg_size = sizeof(FILE*)
78 M_WRITE_MALLOC_LEAK_INFO_TO_FILE = 5,
79#define M_WRITE_MALLOC_LEAK_INFO_TO_FILE M_WRITE_MALLOC_LEAK_INFO_TO_FILE
80 // Get information about the backtraces of all
81 // arg = android_mallopt_leak_info_t*
82 // arg_size = sizeof(android_mallopt_leak_info_t)
83 M_GET_MALLOC_LEAK_INFO = 6,
84#define M_GET_MALLOC_LEAK_INFO M_GET_MALLOC_LEAK_INFO
85 // Free the memory allocated and returned by M_GET_MALLOC_LEAK_INFO.
86 // arg = android_mallopt_leak_info_t*
87 // arg_size = sizeof(android_mallopt_leak_info_t)
88 M_FREE_MALLOC_LEAK_INFO = 7,
89#define M_FREE_MALLOC_LEAK_INFO M_FREE_MALLOC_LEAK_INFO
Ryan Savitski175c8862020-01-02 19:54:57 +000090 // Query whether the current process is considered to be profileable by the
91 // Android platform. Result is assigned to the arg pointer's destination.
92 // arg = bool*
93 // arg_size = sizeof(bool)
94 M_GET_PROCESS_PROFILEABLE = 9,
95#define M_GET_PROCESS_PROFILEABLE M_GET_PROCESS_PROFILEABLE
Mitch Phillipsf3968e82020-01-31 19:57:04 -080096 // Maybe enable GWP-ASan. Set *arg to force GWP-ASan to be turned on,
97 // otherwise this mallopt() will internally decide whether to sample the
98 // process. The program must be single threaded at the point when the
99 // android_mallopt function is called.
Mitch Phillipse6997d52020-11-30 15:04:14 -0800100 // arg = android_mallopt_gwp_asan_options_t*
101 // arg_size = sizeof(android_mallopt_gwp_asan_options_t)
Mitch Phillipsf3968e82020-01-31 19:57:04 -0800102 M_INITIALIZE_GWP_ASAN = 10,
103#define M_INITIALIZE_GWP_ASAN M_INITIALIZE_GWP_ASAN
Florian Mayercc61ad82022-08-31 11:43:30 -0700104 // Query whether memtag stack is enabled for this process.
105 M_MEMTAG_STACK_IS_ON = 11,
106#define M_MEMTAG_STACK_IS_ON M_MEMTAG_STACK_IS_ON
Christopher Ferrisb4e560e2023-10-26 17:00:00 -0700107 // Query whether the current process has the decay time enabled so that
108 // the memory from allocations are not immediately released to the OS.
109 // Result is assigned to the arg pointer's destination.
110 // arg = bool*
111 // arg_size = sizeof(bool)
112 M_GET_DECAY_TIME_ENABLED = 12,
113#define M_GET_DECAY_TIME_ENABLED M_GET_DECAY_TIME_ENABLED
Peter Collingbourne1e110fb2020-01-09 10:48:22 -0800114};
115
zijunzhaof0fb4182023-06-13 01:19:37 +0000116#pragma clang diagnostic push
117#pragma clang diagnostic ignored "-Wnullability-completeness"
Mitch Phillipse6997d52020-11-30 15:04:14 -0800118typedef struct {
119 // The null-terminated name that the zygote is spawning. Because native
120 // SpecializeCommon (where the GWP-ASan mallopt() is called from) happens
121 // before argv[0] is set, we need the zygote to tell us the new app name.
122 const char* program_name = nullptr;
123
124 // An android_mallopt(M_INITIALIZE_GWP_ASAN) is always issued on process
125 // startup and app startup, regardless of whether GWP-ASan is desired or not.
126 // This allows the process/app's desire to be overwritten by the
127 // "libc.debug.gwp_asan.*.app_default" or "libc.debug.gwp_asan.*.<name>"
128 // system properties, as well as the "GWP_ASAN_*" environment variables.
129 //
130 // Worth noting, the "libc.debug.gwp_asan.*.app_default" sysprops *do not*
131 // apply to system apps. They use the "libc.debug.gwp_asan.*.system_default"
132 // sysprops.
Mitch Phillipsebc2ac92024-05-02 13:25:46 +0200133 //
134 // In recoverable mode, GWP-ASan will detect heap memory safety bugs, and bug
135 // reports will be created by debuggerd, however the process will recover and
136 // continue to function as if the memory safety bug wasn't detected. This
137 // prevents any user-visible impact as apps and processes don't crash, and
138 // probably saves us some CPU time in restarting the process.
139 //
140 // Process sampling enables GWP-ASan, but only a small percentage of the time
141 // (~1%). This helps mitigate any recurring high-frequency problems in certain
142 // processes, as it's highly likely the next restart of said process won't
143 // have GWP-ASan. In addition, for system processes and system apps, this
144 // allows us to mitigate system-wide memory overhead concerns, as each
145 // GWP-ASan enabled process uses ~70KiB of extra memory.
146 enum Mode {
147 // Used by default for apps, or by those that have an explicit
148 // `gwpAsanMode=default` in the manifest.
Mitch Phillipsaa8c2292024-04-09 12:35:31 +0200149 //
Mitch Phillipsebc2ac92024-05-02 13:25:46 +0200150 // Result:
151 // - Android 13 and before: GWP-ASan is not enabled.
152 // - Android 14 and after: Enables GWP-ASan with process sampling in
153 // recoverable mode.
154 APP_MANIFEST_DEFAULT = 3,
155 // This is used by apps that have `gwpAsanMode=always` in the manifest.
156 //
157 // Result:
158 // - Android 14 and before: Enables GWP-ASan in non-recoverable mode,
159 // without process sampling.
160 // - Android 15 and after: Enables GWP-ASan in recoverable mode, without
161 // process sampling.
162 APP_MANIFEST_ALWAYS = 0,
163 // This is used by apps that have `gwpAsanMode=never` in the manifest.
164 //
165 // Result:
166 // - GWP-ASan is not enabled, unless it's force-enabled by a system
167 // property or environment variable.
168 APP_MANIFEST_NEVER = 2,
169 // Used by system processes and system apps.
170 //
171 // Result:
172 // - Android 14 and before: Enables GWP-ASan with process sampling in
173 // non-recoverable mode.
174 // - Android 15 and after: Enables GWP-ASan with process sampling in
175 // recoverable mode.
176 SYSTEM_PROCESS_OR_SYSTEM_APP = 1,
177 // Next enum value = 4. Numbered non-sequentially above to preserve ABI
178 // stability, but now ordered more logically.
Mitch Phillipse6997d52020-11-30 15:04:14 -0800179 };
180
Mitch Phillipsebc2ac92024-05-02 13:25:46 +0200181 Mode mode = APP_MANIFEST_NEVER;
Mitch Phillipse6997d52020-11-30 15:04:14 -0800182} android_mallopt_gwp_asan_options_t;
zijunzhaof0fb4182023-06-13 01:19:37 +0000183#pragma clang diagnostic pop
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000184// Manipulates bionic-specific handling of memory allocation APIs such as
Florian Mayerd71bc4b2022-08-31 22:30:03 +0000185// malloc. Only for use by the Android platform and APEXes.
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000186//
187// On success, returns true. On failure, returns false and sets errno.
zijunzhaof0fb4182023-06-13 01:19:37 +0000188extern "C" bool android_mallopt(int opcode, void* _Nullable arg, size_t arg_size);