blob: 29e4edc29f430abc3baa792eed87922fdcc39382 [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
Ryan Savitskif77928d2019-01-23 18:39:35 +000031#include <stdbool.h>
Peter Collingbourne1e110fb2020-01-09 10:48:22 -080032#include <stdint.h>
Ryan Savitskif77928d2019-01-23 18:39:35 +000033
Christopher Ferris30659fd2019-04-15 19:01:08 -070034// Structures for android_mallopt.
35
36typedef 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 Savitskiecc37e32018-12-14 15:57:21 +000050// Opcodes for android_mallopt.
51
Ryan Savitskif77928d2019-01-23 18:39:35 +000052enum {
Florian Mayerdb59b892018-11-27 17:06:54 +000053 // Marks the calling process as a profileable zygote child, possibly
54 // initializing profiling infrastructure.
Ryan Savitskif77928d2019-01-23 18:39:35 +000055 M_INIT_ZYGOTE_CHILD_PROFILING = 1,
56#define M_INIT_ZYGOTE_CHILD_PROFILING M_INIT_ZYGOTE_CHILD_PROFILING
Florian Mayerdb59b892018-11-27 17:06:54 +000057 M_RESET_HOOKS = 2,
58#define M_RESET_HOOKS M_RESET_HOOKS
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080059 // 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 Ferris8189e772019-04-09 16:37:23 -070065 // 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 Ferris30659fd2019-04-15 19:01:08 -070068
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 Collingbourne1e110fb2020-01-09 10:48:22 -080088 // 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 Savitski175c8862020-01-02 19:54:57 +000094 // 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
Peter Collingbourne1e110fb2020-01-09 10:48:22 -0800100};
101
102enum HeapTaggingLevel {
103 // Disable heap tagging. The program must use prctl(PR_SET_TAGGED_ADDR_CTRL) to disable memory tag
104 // checks before disabling heap tagging. Heap tagging may not be re-enabled after being disabled.
105 M_HEAP_TAGGING_LEVEL_NONE = 0,
Ryan Savitskif77928d2019-01-23 18:39:35 +0000106};
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000107
108// Manipulates bionic-specific handling of memory allocation APIs such as
109// malloc. Only for use by the Android platform itself.
110//
111// On success, returns true. On failure, returns false and sets errno.
112extern "C" bool android_mallopt(int opcode, void* arg, size_t arg_size);