1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2015 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker * All rights reserved.
4*8d67ca89SAndroid Build Coastguard Worker *
5*8d67ca89SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
6*8d67ca89SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
7*8d67ca89SAndroid Build Coastguard Worker * are met:
8*8d67ca89SAndroid Build Coastguard Worker * * Redistributions of source code must retain the above copyright
9*8d67ca89SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
10*8d67ca89SAndroid Build Coastguard Worker * * Redistributions in binary form must reproduce the above copyright
11*8d67ca89SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in
12*8d67ca89SAndroid Build Coastguard Worker * the documentation and/or other materials provided with the
13*8d67ca89SAndroid Build Coastguard Worker * distribution.
14*8d67ca89SAndroid Build Coastguard Worker *
15*8d67ca89SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*8d67ca89SAndroid Build Coastguard Worker * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*8d67ca89SAndroid Build Coastguard Worker * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18*8d67ca89SAndroid Build Coastguard Worker * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19*8d67ca89SAndroid Build Coastguard Worker * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*8d67ca89SAndroid Build Coastguard Worker * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*8d67ca89SAndroid Build Coastguard Worker * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22*8d67ca89SAndroid Build Coastguard Worker * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*8d67ca89SAndroid Build Coastguard Worker * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*8d67ca89SAndroid Build Coastguard Worker * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25*8d67ca89SAndroid Build Coastguard Worker * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8d67ca89SAndroid Build Coastguard Worker * SUCH DAMAGE.
27*8d67ca89SAndroid Build Coastguard Worker */
28*8d67ca89SAndroid Build Coastguard Worker
29*8d67ca89SAndroid Build Coastguard Worker #include <stdint.h>
30*8d67ca89SAndroid Build Coastguard Worker #include <string.h>
31*8d67ca89SAndroid Build Coastguard Worker
32*8d67ca89SAndroid Build Coastguard Worker #include <vector>
33*8d67ca89SAndroid Build Coastguard Worker
34*8d67ca89SAndroid Build Coastguard Worker #include "Config.h"
35*8d67ca89SAndroid Build Coastguard Worker #include "DebugData.h"
36*8d67ca89SAndroid Build Coastguard Worker #include "GuardData.h"
37*8d67ca89SAndroid Build Coastguard Worker #include "backtrace.h"
38*8d67ca89SAndroid Build Coastguard Worker #include "debug_disable.h"
39*8d67ca89SAndroid Build Coastguard Worker #include "debug_log.h"
40*8d67ca89SAndroid Build Coastguard Worker #include "malloc_debug.h"
41*8d67ca89SAndroid Build Coastguard Worker
GuardData(DebugData * debug_data,int init_value,size_t num_bytes)42*8d67ca89SAndroid Build Coastguard Worker GuardData::GuardData(DebugData* debug_data, int init_value, size_t num_bytes)
43*8d67ca89SAndroid Build Coastguard Worker : OptionData(debug_data) {
44*8d67ca89SAndroid Build Coastguard Worker // Create a buffer for fast comparisons of the front guard.
45*8d67ca89SAndroid Build Coastguard Worker cmp_mem_.resize(num_bytes);
46*8d67ca89SAndroid Build Coastguard Worker memset(cmp_mem_.data(), init_value, cmp_mem_.size());
47*8d67ca89SAndroid Build Coastguard Worker }
48*8d67ca89SAndroid Build Coastguard Worker
LogFailure(const Header * header,const void * pointer,const void * data)49*8d67ca89SAndroid Build Coastguard Worker void GuardData::LogFailure(const Header* header, const void* pointer, const void* data) {
50*8d67ca89SAndroid Build Coastguard Worker error_log(LOG_DIVIDER);
51*8d67ca89SAndroid Build Coastguard Worker error_log("+++ ALLOCATION %p SIZE %zu HAS A CORRUPTED %s GUARD", pointer, header->size,
52*8d67ca89SAndroid Build Coastguard Worker GetTypeName());
53*8d67ca89SAndroid Build Coastguard Worker
54*8d67ca89SAndroid Build Coastguard Worker // Log all of the failing bytes.
55*8d67ca89SAndroid Build Coastguard Worker const uint8_t* expected = cmp_mem_.data();
56*8d67ca89SAndroid Build Coastguard Worker int pointer_idx = reinterpret_cast<uintptr_t>(data) - reinterpret_cast<uintptr_t>(pointer);
57*8d67ca89SAndroid Build Coastguard Worker const uint8_t* real = reinterpret_cast<const uint8_t*>(data);
58*8d67ca89SAndroid Build Coastguard Worker for (size_t i = 0; i < cmp_mem_.size(); i++, pointer_idx++) {
59*8d67ca89SAndroid Build Coastguard Worker if (real[i] != expected[i]) {
60*8d67ca89SAndroid Build Coastguard Worker error_log(" allocation[%d] = 0x%02x (expected 0x%02x)", pointer_idx, real[i], expected[i]);
61*8d67ca89SAndroid Build Coastguard Worker }
62*8d67ca89SAndroid Build Coastguard Worker }
63*8d67ca89SAndroid Build Coastguard Worker
64*8d67ca89SAndroid Build Coastguard Worker error_log("Backtrace at time of failure:");
65*8d67ca89SAndroid Build Coastguard Worker BacktraceAndLog();
66*8d67ca89SAndroid Build Coastguard Worker error_log(LOG_DIVIDER);
67*8d67ca89SAndroid Build Coastguard Worker if (g_debug->config().options() & ABORT_ON_ERROR) {
68*8d67ca89SAndroid Build Coastguard Worker abort();
69*8d67ca89SAndroid Build Coastguard Worker }
70*8d67ca89SAndroid Build Coastguard Worker }
71*8d67ca89SAndroid Build Coastguard Worker
FrontGuardData(DebugData * debug_data,const Config & config,size_t * offset)72*8d67ca89SAndroid Build Coastguard Worker FrontGuardData::FrontGuardData(DebugData* debug_data, const Config& config, size_t* offset)
73*8d67ca89SAndroid Build Coastguard Worker : GuardData(debug_data, config.front_guard_value(), config.front_guard_bytes()) {
74*8d67ca89SAndroid Build Coastguard Worker // Create a buffer for fast comparisons of the front guard.
75*8d67ca89SAndroid Build Coastguard Worker cmp_mem_.resize(config.front_guard_bytes());
76*8d67ca89SAndroid Build Coastguard Worker memset(cmp_mem_.data(), config.front_guard_value(), cmp_mem_.size());
77*8d67ca89SAndroid Build Coastguard Worker // Assumes that front_bytes is a multiple of MINIMUM_ALIGNMENT_BYTES.
78*8d67ca89SAndroid Build Coastguard Worker offset_ = *offset;
79*8d67ca89SAndroid Build Coastguard Worker *offset += config.front_guard_bytes();
80*8d67ca89SAndroid Build Coastguard Worker }
81*8d67ca89SAndroid Build Coastguard Worker
Valid(const Header * header)82*8d67ca89SAndroid Build Coastguard Worker bool FrontGuardData::Valid(const Header* header) {
83*8d67ca89SAndroid Build Coastguard Worker return GuardData::Valid(debug_->GetFrontGuard(header));
84*8d67ca89SAndroid Build Coastguard Worker }
85*8d67ca89SAndroid Build Coastguard Worker
LogFailure(const Header * header)86*8d67ca89SAndroid Build Coastguard Worker void FrontGuardData::LogFailure(const Header* header) {
87*8d67ca89SAndroid Build Coastguard Worker GuardData::LogFailure(header, debug_->GetPointer(header), debug_->GetFrontGuard(header));
88*8d67ca89SAndroid Build Coastguard Worker }
89*8d67ca89SAndroid Build Coastguard Worker
RearGuardData(DebugData * debug_data,const Config & config)90*8d67ca89SAndroid Build Coastguard Worker RearGuardData::RearGuardData(DebugData* debug_data, const Config& config)
91*8d67ca89SAndroid Build Coastguard Worker : GuardData(debug_data, config.rear_guard_value(), config.rear_guard_bytes()) {}
92*8d67ca89SAndroid Build Coastguard Worker
Valid(const Header * header)93*8d67ca89SAndroid Build Coastguard Worker bool RearGuardData::Valid(const Header* header) {
94*8d67ca89SAndroid Build Coastguard Worker return GuardData::Valid(debug_->GetRearGuard(header));
95*8d67ca89SAndroid Build Coastguard Worker }
96*8d67ca89SAndroid Build Coastguard Worker
LogFailure(const Header * header)97*8d67ca89SAndroid Build Coastguard Worker void RearGuardData::LogFailure(const Header* header) {
98*8d67ca89SAndroid Build Coastguard Worker GuardData::LogFailure(header, debug_->GetPointer(header), debug_->GetRearGuard(header));
99*8d67ca89SAndroid Build Coastguard Worker }
100