| 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 |  | 
| Ryan Savitski | f77928d | 2019-01-23 18:39:35 +0000 | [diff] [blame] | 31 | #include <stdbool.h> | 
| Peter Collingbourne | 1e110fb | 2020-01-09 10:48:22 -0800 | [diff] [blame] | 32 | #include <stdint.h> | 
| Ryan Savitski | f77928d | 2019-01-23 18:39:35 +0000 | [diff] [blame] | 33 |  | 
| Christopher Ferris | 30659fd | 2019-04-15 19:01:08 -0700 | [diff] [blame] | 34 | // Structures for android_mallopt. | 
 | 35 |  | 
 | 36 | typedef struct { | 
 | 37 |   // Pointer to the buffer allocated by a call to M_GET_MALLOC_LEAK_INFO. | 
 | 38 |   uint8_t* buffer; | 
 | 39 |   // The size of the "info" buffer. | 
 | 40 |   size_t overall_size; | 
 | 41 |   // The size of a single entry. | 
 | 42 |   size_t info_size; | 
 | 43 |   // The sum of all allocations that have been tracked. Does not include | 
 | 44 |   // any heap overhead. | 
 | 45 |   size_t total_memory; | 
 | 46 |   // The maximum number of backtrace entries. | 
 | 47 |   size_t backtrace_size; | 
 | 48 | } android_mallopt_leak_info_t; | 
 | 49 |  | 
| Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 50 | // Opcodes for android_mallopt. | 
 | 51 |  | 
| Ryan Savitski | f77928d | 2019-01-23 18:39:35 +0000 | [diff] [blame] | 52 | enum { | 
| Florian Mayer | db59b89 | 2018-11-27 17:06:54 +0000 | [diff] [blame] | 53 |   // Marks the calling process as a profileable zygote child, possibly | 
 | 54 |   // initializing profiling infrastructure. | 
| Ryan Savitski | f77928d | 2019-01-23 18:39:35 +0000 | [diff] [blame] | 55 |   M_INIT_ZYGOTE_CHILD_PROFILING = 1, | 
 | 56 | #define M_INIT_ZYGOTE_CHILD_PROFILING M_INIT_ZYGOTE_CHILD_PROFILING | 
| Florian Mayer | db59b89 | 2018-11-27 17:06:54 +0000 | [diff] [blame] | 57 |   M_RESET_HOOKS = 2, | 
 | 58 | #define M_RESET_HOOKS M_RESET_HOOKS | 
| Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame] | 59 |   // Set an upper bound on the total size in bytes of all allocations made | 
 | 60 |   // using the memory allocation APIs. | 
 | 61 |   //   arg = size_t* | 
 | 62 |   //   arg_size = sizeof(size_t) | 
 | 63 |   M_SET_ALLOCATION_LIMIT_BYTES = 3, | 
 | 64 | #define M_SET_ALLOCATION_LIMIT_BYTES M_SET_ALLOCATION_LIMIT_BYTES | 
| Christopher Ferris | 8189e77 | 2019-04-09 16:37:23 -0700 | [diff] [blame] | 65 |   // Called after the zygote forks to indicate this is a child. | 
 | 66 |   M_SET_ZYGOTE_CHILD = 4, | 
 | 67 | #define M_SET_ZYGOTE_CHILD M_SET_ZYGOTE_CHILD | 
| Christopher Ferris | 30659fd | 2019-04-15 19:01:08 -0700 | [diff] [blame] | 68 |  | 
 | 69 |   // Options to dump backtraces of allocations. These options only | 
 | 70 |   // work when malloc debug has been enabled. | 
 | 71 |  | 
 | 72 |   // Writes the backtrace information of all current allocations to a file. | 
 | 73 |   // NOTE: arg_size has to be sizeof(FILE*) because FILE is an opaque type. | 
 | 74 |   //   arg = FILE* | 
 | 75 |   //   arg_size = sizeof(FILE*) | 
 | 76 |   M_WRITE_MALLOC_LEAK_INFO_TO_FILE = 5, | 
 | 77 | #define M_WRITE_MALLOC_LEAK_INFO_TO_FILE M_WRITE_MALLOC_LEAK_INFO_TO_FILE | 
 | 78 |   // Get information about the backtraces of all | 
 | 79 |   //   arg = android_mallopt_leak_info_t* | 
 | 80 |   //   arg_size = sizeof(android_mallopt_leak_info_t) | 
 | 81 |   M_GET_MALLOC_LEAK_INFO = 6, | 
 | 82 | #define M_GET_MALLOC_LEAK_INFO M_GET_MALLOC_LEAK_INFO | 
 | 83 |   // Free the memory allocated and returned by M_GET_MALLOC_LEAK_INFO. | 
 | 84 |   //   arg = android_mallopt_leak_info_t* | 
 | 85 |   //   arg_size = sizeof(android_mallopt_leak_info_t) | 
 | 86 |   M_FREE_MALLOC_LEAK_INFO = 7, | 
 | 87 | #define M_FREE_MALLOC_LEAK_INFO M_FREE_MALLOC_LEAK_INFO | 
| Peter Collingbourne | 1e110fb | 2020-01-09 10:48:22 -0800 | [diff] [blame] | 88 |   // Change the heap tagging state. The program must be single threaded at the point when the | 
 | 89 |   // android_mallopt function is called. | 
 | 90 |   //   arg = HeapTaggingLevel* | 
 | 91 |   //   arg_size = sizeof(HeapTaggingLevel) | 
 | 92 |   M_SET_HEAP_TAGGING_LEVEL = 8, | 
 | 93 | #define M_SET_HEAP_TAGGING_LEVEL M_SET_HEAP_TAGGING_LEVEL | 
| Ryan Savitski | 175c886 | 2020-01-02 19:54:57 +0000 | [diff] [blame] | 94 |   // Query whether the current process is considered to be profileable by the | 
 | 95 |   // Android platform. Result is assigned to the arg pointer's destination. | 
 | 96 |   //   arg = bool* | 
 | 97 |   //   arg_size = sizeof(bool) | 
 | 98 |   M_GET_PROCESS_PROFILEABLE = 9, | 
 | 99 | #define M_GET_PROCESS_PROFILEABLE M_GET_PROCESS_PROFILEABLE | 
| Mitch Phillips | f3968e8 | 2020-01-31 19:57:04 -0800 | [diff] [blame] | 100 |   // Maybe enable GWP-ASan. Set *arg to force GWP-ASan to be turned on, | 
 | 101 |   // otherwise this mallopt() will internally decide whether to sample the | 
 | 102 |   // process. The program must be single threaded at the point when the | 
 | 103 |   // android_mallopt function is called. | 
 | 104 |   //   arg = bool* | 
 | 105 |   //   arg_size = sizeof(bool) | 
 | 106 |   M_INITIALIZE_GWP_ASAN = 10, | 
 | 107 | #define M_INITIALIZE_GWP_ASAN M_INITIALIZE_GWP_ASAN | 
| Peter Collingbourne | 1e110fb | 2020-01-09 10:48:22 -0800 | [diff] [blame] | 108 | }; | 
 | 109 |  | 
 | 110 | enum HeapTaggingLevel { | 
 | 111 |   // Disable heap tagging. The program must use prctl(PR_SET_TAGGED_ADDR_CTRL) to disable memory tag | 
 | 112 |   // checks before disabling heap tagging. Heap tagging may not be re-enabled after being disabled. | 
 | 113 |   M_HEAP_TAGGING_LEVEL_NONE = 0, | 
| Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 114 |   // Address-only tagging. Heap pointers have a non-zero tag in the most significant byte which is | 
 | 115 |   // checked in free(). Memory accesses ignore the tag. | 
 | 116 |   M_HEAP_TAGGING_LEVEL_TBI = 1, | 
| Peter Collingbourne | 45819dd | 2020-01-09 11:00:43 -0800 | [diff] [blame] | 117 |   // Enable heap tagging if supported, at a level appropriate for asynchronous memory tag checks. | 
 | 118 |   M_HEAP_TAGGING_LEVEL_ASYNC = 2, | 
| Peter Collingbourne | d306001 | 2020-04-01 19:54:48 -0700 | [diff] [blame] | 119 |   // Enable heap tagging if supported, at a level appropriate for synchronous memory tag checks. | 
 | 120 |   M_HEAP_TAGGING_LEVEL_SYNC = 3, | 
| Ryan Savitski | f77928d | 2019-01-23 18:39:35 +0000 | [diff] [blame] | 121 | }; | 
| Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 122 |  | 
 | 123 | // Manipulates bionic-specific handling of memory allocation APIs such as | 
 | 124 | // malloc. Only for use by the Android platform itself. | 
 | 125 | // | 
 | 126 | // On success, returns true. On failure, returns false and sets errno. | 
 | 127 | extern "C" bool android_mallopt(int opcode, void* arg, size_t arg_size); |