blob: c307dc9961ba1559190e2e01995434a30ddd6b3d [file] [log] [blame]
Christopher Ferris63860cb2015-11-16 17:30:32 -08001/*
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 Ferris63860cb2015-11-16 17:30:32 -080034#include "Config.h"
Christopher Ferris4da25032018-03-07 13:38:48 -080035#include "DebugData.h"
36#include "GuardData.h"
37#include "backtrace.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080038#include "debug_disable.h"
39#include "debug_log.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080040#include "malloc_debug.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080041
Christopher Ferris55a89a42016-04-07 17:14:53 -070042GuardData::GuardData(DebugData* debug_data, int init_value, size_t num_bytes)
43 : OptionData(debug_data) {
Christopher Ferris63860cb2015-11-16 17:30:32 -080044 // 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
49void GuardData::LogFailure(const Header* header, const void* pointer, const void* data) {
Christopher Ferris63860cb2015-11-16 17:30:32 -080050 error_log(LOG_DIVIDER);
Christopher Ferris4da25032018-03-07 13:38:48 -080051 error_log("+++ ALLOCATION %p SIZE %zu HAS A CORRUPTED %s GUARD", pointer, header->size,
52 GetTypeName());
Christopher Ferris63860cb2015-11-16 17:30:32 -080053
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 Ferrisea26b332016-04-15 14:13:52 -070060 error_log(" allocation[%d] = 0x%02x (expected 0x%02x)", pointer_idx, real[i], expected[i]);
Christopher Ferris63860cb2015-11-16 17:30:32 -080061 }
62 }
63
64 error_log("Backtrace at time of failure:");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070065 BacktraceAndLog();
Christopher Ferris63860cb2015-11-16 17:30:32 -080066 error_log(LOG_DIVIDER);
Iris Chang7f209a92019-01-16 11:17:15 +080067 if (g_debug->config().options() & ABORT_ON_ERROR) {
68 abort();
69 }
Christopher Ferris63860cb2015-11-16 17:30:32 -080070}
71
Christopher Ferris55a89a42016-04-07 17:14:53 -070072FrontGuardData::FrontGuardData(DebugData* debug_data, const Config& config, size_t* offset)
Christopher Ferris4da25032018-03-07 13:38:48 -080073 : GuardData(debug_data, config.front_guard_value(), config.front_guard_bytes()) {
Christopher Ferris63860cb2015-11-16 17:30:32 -080074 // Create a buffer for fast comparisons of the front guard.
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070075 cmp_mem_.resize(config.front_guard_bytes());
76 memset(cmp_mem_.data(), config.front_guard_value(), cmp_mem_.size());
Christopher Ferris72df6702016-02-11 15:51:31 -080077 // Assumes that front_bytes is a multiple of MINIMUM_ALIGNMENT_BYTES.
Christopher Ferris63860cb2015-11-16 17:30:32 -080078 offset_ = *offset;
Christopher Ferris2b2b25b2017-04-05 19:13:03 -070079 *offset += config.front_guard_bytes();
Christopher Ferris63860cb2015-11-16 17:30:32 -080080}
81
Christopher Ferris55a89a42016-04-07 17:14:53 -070082bool FrontGuardData::Valid(const Header* header) {
83 return GuardData::Valid(debug_->GetFrontGuard(header));
Christopher Ferris63860cb2015-11-16 17:30:32 -080084}
85
Christopher Ferris55a89a42016-04-07 17:14:53 -070086void FrontGuardData::LogFailure(const Header* header) {
87 GuardData::LogFailure(header, debug_->GetPointer(header), debug_->GetFrontGuard(header));
Christopher Ferris63860cb2015-11-16 17:30:32 -080088}
89
Christopher Ferris55a89a42016-04-07 17:14:53 -070090RearGuardData::RearGuardData(DebugData* debug_data, const Config& config)
Christopher Ferris4da25032018-03-07 13:38:48 -080091 : GuardData(debug_data, config.rear_guard_value(), config.rear_guard_bytes()) {}
Christopher Ferris63860cb2015-11-16 17:30:32 -080092
Christopher Ferris55a89a42016-04-07 17:14:53 -070093bool RearGuardData::Valid(const Header* header) {
94 return GuardData::Valid(debug_->GetRearGuard(header));
Christopher Ferris63860cb2015-11-16 17:30:32 -080095}
96
Christopher Ferris55a89a42016-04-07 17:14:53 -070097void RearGuardData::LogFailure(const Header* header) {
98 GuardData::LogFailure(header, debug_->GetPointer(header), debug_->GetRearGuard(header));
Christopher Ferris63860cb2015-11-16 17:30:32 -080099}