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
34 #include "Config.h"
35 #include "DebugData.h"
36 #include "GuardData.h"
37 #include "backtrace.h"
38 #include "debug_disable.h"
39 #include "debug_log.h"
40 #include "malloc_debug.h"
41
GuardData(DebugData * debug_data,int init_value,size_t num_bytes)42 GuardData::GuardData(DebugData* debug_data, int init_value, size_t num_bytes)
43 : OptionData(debug_data) {
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
LogFailure(const Header * header,const void * pointer,const void * data)49 void GuardData::LogFailure(const Header* header, const void* pointer, const void* data) {
50 error_log(LOG_DIVIDER);
51 error_log("+++ ALLOCATION %p SIZE %zu HAS A CORRUPTED %s GUARD", pointer, header->size,
52 GetTypeName());
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]) {
60 error_log(" allocation[%d] = 0x%02x (expected 0x%02x)", pointer_idx, real[i], expected[i]);
61 }
62 }
63
64 error_log("Backtrace at time of failure:");
65 BacktraceAndLog();
66 error_log(LOG_DIVIDER);
67 if (g_debug->config().options() & ABORT_ON_ERROR) {
68 abort();
69 }
70 }
71
FrontGuardData(DebugData * debug_data,const Config & config,size_t * offset)72 FrontGuardData::FrontGuardData(DebugData* debug_data, const Config& config, size_t* offset)
73 : GuardData(debug_data, config.front_guard_value(), config.front_guard_bytes()) {
74 // Create a buffer for fast comparisons of the front guard.
75 cmp_mem_.resize(config.front_guard_bytes());
76 memset(cmp_mem_.data(), config.front_guard_value(), cmp_mem_.size());
77 // Assumes that front_bytes is a multiple of MINIMUM_ALIGNMENT_BYTES.
78 offset_ = *offset;
79 *offset += config.front_guard_bytes();
80 }
81
Valid(const Header * header)82 bool FrontGuardData::Valid(const Header* header) {
83 return GuardData::Valid(debug_->GetFrontGuard(header));
84 }
85
LogFailure(const Header * header)86 void FrontGuardData::LogFailure(const Header* header) {
87 GuardData::LogFailure(header, debug_->GetPointer(header), debug_->GetFrontGuard(header));
88 }
89
RearGuardData(DebugData * debug_data,const Config & config)90 RearGuardData::RearGuardData(DebugData* debug_data, const Config& config)
91 : GuardData(debug_data, config.rear_guard_value(), config.rear_guard_bytes()) {}
92
Valid(const Header * header)93 bool RearGuardData::Valid(const Header* header) {
94 return GuardData::Valid(debug_->GetRearGuard(header));
95 }
96
LogFailure(const Header * header)97 void RearGuardData::LogFailure(const Header* header) {
98 GuardData::LogFailure(header, debug_->GetPointer(header), debug_->GetRearGuard(header));
99 }
100