Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: lib/Bitcode/NaCl/TestUtils/NaClSimpleRecordFuzzer.cpp

Issue 1156103003: Initial implementation of a record-level bitcode fuzzer. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-llvm.git@fuzz
Patch Set: Fix nits. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 //===- NaClSimpleRecordFuzzer.cpp - Simple fuzzer of bitcode ----*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
9 //
10 // This file implements a simple fuzzer for a list of PNaCl bitcode records.
11
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/Bitcode/NaCl/NaClFuzz.h"
14 #include "llvm/Support/Format.h"
15
16 #include <array>
17 #include <cmath>
18 #include <cstdlib>
19 #include <map>
20 #include <set>
21
22
23 using namespace llvm;
24 using namespace naclfuzz;
25
26 namespace {
27
28 // Calls random number generator and returns value in [0..Range)
29 unsigned chooseInRange(unsigned Range) {
30 // TODO(kschimpf) Use a better random number generator?
31 return rand() % Range;
32 }
33
34 // Counts the number of times each value in a range [0..N) is used (based
35 // on the number of calls to method increment()).
36 class DistCounter {
37 DistCounter(const DistCounter&) = delete;
38 void operator=(const DistCounter&) = delete;
39 public:
40 explicit DistCounter(size_t DistSize)
41 : Dist(DistSize, 0), Total(0) {}
42
43 // Increments the count for the given value
44 size_t increment(size_t Value) {
45 ++Dist[Value];
46 ++Total;
47 return Value;
48 }
49
50 // Returns the end of the range being checked.
51 size_t size() const {
52 return Dist.size();
53 }
54
55 // Returns the number of times value was incremented.
56 size_t operator[](size_t Value) const {
57 return Dist[Value];
58 }
59
60 // Retrns the number of times any value in the distribution was
61 // incremented.
62 size_t getTotal() const {
63 return Total;
64 }
65 private:
66 // The number of times each value was incremented.
67 std::vector<size_t> Dist;
68 // The total number of times any value was incremented.
69 size_t Total;
70 };
71
72 // Model weights when randomly choosing values.
73 typedef unsigned WeightType;
74
75 // Association weights with values. Used to generate weighted
76 // distributions (see class WeightedDistribution below).
77 template<typename T>
78 struct WeightedValue {
79 T Value;
80 WeightType Weight;
81 };
82
83 // Weighted distribution for a set of values in [0..DistSize).
84 template<typename T>
85 class WeightedDistribution {
86 WeightedDistribution(const WeightedDistribution&) = delete;
87 void operator=(const WeightedDistribution&) = delete;
88 public:
89 typedef const WeightedValue<T> *const_iterator;
90
91 WeightedDistribution(const WeightedValue<T> Dist[],
92 size_t DistSize)
93 : Dist(Dist), DistSize(DistSize), TotalWeight(0) {
94 for (size_t i = 0; i < DistSize; ++i)
95 TotalWeight += Dist[i].Weight;
96 }
97
98 virtual ~WeightedDistribution() {}
99
100 /// Returns the number of weighted values in the distribution.
101 size_t size() const {
102 return DistSize;
103 }
104
105 /// Returns const iterator at beginning of weighted distribution.
106 const_iterator begin() const {
107 return Dist;
108 }
109
110 /// Returns const iterator at end of weighted distribution.
111 const_iterator end() const {
112 return Dist + DistSize;
113 }
114
115 /// Randomly chooses a weighted value in the distribution, based on
116 /// the weights of the distrubution.
117 virtual const WeightedValue<T> &choose() {
118 return Dist[chooseIndex()];
119 }
120
121 /// Returns the total weight associated with the distribution.
122 size_t getTotalWeight() const {
123 return TotalWeight;
124 }
125
126 protected:
127 // The weighted values defining the distribution.
128 const WeightedValue<T> *Dist;
129 // The number of weighed values in the distribution.
130 size_t DistSize;
131 // The total weight of the distribution (sum of weights in weighted values).
132 size_t TotalWeight;
133
134 // Randomly chooses an index of a weighed value in the distribution,
135 // based on the weights of the distribution.
136 // chosen element.
137 size_t chooseIndex() {
138 /// TODO(kschimpf) Speed this up?
139 WeightType WeightedSum = chooseInRange(TotalWeight);
140 assert(WeightedSum < TotalWeight);
141 for (size_t Choice = 0; Choice < DistSize; ++Choice) {
142 WeightType NextWeight = Dist[Choice].Weight;
143 if (WeightedSum < NextWeight)
144 return Choice;
145 WeightedSum -= NextWeight;
146 }
147 llvm_unreachable("no index for WeightedDistribution.chooseIndex()");
148 }
149 };
150
151 // Defines a range [min..limit).
152 template<typename T>
153 struct RangeType {
154 T min;
155 T limit;
156 };
157
158 // Weighted distribution of a set of value ranges.
159 template<typename T>
160 class WeightedRangeDistribution : public WeightedDistribution<RangeType<T>> {
161 WeightedRangeDistribution(const WeightedRangeDistribution<T>&) = delete;
162 void operator=(const WeightedRangeDistribution<T>&) = delete;
163 public:
164 WeightedRangeDistribution(const WeightedValue<RangeType<T>> Dist[],
165 size_t DistSize)
166 : WeightedDistribution<RangeType<T>>(Dist, DistSize) {}
167
168 // Choose a value from one of the value ranges.
169 T chooseValue() {
170 const RangeType<T> Range = this->choose().Value;
171 return Range.min + chooseInRange(Range.limit - Range.min + 1);
172 }
173 };
174
175 /// Weighted distribution with a counter, capturing the distribution
176 /// of random choices for each weighted value.
177 template<typename T>
178 class CountedWeightedDistribution : public WeightedDistribution<T> {
179 CountedWeightedDistribution(const CountedWeightedDistribution&) = delete;
180 void operator=(const CountedWeightedDistribution&) = delete;
181 public:
182 CountedWeightedDistribution(const WeightedValue<T> Dist[],
183 size_t DistSize)
184 : WeightedDistribution<T>(Dist, DistSize), Counter(DistSize) {}
185
186 const WeightedValue<T> &choose() final {
187 return this->Dist[Counter.increment(
188 WeightedDistribution<T>::chooseIndex())];
189 }
190
191 /// Returns the number of times the Index-th weighted value was
192 /// chosen.
193 size_t getChooseCount(size_t Index) const {
194 return Counter[Index];
195 }
196
197 /// Returns the total number of times method choose was called.
198 size_t getTotalChooseCount() const {
199 return Counter.getTotal();
200 }
201
202 private:
203 DistCounter Counter;
204 };
205
206 #define ARRAY(array) array, array_lengthof(array)
207
208 // Weighted distribution used to select edit actions.
209 const WeightedValue<RecordFuzzer::EditAction> ActionDist[] = {
210 {RecordFuzzer::InsertRecord, 3},
211 {RecordFuzzer::MutateRecord, 5},
212 {RecordFuzzer::RemoveRecord, 1},
213 {RecordFuzzer::ReplaceRecord, 1},
214 {RecordFuzzer::SwapRecord, 1}
215 };
216
217 // Type of values in bitcode records.
218 typedef uint64_t ValueType;
219 // Signed version of values.
220 typedef int64_t SignedValueType;
221
222 // Weighted ranges for nonegative values in records.
223 const WeightedValue<RangeType<ValueType>> PosValueDist[] = {
224 {{0, 6}, 100},
225 {{7, 20}, 50},
226 {{21, 40}, 10},
227 {{41, 100}, 2},
228 {{101, 4096}, 1}
229 };
230
231 // Distribution used to decide when use negative values in records.
232 WeightedValue<bool> NegValueDist[] = {
233 {true, 1}, // i.e. make value negative.
234 {false, 100} // i.e. leave value positive.
235 };
236
237 // Range distribution for records sizes (must be greater than 0).
238 const WeightedValue<RangeType<size_t>> RecordSizeDist[] = {
239 {{1, 3}, 1000},
240 {{4, 7}, 100},
241 {{7, 100}, 1}
242 };
243
244 // Defines valid record codes.
245 typedef unsigned RecordCodeType;
246
247 // Special code to signify adding random other record codes.
248 static const RecordCodeType OtherRecordCode = 575757575;
249
250 // List of record codes we can generate. The weights are based
251 // on record counts in pnacl-llc.pexed, using how many thousand of
252 // each record code appeared (or 1 if less than 1 thousand).
253 WeightedValue<RecordCodeType> RecordCodeDist[] = {
254 {naclbitc::BLOCKINFO_CODE_SETBID, 1},
255 {naclbitc::MODULE_CODE_VERSION, 1},
256 {naclbitc::MODULE_CODE_FUNCTION, 7},
257 {naclbitc::TYPE_CODE_NUMENTRY, 1},
258 {naclbitc::TYPE_CODE_VOID, 1},
259 {naclbitc::TYPE_CODE_FLOAT, 1},
260 {naclbitc::TYPE_CODE_DOUBLE, 1},
261 {naclbitc::TYPE_CODE_INTEGER, 1},
262 {naclbitc::TYPE_CODE_VECTOR, 1},
263 {naclbitc::TYPE_CODE_FUNCTION, 1},
264 {naclbitc::VST_CODE_ENTRY, 1},
265 {naclbitc::VST_CODE_BBENTRY, 1},
266 {naclbitc::CST_CODE_SETTYPE, 15},
267 {naclbitc::CST_CODE_UNDEF, 1},
268 {naclbitc::CST_CODE_INTEGER, 115},
269 {naclbitc::CST_CODE_FLOAT, 1},
270 {naclbitc::GLOBALVAR_VAR, 14},
271 {naclbitc::GLOBALVAR_COMPOUND, 1},
272 {naclbitc::GLOBALVAR_ZEROFILL, 2},
273 {naclbitc::GLOBALVAR_DATA, 18},
274 {naclbitc::GLOBALVAR_RELOC, 20},
275 {naclbitc::GLOBALVAR_COUNT, 1},
276 {naclbitc::FUNC_CODE_DECLAREBLOCKS, 6},
277 {naclbitc::FUNC_CODE_INST_BINOP, 402},
278 {naclbitc::FUNC_CODE_INST_CAST, 61},
279 {naclbitc::FUNC_CODE_INST_EXTRACTELT, 1},
280 {naclbitc::FUNC_CODE_INST_INSERTELT, 1},
281 {naclbitc::FUNC_CODE_INST_RET, 7},
282 {naclbitc::FUNC_CODE_INST_BR, 223},
283 {naclbitc::FUNC_CODE_INST_SWITCH, 7},
284 {naclbitc::FUNC_CODE_INST_UNREACHABLE, 1},
285 {naclbitc::FUNC_CODE_INST_PHI, 84},
286 {naclbitc::FUNC_CODE_INST_ALLOCA, 34},
287 {naclbitc::FUNC_CODE_INST_LOAD, 225},
288 {naclbitc::FUNC_CODE_INST_STORE, 461},
289 {naclbitc::FUNC_CODE_INST_CMP2, 140},
290 {naclbitc::FUNC_CODE_INST_VSELECT, 10},
291 {naclbitc::FUNC_CODE_INST_CALL, 80},
292 {naclbitc::FUNC_CODE_INST_FORWARDTYPEREF, 36},
293 {naclbitc::FUNC_CODE_INST_CALL_INDIRECT, 5},
294 {naclbitc::BLK_CODE_ENTER, 1},
295 {naclbitc::BLK_CODE_EXIT, 1},
296 {naclbitc::BLK_CODE_DEFINE_ABBREV, 1},
297 {OtherRecordCode, 1}
298 };
299
300 // *Warning* The current implementation does not work on empty bitcode
301 // record lists.
302 class SimpleRecordFuzzer : public RecordFuzzer {
303 public:
304 explicit SimpleRecordFuzzer(NaClMungedBitcode &Bitcode)
305 : RecordFuzzer(Bitcode), RecordCounter(Bitcode.getBaseRecords().size()),
306 ActionWeight(ARRAY(ActionDist)),
307 RecordSizeWeight(ARRAY(RecordSizeDist)),
308 PosValueWeight(ARRAY(PosValueDist)),
309 NegValueWeight(ARRAY(NegValueDist)),
310 RecordCodeWeight(ARRAY(RecordCodeDist)) {
311 assert(!Bitcode.getBaseRecords().empty()
312 && "Can't fuzz empty list of records");
313
314 // Check on limitations of current random value generator.
315 if (Bitcode.getBaseRecords().size() > RAND_MAX)
Martin Barbella 2015/05/26 20:06:41 What sort of sizes are we expecting to see here?
Karl 2015/05/26 20:39:21 The main reason for this comment is that the docum
jvoung (off chromium) 2015/05/26 20:43:14 http://llvm.org/docs/doxygen/html/RandomNumberGene
Karl 2015/05/29 20:59:34 Modified API to allow the random number generator
316 errs() << "*Warning*: Only first " << RAND_MAX
317 << " records will be fuzzed!\n"
318 "";
319 for (const auto RecordCode : RecordCodeWeight)
320 UsedRecordCodes.insert(RecordCode.Value);
321
322 }
323
324 ~SimpleRecordFuzzer() final;
325
326 bool fuzz(float Percentage) final;
327
328 void setRandomGeneratorSeed(uint64_t NewValue) final {
329 srand(NewValue);
330 }
331
332 void showRecordDistribution(raw_ostream &Out) const final;
333
334 void showEditDistribution(raw_ostream &Out) const final;
335
336 private:
337 // Count how many edits are applied to each record in the bitcode.
338 DistCounter RecordCounter;
339 // Distribution used to randomly choose edit actions.
340 CountedWeightedDistribution<RecordFuzzer::EditAction> ActionWeight;
341 // Distribution used to randomly choose the size of created records.
342 WeightedRangeDistribution<size_t> RecordSizeWeight;
343 // Distribution used to randomly choose positive values for records.
344 WeightedRangeDistribution<ValueType> PosValueWeight;
345 // Distribution of value sign used to randomly choose value for records.
346 WeightedDistribution<bool> NegValueWeight;
347 // Distribution used to choose record codes for records.
348 WeightedDistribution<RecordCodeType> RecordCodeWeight;
349 // Filter to make sure the "other" choice for record codes will not
350 // choose any other record code in RecordCodeWeight.
351 std::set<size_t> UsedRecordCodes;
352
353 // Randomly choose an edit action.
354 RecordFuzzer::EditAction chooseAction() {
355 return ActionWeight.choose().Value;
356 }
357 // Randomly choose a record index from the list of records to edit.
358 size_t chooseRecordIndex() {
359 return RecordCounter.increment(
360 chooseInRange(Bitcode.getBaseRecords().size()));
361 }
362
363 // Randomly choose a record code.
364 RecordCodeType chooseRecordCode() {
365 RecordCodeType Code = RecordCodeWeight.choose().Value;
366 if (Code != OtherRecordCode)
367 return Code;
368 Code = chooseInRange(UINT_MAX);
369 while (UsedRecordCodes.count(Code)) // don't use predefined values.
370 ++Code;
371 return Code;
372 }
373
374 // Randomly choose a positive value for use in a record.
375 ValueType choosePositiveValue() {
376 return PosValueWeight.chooseValue();
377 }
378
379 // Randomly choose a positive/negative value for use in a record.
380 ValueType chooseValue() {
381 ValueType Value = choosePositiveValue();
382 if (NegValueWeight.choose().Value) {
383 SignedValueType SignedValue = static_cast<SignedValueType>(Value);
384 SignedValue = -SignedValue;
385 Value = static_cast<ValueType>(SignedValue);
386 }
387 return Value;
388 }
389
390 // Randomly fill in a record with record values.
391 void chooseRecordValues(NaClBitcodeAbbrevRecord &Record) {
392 Record.Abbrev = naclbitc::UNABBREV_RECORD;
393 Record.Code = chooseRecordCode();
394 Record.Values.clear();
395 size_t NumValues = RecordSizeWeight.chooseValue();
396 for (size_t i = 0; i < NumValues; ++i) {
397 Record.Values.push_back(chooseValue());
398 }
399 }
400
401 // Apply the given edit action to a random record.
402 void applyAction(EditAction Action);
403
404 // Randomly mutate a record.
405 void mutateRecord(NaClBitcodeAbbrevRecord &Record) {
406 // TODO: Do something smarter than just changing a value
407 // in the record.
408 size_t Index = chooseInRange(Record.Values.size() + 1);
409 if (Index == 0)
410 Record.Code = chooseRecordCode();
411 else
412 Record.Values[Index - 1] = chooseValue();
413 }
414 };
415
416 SimpleRecordFuzzer::~SimpleRecordFuzzer() {}
417
418 bool SimpleRecordFuzzer::fuzz(float Percentage) {
419 // TODO(kschimpf): Add some randomness in the number of actions selected.
420 clear();
421 size_t NumRecords = Bitcode.getBaseRecords().size();
422 size_t NumActions = nearbyintf(Percentage * NumRecords);
423 if (NumActions == 0)
424 NumActions = 1;
425 for (size_t i = 0; i < NumActions; ++i)
426 applyAction(chooseAction());
427 return true;
428 }
429
430 void SimpleRecordFuzzer::applyAction(EditAction Action) {
431 size_t Index = chooseRecordIndex();
432 switch(Action) {
433 case InsertRecord: {
434 NaClBitcodeAbbrevRecord Record;
435 chooseRecordValues(Record);
436 if (chooseInRange(2))
437 Bitcode.addBefore(Index, Record);
438 else
439 Bitcode.addAfter(Index, Record);
440 return;
441 }
442 case RemoveRecord:
443 Bitcode.remove(Index);
444 return;
445 case ReplaceRecord: {
446 NaClBitcodeAbbrevRecord Record;
447 chooseRecordValues(Record);
448 Bitcode.replace(Index, Record);
449 return;
450 }
451 case MutateRecord: {
452 NaClBitcodeAbbrevRecord Copy(*Bitcode.getBaseRecords()[Index]);
453 mutateRecord(Copy);
454 // Bitcode.replace(Index, Copy);
455 return;
456 }
457 case SwapRecord: {
458 size_t Index2 = chooseRecordIndex();
459 Bitcode.replace(Index, *Bitcode.getBaseRecords()[Index2]);
460 Bitcode.replace(Index2, *Bitcode.getBaseRecords()[Index]);
461 return;
462 }
463 }
464 }
465
466 // Returns corresponding percenage defined by Count/Total, in a form
467 // that can be printed to a raw_ostream.
468 format_object1<float> percentage(size_t Count, size_t Total) {
469 float percent = Total == 0.0 ? 0.0 : 100.0 * Count / Total;
470 return format("%1.0f", nearbyintf(percent));
471 }
472
473 void SimpleRecordFuzzer::showRecordDistribution(raw_ostream &Out) const {
474 Out << "Edit Record Distribution (Total: " << RecordCounter.getTotal()
475 << "):\n";
476 size_t Total = RecordCounter.getTotal();
477 for (size_t i = 0; i < Bitcode.getBaseRecords().size(); ++i) {
478 size_t Count = RecordCounter[i];
479 Out << " " << format("%zd", i) << ": "
480 << Count << " (" << percentage(Count, Total) << "%)\n";
481 }
482 }
483
484 void SimpleRecordFuzzer::showEditDistribution(raw_ostream &Out) const {
485 size_t TotalWeight = ActionWeight.getTotalWeight();
486 size_t TotalCount = ActionWeight.getTotalChooseCount();
487 Out << "Edit Action Distribution(Total: " << TotalCount << "):\n";
488 size_t ActionIndex = 0;
489 for (const auto &Action : ActionWeight) {
490 size_t ActionCount = ActionWeight.getChooseCount(ActionIndex);
491 Out << " " << actionName(Action.Value) << " - Wanted: "
492 << percentage(Action.Weight, TotalWeight) << "%, Applied: "
493 << ActionCount << " (" << percentage(ActionCount, TotalCount)
494 << "%)\n";
495 ++ActionIndex;
496 }
497 }
498
499 } // end of anonymous
500
501 namespace naclfuzz {
502
503 RecordFuzzer *RecordFuzzer::createSimpleRecordFuzzer(
504 NaClMungedBitcode &Bitcode) {
505 return new SimpleRecordFuzzer(Bitcode);
506 }
507
508 } // end of namespace naclfuzz
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698