Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 1 | //===- FuzzerTraceState.cpp - Trace-based fuzzer mutator ------------------===// |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 9 | // This file implements a mutation algorithm based on instruction traces and |
| 10 | // on taint analysis feedback from DFSan. |
| 11 | // |
| 12 | // Instruction traces are special hooks inserted by the compiler around |
| 13 | // interesting instructions. Currently supported traces: |
| 14 | // * __sanitizer_cov_trace_cmp -- inserted before every ICMP instruction, |
| 15 | // receives the type, size and arguments of ICMP. |
| 16 | // |
| 17 | // Every time a traced event is intercepted we analyse the data involved |
| 18 | // in the event and suggest a mutation for future executions. |
| 19 | // For example if 4 bytes of data that derive from input bytes {4,5,6,7} |
| 20 | // are compared with a constant 12345, |
| 21 | // we try to insert 12345, 12344, 12346 into bytes |
| 22 | // {4,5,6,7} of the next fuzzed inputs. |
| 23 | // |
| 24 | // The fuzzer can work only with the traces, or with both traces and DFSan. |
| 25 | // |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 26 | // DataFlowSanitizer (DFSan) is a tool for |
| 27 | // generalised dynamic data flow (taint) analysis: |
| 28 | // http://clang.llvm.org/docs/DataFlowSanitizer.html . |
| 29 | // |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 30 | // The approach with DFSan-based fuzzing has some similarity to |
| 31 | // "Taint-based Directed Whitebox Fuzzing" |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 32 | // by Vijay Ganesh & Tim Leek & Martin Rinard: |
| 33 | // http://dspace.mit.edu/openaccess-disseminate/1721.1/59320, |
| 34 | // but it uses a full blown LLVM IR taint analysis and separate instrumentation |
| 35 | // to analyze all of the "attack points" at once. |
| 36 | // |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 37 | // Workflow with DFSan: |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 38 | // * lib/Fuzzer/Fuzzer*.cpp is compiled w/o any instrumentation. |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 39 | // * The code under test is compiled with DFSan *and* with instruction traces. |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 40 | // * Every call to HOOK(a,b) is replaced by DFSan with |
| 41 | // __dfsw_HOOK(a, b, label(a), label(b)) so that __dfsw_HOOK |
| 42 | // gets all the taint labels for the arguments. |
| 43 | // * At the Fuzzer startup we assign a unique DFSan label |
| 44 | // to every byte of the input string (Fuzzer::CurrentUnit) so that for any |
| 45 | // chunk of data we know which input bytes it has derived from. |
| 46 | // * The __dfsw_* functions (implemented in this file) record the |
| 47 | // parameters (i.e. the application data and the corresponding taint labels) |
| 48 | // in a global state. |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 49 | // * Fuzzer::ApplyTraceBasedMutation() tries to use the data recorded |
| 50 | // by __dfsw_* hooks to guide the fuzzing towards new application states. |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 51 | // |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 52 | // Parts of this code will not function when DFSan is not linked in. |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 53 | // Instead of using ifdefs and thus requiring a separate build of lib/Fuzzer |
| 54 | // we redeclare the dfsan_* interface functions as weak and check if they |
| 55 | // are nullptr before calling. |
| 56 | // If this approach proves to be useful we may add attribute(weak) to the |
| 57 | // dfsan declarations in dfsan_interface.h |
| 58 | // |
| 59 | // This module is in the "proof of concept" stage. |
| 60 | // It is capable of solving only the simplest puzzles |
| 61 | // like test/dfsan/DFSanSimpleCmpTest.cpp. |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 64 | /* Example of manual usage (-fsanitize=dataflow is optional): |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 65 | ( |
| 66 | cd $LLVM/lib/Fuzzer/ |
| 67 | clang -fPIC -c -g -O2 -std=c++11 Fuzzer*.cpp |
Alexey Samsonov | 21a3381 | 2015-05-07 23:33:24 +0000 | [diff] [blame] | 68 | clang++ -O0 -std=c++11 -fsanitize-coverage=edge,trace-cmp \ |
Kostya Serebryany | 3befe94 | 2015-05-06 22:47:24 +0000 | [diff] [blame] | 69 | -fsanitize=dataflow \ |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 70 | test/dfsan/DFSanSimpleCmpTest.cpp Fuzzer*.o |
| 71 | ./a.out |
| 72 | ) |
| 73 | */ |
| 74 | |
| 75 | #include "FuzzerInternal.h" |
| 76 | #include <sanitizer/dfsan_interface.h> |
| 77 | |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 78 | #include <algorithm> |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 79 | #include <cstring> |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 80 | #include <unordered_map> |
| 81 | |
| 82 | extern "C" { |
| 83 | __attribute__((weak)) |
| 84 | dfsan_label dfsan_create_label(const char *desc, void *userdata); |
| 85 | __attribute__((weak)) |
| 86 | void dfsan_set_label(dfsan_label label, void *addr, size_t size); |
| 87 | __attribute__((weak)) |
| 88 | void dfsan_add_label(dfsan_label label, void *addr, size_t size); |
| 89 | __attribute__((weak)) |
| 90 | const struct dfsan_label_info *dfsan_get_label_info(dfsan_label label); |
Kostya Serebryany | a407dde | 2015-05-07 00:11:33 +0000 | [diff] [blame] | 91 | __attribute__((weak)) |
| 92 | dfsan_label dfsan_read_label(const void *addr, size_t size); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 93 | } // extern "C" |
| 94 | |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 95 | namespace fuzzer { |
| 96 | |
| 97 | static bool ReallyHaveDFSan() { |
| 98 | return &dfsan_create_label != nullptr; |
| 99 | } |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 100 | |
| 101 | // These values are copied from include/llvm/IR/InstrTypes.h. |
| 102 | // We do not include the LLVM headers here to remain independent. |
| 103 | // If these values ever change, an assertion in ComputeCmp will fail. |
| 104 | enum Predicate { |
| 105 | ICMP_EQ = 32, ///< equal |
| 106 | ICMP_NE = 33, ///< not equal |
| 107 | ICMP_UGT = 34, ///< unsigned greater than |
| 108 | ICMP_UGE = 35, ///< unsigned greater or equal |
| 109 | ICMP_ULT = 36, ///< unsigned less than |
| 110 | ICMP_ULE = 37, ///< unsigned less or equal |
| 111 | ICMP_SGT = 38, ///< signed greater than |
| 112 | ICMP_SGE = 39, ///< signed greater or equal |
| 113 | ICMP_SLT = 40, ///< signed less than |
| 114 | ICMP_SLE = 41, ///< signed less or equal |
| 115 | }; |
| 116 | |
| 117 | template <class U, class S> |
| 118 | bool ComputeCmp(size_t CmpType, U Arg1, U Arg2) { |
| 119 | switch(CmpType) { |
| 120 | case ICMP_EQ : return Arg1 == Arg2; |
| 121 | case ICMP_NE : return Arg1 != Arg2; |
| 122 | case ICMP_UGT: return Arg1 > Arg2; |
| 123 | case ICMP_UGE: return Arg1 >= Arg2; |
| 124 | case ICMP_ULT: return Arg1 < Arg2; |
| 125 | case ICMP_ULE: return Arg1 <= Arg2; |
| 126 | case ICMP_SGT: return (S)Arg1 > (S)Arg2; |
| 127 | case ICMP_SGE: return (S)Arg1 >= (S)Arg2; |
| 128 | case ICMP_SLT: return (S)Arg1 < (S)Arg2; |
| 129 | case ICMP_SLE: return (S)Arg1 <= (S)Arg2; |
| 130 | default: assert(0 && "unsupported CmpType"); |
| 131 | } |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | static bool ComputeCmp(size_t CmpSize, size_t CmpType, uint64_t Arg1, |
| 136 | uint64_t Arg2) { |
| 137 | if (CmpSize == 8) return ComputeCmp<uint64_t, int64_t>(CmpType, Arg1, Arg2); |
| 138 | if (CmpSize == 4) return ComputeCmp<uint32_t, int32_t>(CmpType, Arg1, Arg2); |
| 139 | if (CmpSize == 2) return ComputeCmp<uint16_t, int16_t>(CmpType, Arg1, Arg2); |
| 140 | if (CmpSize == 1) return ComputeCmp<uint8_t, int8_t>(CmpType, Arg1, Arg2); |
Kostya Serebryany | b74ba42 | 2015-07-30 02:33:45 +0000 | [diff] [blame] | 141 | // Other size, == |
| 142 | if (CmpType == ICMP_EQ) return Arg1 == Arg2; |
Kostya Serebryany | 8ce7424 | 2015-08-01 01:42:51 +0000 | [diff] [blame] | 143 | // assert(0 && "unsupported cmp and type size combination"); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 144 | return true; |
| 145 | } |
| 146 | |
| 147 | // As a simplification we use the range of input bytes instead of a set of input |
| 148 | // bytes. |
| 149 | struct LabelRange { |
| 150 | uint16_t Beg, End; // Range is [Beg, End), thus Beg==End is an empty range. |
| 151 | |
| 152 | LabelRange(uint16_t Beg = 0, uint16_t End = 0) : Beg(Beg), End(End) {} |
| 153 | |
| 154 | static LabelRange Join(LabelRange LR1, LabelRange LR2) { |
| 155 | if (LR1.Beg == LR1.End) return LR2; |
| 156 | if (LR2.Beg == LR2.End) return LR1; |
| 157 | return {std::min(LR1.Beg, LR2.Beg), std::max(LR1.End, LR2.End)}; |
| 158 | } |
| 159 | LabelRange &Join(LabelRange LR) { |
| 160 | return *this = Join(*this, LR); |
| 161 | } |
| 162 | static LabelRange Singleton(const dfsan_label_info *LI) { |
| 163 | uint16_t Idx = (uint16_t)(uintptr_t)LI->userdata; |
| 164 | assert(Idx > 0); |
| 165 | return {(uint16_t)(Idx - 1), Idx}; |
| 166 | } |
| 167 | }; |
| 168 | |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 169 | // For now, very simple: put Size bytes of Data at position Pos. |
| 170 | struct TraceBasedMutation { |
| 171 | size_t Pos; |
| 172 | size_t Size; |
| 173 | uint64_t Data; |
| 174 | }; |
| 175 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 176 | class TraceState { |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 177 | public: |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 178 | TraceState(const Fuzzer::FuzzingOptions &Options, const Unit &CurrentUnit) |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 179 | : Options(Options), CurrentUnit(CurrentUnit) {} |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 180 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 181 | LabelRange GetLabelRange(dfsan_label L); |
| 182 | void DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType, |
| 183 | uint64_t Arg1, uint64_t Arg2, dfsan_label L1, |
| 184 | dfsan_label L2); |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 185 | void DFSanSwitchCallback(uint64_t PC, size_t ValSizeInBits, uint64_t Val, |
| 186 | size_t NumCases, uint64_t *Cases, dfsan_label L); |
Kostya Serebryany | e641dd6 | 2015-09-04 22:32:25 +0000 | [diff] [blame] | 187 | void TraceCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType, |
| 188 | uint64_t Arg1, uint64_t Arg2); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 189 | |
| 190 | void TraceSwitchCallback(uintptr_t PC, size_t ValSizeInBits, uint64_t Val, |
| 191 | size_t NumCases, uint64_t *Cases); |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 192 | int TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData, |
| 193 | size_t DataSize); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 194 | |
| 195 | void StartTraceRecording() { |
Kostya Serebryany | 8817e86 | 2015-05-11 23:25:28 +0000 | [diff] [blame] | 196 | if (!Options.UseTraces) return; |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 197 | RecordingTraces = true; |
| 198 | Mutations.clear(); |
| 199 | } |
| 200 | |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 201 | size_t StopTraceRecording(FuzzerRandomBase &Rand) { |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 202 | RecordingTraces = false; |
Kostya Serebryany | 12c7837 | 2015-08-12 01:55:37 +0000 | [diff] [blame] | 203 | return Mutations.size(); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void ApplyTraceBasedMutation(size_t Idx, fuzzer::Unit *U); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 207 | |
| 208 | private: |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 209 | bool IsTwoByteData(uint64_t Data) { |
| 210 | int64_t Signed = static_cast<int64_t>(Data); |
| 211 | Signed >>= 16; |
| 212 | return Signed == 0 || Signed == -1L; |
| 213 | } |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 214 | bool RecordingTraces = false; |
| 215 | std::vector<TraceBasedMutation> Mutations; |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 216 | LabelRange LabelRanges[1 << (sizeof(dfsan_label) * 8)] = {}; |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 217 | const Fuzzer::FuzzingOptions &Options; |
| 218 | const Unit &CurrentUnit; |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 219 | }; |
| 220 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 221 | LabelRange TraceState::GetLabelRange(dfsan_label L) { |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 222 | LabelRange &LR = LabelRanges[L]; |
| 223 | if (LR.Beg < LR.End || L == 0) |
| 224 | return LR; |
| 225 | const dfsan_label_info *LI = dfsan_get_label_info(L); |
| 226 | if (LI->l1 || LI->l2) |
| 227 | return LR = LabelRange::Join(GetLabelRange(LI->l1), GetLabelRange(LI->l2)); |
| 228 | return LR = LabelRange::Singleton(LI); |
| 229 | } |
| 230 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 231 | void TraceState::ApplyTraceBasedMutation(size_t Idx, fuzzer::Unit *U) { |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 232 | assert(Idx < Mutations.size()); |
| 233 | auto &M = Mutations[Idx]; |
| 234 | if (Options.Verbosity >= 3) |
Kostya Serebryany | 20e9bcb | 2015-05-23 01:07:46 +0000 | [diff] [blame] | 235 | Printf("TBM %zd %zd %zd\n", M.Pos, M.Size, M.Data); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 236 | if (M.Pos + M.Size > U->size()) return; |
| 237 | memcpy(U->data() + M.Pos, &M.Data, M.Size); |
| 238 | } |
| 239 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 240 | void TraceState::DFSanCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType, |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 241 | uint64_t Arg1, uint64_t Arg2, dfsan_label L1, |
| 242 | dfsan_label L2) { |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 243 | assert(ReallyHaveDFSan()); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 244 | if (!RecordingTraces) return; |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 245 | if (L1 == 0 && L2 == 0) |
| 246 | return; // Not actionable. |
| 247 | if (L1 != 0 && L2 != 0) |
| 248 | return; // Probably still actionable. |
| 249 | bool Res = ComputeCmp(CmpSize, CmpType, Arg1, Arg2); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 250 | uint64_t Data = L1 ? Arg2 : Arg1; |
| 251 | LabelRange LR = L1 ? GetLabelRange(L1) : GetLabelRange(L2); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 252 | |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 253 | for (size_t Pos = LR.Beg; Pos + CmpSize <= LR.End; Pos++) { |
| 254 | Mutations.push_back({Pos, CmpSize, Data}); |
| 255 | Mutations.push_back({Pos, CmpSize, Data + 1}); |
| 256 | Mutations.push_back({Pos, CmpSize, Data - 1}); |
| 257 | } |
| 258 | |
| 259 | if (CmpSize > LR.End - LR.Beg) |
| 260 | Mutations.push_back({LR.Beg, (unsigned)(LR.End - LR.Beg), Data}); |
| 261 | |
| 262 | |
| 263 | if (Options.Verbosity >= 3) |
Kostya Serebryany | ae7df1c | 2015-07-28 01:25:00 +0000 | [diff] [blame] | 264 | Printf("DFSanCmpCallback: PC %lx S %zd T %zd A1 %llx A2 %llx R %d L1 %d L2 " |
| 265 | "%d MU %zd\n", |
Kostya Serebryany | 20e9bcb | 2015-05-23 01:07:46 +0000 | [diff] [blame] | 266 | PC, CmpSize, CmpType, Arg1, Arg2, Res, L1, L2, Mutations.size()); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 269 | void TraceState::DFSanSwitchCallback(uint64_t PC, size_t ValSizeInBits, |
| 270 | uint64_t Val, size_t NumCases, |
| 271 | uint64_t *Cases, dfsan_label L) { |
| 272 | assert(ReallyHaveDFSan()); |
| 273 | if (!RecordingTraces) return; |
| 274 | if (!L) return; // Not actionable. |
| 275 | LabelRange LR = GetLabelRange(L); |
| 276 | size_t ValSize = ValSizeInBits / 8; |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 277 | bool TryShort = IsTwoByteData(Val); |
| 278 | for (size_t i = 0; i < NumCases; i++) |
| 279 | TryShort &= IsTwoByteData(Cases[i]); |
| 280 | |
| 281 | for (size_t Pos = LR.Beg; Pos + ValSize <= LR.End; Pos++) |
| 282 | for (size_t i = 0; i < NumCases; i++) |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 283 | Mutations.push_back({Pos, ValSize, Cases[i]}); |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 284 | |
| 285 | if (TryShort) |
| 286 | for (size_t Pos = LR.Beg; Pos + 2 <= LR.End; Pos++) |
| 287 | for (size_t i = 0; i < NumCases; i++) |
| 288 | Mutations.push_back({Pos, 2, Cases[i]}); |
| 289 | |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 290 | if (Options.Verbosity >= 3) |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 291 | Printf("DFSanSwitchCallback: PC %lx Val %zd SZ %zd # %zd L %d: {%d, %d} " |
| 292 | "TryShort %d\n", |
| 293 | PC, Val, ValSize, NumCases, L, LR.Beg, LR.End, TryShort); |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 296 | int TraceState::TryToAddDesiredData(uint64_t PresentData, uint64_t DesiredData, |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 297 | size_t DataSize) { |
| 298 | int Res = 0; |
| 299 | const uint8_t *Beg = CurrentUnit.data(); |
| 300 | const uint8_t *End = Beg + CurrentUnit.size(); |
Kostya Serebryany | e641dd6 | 2015-09-04 22:32:25 +0000 | [diff] [blame] | 301 | for (const uint8_t *Cur = Beg; Cur < End; Cur++) { |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 302 | Cur = (uint8_t *)memmem(Cur, End - Cur, &PresentData, DataSize); |
| 303 | if (!Cur) |
| 304 | break; |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 305 | size_t Pos = Cur - Beg; |
| 306 | assert(Pos < CurrentUnit.size()); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 307 | if (Mutations.size() > 100000U) return Res; // Just in case. |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 308 | Mutations.push_back({Pos, DataSize, DesiredData}); |
| 309 | Mutations.push_back({Pos, DataSize, DesiredData + 1}); |
| 310 | Mutations.push_back({Pos, DataSize, DesiredData - 1}); |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 311 | Res++; |
| 312 | } |
| 313 | return Res; |
| 314 | } |
| 315 | |
Kostya Serebryany | e641dd6 | 2015-09-04 22:32:25 +0000 | [diff] [blame] | 316 | void TraceState::TraceCmpCallback(uintptr_t PC, size_t CmpSize, size_t CmpType, |
| 317 | uint64_t Arg1, uint64_t Arg2) { |
Kostya Serebryany | 8817e86 | 2015-05-11 23:25:28 +0000 | [diff] [blame] | 318 | if (!RecordingTraces) return; |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 319 | int Added = 0; |
| 320 | if (Options.Verbosity >= 3) |
Kostya Serebryany | 4b82de2 | 2015-09-08 20:40:10 +0000 | [diff] [blame^] | 321 | Printf("TraceCmp %zd/%zd: %p %zd %zd\n", CmpSize, CmpType, PC, Arg1, Arg2); |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 322 | Added += TryToAddDesiredData(Arg1, Arg2, CmpSize); |
| 323 | Added += TryToAddDesiredData(Arg2, Arg1, CmpSize); |
| 324 | if (!Added && CmpSize == 4 && IsTwoByteData(Arg1) && IsTwoByteData(Arg2)) { |
| 325 | Added += TryToAddDesiredData(Arg1, Arg2, 2); |
| 326 | Added += TryToAddDesiredData(Arg2, Arg1, 2); |
| 327 | } |
| 328 | } |
| 329 | |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 330 | void TraceState::TraceSwitchCallback(uintptr_t PC, size_t ValSizeInBits, |
| 331 | uint64_t Val, size_t NumCases, |
| 332 | uint64_t *Cases) { |
Kostya Serebryany | 73932e5 | 2015-07-31 18:09:08 +0000 | [diff] [blame] | 333 | if (!RecordingTraces) return; |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 334 | size_t ValSize = ValSizeInBits / 8; |
| 335 | bool TryShort = IsTwoByteData(Val); |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 336 | for (size_t i = 0; i < NumCases; i++) |
Kostya Serebryany | fe7e41e | 2015-07-31 20:58:55 +0000 | [diff] [blame] | 337 | TryShort &= IsTwoByteData(Cases[i]); |
| 338 | |
| 339 | if (Options.Verbosity >= 3) |
| 340 | Printf("TraceSwitch: %p %zd # %zd; TryShort %d\n", PC, Val, NumCases, |
| 341 | TryShort); |
| 342 | |
| 343 | for (size_t i = 0; i < NumCases; i++) { |
| 344 | TryToAddDesiredData(Val, Cases[i], ValSize); |
| 345 | if (TryShort) |
| 346 | TryToAddDesiredData(Val, Cases[i], 2); |
| 347 | } |
| 348 | |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 351 | static TraceState *TS; |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 352 | |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 353 | void Fuzzer::StartTraceRecording() { |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 354 | if (!TS) return; |
Kostya Serebryany | 4cc10d4 | 2015-08-05 23:02:57 +0000 | [diff] [blame] | 355 | if (ReallyHaveDFSan()) |
| 356 | for (size_t i = 0; i < static_cast<size_t>(Options.MaxLen); i++) |
| 357 | dfsan_set_label(i + 1, &CurrentUnit[i], 1); |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 358 | TS->StartTraceRecording(); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | size_t Fuzzer::StopTraceRecording() { |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 362 | if (!TS) return 0; |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 363 | return TS->StopTraceRecording(USF.GetRand()); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | void Fuzzer::ApplyTraceBasedMutation(size_t Idx, Unit *U) { |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 367 | assert(TS); |
| 368 | TS->ApplyTraceBasedMutation(Idx, U); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 371 | void Fuzzer::InitializeTraceState() { |
Kostya Serebryany | d8c5472 | 2015-05-12 01:58:34 +0000 | [diff] [blame] | 372 | if (!Options.UseTraces) return; |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 373 | TS = new TraceState(Options, CurrentUnit); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 374 | CurrentUnit.resize(Options.MaxLen); |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 375 | // The rest really requires DFSan. |
Kostya Serebryany | d8c5472 | 2015-05-12 01:58:34 +0000 | [diff] [blame] | 376 | if (!ReallyHaveDFSan()) return; |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 377 | for (size_t i = 0; i < static_cast<size_t>(Options.MaxLen); i++) { |
| 378 | dfsan_label L = dfsan_create_label("input", (void*)(i + 1)); |
| 379 | // We assume that no one else has called dfsan_create_label before. |
Kostya Serebryany | d46369d | 2015-08-05 23:44:42 +0000 | [diff] [blame] | 380 | if (L != i + 1) { |
| 381 | Printf("DFSan labels are not starting from 1, exiting\n"); |
| 382 | exit(1); |
| 383 | } |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
Kostya Serebryany | c9dc96b | 2015-07-30 21:22:22 +0000 | [diff] [blame] | 387 | static size_t InternalStrnlen(const char *S, size_t MaxLen) { |
| 388 | size_t Len = 0; |
| 389 | for (; Len < MaxLen && S[Len]; Len++) {} |
| 390 | return Len; |
| 391 | } |
| 392 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 393 | } // namespace fuzzer |
| 394 | |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 395 | using fuzzer::TS; |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 396 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 397 | extern "C" { |
| 398 | void __dfsw___sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1, |
| 399 | uint64_t Arg2, dfsan_label L0, |
| 400 | dfsan_label L1, dfsan_label L2) { |
Kostya Serebryany | 3fe7682 | 2015-05-29 20:31:17 +0000 | [diff] [blame] | 401 | if (!TS) return; |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 402 | assert(L0 == 0); |
| 403 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 404 | uint64_t CmpSize = (SizeAndType >> 32) / 8; |
| 405 | uint64_t Type = (SizeAndType << 32) >> 32; |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 406 | TS->DFSanCmpCallback(PC, CmpSize, Type, Arg1, Arg2, L1, L2); |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 407 | } |
Kostya Serebryany | a407dde | 2015-05-07 00:11:33 +0000 | [diff] [blame] | 408 | |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 409 | void __dfsw___sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases, |
| 410 | dfsan_label L1, dfsan_label L2) { |
| 411 | if (!TS) return; |
| 412 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 413 | TS->DFSanSwitchCallback(PC, Cases[1], Val, Cases[0], Cases+2, L1); |
| 414 | } |
| 415 | |
Kostya Serebryany | a407dde | 2015-05-07 00:11:33 +0000 | [diff] [blame] | 416 | void dfsan_weak_hook_memcmp(void *caller_pc, const void *s1, const void *s2, |
| 417 | size_t n, dfsan_label s1_label, |
| 418 | dfsan_label s2_label, dfsan_label n_label) { |
Kostya Serebryany | 3fe7682 | 2015-05-29 20:31:17 +0000 | [diff] [blame] | 419 | if (!TS) return; |
Kostya Serebryany | a407dde | 2015-05-07 00:11:33 +0000 | [diff] [blame] | 420 | uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 421 | uint64_t S1 = 0, S2 = 0; |
Kostya Serebryany | a407dde | 2015-05-07 00:11:33 +0000 | [diff] [blame] | 422 | // Simplification: handle only first 8 bytes. |
| 423 | memcpy(&S1, s1, std::min(n, sizeof(S1))); |
| 424 | memcpy(&S2, s2, std::min(n, sizeof(S2))); |
| 425 | dfsan_label L1 = dfsan_read_label(s1, n); |
| 426 | dfsan_label L2 = dfsan_read_label(s2, n); |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 427 | TS->DFSanCmpCallback(PC, n, fuzzer::ICMP_EQ, S1, S2, L1, L2); |
Kostya Serebryany | a407dde | 2015-05-07 00:11:33 +0000 | [diff] [blame] | 428 | } |
Kostya Serebryany | 7d470cf | 2015-05-07 18:32:29 +0000 | [diff] [blame] | 429 | |
Kostya Serebryany | b74ba42 | 2015-07-30 02:33:45 +0000 | [diff] [blame] | 430 | void dfsan_weak_hook_strncmp(void *caller_pc, const char *s1, const char *s2, |
| 431 | size_t n, dfsan_label s1_label, |
| 432 | dfsan_label s2_label, dfsan_label n_label) { |
Kostya Serebryany | c9dc96b | 2015-07-30 21:22:22 +0000 | [diff] [blame] | 433 | if (!TS) return; |
| 434 | uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc); |
| 435 | uint64_t S1 = 0, S2 = 0; |
| 436 | n = std::min(n, fuzzer::InternalStrnlen(s1, n)); |
| 437 | n = std::min(n, fuzzer::InternalStrnlen(s2, n)); |
| 438 | // Simplification: handle only first 8 bytes. |
| 439 | memcpy(&S1, s1, std::min(n, sizeof(S1))); |
| 440 | memcpy(&S2, s2, std::min(n, sizeof(S2))); |
| 441 | dfsan_label L1 = dfsan_read_label(s1, n); |
| 442 | dfsan_label L2 = dfsan_read_label(s2, n); |
| 443 | TS->DFSanCmpCallback(PC, n, fuzzer::ICMP_EQ, S1, S2, L1, L2); |
Kostya Serebryany | b74ba42 | 2015-07-30 02:33:45 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Kostya Serebryany | 7f4227d | 2015-08-05 18:23:01 +0000 | [diff] [blame] | 446 | void dfsan_weak_hook_strcmp(void *caller_pc, const char *s1, const char *s2, |
| 447 | dfsan_label s1_label, dfsan_label s2_label) { |
| 448 | if (!TS) return; |
| 449 | uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc); |
| 450 | uint64_t S1 = 0, S2 = 0; |
| 451 | size_t Len1 = strlen(s1); |
| 452 | size_t Len2 = strlen(s2); |
| 453 | size_t N = std::min(Len1, Len2); |
| 454 | if (N <= 1) return; // Not interesting. |
| 455 | // Simplification: handle only first 8 bytes. |
| 456 | memcpy(&S1, s1, std::min(N, sizeof(S1))); |
| 457 | memcpy(&S2, s2, std::min(N, sizeof(S2))); |
| 458 | dfsan_label L1 = dfsan_read_label(s1, Len1); |
| 459 | dfsan_label L2 = dfsan_read_label(s2, Len2); |
| 460 | TS->DFSanCmpCallback(PC, N, fuzzer::ICMP_EQ, S1, S2, L1, L2); |
| 461 | } |
| 462 | |
Kostya Serebryany | 0e776a2 | 2015-07-30 01:34:58 +0000 | [diff] [blame] | 463 | void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1, |
| 464 | const void *s2, size_t n) { |
| 465 | if (!TS) return; |
| 466 | uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc); |
| 467 | uint64_t S1 = 0, S2 = 0; |
| 468 | // Simplification: handle only first 8 bytes. |
| 469 | memcpy(&S1, s1, std::min(n, sizeof(S1))); |
| 470 | memcpy(&S2, s2, std::min(n, sizeof(S2))); |
| 471 | TS->TraceCmpCallback(PC, n, fuzzer::ICMP_EQ, S1, S2); |
Kostya Serebryany | b74ba42 | 2015-07-30 02:33:45 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1, |
| 475 | const char *s2, size_t n) { |
Kostya Serebryany | c9dc96b | 2015-07-30 21:22:22 +0000 | [diff] [blame] | 476 | if (!TS) return; |
| 477 | uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc); |
| 478 | uint64_t S1 = 0, S2 = 0; |
Kostya Serebryany | cd6a466 | 2015-07-31 17:05:05 +0000 | [diff] [blame] | 479 | size_t Len1 = fuzzer::InternalStrnlen(s1, n); |
| 480 | size_t Len2 = fuzzer::InternalStrnlen(s2, n); |
| 481 | n = std::min(n, Len1); |
| 482 | n = std::min(n, Len2); |
| 483 | if (n <= 1) return; // Not interesting. |
Kostya Serebryany | c9dc96b | 2015-07-30 21:22:22 +0000 | [diff] [blame] | 484 | // Simplification: handle only first 8 bytes. |
| 485 | memcpy(&S1, s1, std::min(n, sizeof(S1))); |
| 486 | memcpy(&S2, s2, std::min(n, sizeof(S2))); |
| 487 | TS->TraceCmpCallback(PC, n, fuzzer::ICMP_EQ, S1, S2); |
Kostya Serebryany | 0e776a2 | 2015-07-30 01:34:58 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Kostya Serebryany | 7f4227d | 2015-08-05 18:23:01 +0000 | [diff] [blame] | 490 | void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1, |
| 491 | const char *s2) { |
| 492 | if (!TS) return; |
| 493 | uintptr_t PC = reinterpret_cast<uintptr_t>(caller_pc); |
| 494 | uint64_t S1 = 0, S2 = 0; |
| 495 | size_t Len1 = strlen(s1); |
| 496 | size_t Len2 = strlen(s2); |
| 497 | size_t N = std::min(Len1, Len2); |
| 498 | if (N <= 1) return; // Not interesting. |
| 499 | // Simplification: handle only first 8 bytes. |
| 500 | memcpy(&S1, s1, std::min(N, sizeof(S1))); |
| 501 | memcpy(&S2, s2, std::min(N, sizeof(S2))); |
| 502 | TS->TraceCmpCallback(PC, N, fuzzer::ICMP_EQ, S1, S2); |
| 503 | } |
| 504 | |
| 505 | |
Kostya Serebryany | 7d470cf | 2015-05-07 18:32:29 +0000 | [diff] [blame] | 506 | void __sanitizer_cov_trace_cmp(uint64_t SizeAndType, uint64_t Arg1, |
| 507 | uint64_t Arg2) { |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 508 | if (!TS) return; |
Kostya Serebryany | 3595959 | 2015-07-28 00:59:53 +0000 | [diff] [blame] | 509 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 510 | uint64_t CmpSize = (SizeAndType >> 32) / 8; |
| 511 | uint64_t Type = (SizeAndType << 32) >> 32; |
Kostya Serebryany | 3595959 | 2015-07-28 00:59:53 +0000 | [diff] [blame] | 512 | TS->TraceCmpCallback(PC, CmpSize, Type, Arg1, Arg2); |
Kostya Serebryany | 7d470cf | 2015-05-07 18:32:29 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Kostya Serebryany | fb7d8d9 | 2015-07-31 01:33:06 +0000 | [diff] [blame] | 515 | void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) { |
| 516 | if (!TS) return; |
| 517 | uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0)); |
| 518 | TS->TraceSwitchCallback(PC, Cases[1], Val, Cases[0], Cases + 2); |
| 519 | } |
| 520 | |
Kostya Serebryany | 16d03bd | 2015-03-30 22:09:51 +0000 | [diff] [blame] | 521 | } // extern "C" |