xref: /aosp_15_r20/bionic/libc/malloc_debug/Config.h (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker  * All rights reserved.
4*8d67ca89SAndroid Build Coastguard Worker  *
5*8d67ca89SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*8d67ca89SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
7*8d67ca89SAndroid Build Coastguard Worker  * are met:
8*8d67ca89SAndroid Build Coastguard Worker  *  * Redistributions of source code must retain the above copyright
9*8d67ca89SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer.
10*8d67ca89SAndroid Build Coastguard Worker  *  * Redistributions in binary form must reproduce the above copyright
11*8d67ca89SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer in
12*8d67ca89SAndroid Build Coastguard Worker  *    the documentation and/or other materials provided with the
13*8d67ca89SAndroid Build Coastguard Worker  *    distribution.
14*8d67ca89SAndroid Build Coastguard Worker  *
15*8d67ca89SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*8d67ca89SAndroid Build Coastguard Worker  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*8d67ca89SAndroid Build Coastguard Worker  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18*8d67ca89SAndroid Build Coastguard Worker  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19*8d67ca89SAndroid Build Coastguard Worker  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*8d67ca89SAndroid Build Coastguard Worker  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*8d67ca89SAndroid Build Coastguard Worker  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22*8d67ca89SAndroid Build Coastguard Worker  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*8d67ca89SAndroid Build Coastguard Worker  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*8d67ca89SAndroid Build Coastguard Worker  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25*8d67ca89SAndroid Build Coastguard Worker  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8d67ca89SAndroid Build Coastguard Worker  * SUCH DAMAGE.
27*8d67ca89SAndroid Build Coastguard Worker  */
28*8d67ca89SAndroid Build Coastguard Worker 
29*8d67ca89SAndroid Build Coastguard Worker #pragma once
30*8d67ca89SAndroid Build Coastguard Worker 
31*8d67ca89SAndroid Build Coastguard Worker #include <stdint.h>
32*8d67ca89SAndroid Build Coastguard Worker 
33*8d67ca89SAndroid Build Coastguard Worker #include <string>
34*8d67ca89SAndroid Build Coastguard Worker #include <unordered_map>
35*8d67ca89SAndroid Build Coastguard Worker 
36*8d67ca89SAndroid Build Coastguard Worker constexpr uint64_t FRONT_GUARD = 0x1;
37*8d67ca89SAndroid Build Coastguard Worker constexpr uint64_t REAR_GUARD = 0x2;
38*8d67ca89SAndroid Build Coastguard Worker constexpr uint64_t BACKTRACE = 0x4;
39*8d67ca89SAndroid Build Coastguard Worker constexpr uint64_t FILL_ON_ALLOC = 0x8;
40*8d67ca89SAndroid Build Coastguard Worker constexpr uint64_t FILL_ON_FREE = 0x10;
41*8d67ca89SAndroid Build Coastguard Worker constexpr uint64_t EXPAND_ALLOC = 0x20;
42*8d67ca89SAndroid Build Coastguard Worker constexpr uint64_t FREE_TRACK = 0x40;
43*8d67ca89SAndroid Build Coastguard Worker constexpr uint64_t TRACK_ALLOCS = 0x80;
44*8d67ca89SAndroid Build Coastguard Worker constexpr uint64_t LEAK_TRACK = 0x100;
45*8d67ca89SAndroid Build Coastguard Worker constexpr uint64_t RECORD_ALLOCS = 0x200;
46*8d67ca89SAndroid Build Coastguard Worker constexpr uint64_t BACKTRACE_FULL = 0x400;
47*8d67ca89SAndroid Build Coastguard Worker constexpr uint64_t ABORT_ON_ERROR = 0x800;
48*8d67ca89SAndroid Build Coastguard Worker constexpr uint64_t VERBOSE = 0x1000;
49*8d67ca89SAndroid Build Coastguard Worker constexpr uint64_t CHECK_UNREACHABLE_ON_SIGNAL = 0x2000;
50*8d67ca89SAndroid Build Coastguard Worker constexpr uint64_t BACKTRACE_SPECIFIC_SIZES = 0x4000;
51*8d67ca89SAndroid Build Coastguard Worker constexpr uint64_t LOG_ALLOCATOR_STATS_ON_SIGNAL = 0x8000;
52*8d67ca89SAndroid Build Coastguard Worker constexpr uint64_t LOG_ALLOCATOR_STATS_ON_EXIT = 0x10000;
53*8d67ca89SAndroid Build Coastguard Worker 
54*8d67ca89SAndroid Build Coastguard Worker // In order to guarantee posix compliance, set the minimum alignment
55*8d67ca89SAndroid Build Coastguard Worker // to 8 bytes for 32 bit systems and 16 bytes for 64 bit systems.
56*8d67ca89SAndroid Build Coastguard Worker #if defined(__LP64__)
57*8d67ca89SAndroid Build Coastguard Worker constexpr size_t MINIMUM_ALIGNMENT_BYTES = 16;
58*8d67ca89SAndroid Build Coastguard Worker #else
59*8d67ca89SAndroid Build Coastguard Worker constexpr size_t MINIMUM_ALIGNMENT_BYTES = 8;
60*8d67ca89SAndroid Build Coastguard Worker #endif
61*8d67ca89SAndroid Build Coastguard Worker 
62*8d67ca89SAndroid Build Coastguard Worker // If one or more of these options is set, then a special header is needed.
63*8d67ca89SAndroid Build Coastguard Worker constexpr uint64_t HEADER_OPTIONS = FRONT_GUARD | REAR_GUARD;
64*8d67ca89SAndroid Build Coastguard Worker 
65*8d67ca89SAndroid Build Coastguard Worker class Config {
66*8d67ca89SAndroid Build Coastguard Worker  public:
67*8d67ca89SAndroid Build Coastguard Worker   bool Init(const char* options_str);
68*8d67ca89SAndroid Build Coastguard Worker 
69*8d67ca89SAndroid Build Coastguard Worker   void LogUsage() const;
70*8d67ca89SAndroid Build Coastguard Worker 
options()71*8d67ca89SAndroid Build Coastguard Worker   uint64_t options() const { return options_; }
72*8d67ca89SAndroid Build Coastguard Worker 
backtrace_signal()73*8d67ca89SAndroid Build Coastguard Worker   int backtrace_signal() const { return backtrace_signal_; }
backtrace_dump_signal()74*8d67ca89SAndroid Build Coastguard Worker   int backtrace_dump_signal() const { return backtrace_dump_signal_; }
backtrace_frames()75*8d67ca89SAndroid Build Coastguard Worker   size_t backtrace_frames() const { return backtrace_frames_; }
backtrace_enabled()76*8d67ca89SAndroid Build Coastguard Worker   size_t backtrace_enabled() const { return backtrace_enabled_; }
backtrace_enable_on_signal()77*8d67ca89SAndroid Build Coastguard Worker   size_t backtrace_enable_on_signal() const { return backtrace_enable_on_signal_; }
backtrace_dump_on_exit()78*8d67ca89SAndroid Build Coastguard Worker   bool backtrace_dump_on_exit() const { return backtrace_dump_on_exit_; }
backtrace_dump_prefix()79*8d67ca89SAndroid Build Coastguard Worker   const std::string& backtrace_dump_prefix() const { return backtrace_dump_prefix_; }
80*8d67ca89SAndroid Build Coastguard Worker 
front_guard_bytes()81*8d67ca89SAndroid Build Coastguard Worker   size_t front_guard_bytes() const { return front_guard_bytes_; }
rear_guard_bytes()82*8d67ca89SAndroid Build Coastguard Worker   size_t rear_guard_bytes() const { return rear_guard_bytes_; }
front_guard_value()83*8d67ca89SAndroid Build Coastguard Worker   uint8_t front_guard_value() const { return front_guard_value_; }
rear_guard_value()84*8d67ca89SAndroid Build Coastguard Worker   uint8_t rear_guard_value() const { return rear_guard_value_; }
85*8d67ca89SAndroid Build Coastguard Worker 
expand_alloc_bytes()86*8d67ca89SAndroid Build Coastguard Worker   size_t expand_alloc_bytes() const { return expand_alloc_bytes_; }
87*8d67ca89SAndroid Build Coastguard Worker 
free_track_allocations()88*8d67ca89SAndroid Build Coastguard Worker   size_t free_track_allocations() const { return free_track_allocations_; }
free_track_backtrace_num_frames()89*8d67ca89SAndroid Build Coastguard Worker   size_t free_track_backtrace_num_frames() const { return free_track_backtrace_num_frames_; }
90*8d67ca89SAndroid Build Coastguard Worker 
fill_on_alloc_bytes()91*8d67ca89SAndroid Build Coastguard Worker   size_t fill_on_alloc_bytes() const { return fill_on_alloc_bytes_; }
fill_on_free_bytes()92*8d67ca89SAndroid Build Coastguard Worker   size_t fill_on_free_bytes() const { return fill_on_free_bytes_; }
fill_alloc_value()93*8d67ca89SAndroid Build Coastguard Worker   uint8_t fill_alloc_value() const { return fill_alloc_value_; }
fill_free_value()94*8d67ca89SAndroid Build Coastguard Worker   uint8_t fill_free_value() const { return fill_free_value_; }
95*8d67ca89SAndroid Build Coastguard Worker 
backtrace_min_size_bytes()96*8d67ca89SAndroid Build Coastguard Worker   size_t backtrace_min_size_bytes() const { return backtrace_min_size_bytes_; }
backtrace_max_size_bytes()97*8d67ca89SAndroid Build Coastguard Worker   size_t backtrace_max_size_bytes() const { return backtrace_max_size_bytes_; }
98*8d67ca89SAndroid Build Coastguard Worker 
record_allocs_signal()99*8d67ca89SAndroid Build Coastguard Worker   int record_allocs_signal() const { return record_allocs_signal_; }
record_allocs_num_entries()100*8d67ca89SAndroid Build Coastguard Worker   size_t record_allocs_num_entries() const { return record_allocs_num_entries_; }
record_allocs_file()101*8d67ca89SAndroid Build Coastguard Worker   const std::string& record_allocs_file() const { return record_allocs_file_; }
record_allocs_on_exit()102*8d67ca89SAndroid Build Coastguard Worker   bool record_allocs_on_exit() const { return record_allocs_on_exit_; }
103*8d67ca89SAndroid Build Coastguard Worker 
check_unreachable_signal()104*8d67ca89SAndroid Build Coastguard Worker   int check_unreachable_signal() const { return check_unreachable_signal_; }
105*8d67ca89SAndroid Build Coastguard Worker 
log_allocator_stats_signal()106*8d67ca89SAndroid Build Coastguard Worker   int log_allocator_stats_signal() const { return log_allocator_stats_signal_; }
107*8d67ca89SAndroid Build Coastguard Worker 
108*8d67ca89SAndroid Build Coastguard Worker  private:
109*8d67ca89SAndroid Build Coastguard Worker   struct OptionInfo {
110*8d67ca89SAndroid Build Coastguard Worker     uint64_t option;
111*8d67ca89SAndroid Build Coastguard Worker     bool (Config::*process_func)(const std::string&, const std::string&);
112*8d67ca89SAndroid Build Coastguard Worker   };
113*8d67ca89SAndroid Build Coastguard Worker 
114*8d67ca89SAndroid Build Coastguard Worker   bool ParseValue(const std::string& option, const std::string& value, size_t default_value,
115*8d67ca89SAndroid Build Coastguard Worker                   size_t min_value, size_t max_value, size_t* new_value) const;
116*8d67ca89SAndroid Build Coastguard Worker 
117*8d67ca89SAndroid Build Coastguard Worker   bool ParseValue(const std::string& option, const std::string& value, size_t min_value,
118*8d67ca89SAndroid Build Coastguard Worker                   size_t max_value, size_t* parsed_value) const;
119*8d67ca89SAndroid Build Coastguard Worker 
120*8d67ca89SAndroid Build Coastguard Worker   bool SetGuard(const std::string& option, const std::string& value);
121*8d67ca89SAndroid Build Coastguard Worker   bool SetFrontGuard(const std::string& option, const std::string& value);
122*8d67ca89SAndroid Build Coastguard Worker   bool SetRearGuard(const std::string& option, const std::string& value);
123*8d67ca89SAndroid Build Coastguard Worker 
124*8d67ca89SAndroid Build Coastguard Worker   bool SetFill(const std::string& option, const std::string& value);
125*8d67ca89SAndroid Build Coastguard Worker   bool SetFillOnAlloc(const std::string& option, const std::string& value);
126*8d67ca89SAndroid Build Coastguard Worker   bool SetFillOnFree(const std::string& option, const std::string& value);
127*8d67ca89SAndroid Build Coastguard Worker 
128*8d67ca89SAndroid Build Coastguard Worker   bool SetBacktrace(const std::string& option, const std::string& value);
129*8d67ca89SAndroid Build Coastguard Worker   bool SetBacktraceEnableOnSignal(const std::string& option, const std::string& value);
130*8d67ca89SAndroid Build Coastguard Worker   bool SetBacktraceDumpOnExit(const std::string& option, const std::string& value);
131*8d67ca89SAndroid Build Coastguard Worker   bool SetBacktraceDumpPrefix(const std::string& option, const std::string& value);
132*8d67ca89SAndroid Build Coastguard Worker 
133*8d67ca89SAndroid Build Coastguard Worker   bool SetBacktraceSize(const std::string& option, const std::string& value);
134*8d67ca89SAndroid Build Coastguard Worker   bool SetBacktraceMinSize(const std::string& option, const std::string& value);
135*8d67ca89SAndroid Build Coastguard Worker   bool SetBacktraceMaxSize(const std::string& option, const std::string& value);
136*8d67ca89SAndroid Build Coastguard Worker 
137*8d67ca89SAndroid Build Coastguard Worker   bool SetExpandAlloc(const std::string& option, const std::string& value);
138*8d67ca89SAndroid Build Coastguard Worker 
139*8d67ca89SAndroid Build Coastguard Worker   bool SetFreeTrack(const std::string& option, const std::string& value);
140*8d67ca89SAndroid Build Coastguard Worker   bool SetFreeTrackBacktraceNumFrames(const std::string& option, const std::string& value);
141*8d67ca89SAndroid Build Coastguard Worker 
142*8d67ca89SAndroid Build Coastguard Worker   bool SetRecordAllocs(const std::string& option, const std::string& value);
143*8d67ca89SAndroid Build Coastguard Worker   bool SetRecordAllocsFile(const std::string& option, const std::string& value);
144*8d67ca89SAndroid Build Coastguard Worker   bool SetRecordAllocsOnExit(const std::string& option, const std::string& value);
145*8d67ca89SAndroid Build Coastguard Worker 
146*8d67ca89SAndroid Build Coastguard Worker   bool VerifyValueEmpty(const std::string& option, const std::string& value);
147*8d67ca89SAndroid Build Coastguard Worker 
148*8d67ca89SAndroid Build Coastguard Worker   static bool GetOption(const char** option_str, std::string* option, std::string* value);
149*8d67ca89SAndroid Build Coastguard Worker 
150*8d67ca89SAndroid Build Coastguard Worker   const static std::unordered_map<std::string, OptionInfo> kOptions;
151*8d67ca89SAndroid Build Coastguard Worker 
152*8d67ca89SAndroid Build Coastguard Worker   size_t front_guard_bytes_ = 0;
153*8d67ca89SAndroid Build Coastguard Worker   size_t rear_guard_bytes_ = 0;
154*8d67ca89SAndroid Build Coastguard Worker 
155*8d67ca89SAndroid Build Coastguard Worker   bool backtrace_enable_on_signal_ = false;
156*8d67ca89SAndroid Build Coastguard Worker   int backtrace_signal_ = 0;
157*8d67ca89SAndroid Build Coastguard Worker   int backtrace_dump_signal_ = 0;
158*8d67ca89SAndroid Build Coastguard Worker   bool backtrace_enabled_ = false;
159*8d67ca89SAndroid Build Coastguard Worker   size_t backtrace_frames_ = 0;
160*8d67ca89SAndroid Build Coastguard Worker   bool backtrace_dump_on_exit_ = false;
161*8d67ca89SAndroid Build Coastguard Worker   std::string backtrace_dump_prefix_;
162*8d67ca89SAndroid Build Coastguard Worker   size_t backtrace_min_size_bytes_ = 0;
163*8d67ca89SAndroid Build Coastguard Worker   size_t backtrace_max_size_bytes_ = 0;
164*8d67ca89SAndroid Build Coastguard Worker 
165*8d67ca89SAndroid Build Coastguard Worker   size_t fill_on_alloc_bytes_ = 0;
166*8d67ca89SAndroid Build Coastguard Worker   size_t fill_on_free_bytes_ = 0;
167*8d67ca89SAndroid Build Coastguard Worker 
168*8d67ca89SAndroid Build Coastguard Worker   size_t expand_alloc_bytes_ = 0;
169*8d67ca89SAndroid Build Coastguard Worker 
170*8d67ca89SAndroid Build Coastguard Worker   size_t free_track_allocations_ = 0;
171*8d67ca89SAndroid Build Coastguard Worker   size_t free_track_backtrace_num_frames_ = 0;
172*8d67ca89SAndroid Build Coastguard Worker 
173*8d67ca89SAndroid Build Coastguard Worker   int record_allocs_signal_ = 0;
174*8d67ca89SAndroid Build Coastguard Worker   size_t record_allocs_num_entries_ = 0;
175*8d67ca89SAndroid Build Coastguard Worker   std::string record_allocs_file_;
176*8d67ca89SAndroid Build Coastguard Worker   bool record_allocs_on_exit_ = false;
177*8d67ca89SAndroid Build Coastguard Worker 
178*8d67ca89SAndroid Build Coastguard Worker   uint64_t options_ = 0;
179*8d67ca89SAndroid Build Coastguard Worker   uint8_t fill_alloc_value_;
180*8d67ca89SAndroid Build Coastguard Worker   uint8_t fill_free_value_;
181*8d67ca89SAndroid Build Coastguard Worker   uint8_t front_guard_value_;
182*8d67ca89SAndroid Build Coastguard Worker   uint8_t rear_guard_value_;
183*8d67ca89SAndroid Build Coastguard Worker 
184*8d67ca89SAndroid Build Coastguard Worker   int check_unreachable_signal_ = 0;
185*8d67ca89SAndroid Build Coastguard Worker   int log_allocator_stats_signal_ = 0;
186*8d67ca89SAndroid Build Coastguard Worker };
187