OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 Google Inc. All Rights Reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #ifndef SYZYGY_PROTECT_PROTECT_LIB_PROTECT_UTILS_H_ |
| 16 #define SYZYGY_PROTECT_PROTECT_LIB_PROTECT_UTILS_H_ |
| 17 |
| 18 #include "syzygy/block_graph/block_graph.h" |
| 19 |
| 20 namespace protect { |
| 21 |
| 22 typedef block_graph::BlockGraph BlockGraph; |
| 23 |
| 24 // TODO: private members and public getters / setters |
| 25 struct ChunkInfo { |
| 26 |
| 27 ChunkInfo(uint64_t block_id, uint32_t size, uint8_t hash, |
| 28 uint32_t chunk_index, uint32_t next_instruction_size) : |
| 29 block_id_(block_id), size_(size), hash_(hash), |
| 30 chunk_index_(chunk_index), next_instruction_size_(next_instruction_s
ize), |
| 31 hash_of_next_instruction_(0), |
| 32 original_block_id_(0){}; |
| 33 |
| 34 bool operator< (const ChunkInfo& e) const { |
| 35 if (this->block_id_ < e.block_id_) { |
| 36 return true; |
| 37 |
| 38 } |
| 39 else if (this->block_id_ == e.block_id_) { |
| 40 if (this->size_ < e.size_) { |
| 41 return true; |
| 42 |
| 43 } |
| 44 else if (this->size_ == e.size_) { |
| 45 if (this->hash_ < e.hash_) { |
| 46 return true; |
| 47 |
| 48 } |
| 49 else if (this->hash_ == e.hash_) { |
| 50 if (this->chunk_index_ < e.chunk_index_) |
| 51 return true; |
| 52 } |
| 53 } |
| 54 } |
| 55 |
| 56 return false; |
| 57 } |
| 58 |
| 59 uint64_t block_id_; |
| 60 uint32_t size_; |
| 61 uint32_t chunk_index_; |
| 62 mutable uint8_t hash_; |
| 63 uint32_t next_instruction_size_; |
| 64 uint8_t hash_of_next_instruction_; |
| 65 mutable uint64_t original_block_id_; |
| 66 }; // struct ChunkInfo |
| 67 |
| 68 // Checks if the block @p is in the map @p and if its entry is set to true. |
| 69 // @param block the block that is to be checked. |
| 70 // @param target_names a map of block names to a boolean value indicating if |
| 71 // this block should be process or not. |
| 72 // @return true if this block @p is in map @p, false otherwise. |
| 73 bool ShouldProcessBlock(const BlockGraph::Block* block, |
| 74 const std::map<std::string, bool> target_names); |
| 75 |
| 76 bool ShouldPostProcessBlock( |
| 77 const BlockGraph::Block* block, |
| 78 const std::map<uint64_t, BlockGraph::Label> *id_to_label); |
| 79 |
| 80 // Retrieves a unique ID for a BB, which is marked with label @p |
| 81 // @param label to search for |
| 82 // @param a map of BB IDs to labels |
| 83 // @return a unique ID for the basic block, -1 if not found |
| 84 uint64_t GetBasicBlockIdByLabel( |
| 85 const BlockGraph::Label label, |
| 86 const std::map<uint64_t, BlockGraph::Label> *id_to_label); |
| 87 |
| 88 // |
| 89 void GetChunkTokensFromlabel(const std::string label, |
| 90 uint64_t *chunk_bb_id, |
| 91 uint32_t *chunk_index); |
| 92 |
| 93 // |
| 94 uint64_t GetChunkUniqueKey(const uint64_t bb_id, const uint32_t chunk_index); |
| 95 |
| 96 class FlummoxConfig { |
| 97 public: |
| 98 FlummoxConfig() : add_copy_(false) { } |
| 99 ~FlummoxConfig() { } |
| 100 |
| 101 // Loads (from a JSON string) configurations for the flummox instrumenter. |
| 102 // The contents of the 'json' string should follow the format below: |
| 103 // { |
| 104 // "targets": { |
| 105 // "function_name1": [], |
| 106 // "function_name2": [], |
| 107 // ... |
| 108 // }, |
| 109 // "add_copy": true|false |
| 110 // } |
| 111 // @param json A JSON string containing the configuration following the |
| 112 // format described above. |
| 113 // @param path Path to a JSON file, to use a file instead of a string. |
| 114 // @returns True if the operation succeeded, false otherwise. |
| 115 // @{ |
| 116 bool ReadFromJSON(const std::string& json); |
| 117 bool ReadFromJSONPath(const base::FilePath& path); |
| 118 // @} |
| 119 |
| 120 // Accessors |
| 121 // @{ |
| 122 const std::set<std::string>& target_set() const { return target_set_; } |
| 123 |
| 124 bool add_copy() const { return add_copy_; } |
| 125 |
| 126 const float chunk_checking_coverage() const { |
| 127 return chunk_checking_coverage_; |
| 128 } |
| 129 |
| 130 std::map<uint64_t, std::map<uint64_t, int>>* checker_to_checkee_map() { |
| 131 return &checker_to_checkee_map_; |
| 132 } |
| 133 |
| 134 std::vector<protect::ChunkInfo>* ic_block_reference_free_chunks() { |
| 135 return &ic_block_reference_free_chunks_; |
| 136 } |
| 137 |
| 138 std::map<uint64_t, uint32_t>* ic_block_chunk_index_map() { |
| 139 return &ic_block_chunk_index_map_; |
| 140 } |
| 141 |
| 142 std::map<uint64_t, std::set<uint32_t>>* ic_chunk_checker_to_checkee_map() { |
| 143 return &ic_chunk_checker_to_checkee_map_; |
| 144 } |
| 145 |
| 146 std::map<uint64_t, uint32_t>* precomputed_hashes() { |
| 147 return &precomputed_hashes_; |
| 148 } |
| 149 |
| 150 std::map<uint64_t, uint32_t>* basic_block_sizes() { |
| 151 return &basic_block_sizes_; |
| 152 } |
| 153 |
| 154 std::map<std::string, std::pair<BlockGraph::Block*, uint32_t>>* |
| 155 label_name_to_block() { |
| 156 return &label_name_to_block_; |
| 157 } |
| 158 |
| 159 std::map<uint64_t, BlockGraph::Label>* id_to_label() { |
| 160 return &id_to_label_; |
| 161 } |
| 162 |
| 163 bool* perform_chunk_checks() { |
| 164 return &perform_chunk_checks_; |
| 165 } |
| 166 |
| 167 int* nr_hashes_patched() { |
| 168 return &nr_hashes_patched_; |
| 169 } |
| 170 // @} |
| 171 |
| 172 |
| 173 protected: |
| 174 std::set<std::string> target_set_; |
| 175 bool add_copy_; |
| 176 float chunk_checking_coverage_ = 1.0f; |
| 177 // Map indicating which BBs will be hashed by the checker |
| 178 std::map<uint64_t, std::map<uint64_t, int>> checker_to_checkee_map_; |
| 179 |
| 180 // Vector indicating chunks within Integrity checker block without absolute re
ferences |
| 181 std::vector<protect::ChunkInfo> ic_block_reference_free_chunks_; |
| 182 // Map for retrieveing chunk id(unit32) from bb_id + chunk_index |
| 183 // useful in patching bb chunks |
| 184 std::map<uint64_t, uint32_t> ic_block_chunk_index_map_; |
| 185 |
| 186 // Map< CheckerId, set < Chunk indexes > > |
| 187 std::map<uint64_t, std::set<uint32_t>> ic_chunk_checker_to_checkee_map_; |
| 188 |
| 189 // Map holding precomputed hashes of original BB |
| 190 std::map<uint64_t, uint32_t> precomputed_hashes_; |
| 191 |
| 192 // Map from BB address to its size |
| 193 std::map<uint64_t, uint32_t> basic_block_sizes_; |
| 194 |
| 195 // |
| 196 std::map<std::string, std::pair<BlockGraph::Block*, uint32_t>> |
| 197 label_name_to_block_; |
| 198 |
| 199 // Map of original custom basic block ID to a label |
| 200 std::map<uint64_t, BlockGraph::Label> id_to_label_; |
| 201 |
| 202 // |
| 203 bool perform_chunk_checks_ = true; |
| 204 |
| 205 // Number of precomputed hash values which were patched |
| 206 int nr_hashes_patched_; |
| 207 private: |
| 208 DISALLOW_COPY_AND_ASSIGN(FlummoxConfig); |
| 209 }; |
| 210 |
| 211 } // namespace protect |
| 212 #endif // SYZYGY_PROTECT_PROTECT_LIB_PROTECT_UTILS_H_ |
OLD | NEW |