George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 1 | //===- FuzzerValueBitMap.h - INTERNAL - Bit map -----------------*- C++ -* ===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // ValueBitMap. |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef LLVM_FUZZER_VALUE_BIT_MAP_H |
| 12 | #define LLVM_FUZZER_VALUE_BIT_MAP_H |
| 13 | |
Dokyung Song | 226866e | 2020-07-14 21:02:32 | [diff] [blame] | 14 | #include "FuzzerPlatform.h" |
| 15 | #include <cstdint> |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 16 | |
| 17 | namespace fuzzer { |
| 18 | |
| 19 | // A bit map containing kMapSizeInWords bits. |
| 20 | struct ValueBitMap { |
| 21 | static const size_t kMapSizeInBits = 1 << 16; |
| 22 | static const size_t kMapPrimeMod = 65371; // Largest Prime < kMapSizeInBits; |
| 23 | static const size_t kBitsInWord = (sizeof(uintptr_t) * 8); |
| 24 | static const size_t kMapSizeInWords = kMapSizeInBits / kBitsInWord; |
| 25 | public: |
| 26 | |
| 27 | // Clears all bits. |
| 28 | void Reset() { memset(Map, 0, sizeof(Map)); } |
| 29 | |
| 30 | // Computes a hash function of Value and sets the corresponding bit. |
| 31 | // Returns true if the bit was changed from 0 to 1. |
| 32 | ATTRIBUTE_NO_SANITIZE_ALL |
| 33 | inline bool AddValue(uintptr_t Value) { |
| 34 | uintptr_t Idx = Value % kMapSizeInBits; |
| 35 | uintptr_t WordIdx = Idx / kBitsInWord; |
| 36 | uintptr_t BitIdx = Idx % kBitsInWord; |
| 37 | uintptr_t Old = Map[WordIdx]; |
Jonathan Metzman | 3c535a6 | 2019-01-22 18:59:25 | [diff] [blame] | 38 | uintptr_t New = Old | (1ULL << BitIdx); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 39 | Map[WordIdx] = New; |
| 40 | return New != Old; |
| 41 | } |
| 42 | |
| 43 | ATTRIBUTE_NO_SANITIZE_ALL |
| 44 | inline bool AddValueModPrime(uintptr_t Value) { |
| 45 | return AddValue(Value % kMapPrimeMod); |
| 46 | } |
| 47 | |
| 48 | inline bool Get(uintptr_t Idx) { |
| 49 | assert(Idx < kMapSizeInBits); |
| 50 | uintptr_t WordIdx = Idx / kBitsInWord; |
| 51 | uintptr_t BitIdx = Idx % kBitsInWord; |
Jonathan Metzman | 3c535a6 | 2019-01-22 18:59:25 | [diff] [blame] | 52 | return Map[WordIdx] & (1ULL << BitIdx); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | size_t SizeInBits() const { return kMapSizeInBits; } |
| 56 | |
| 57 | template <class Callback> |
| 58 | ATTRIBUTE_NO_SANITIZE_ALL |
| 59 | void ForEach(Callback CB) const { |
| 60 | for (size_t i = 0; i < kMapSizeInWords; i++) |
| 61 | if (uintptr_t M = Map[i]) |
| 62 | for (size_t j = 0; j < sizeof(M) * 8; j++) |
| 63 | if (M & ((uintptr_t)1 << j)) |
| 64 | CB(i * sizeof(M) * 8 + j); |
| 65 | } |
| 66 | |
| 67 | private: |
Jonathan Metzman | b795c31 | 2019-01-17 16:36:05 | [diff] [blame] | 68 | ATTRIBUTE_ALIGNED(512) uintptr_t Map[kMapSizeInWords]; |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | } // namespace fuzzer |
| 72 | |
| 73 | #endif // LLVM_FUZZER_VALUE_BIT_MAP_H |