| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 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 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 31 | #include "Config.h" | 
 | 32 | #include "DebugData.h" | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 33 | #include "GuardData.h" | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 34 | #include "PointerData.h" | 
 | 35 | #include "debug_disable.h" | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 36 | #include "malloc_debug.h" | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 37 |  | 
| Tamas Berghammer | ac81fe8 | 2016-08-26 15:54:59 +0100 | [diff] [blame] | 38 | bool DebugData::Initialize(const char* options) { | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 39 |   if (!config_.Init(options)) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 40 |     return false; | 
 | 41 |   } | 
 | 42 |  | 
 | 43 |   // Check to see if the options that require a header are enabled. | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 44 |   if (config_.options() & HEADER_OPTIONS) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 45 |     // Initialize all of the static header offsets. | 
| Dan Albert | a613d0d | 2017-10-05 16:39:33 -0700 | [diff] [blame] | 46 |     pointer_offset_ = __BIONIC_ALIGN(sizeof(Header), MINIMUM_ALIGNMENT_BYTES); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 47 |  | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 48 |     if (config_.options() & FRONT_GUARD) { | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 49 |       front_guard.reset(new FrontGuardData(this, config_, &pointer_offset_)); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 50 |     } | 
 | 51 |  | 
 | 52 |     extra_bytes_ = pointer_offset_; | 
 | 53 |  | 
 | 54 |     // Initialize all of the non-header data. | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 55 |     if (config_.options() & REAR_GUARD) { | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 56 |       rear_guard.reset(new RearGuardData(this, config_)); | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 57 |       extra_bytes_ += config_.rear_guard_bytes(); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 58 |     } | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 59 |   } | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 60 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 61 |   if (TrackPointers()) { | 
 | 62 |     pointer.reset(new PointerData(this)); | 
 | 63 |     if (!pointer->Initialize(config_)) { | 
 | 64 |       return false; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 65 |     } | 
 | 66 |   } | 
 | 67 |  | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 68 |   if (config_.options() & RECORD_ALLOCS) { | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 69 |     record.reset(new RecordData()); | 
 | 70 |     if (!record->Initialize(config_)) { | 
 | 71 |       return false; | 
 | 72 |     } | 
 | 73 |   } | 
 | 74 |  | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 75 |   if (config_.options() & EXPAND_ALLOC) { | 
 | 76 |     extra_bytes_ += config_.expand_alloc_bytes(); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 77 |   } | 
 | 78 |   return true; | 
 | 79 | } | 
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 80 |  | 
 | 81 | void DebugData::PrepareFork() { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 82 |   if (pointer != nullptr) { | 
 | 83 |     pointer->PrepareFork(); | 
| Denis Hsu | 1a8106e | 2018-01-15 18:49:01 +0800 | [diff] [blame] | 84 |   } | 
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 85 | } | 
 | 86 |  | 
 | 87 | void DebugData::PostForkParent() { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 88 |   if (pointer != nullptr) { | 
 | 89 |     pointer->PostForkParent(); | 
| Denis Hsu | 1a8106e | 2018-01-15 18:49:01 +0800 | [diff] [blame] | 90 |   } | 
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 91 | } | 
 | 92 |  | 
 | 93 | void DebugData::PostForkChild() { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 94 |   if (pointer != nullptr) { | 
 | 95 |     pointer->PostForkChild(); | 
| Denis Hsu | 1a8106e | 2018-01-15 18:49:01 +0800 | [diff] [blame] | 96 |   } | 
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 97 | } |