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