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 "backtrace.h"
35 #include "Config.h"
36 #include "debug_disable.h"
37 #include "debug_log.h"
38 #include "DebugData.h"
39 #include "malloc_debug.h"
40 #include "GuardData.h"
41
GuardData(int init_value,size_t num_bytes)42 GuardData::GuardData(int init_value, size_t num_bytes) {
43 // Create a buffer for fast comparisons of the front guard.
44 cmp_mem_.resize(num_bytes);
45 memset(cmp_mem_.data(), init_value, cmp_mem_.size());
46 }
47
LogFailure(const Header * header,const void * pointer,const void * data)48 void GuardData::LogFailure(const Header* header, const void* pointer, const void* data) {
49 ScopedDisableDebugCalls disable;
50
51 error_log(LOG_DIVIDER);
52 error_log("+++ ALLOCATION %p SIZE %zu HAS A CORRUPTED %s GUARD", pointer,
53 header->real_size(), GetTypeName());
54
55 // Log all of the failing bytes.
56 const uint8_t* expected = cmp_mem_.data();
57 int pointer_idx = reinterpret_cast<uintptr_t>(data) - reinterpret_cast<uintptr_t>(pointer);
58 const uint8_t* real = reinterpret_cast<const uint8_t*>(data);
59 for (size_t i = 0; i < cmp_mem_.size(); i++, pointer_idx++) {
60 if (real[i] != expected[i]) {
61 error_log(" allocation[%d] = 0x%02x (expected 0x%02x)", pointer_idx, real[i], expected[i]);
62 }
63 }
64
65 error_log("Backtrace at time of failure:");
66 std::vector<uintptr_t> frames(64);
67 size_t frame_num = backtrace_get(frames.data(), frames.size());
68 frames.resize(frame_num);
69 backtrace_log(frames.data(), frames.size());
70 error_log(LOG_DIVIDER);
71 }
72
FrontGuardData(const Config & config,size_t * offset)73 FrontGuardData::FrontGuardData(const Config& config, size_t* offset)
74 : GuardData(config.front_guard_value, config.front_guard_bytes) {
75 // Create a buffer for fast comparisons of the front guard.
76 cmp_mem_.resize(config.front_guard_bytes);
77 memset(cmp_mem_.data(), config.front_guard_value, cmp_mem_.size());
78 // Assumes that front_bytes is a multiple of MINIMUM_ALIGNMENT_BYTES.
79 offset_ = *offset;
80 *offset += config.front_guard_bytes;
81 }
82
Valid(DebugData & debug,const Header * header)83 bool FrontGuardData::Valid(DebugData& debug, const Header* header) {
84 return GuardData::Valid(debug.GetFrontGuard(header));
85 }
86
LogFailure(DebugData & debug,const Header * header)87 void FrontGuardData::LogFailure(DebugData& debug, const Header* header) {
88 GuardData::LogFailure(header, debug.GetPointer(header), debug.GetFrontGuard(header));
89 }
90
RearGuardData(const Config & config)91 RearGuardData::RearGuardData(const Config& config)
92 : GuardData(config.rear_guard_value, config.rear_guard_bytes) {
93 }
94
Valid(DebugData & debug,const Header * header)95 bool RearGuardData::Valid(DebugData& debug, const Header* header) {
96 return GuardData::Valid(debug.GetRearGuard(header));
97 }
98
LogFailure(DebugData & debug,const Header * header)99 void RearGuardData::LogFailure(DebugData& debug, const Header* header) {
100 GuardData::LogFailure(header, debug.GetPointer(header), debug.GetRearGuard(header));
101 }
102