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 #ifndef DEBUG_MALLOC_GUARDDATA_H 30 #define DEBUG_MALLOC_GUARDDATA_H 31 32 #include <stdint.h> 33 #include <string.h> 34 35 #include <vector> 36 37 #include <private/bionic_macros.h> 38 39 // Forward declarations. 40 class DebugData; 41 struct Header; 42 struct Config; 43 44 class GuardData { 45 public: 46 GuardData(int init_value, size_t num_bytes); 47 virtual ~GuardData() = default; 48 Valid(void * data)49 bool Valid(void* data) { return memcmp(data, cmp_mem_.data(), cmp_mem_.size()) == 0; } 50 51 void LogFailure(const Header* header, const void* pointer, const void* data); 52 53 protected: 54 std::vector<uint8_t> cmp_mem_; 55 56 virtual const char* GetTypeName() = 0; 57 58 DISALLOW_COPY_AND_ASSIGN(GuardData); 59 }; 60 61 class FrontGuardData : public GuardData { 62 public: 63 FrontGuardData(const Config& config, size_t* offset); 64 virtual ~FrontGuardData() = default; 65 66 bool Valid(DebugData& debug, const Header* header); 67 68 void LogFailure(DebugData& debug, const Header* header); 69 offset()70 size_t offset() { return offset_; } 71 72 private: GetTypeName()73 const char* GetTypeName() override { return "FRONT"; } 74 75 size_t offset_ = 0; 76 77 DISALLOW_COPY_AND_ASSIGN(FrontGuardData); 78 }; 79 80 class RearGuardData : public GuardData { 81 public: 82 RearGuardData(const Config& config); 83 virtual ~RearGuardData() = default; 84 85 bool Valid(DebugData& debug, const Header* header); 86 87 void LogFailure(DebugData& debug, const Header* header); 88 89 private: GetTypeName()90 const char* GetTypeName() override { return "REAR"; } 91 92 DISALLOW_COPY_AND_ASSIGN(RearGuardData); 93 }; 94 95 #endif // DEBUG_MALLOC_GUARDDATA_H 96