Path: blob/main/contrib/llvm-project/compiler-rt/lib/fuzzer/FuzzerValueBitMap.h
35262 views
//===- FuzzerValueBitMap.h - INTERNAL - Bit map -----------------*- C++ -* ===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7// ValueBitMap.8//===----------------------------------------------------------------------===//910#ifndef LLVM_FUZZER_VALUE_BIT_MAP_H11#define LLVM_FUZZER_VALUE_BIT_MAP_H1213#include "FuzzerPlatform.h"14#include <cstdint>1516namespace fuzzer {1718// A bit map containing kMapSizeInWords bits.19struct ValueBitMap {20static const size_t kMapSizeInBits = 1 << 16;21static const size_t kMapPrimeMod = 65371; // Largest Prime < kMapSizeInBits;22static const size_t kBitsInWord = (sizeof(uintptr_t) * 8);23static const size_t kMapSizeInWords = kMapSizeInBits / kBitsInWord;24public:2526// Clears all bits.27void Reset() { memset(Map, 0, sizeof(Map)); }2829// Computes a hash function of Value and sets the corresponding bit.30// Returns true if the bit was changed from 0 to 1.31ATTRIBUTE_NO_SANITIZE_ALL32inline bool AddValue(uintptr_t Value) {33uintptr_t Idx = Value % kMapSizeInBits;34uintptr_t WordIdx = Idx / kBitsInWord;35uintptr_t BitIdx = Idx % kBitsInWord;36uintptr_t Old = Map[WordIdx];37uintptr_t New = Old | (1ULL << BitIdx);38Map[WordIdx] = New;39return New != Old;40}4142ATTRIBUTE_NO_SANITIZE_ALL43inline bool AddValueModPrime(uintptr_t Value) {44return AddValue(Value % kMapPrimeMod);45}4647inline bool Get(uintptr_t Idx) {48assert(Idx < kMapSizeInBits);49uintptr_t WordIdx = Idx / kBitsInWord;50uintptr_t BitIdx = Idx % kBitsInWord;51return Map[WordIdx] & (1ULL << BitIdx);52}5354size_t SizeInBits() const { return kMapSizeInBits; }5556template <class Callback>57ATTRIBUTE_NO_SANITIZE_ALL58void ForEach(Callback CB) const {59for (size_t i = 0; i < kMapSizeInWords; i++)60if (uintptr_t M = Map[i])61for (size_t j = 0; j < sizeof(M) * 8; j++)62if (M & ((uintptr_t)1 << j))63CB(i * sizeof(M) * 8 + j);64}6566private:67ATTRIBUTE_ALIGNED(512) uintptr_t Map[kMapSizeInWords];68};6970} // namespace fuzzer7172#endif // LLVM_FUZZER_VALUE_BIT_MAP_H737475