OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 |
| 16 #include "syzygy/instrument/instrumenters/afl_instrumenter.h" |
| 17 |
| 18 #include "base/logging.h" |
| 19 #include "base/values.h" |
| 20 #include "base/files/file_util.h" |
| 21 #include "base/json/json_reader.h" |
| 22 #include "syzygy/application/application.h" |
| 23 #include "syzygy/common/indexed_frequency_data.h" |
| 24 |
| 25 namespace instrument { |
| 26 namespace instrumenters { |
| 27 |
| 28 bool AFLInstrumenter::ReadFromJSON(const std::string& json) { |
| 29 std::unique_ptr<base::Value> value(base::JSONReader::Read(json).release()); |
| 30 |
| 31 if (!value || !value->IsType(base::Value::TYPE_DICTIONARY)) { |
| 32 LOG(ERROR) << "Invalid or empty JSON configuration."; |
| 33 return false; |
| 34 } |
| 35 |
| 36 const base::DictionaryValue* outer_dict = |
| 37 reinterpret_cast<const base::DictionaryValue*>(value.get()); |
| 38 |
| 39 const base::ListValue* whitelist = nullptr; |
| 40 const base::ListValue* blacklist = nullptr; |
| 41 const base::ListValue* to_parse_list = nullptr; |
| 42 |
| 43 outer_dict->GetList("whitelist", &whitelist); |
| 44 outer_dict->GetList("blacklist", &blacklist); |
| 45 |
| 46 if (whitelist == nullptr && blacklist == nullptr) { |
| 47 LOG(ERROR) << "JSON file must contain either 'whitelist' or 'blacklist'."; |
| 48 return false; |
| 49 } |
| 50 |
| 51 if (whitelist != nullptr && blacklist != nullptr) { |
| 52 LOG(ERROR) << "'whitelist' and 'blacklist' are mutally exclusive."; |
| 53 return false; |
| 54 } |
| 55 |
| 56 whitelist_mode_ = whitelist != nullptr; |
| 57 if (whitelist_mode_) { |
| 58 to_parse_list = whitelist; |
| 59 } else { |
| 60 to_parse_list = blacklist; |
| 61 } |
| 62 |
| 63 base::ListValue::const_iterator list_iter = to_parse_list->begin(); |
| 64 for (; list_iter != to_parse_list->end(); ++list_iter) { |
| 65 std::string fname; |
| 66 if (!(*list_iter)->GetAsString(&fname)) { |
| 67 LOG(ERROR) << "The list must be composed of strings only."; |
| 68 return false; |
| 69 } |
| 70 |
| 71 target_set_.insert(fname); |
| 72 } |
| 73 |
| 74 if (target_set_.size() == 0) { |
| 75 LOG(ERROR) << "List cannot be empty."; |
| 76 return false; |
| 77 } |
| 78 |
| 79 return true; |
| 80 } |
| 81 |
| 82 bool AFLInstrumenter::ReadFromJSONPath(const base::FilePath& path) { |
| 83 std::string file_string; |
| 84 if (!base::ReadFileToString(path, &file_string)) { |
| 85 LOG(ERROR) << "Unable to read file to string."; |
| 86 return false; |
| 87 } |
| 88 |
| 89 if (!ReadFromJSON(file_string)) { |
| 90 LOG(ERROR) << "Unable to parse JSON string."; |
| 91 return false; |
| 92 } |
| 93 |
| 94 return true; |
| 95 } |
| 96 |
| 97 bool AFLInstrumenter::DoCommandLineParse( |
| 98 const base::CommandLine* command_line) { |
| 99 if (!Super::DoCommandLineParse(command_line)) |
| 100 return false; |
| 101 |
| 102 // Parse the config path parameter (optional). |
| 103 if (command_line->HasSwitch("config")) { |
| 104 base::FilePath config_path = application::AppImplBase::AbsolutePath( |
| 105 command_line->GetSwitchValuePath("config")); |
| 106 |
| 107 if (!ReadFromJSONPath(config_path)) { |
| 108 LOG(ERROR) << "Unable to parse JSON file."; |
| 109 return false; |
| 110 } |
| 111 } |
| 112 |
| 113 // Parse the force decomposition flag (optional). |
| 114 force_decomposition_ = command_line->HasSwitch("force-decompose"); |
| 115 if (force_decomposition_) { |
| 116 LOG(INFO) << "Force decomposition mode enabled."; |
| 117 } |
| 118 |
| 119 // Parse the multithread flag (optional). |
| 120 multithread_mode_ = command_line->HasSwitch("multithread"); |
| 121 if (multithread_mode_) { |
| 122 LOG(INFO) << "Thread-safe instrumentation mode enabled."; |
| 123 } |
| 124 |
| 125 // Parse the cookie check hook flag (optional). |
| 126 cookie_check_hook_ = command_line->HasSwitch("cookie-check-hook"); |
| 127 if (cookie_check_hook_) { |
| 128 LOG(INFO) << "Cookie check hook mode enabled."; |
| 129 } |
| 130 |
| 131 return true; |
| 132 } |
| 133 |
| 134 bool AFLInstrumenter::InstrumentPrepare() { |
| 135 return true; |
| 136 } |
| 137 |
| 138 bool AFLInstrumenter::InstrumentImpl() { |
| 139 transformer_.reset(new instrument::transforms::AFLTransform( |
| 140 target_set_, whitelist_mode_, force_decomposition_, multithread_mode_, |
| 141 cookie_check_hook_)); |
| 142 |
| 143 if (!relinker_->AppendTransform(transformer_.get())) { |
| 144 LOG(ERROR) << "AppendTransform failed."; |
| 145 return false; |
| 146 } |
| 147 |
| 148 add_bb_addr_stream_mutator_.reset( |
| 149 new instrument::mutators::AddIndexedDataRangesStreamPdbMutator( |
| 150 transformer_->bb_ranges(), common::kBasicBlockRangesStreamName)); |
| 151 |
| 152 if (!relinker_->AppendPdbMutator(add_bb_addr_stream_mutator_.get())) { |
| 153 LOG(ERROR) << "AppendPdbMutator failed."; |
| 154 return false; |
| 155 } |
| 156 |
| 157 return true; |
| 158 } |
| 159 |
| 160 const char* AFLInstrumenter::InstrumentationMode() { |
| 161 return "afl"; |
| 162 } |
| 163 } // namespace instrumenters |
| 164 } // namespace instrument |
OLD | NEW |