blob: 885cc95911af119f5609ab0eadbcb88383209ae5 [file] [log] [blame]
Christopher Ferris63860cb2015-11-16 17:30:32 -08001/*
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
Christopher Ferris63860cb2015-11-16 17:30:32 -080031#include "Config.h"
32#include "DebugData.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080033#include "GuardData.h"
Christopher Ferris5610d5a2023-11-14 15:04:50 -080034#include "LogAllocatorStats.h"
Christopher Ferris4da25032018-03-07 13:38:48 -080035#include "PointerData.h"
36#include "debug_disable.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080037#include "malloc_debug.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080038
Tamas Berghammerac81fe82016-08-26 15:54:59 +010039bool DebugData::Initialize(const char* options) {
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070040 if (!config_.Init(options)) {
Christopher Ferris63860cb2015-11-16 17:30:32 -080041 return false;
42 }
43
44 // Check to see if the options that require a header are enabled.
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070045 if (config_.options() & HEADER_OPTIONS) {
Christopher Ferris63860cb2015-11-16 17:30:32 -080046 // Initialize all of the static header offsets.
Dan Alberta613d0d2017-10-05 16:39:33 -070047 pointer_offset_ = __BIONIC_ALIGN(sizeof(Header), MINIMUM_ALIGNMENT_BYTES);
Christopher Ferris63860cb2015-11-16 17:30:32 -080048
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070049 if (config_.options() & FRONT_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -070050 front_guard.reset(new FrontGuardData(this, config_, &pointer_offset_));
Christopher Ferris63860cb2015-11-16 17:30:32 -080051 }
52
53 extra_bytes_ = pointer_offset_;
54
55 // Initialize all of the non-header data.
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070056 if (config_.options() & REAR_GUARD) {
Christopher Ferris55a89a42016-04-07 17:14:53 -070057 rear_guard.reset(new RearGuardData(this, config_));
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070058 extra_bytes_ += config_.rear_guard_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -080059 }
Christopher Ferris4da25032018-03-07 13:38:48 -080060 }
Christopher Ferris63860cb2015-11-16 17:30:32 -080061
Christopher Ferris4da25032018-03-07 13:38:48 -080062 if (TrackPointers()) {
63 pointer.reset(new PointerData(this));
64 if (!pointer->Initialize(config_)) {
65 return false;
Christopher Ferris63860cb2015-11-16 17:30:32 -080066 }
67 }
68
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070069 if (config_.options() & RECORD_ALLOCS) {
Christopher Ferris7bd01782016-04-20 12:30:58 -070070 record.reset(new RecordData());
71 if (!record->Initialize(config_)) {
72 return false;
73 }
74 }
75
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070076 if (config_.options() & EXPAND_ALLOC) {
77 extra_bytes_ += config_.expand_alloc_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -080078 }
Christopher Ferris5610d5a2023-11-14 15:04:50 -080079
80 if (config_.options() & LOG_ALLOCATOR_STATS_ON_SIGNAL) {
81 if (!LogAllocatorStats::Initialize(config_)) {
82 return false;
83 }
84 }
85
Christopher Ferris63860cb2015-11-16 17:30:32 -080086 return true;
87}
Colin Cross7a28a3c2016-02-07 22:51:15 -080088
89void DebugData::PrepareFork() {
Christopher Ferris4da25032018-03-07 13:38:48 -080090 if (pointer != nullptr) {
91 pointer->PrepareFork();
Denis Hsu1a8106e2018-01-15 18:49:01 +080092 }
Colin Cross7a28a3c2016-02-07 22:51:15 -080093}
94
95void DebugData::PostForkParent() {
Christopher Ferris4da25032018-03-07 13:38:48 -080096 if (pointer != nullptr) {
97 pointer->PostForkParent();
Denis Hsu1a8106e2018-01-15 18:49:01 +080098 }
Colin Cross7a28a3c2016-02-07 22:51:15 -080099}
100
101void DebugData::PostForkChild() {
Christopher Ferris4da25032018-03-07 13:38:48 -0800102 if (pointer != nullptr) {
103 pointer->PostForkChild();
Denis Hsu1a8106e2018-01-15 18:49:01 +0800104 }
Colin Cross7a28a3c2016-02-07 22:51:15 -0800105}