Index: lib/fuzzer/FuzzerTracePC.h =================================================================== --- lib/fuzzer/FuzzerTracePC.h +++ lib/fuzzer/FuzzerTracePC.h @@ -178,6 +178,9 @@ Set ObservedPCs; Set ObservedFuncs; + template + void IterateInline8bitCounters(Callback CB) const; + std::pair FocusFunction = {-1, -1}; // Module and PC IDs. ValueBitMap ValueProfileMap; Index: lib/fuzzer/FuzzerTracePC.cpp =================================================================== --- lib/fuzzer/FuzzerTracePC.cpp +++ lib/fuzzer/FuzzerTracePC.cpp @@ -57,36 +57,36 @@ return Res; } +template +void TracePC::IterateInline8bitCounters(CallBack CB) const { + size_t CounterIdx = 0; + for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) { + uint8_t *Beg = ModuleCounters[i].Start; + size_t Size = ModuleCounters[i].Stop - Beg; + assert(Size == (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start)); + for (size_t j = 0; j < Size; j++, CounterIdx++) + CB(i, j, CounterIdx); + } +} + // Initializes unstable counters by copying Inline8bitCounters to unstable // counters. void TracePC::InitializeUnstableCounters() { - if (NumInline8bitCounters && NumInline8bitCounters == NumPCsInPCTables) { - size_t UnstableIdx = 0; - for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) { - uint8_t *Beg = ModuleCounters[i].Start; - size_t Size = ModuleCounters[i].Stop - Beg; - assert(Size == (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start)); - for (size_t j = 0; j < Size; j++, UnstableIdx++) - if (UnstableCounters[UnstableIdx] != kUnstableCounter) - UnstableCounters[UnstableIdx] = Beg[j]; - } - } + if (NumInline8bitCounters && NumInline8bitCounters == NumPCsInPCTables) + IterateInline8bitCounters([&](int i, int j, int UnstableIdx) { + if (UnstableCounters[UnstableIdx] != kUnstableCounter) + UnstableCounters[UnstableIdx] = ModuleCounters[i].Start[j]; + }); } // Compares the current counters with counters from previous runs // and records differences as unstable edges. void TracePC::UpdateUnstableCounters() { - if (NumInline8bitCounters && NumInline8bitCounters == NumPCsInPCTables) { - size_t UnstableIdx = 0; - for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) { - uint8_t *Beg = ModuleCounters[i].Start; - size_t Size = ModuleCounters[i].Stop - Beg; - assert(Size == (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start)); - for (size_t j = 0; j < Size; j++, UnstableIdx++) - if (Beg[j] != UnstableCounters[UnstableIdx]) - UnstableCounters[UnstableIdx] = kUnstableCounter; - } - } + if (NumInline8bitCounters && NumInline8bitCounters == NumPCsInPCTables) + IterateInline8bitCounters([&](int i, int j, int UnstableIdx) { + if (ModuleCounters[i].Start[j] != UnstableCounters[UnstableIdx]) + UnstableCounters[UnstableIdx] = kUnstableCounter; + }); } void TracePC::HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop) { @@ -191,26 +191,15 @@ if (NumPCsInPCTables) { if (NumInline8bitCounters == NumPCsInPCTables) { - for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) { - uint8_t *Beg = ModuleCounters[i].Start; - size_t Size = ModuleCounters[i].Stop - Beg; - assert(Size == - (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start)); - for (size_t j = 0; j < Size; j++) - if (Beg[j]) - Observe(ModulePCTable[i].Start[j]); - } + IterateInline8bitCounters([&](int i, int j, int CounterIdx) { + if (ModuleCounters[i].Start[j]) + Observe(ModulePCTable[i].Start[j]); + }); } else if (NumGuards == NumPCsInPCTables) { - size_t GuardIdx = 1; - for (size_t i = 0; i < NumModules; i++) { - uint32_t *Beg = Modules[i].Start; - size_t Size = Modules[i].Stop - Beg; - assert(Size == - (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start)); - for (size_t j = 0; j < Size; j++, GuardIdx++) - if (Counters()[GuardIdx]) - Observe(ModulePCTable[i].Start[j]); - } + IterateInline8bitCounters([&](int i, int j, int CounterIdx) { + if (Counters()[CounterIdx+1]) + Observe(ModulePCTable[i].Start[j]); + }); } }