| 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 |  | 
| Elliott Hughes | cbc80ba | 2018-02-13 14:26:29 -0800 | [diff] [blame] | 29 | #pragma once | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 30 |  | 
|  | 31 | #include <stdint.h> | 
|  | 32 |  | 
|  | 33 | #include <memory> | 
|  | 34 | #include <vector> | 
|  | 35 |  | 
| Josh Gao | 4956c37 | 2019-12-19 16:35:51 -0800 | [diff] [blame] | 36 | #include <platform/bionic/macros.h> | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 37 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 38 | #include "Config.h" | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 39 | #include "GuardData.h" | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 40 | #include "PointerData.h" | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 41 | #include "RecordData.h" | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 42 | #include "malloc_debug.h" | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 43 |  | 
|  | 44 | class DebugData { | 
|  | 45 | public: | 
|  | 46 | DebugData() = default; | 
|  | 47 | ~DebugData() = default; | 
|  | 48 |  | 
| Tamas Berghammer | ac81fe8 | 2016-08-26 15:54:59 +0100 | [diff] [blame] | 49 | bool Initialize(const char* options); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 50 |  | 
|  | 51 | static bool Disabled(); | 
|  | 52 |  | 
|  | 53 | inline void* GetPointer(const Header* header) { | 
|  | 54 | uintptr_t value = reinterpret_cast<uintptr_t>(header); | 
|  | 55 | return reinterpret_cast<void*>(value + pointer_offset_); | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | Header* GetHeader(const void* pointer) { | 
|  | 59 | uintptr_t value = reinterpret_cast<uintptr_t>(pointer); | 
|  | 60 | return reinterpret_cast<Header*>(value - pointer_offset_); | 
|  | 61 | } | 
|  | 62 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 63 | uint8_t* GetFrontGuard(const Header* header) { | 
|  | 64 | uintptr_t value = reinterpret_cast<uintptr_t>(header); | 
|  | 65 | return reinterpret_cast<uint8_t*>(value + front_guard->offset()); | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | uint8_t* GetRearGuard(const Header* header) { | 
|  | 69 | uintptr_t value = reinterpret_cast<uintptr_t>(GetPointer(header)); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 70 | return reinterpret_cast<uint8_t*>(value + header->size); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 71 | } | 
|  | 72 |  | 
|  | 73 | const Config& config() { return config_; } | 
|  | 74 | size_t pointer_offset() { return pointer_offset_; } | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 75 | size_t extra_bytes() { return extra_bytes_; } | 
|  | 76 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 77 | bool TrackPointers() { return config_.options() & TRACK_ALLOCS; } | 
|  | 78 |  | 
|  | 79 | bool HeaderEnabled() { return config_.options() & HEADER_OPTIONS; } | 
|  | 80 |  | 
| Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 81 | void PrepareFork(); | 
|  | 82 | void PostForkParent(); | 
|  | 83 | void PostForkChild(); | 
|  | 84 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 85 | std::unique_ptr<FrontGuardData> front_guard; | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 86 | std::unique_ptr<PointerData> pointer; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 87 | std::unique_ptr<RearGuardData> rear_guard; | 
| Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame] | 88 | std::unique_ptr<RecordData> record; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 89 |  | 
|  | 90 | private: | 
|  | 91 | size_t extra_bytes_ = 0; | 
|  | 92 |  | 
|  | 93 | size_t pointer_offset_ = 0; | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 94 |  | 
|  | 95 | Config config_; | 
|  | 96 |  | 
| Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 97 | BIONIC_DISALLOW_COPY_AND_ASSIGN(DebugData); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 98 | }; | 
|  | 99 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 100 | extern DebugData* g_debug; |