2016-08-17 01:37:13 +08:00
|
|
|
//===- FuzzerValueBitMap.h - INTERNAL - Bit map -----------------*- C++ -* ===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ValueBitMap.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_FUZZER_VALUE_BIT_MAP_H
|
|
|
|
#define LLVM_FUZZER_VALUE_BIT_MAP_H
|
|
|
|
|
2016-09-22 05:17:23 +08:00
|
|
|
#include "FuzzerDefs.h"
|
|
|
|
|
2016-08-17 01:37:13 +08:00
|
|
|
namespace fuzzer {
|
|
|
|
|
|
|
|
// A bit map containing kMapSizeInWords bits.
|
|
|
|
struct ValueBitMap {
|
2017-01-27 08:39:12 +08:00
|
|
|
static const size_t kMapSizeInBits = 1 << 16;
|
|
|
|
static const size_t kMapPrimeMod = 65371; // Largest Prime < kMapSizeInBits;
|
2016-08-17 01:37:13 +08:00
|
|
|
static const size_t kBitsInWord = (sizeof(uintptr_t) * 8);
|
2017-01-27 08:39:12 +08:00
|
|
|
static const size_t kMapSizeInWords = kMapSizeInBits / kBitsInWord;
|
2016-08-17 01:37:13 +08:00
|
|
|
public:
|
2017-01-27 08:39:12 +08:00
|
|
|
|
2016-08-17 01:37:13 +08:00
|
|
|
// Clears all bits.
|
|
|
|
void Reset() { memset(Map, 0, sizeof(Map)); }
|
|
|
|
|
2016-09-15 12:36:45 +08:00
|
|
|
// Computes a hash function of Value and sets the corresponding bit.
|
|
|
|
// Returns true if the bit was changed from 0 to 1.
|
2017-01-27 08:09:59 +08:00
|
|
|
ATTRIBUTE_NO_SANITIZE_ALL
|
2016-09-15 12:36:45 +08:00
|
|
|
inline bool AddValue(uintptr_t Value) {
|
2017-01-27 08:39:12 +08:00
|
|
|
uintptr_t Idx = Value % kMapSizeInBits;
|
2016-08-17 01:37:13 +08:00
|
|
|
uintptr_t WordIdx = Idx / kBitsInWord;
|
|
|
|
uintptr_t BitIdx = Idx % kBitsInWord;
|
2016-09-15 12:36:45 +08:00
|
|
|
uintptr_t Old = Map[WordIdx];
|
|
|
|
uintptr_t New = Old | (1UL << BitIdx);
|
|
|
|
Map[WordIdx] = New;
|
|
|
|
return New != Old;
|
2016-08-17 01:37:13 +08:00
|
|
|
}
|
|
|
|
|
2017-01-27 08:39:12 +08:00
|
|
|
ATTRIBUTE_NO_SANITIZE_ALL
|
|
|
|
inline bool AddValueModPrime(uintptr_t Value) {
|
|
|
|
return AddValue(Value % kMapPrimeMod);
|
|
|
|
}
|
|
|
|
|
2016-09-24 07:51:58 +08:00
|
|
|
inline bool Get(uintptr_t Idx) {
|
|
|
|
assert(Idx < kMapSizeInBits);
|
|
|
|
uintptr_t WordIdx = Idx / kBitsInWord;
|
|
|
|
uintptr_t BitIdx = Idx % kBitsInWord;
|
|
|
|
return Map[WordIdx] & (1UL << BitIdx);
|
|
|
|
}
|
|
|
|
|
2017-07-20 09:35:17 +08:00
|
|
|
size_t SizeInBits() const { return kMapSizeInBits; }
|
|
|
|
|
2016-10-06 06:56:21 +08:00
|
|
|
template <class Callback>
|
2017-01-27 08:09:59 +08:00
|
|
|
ATTRIBUTE_NO_SANITIZE_ALL
|
2017-03-15 05:40:53 +08:00
|
|
|
void ForEach(Callback CB) const {
|
2016-10-06 06:56:21 +08:00
|
|
|
for (size_t i = 0; i < kMapSizeInWords; i++)
|
|
|
|
if (uintptr_t M = Map[i])
|
|
|
|
for (size_t j = 0; j < sizeof(M) * 8; j++)
|
|
|
|
if (M & ((uintptr_t)1 << j))
|
|
|
|
CB(i * sizeof(M) * 8 + j);
|
|
|
|
}
|
|
|
|
|
2016-08-17 01:37:13 +08:00
|
|
|
private:
|
|
|
|
uintptr_t Map[kMapSizeInWords] __attribute__((aligned(512)));
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace fuzzer
|
|
|
|
|
|
|
|
#endif // LLVM_FUZZER_VALUE_BIT_MAP_H
|