blob: 8f7ff74300f45c4c55d0de83196125a9be305844 [file] [log] [blame]
Kostya Serebryanyc98ef712016-08-16 17:37:13 +00001//===- FuzzerValueBitMap.h - INTERNAL - Bit map -----------------*- 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// ValueBitMap.
10//===----------------------------------------------------------------------===//
11
12#ifndef LLVM_FUZZER_VALUE_BIT_MAP_H
13#define LLVM_FUZZER_VALUE_BIT_MAP_H
14
Kostya Serebryany86586182016-09-21 21:17:23 +000015#include "FuzzerDefs.h"
16
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000017namespace fuzzer {
18
19// A bit map containing kMapSizeInWords bits.
20struct ValueBitMap {
Kostya Serebryany70182de2017-01-27 00:39:12 +000021 static const size_t kMapSizeInBits = 1 << 16;
22 static const size_t kMapPrimeMod = 65371; // Largest Prime < kMapSizeInBits;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000023 static const size_t kBitsInWord = (sizeof(uintptr_t) * 8);
Kostya Serebryany70182de2017-01-27 00:39:12 +000024 static const size_t kMapSizeInWords = kMapSizeInBits / kBitsInWord;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000025 public:
Kostya Serebryany70182de2017-01-27 00:39:12 +000026
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000027 // Clears all bits.
28 void Reset() { memset(Map, 0, sizeof(Map)); }
29
Kostya Serebryany53501782016-09-15 04:36:45 +000030 // Computes a hash function of Value and sets the corresponding bit.
31 // Returns true if the bit was changed from 0 to 1.
Kostya Serebryany7f058972017-01-27 00:09:59 +000032 ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany53501782016-09-15 04:36:45 +000033 inline bool AddValue(uintptr_t Value) {
Kostya Serebryany70182de2017-01-27 00:39:12 +000034 uintptr_t Idx = Value % kMapSizeInBits;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000035 uintptr_t WordIdx = Idx / kBitsInWord;
36 uintptr_t BitIdx = Idx % kBitsInWord;
Kostya Serebryany53501782016-09-15 04:36:45 +000037 uintptr_t Old = Map[WordIdx];
38 uintptr_t New = Old | (1UL << BitIdx);
39 Map[WordIdx] = New;
40 return New != Old;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000041 }
42
Kostya Serebryany70182de2017-01-27 00:39:12 +000043 ATTRIBUTE_NO_SANITIZE_ALL
44 inline bool AddValueModPrime(uintptr_t Value) {
45 return AddValue(Value % kMapPrimeMod);
46 }
47
Kostya Serebryany0800b812016-09-23 23:51:58 +000048 inline bool Get(uintptr_t Idx) {
49 assert(Idx < kMapSizeInBits);
50 uintptr_t WordIdx = Idx / kBitsInWord;
51 uintptr_t BitIdx = Idx % kBitsInWord;
52 return Map[WordIdx] & (1UL << BitIdx);
53 }
54
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000055 size_t GetNumBitsSinceLastMerge() const { return NumBits; }
56
57 // Merges 'Other' into 'this', clears 'Other', updates NumBits,
58 // returns true if new bits were added.
Kostya Serebryanybceadcf2016-08-24 01:38:42 +000059 ATTRIBUTE_TARGET_POPCNT
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000060 bool MergeFrom(ValueBitMap &Other) {
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000061 uintptr_t Res = 0;
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000062 size_t OldNumBits = NumBits;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000063 for (size_t i = 0; i < kMapSizeInWords; i++) {
64 auto O = Other.Map[i];
65 auto M = Map[i];
66 if (O) {
67 Map[i] = (M |= O);
68 Other.Map[i] = 0;
69 }
Kostya Serebryanyd46a59f2016-08-16 19:33:51 +000070 if (M)
Marcos Pividori5a535672017-02-08 00:03:31 +000071 Res += __builtin_popcountll(M);
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000072 }
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000073 NumBits = Res;
74 return OldNumBits < NumBits;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000075 }
76
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000077 template <class Callback>
Kostya Serebryany7f058972017-01-27 00:09:59 +000078 ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany24d00162017-03-14 21:40:53 +000079 void ForEach(Callback CB) const {
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000080 for (size_t i = 0; i < kMapSizeInWords; i++)
81 if (uintptr_t M = Map[i])
82 for (size_t j = 0; j < sizeof(M) * 8; j++)
83 if (M & ((uintptr_t)1 << j))
84 CB(i * sizeof(M) * 8 + j);
85 }
86
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000087 private:
Kostya Serebryanya9a135b2016-09-29 15:51:28 +000088 size_t NumBits = 0;
Kostya Serebryanyc98ef712016-08-16 17:37:13 +000089 uintptr_t Map[kMapSizeInWords] __attribute__((aligned(512)));
90};
91
92} // namespace fuzzer
93
94#endif // LLVM_FUZZER_VALUE_BIT_MAP_H