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