| Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 1 | /* | 
 | 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 Hughes | 446b4dd | 2021-01-14 13:34:20 -0800 | [diff] [blame] | 31 | #include <malloc.h> | 
| Ryan Savitski | f77928d | 2019-01-23 18:39:35 +0000 | [diff] [blame] | 32 | #include <stdbool.h> | 
| Peter Collingbourne | 1e110fb | 2020-01-09 10:48:22 -0800 | [diff] [blame] | 33 | #include <stdint.h> | 
| Ryan Savitski | f77928d | 2019-01-23 18:39:35 +0000 | [diff] [blame] | 34 |  | 
| Christopher Ferris | 30659fd | 2019-04-15 19:01:08 -0700 | [diff] [blame] | 35 | // Structures for android_mallopt. | 
 | 36 |  | 
 | 37 | typedef struct { | 
 | 38 |   // Pointer to the buffer allocated by a call to M_GET_MALLOC_LEAK_INFO. | 
 | 39 |   uint8_t* buffer; | 
 | 40 |   // The size of the "info" buffer. | 
 | 41 |   size_t overall_size; | 
 | 42 |   // The size of a single entry. | 
 | 43 |   size_t info_size; | 
 | 44 |   // The sum of all allocations that have been tracked. Does not include | 
 | 45 |   // any heap overhead. | 
 | 46 |   size_t total_memory; | 
 | 47 |   // The maximum number of backtrace entries. | 
 | 48 |   size_t backtrace_size; | 
 | 49 | } android_mallopt_leak_info_t; | 
 | 50 |  | 
| Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 51 | // Opcodes for android_mallopt. | 
 | 52 |  | 
| Ryan Savitski | f77928d | 2019-01-23 18:39:35 +0000 | [diff] [blame] | 53 | enum { | 
| Florian Mayer | db59b89 | 2018-11-27 17:06:54 +0000 | [diff] [blame] | 54 |   // Marks the calling process as a profileable zygote child, possibly | 
 | 55 |   // initializing profiling infrastructure. | 
| Ryan Savitski | f77928d | 2019-01-23 18:39:35 +0000 | [diff] [blame] | 56 |   M_INIT_ZYGOTE_CHILD_PROFILING = 1, | 
 | 57 | #define M_INIT_ZYGOTE_CHILD_PROFILING M_INIT_ZYGOTE_CHILD_PROFILING | 
| Florian Mayer | db59b89 | 2018-11-27 17:06:54 +0000 | [diff] [blame] | 58 |   M_RESET_HOOKS = 2, | 
 | 59 | #define M_RESET_HOOKS M_RESET_HOOKS | 
| Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame] | 60 |   // Set an upper bound on the total size in bytes of all allocations made | 
 | 61 |   // using the memory allocation APIs. | 
 | 62 |   //   arg = size_t* | 
 | 63 |   //   arg_size = sizeof(size_t) | 
 | 64 |   M_SET_ALLOCATION_LIMIT_BYTES = 3, | 
 | 65 | #define M_SET_ALLOCATION_LIMIT_BYTES M_SET_ALLOCATION_LIMIT_BYTES | 
| Christopher Ferris | 8189e77 | 2019-04-09 16:37:23 -0700 | [diff] [blame] | 66 |   // Called after the zygote forks to indicate this is a child. | 
 | 67 |   M_SET_ZYGOTE_CHILD = 4, | 
 | 68 | #define M_SET_ZYGOTE_CHILD M_SET_ZYGOTE_CHILD | 
| Christopher Ferris | 30659fd | 2019-04-15 19:01:08 -0700 | [diff] [blame] | 69 |  | 
 | 70 |   // Options to dump backtraces of allocations. These options only | 
 | 71 |   // work when malloc debug has been enabled. | 
 | 72 |  | 
 | 73 |   // Writes the backtrace information of all current allocations to a file. | 
 | 74 |   // NOTE: arg_size has to be sizeof(FILE*) because FILE is an opaque type. | 
 | 75 |   //   arg = FILE* | 
 | 76 |   //   arg_size = sizeof(FILE*) | 
 | 77 |   M_WRITE_MALLOC_LEAK_INFO_TO_FILE = 5, | 
 | 78 | #define M_WRITE_MALLOC_LEAK_INFO_TO_FILE M_WRITE_MALLOC_LEAK_INFO_TO_FILE | 
 | 79 |   // Get information about the backtraces of all | 
 | 80 |   //   arg = android_mallopt_leak_info_t* | 
 | 81 |   //   arg_size = sizeof(android_mallopt_leak_info_t) | 
 | 82 |   M_GET_MALLOC_LEAK_INFO = 6, | 
 | 83 | #define M_GET_MALLOC_LEAK_INFO M_GET_MALLOC_LEAK_INFO | 
 | 84 |   // Free the memory allocated and returned by M_GET_MALLOC_LEAK_INFO. | 
 | 85 |   //   arg = android_mallopt_leak_info_t* | 
 | 86 |   //   arg_size = sizeof(android_mallopt_leak_info_t) | 
 | 87 |   M_FREE_MALLOC_LEAK_INFO = 7, | 
 | 88 | #define M_FREE_MALLOC_LEAK_INFO M_FREE_MALLOC_LEAK_INFO | 
| Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 89 |   // Query whether the current process is considered to be profileable by the | 
 | 90 |   // Android platform. Result is assigned to the arg pointer's destination. | 
 | 91 |   //   arg = bool* | 
 | 92 |   //   arg_size = sizeof(bool) | 
 | 93 |   M_GET_PROCESS_PROFILEABLE = 9, | 
 | 94 | #define M_GET_PROCESS_PROFILEABLE M_GET_PROCESS_PROFILEABLE | 
| Mitch Phillips | f3968e8 | 2020-01-31 19:57:04 -0800 | [diff] [blame] | 95 |   // Maybe enable GWP-ASan. Set *arg to force GWP-ASan to be turned on, | 
 | 96 |   // otherwise this mallopt() will internally decide whether to sample the | 
 | 97 |   // process. The program must be single threaded at the point when the | 
 | 98 |   // android_mallopt function is called. | 
| Mitch Phillips | e6997d5 | 2020-11-30 15:04:14 -0800 | [diff] [blame] | 99 |   //   arg = android_mallopt_gwp_asan_options_t* | 
 | 100 |   //   arg_size = sizeof(android_mallopt_gwp_asan_options_t) | 
| Mitch Phillips | f3968e8 | 2020-01-31 19:57:04 -0800 | [diff] [blame] | 101 |   M_INITIALIZE_GWP_ASAN = 10, | 
 | 102 | #define M_INITIALIZE_GWP_ASAN M_INITIALIZE_GWP_ASAN | 
| Florian Mayer | cc61ad8 | 2022-08-31 11:43:30 -0700 | [diff] [blame] | 103 |   // Query whether memtag stack is enabled for this process. | 
 | 104 |   M_MEMTAG_STACK_IS_ON = 11, | 
 | 105 | #define M_MEMTAG_STACK_IS_ON M_MEMTAG_STACK_IS_ON | 
| Peter Collingbourne | 1e110fb | 2020-01-09 10:48:22 -0800 | [diff] [blame] | 106 | }; | 
 | 107 |  | 
| Mitch Phillips | e6997d5 | 2020-11-30 15:04:14 -0800 | [diff] [blame] | 108 | typedef struct { | 
 | 109 |   // The null-terminated name that the zygote is spawning. Because native | 
 | 110 |   // SpecializeCommon (where the GWP-ASan mallopt() is called from) happens | 
 | 111 |   // before argv[0] is set, we need the zygote to tell us the new app name. | 
 | 112 |   const char* program_name = nullptr; | 
 | 113 |  | 
 | 114 |   // An android_mallopt(M_INITIALIZE_GWP_ASAN) is always issued on process | 
 | 115 |   // startup and app startup, regardless of whether GWP-ASan is desired or not. | 
 | 116 |   // This allows the process/app's desire to be overwritten by the | 
 | 117 |   // "libc.debug.gwp_asan.*.app_default" or "libc.debug.gwp_asan.*.<name>" | 
 | 118 |   // system properties, as well as the "GWP_ASAN_*" environment variables. | 
 | 119 |   // | 
 | 120 |   // Worth noting, the "libc.debug.gwp_asan.*.app_default" sysprops *do not* | 
 | 121 |   // apply to system apps. They use the "libc.debug.gwp_asan.*.system_default" | 
 | 122 |   // sysprops. | 
 | 123 |   enum Action { | 
 | 124 |     // The app has opted-in to GWP-ASan, and should always have it enabled. This | 
 | 125 |     // should only be used by apps. | 
 | 126 |     TURN_ON_FOR_APP, | 
 | 127 |     // System processes apps have GWP-ASan enabled by default, but use the | 
 | 128 |     // process sampling method. | 
 | 129 |     TURN_ON_WITH_SAMPLING, | 
 | 130 |     // Non-system apps don't have GWP-ASan by default. | 
 | 131 |     DONT_TURN_ON_UNLESS_OVERRIDDEN, | 
 | 132 |     // Note: GWP-ASan cannot be disabled once it's been enabled. | 
 | 133 |   }; | 
 | 134 |  | 
 | 135 |   Action desire = DONT_TURN_ON_UNLESS_OVERRIDDEN; | 
 | 136 | } android_mallopt_gwp_asan_options_t; | 
 | 137 |  | 
| Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 138 | // Manipulates bionic-specific handling of memory allocation APIs such as | 
| Florian Mayer | d71bc4b | 2022-08-31 22:30:03 +0000 | [diff] [blame] | 139 | // malloc. Only for use by the Android platform and APEXes. | 
| Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 140 | // | 
 | 141 | // On success, returns true. On failure, returns false and sets errno. | 
 | 142 | extern "C" bool android_mallopt(int opcode, void* arg, size_t arg_size); |