blob: 313cbdf759f1146b7c11240526cbf3a45473f526 [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.
36
37typedef 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 Savitskiecc37e32018-12-14 15:57:21 +000051// Opcodes for android_mallopt.
52
Ryan Savitskif77928d2019-01-23 18:39:35 +000053enum {
Florian Mayerdb59b892018-11-27 17:06:54 +000054 // Marks the calling process as a profileable zygote child, possibly
55 // initializing profiling infrastructure.
Ryan Savitskif77928d2019-01-23 18:39:35 +000056 M_INIT_ZYGOTE_CHILD_PROFILING = 1,
57#define M_INIT_ZYGOTE_CHILD_PROFILING M_INIT_ZYGOTE_CHILD_PROFILING
Florian Mayerdb59b892018-11-27 17:06:54 +000058 M_RESET_HOOKS = 2,
59#define M_RESET_HOOKS M_RESET_HOOKS
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080060 // 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 Ferris8189e772019-04-09 16:37:23 -070066 // 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 Ferris30659fd2019-04-15 19:01:08 -070069
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
Peter Collingbourne9eb85bf2020-11-02 13:35:28 -080089 // Change the heap tagging state. May be called at any time including when
90 // multiple threads are running.
Peter Collingbourne1e110fb2020-01-09 10:48:22 -080091 // arg = HeapTaggingLevel*
92 // arg_size = sizeof(HeapTaggingLevel)
93 M_SET_HEAP_TAGGING_LEVEL = 8,
94#define M_SET_HEAP_TAGGING_LEVEL M_SET_HEAP_TAGGING_LEVEL
Ryan Savitski175c8862020-01-02 19:54:57 +000095 // Query whether the current process is considered to be profileable by the
96 // Android platform. Result is assigned to the arg pointer's destination.
97 // arg = bool*
98 // arg_size = sizeof(bool)
99 M_GET_PROCESS_PROFILEABLE = 9,
100#define M_GET_PROCESS_PROFILEABLE M_GET_PROCESS_PROFILEABLE
Mitch Phillipsf3968e82020-01-31 19:57:04 -0800101 // Maybe enable GWP-ASan. Set *arg to force GWP-ASan to be turned on,
102 // otherwise this mallopt() will internally decide whether to sample the
103 // process. The program must be single threaded at the point when the
104 // android_mallopt function is called.
105 // arg = bool*
106 // arg_size = sizeof(bool)
107 M_INITIALIZE_GWP_ASAN = 10,
108#define M_INITIALIZE_GWP_ASAN M_INITIALIZE_GWP_ASAN
Peter Collingbourne5d3aa862020-09-11 15:05:17 -0700109 // Disable heap initialization across the whole process. If the hardware supports memory
110 // tagging, it also disables memory tagging. May be called at any time including
111 // when multiple threads are running. arg and arg_size are unused and must be set to 0.
112 // Note that the memory mitigations are only implemented in scudo and therefore this API call will
113 // have no effect when using another allocator.
114 M_DISABLE_MEMORY_MITIGATIONS = 11,
115#define M_DISABLE_MEMORY_MITIGATIONS M_DISABLE_MEMORY_MITIGATIONS
Peter Collingbourne1e110fb2020-01-09 10:48:22 -0800116};
117
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000118// Manipulates bionic-specific handling of memory allocation APIs such as
119// malloc. Only for use by the Android platform itself.
120//
121// On success, returns true. On failure, returns false and sets errno.
122extern "C" bool android_mallopt(int opcode, void* arg, size_t arg_size);