| 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 | #include <string.h> | 
|  | 31 |  | 
|  | 32 | #include <vector> | 
|  | 33 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 34 | #include "Config.h" | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 35 | #include "DebugData.h" | 
|  | 36 | #include "GuardData.h" | 
|  | 37 | #include "backtrace.h" | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 38 | #include "debug_disable.h" | 
|  | 39 | #include "debug_log.h" | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 40 | #include "malloc_debug.h" | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 41 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 42 | GuardData::GuardData(DebugData* debug_data, int init_value, size_t num_bytes) | 
|  | 43 | : OptionData(debug_data) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 44 | // Create a buffer for fast comparisons of the front guard. | 
|  | 45 | cmp_mem_.resize(num_bytes); | 
|  | 46 | memset(cmp_mem_.data(), init_value, cmp_mem_.size()); | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | void GuardData::LogFailure(const Header* header, const void* pointer, const void* data) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 50 | error_log(LOG_DIVIDER); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 51 | error_log("+++ ALLOCATION %p SIZE %zu HAS A CORRUPTED %s GUARD", pointer, header->size, | 
|  | 52 | GetTypeName()); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 53 |  | 
|  | 54 | // Log all of the failing bytes. | 
|  | 55 | const uint8_t* expected = cmp_mem_.data(); | 
|  | 56 | int pointer_idx = reinterpret_cast<uintptr_t>(data) - reinterpret_cast<uintptr_t>(pointer); | 
|  | 57 | const uint8_t* real = reinterpret_cast<const uint8_t*>(data); | 
|  | 58 | for (size_t i = 0; i < cmp_mem_.size(); i++, pointer_idx++) { | 
|  | 59 | if (real[i] != expected[i]) { | 
| Christopher Ferris | ea26b33 | 2016-04-15 14:13:52 -0700 | [diff] [blame] | 60 | error_log("  allocation[%d] = 0x%02x (expected 0x%02x)", pointer_idx, real[i], expected[i]); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 61 | } | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | error_log("Backtrace at time of failure:"); | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 65 | BacktraceAndLog(); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 66 | error_log(LOG_DIVIDER); | 
| Iris Chang | 7f209a9 | 2019-01-16 11:17:15 +0800 | [diff] [blame] | 67 | if (g_debug->config().options() & ABORT_ON_ERROR) { | 
|  | 68 | abort(); | 
|  | 69 | } | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 70 | } | 
|  | 71 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 72 | FrontGuardData::FrontGuardData(DebugData* debug_data, const Config& config, size_t* offset) | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 73 | : GuardData(debug_data, config.front_guard_value(), config.front_guard_bytes()) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 74 | // Create a buffer for fast comparisons of the front guard. | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 75 | cmp_mem_.resize(config.front_guard_bytes()); | 
|  | 76 | memset(cmp_mem_.data(), config.front_guard_value(), cmp_mem_.size()); | 
| Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 77 | // Assumes that front_bytes is a multiple of MINIMUM_ALIGNMENT_BYTES. | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 78 | offset_ = *offset; | 
| Christopher Ferris | 2b2b25b | 2017-04-05 19:13:03 -0700 | [diff] [blame] | 79 | *offset += config.front_guard_bytes(); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 80 | } | 
|  | 81 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 82 | bool FrontGuardData::Valid(const Header* header) { | 
|  | 83 | return GuardData::Valid(debug_->GetFrontGuard(header)); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 86 | void FrontGuardData::LogFailure(const Header* header) { | 
|  | 87 | GuardData::LogFailure(header, debug_->GetPointer(header), debug_->GetFrontGuard(header)); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 88 | } | 
|  | 89 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 90 | RearGuardData::RearGuardData(DebugData* debug_data, const Config& config) | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 91 | : GuardData(debug_data, config.rear_guard_value(), config.rear_guard_bytes()) {} | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 92 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 93 | bool RearGuardData::Valid(const Header* header) { | 
|  | 94 | return GuardData::Valid(debug_->GetRearGuard(header)); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 95 | } | 
|  | 96 |  | 
| Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 97 | void RearGuardData::LogFailure(const Header* header) { | 
|  | 98 | GuardData::LogFailure(header, debug_->GetPointer(header), debug_->GetRearGuard(header)); | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 99 | } |