xref: /aosp_15_r20/bionic/libc/malloc_debug/DebugData.cpp (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1 /*
2  * Copyright (C) 2015 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 #include <stdint.h>
30 
31 #include "Config.h"
32 #include "DebugData.h"
33 #include "GuardData.h"
34 #include "LogAllocatorStats.h"
35 #include "PointerData.h"
36 #include "debug_disable.h"
37 #include "malloc_debug.h"
38 
Initialize(const char * options)39 bool DebugData::Initialize(const char* options) {
40   if (!config_.Init(options)) {
41     return false;
42   }
43 
44   // Check to see if the options that require a header are enabled.
45   if (config_.options() & HEADER_OPTIONS) {
46     // Initialize all of the static header offsets.
47     pointer_offset_ = __BIONIC_ALIGN(sizeof(Header), MINIMUM_ALIGNMENT_BYTES);
48 
49     if (config_.options() & FRONT_GUARD) {
50       front_guard.reset(new FrontGuardData(this, config_, &pointer_offset_));
51     }
52 
53     extra_bytes_ = pointer_offset_;
54 
55     // Initialize all of the non-header data.
56     if (config_.options() & REAR_GUARD) {
57       rear_guard.reset(new RearGuardData(this, config_));
58       extra_bytes_ += config_.rear_guard_bytes();
59     }
60   }
61 
62   if (TrackPointers()) {
63     pointer.reset(new PointerData(this));
64     if (!pointer->Initialize(config_)) {
65       return false;
66     }
67   }
68 
69   if (config_.options() & RECORD_ALLOCS) {
70     record.reset(new RecordData());
71     if (!record->Initialize(config_)) {
72       return false;
73     }
74   }
75 
76   if (config_.options() & EXPAND_ALLOC) {
77     extra_bytes_ += config_.expand_alloc_bytes();
78   }
79 
80   if (config_.options() & LOG_ALLOCATOR_STATS_ON_SIGNAL) {
81     if (!LogAllocatorStats::Initialize(config_)) {
82       return false;
83     }
84   }
85 
86   return true;
87 }
88 
PrepareFork()89 void DebugData::PrepareFork() {
90   if (pointer != nullptr) {
91     pointer->PrepareFork();
92   }
93 }
94 
PostForkParent()95 void DebugData::PostForkParent() {
96   if (pointer != nullptr) {
97     pointer->PostForkParent();
98   }
99 }
100 
PostForkChild()101 void DebugData::PostForkChild() {
102   if (pointer != nullptr) {
103     pointer->PostForkChild();
104   }
105 }
106