fix classify counts
MODIFY
include/afl-fuzz.h
include/afl-fuzz.h
@@ -1046,6 +1046,7 @@ u32 count_bytes(afl_state_t *, u8 *);
u32 count_non_255_bytes(afl_state_t *, u8 *);
void simplify_trace(afl_state_t *, u8 *);
void classify_counts(afl_forkserver_t *);
+void classify_counts_off(afl_forkserver_t *, u32);
#ifdef WORD_SIZE_64
void discover_word(u8 *ret, u64 *current, u64 *virgin);
#else

MODIFY
include/coverage-32.h
include/coverage-32.h
@@ -62,6 +62,23 @@ inline void classify_counts(afl_forkserver_t *fsrv) {
}
+inline void classify_counts_off(afl_forkserver_t *fsrv, u32 off) {
+
+ u32 *mem = (u32 *)(fsrv->trace_bits + off);
+ u32 i = ((fsrv->map_size - off) >> 2);
+
+ while (i--) {
+
+ /* Optimize for sparse bitmaps. */
+
+ if (unlikely(*mem)) { *mem = classify_word(*mem); }
+
+ mem++;
+
+ }
+
+}
+
/* Updates the virgin bits, then reflects whether a new count or a new tuple is
* seen in ret. */
inline void discover_word(u8 *ret, u32 *current, u32 *virgin) {
@@ -70,7 +87,7 @@ inline void discover_word(u8 *ret, u32 *current, u32 *virgin) {
that have not been already cleared from the virgin map - since this will
almost always be the case. */
- if (*current & *virgin) {
+ if (unlikely(*current & *virgin)) {
if (likely(*ret < 2)) {
@@ -80,8 +97,8 @@ inline void discover_word(u8 *ret, u32 *current, u32 *virgin) {
/* Looks like we have not found any new bytes yet; see if any non-zero
bytes in current[] are pristine in virgin[]. */
- if ((cur[0] && vir[0] == 0xff) || (cur[1] && vir[1] == 0xff) ||
- (cur[2] && vir[2] == 0xff) || (cur[3] && vir[3] == 0xff))
+ if (unlikely((cur[0] && vir[0] == 0xff) || (cur[1] && vir[1] == 0xff) ||
+ (cur[2] && vir[2] == 0xff) || (cur[3] && vir[3] == 0xff)))
*ret = 2;
else
*ret = 1;
@@ -97,12 +114,14 @@ inline void discover_word(u8 *ret, u32 *current, u32 *virgin) {
#define PACK_SIZE 16
inline u32 skim(const u32 *virgin, const u32 *current, const u32 *current_end) {
+ u32 *save = (u32*) current;
+
for (; current < current_end; virgin += 4, current += 4) {
- if (current[0] && classify_word(current[0]) & virgin[0]) return 1;
- if (current[1] && classify_word(current[1]) & virgin[1]) return 1;
- if (current[2] && classify_word(current[2]) & virgin[2]) return 1;
- if (current[3] && classify_word(current[3]) & virgin[3]) return 1;
+ if (unlikely(current[0] && classify_word(current[0]) & virgin[0])) return (u32)(&current[1] - save);
+ if (unlikely(current[1] && classify_word(current[1]) & virgin[1])) return (u32)(&current[2] - save);
+ if (unlikely(current[2] && classify_word(current[2]) & virgin[2])) return (u32)(&current[3] - save);
+ if (unlikely(current[3] && classify_word(current[3]) & virgin[3])) return (u32)(&current[4] - save);
}

MODIFY
include/coverage-64.h
include/coverage-64.h
@@ -72,6 +72,23 @@ inline void classify_counts(afl_forkserver_t *fsrv) {
}
+inline void classify_counts_off(afl_forkserver_t *fsrv, u32 off) {
+
+ u64 *mem = (u64 *)(fsrv->trace_bits + off);
+ u32 i = ((fsrv->map_size - off) >> 3);
+
+ while (i--) {
+
+ /* Optimize for sparse bitmaps. */
+
+ if (unlikely(*mem)) { *mem = classify_word(*mem); }
+
+ mem++;
+
+ }
+
+}
+
/* Updates the virgin bits, then reflects whether a new count or a new tuple is
* seen in ret. */
inline void discover_word(u8 *ret, u64 *current, u64 *virgin) {
@@ -110,17 +127,20 @@ inline void discover_word(u8 *ret, u64 *current, u64 *virgin) {
#define PACK_SIZE 64
inline u32 skim(const u64 *virgin, const u64 *current, const u64 *current_end) {
+ u64 *save = (u64*) current;
+
for (; current != current_end; virgin += 8, current += 8) {
__m512i value = *(__m512i *)current;
__mmask8 mask = _mm512_testn_epi64_mask(value, value);
/* All bytes are zero. */
- if (mask == 0xff) continue;
+ if (likely(mask == 0xff)) continue;
/* Look for nonzero bytes and check for new bits. */
#define UNROLL(x) \
- if (!(mask & (1 << x)) && classify_word(current[x]) & virgin[x]) return 1
+ if (unlikely(!(mask & (1 << x)) && classify_word(current[x]) & virgin[x])) \
+ return (u32)(&current[x + 1] - save)
UNROLL(0);
UNROLL(1);
UNROLL(2);
@@ -143,6 +163,7 @@ inline u32 skim(const u64 *virgin, const u64 *current, const u64 *current_end) {
#define PACK_SIZE 32
inline u32 skim(const u64 *virgin, const u64 *current, const u64 *current_end) {
+ u64 *save = (u64*) current;
__m256i zeroes = _mm256_setzero_si256();
for (; current < current_end; virgin += 4, current += 4) {
@@ -152,13 +173,17 @@ inline u32 skim(const u64 *virgin, const u64 *current, const u64 *current_end) {
u32 mask = _mm256_movemask_epi8(cmp);
/* All bytes are zero. */
- if (mask == (u32)-1) continue;
+ if (likely(mask == (u32)-1)) continue;
/* Look for nonzero bytes and check for new bits. */
- if (!(mask & 0xff) && classify_word(current[0]) & virgin[0]) return 1;
- if (!(mask & 0xff00) && classify_word(current[1]) & virgin[1]) return 1;
- if (!(mask & 0xff0000) && classify_word(current[2]) & virgin[2]) return 1;
- if (!(mask & 0xff000000) && classify_word(current[3]) & virgin[3]) return 1;
+ if (unlikely(!(mask & 0xff) && classify_word(current[0]) & virgin[0]))
+ return (u32)(&current[1] - save);
+ if (unlikely(!(mask & 0xff00) && classify_word(current[1]) & virgin[1]))
+ return (u32)(&current[2] - save);
+ if (unlikely(!(mask & 0xff0000) && classify_word(current[2]) & virgin[2]))
+ return (u32)(&current[3] - save);
+ if (unlikely(!(mask & 0xff000000) && classify_word(current[3]) & virgin[3]))
+ return (u32)(&current[4] - save);
}
@@ -172,12 +197,14 @@ inline u32 skim(const u64 *virgin, const u64 *current, const u64 *current_end) {
#define PACK_SIZE 32
inline u32 skim(const u64 *virgin, const u64 *current, const u64 *current_end) {
+ u64 *save = (u64*) current;
+
for (; current < current_end; virgin += 4, current += 4) {
- if (current[0] && classify_word(current[0]) & virgin[0]) return 1;
- if (current[1] && classify_word(current[1]) & virgin[1]) return 1;
- if (current[2] && classify_word(current[2]) & virgin[2]) return 1;
- if (current[3] && classify_word(current[3]) & virgin[3]) return 1;
+ if (unlikely(current[0] && classify_word(current[0]) & virgin[0])) return (u32)(&current[1] - save);
+ if (unlikely(current[1] && classify_word(current[1]) & virgin[1])) return (u32)(&current[2] - save);
+ if (unlikely(current[2] && classify_word(current[2]) & virgin[2])) return (u32)(&current[3] - save);
+ if (unlikely(current[3] && classify_word(current[3]) & virgin[3])) return (u32)(&current[4] - save);
}

MODIFY
instrumentation/afl-llvm-pass.so.cc
instrumentation/afl-llvm-pass.so.cc
@@ -631,18 +631,23 @@ bool AFLCoverage::runOnModule(Module &M) {
LoadInst *PrevLoc;
if (ngram_size) {
+
PrevLoc = IRB.CreateLoad(
#if LLVM_VERSION_MAJOR >= 14
PrevLocTy,
#endif
AFLPrevLoc);
+
} else {
+
PrevLoc = IRB.CreateLoad(
#if LLVM_VERSION_MAJOR >= 14
IRB.getInt32Ty(),
#endif
AFLPrevLoc);
+
}
+
PrevLoc->setMetadata(M.getMDKindID("nosanitize"), MDNode::get(C, None));
Value *PrevLocTrans;

MODIFY
instrumentation/cmplog-instructions-pass.cc
instrumentation/cmplog-instructions-pass.cc
@@ -498,6 +498,7 @@ bool CmpLogInstructions::hookInstrs(Module &M) {
last_val1 = cur_val;
}
+
*/
} else {

MODIFY
src/afl-cc.c
src/afl-cc.c
@@ -876,11 +876,12 @@ static void edit_params(u32 argc, char **argv, char **envp) {
cc_params[cc_par_cnt++] = "-fsanitize=leak";
cc_params[cc_par_cnt++] = "-includesanitizer/lsan_interface.h";
- cc_params[cc_par_cnt++] = "-D__AFL_LEAK_CHECK()={if(__lsan_do_recoverable_leak_check() > 0) _exit(23); }";
+ cc_params[cc_par_cnt++] =
+ "-D__AFL_LEAK_CHECK()={if(__lsan_do_recoverable_leak_check() > 0) "
+ "_exit(23); }";
cc_params[cc_par_cnt++] = "-D__AFL_LSAN_OFF()=__lsan_disable();";
cc_params[cc_par_cnt++] = "-D__AFL_LSAN_ON()=__lsan_enable();";
-
}
if (getenv("AFL_USE_CFISAN")) {

MODIFY
src/afl-fuzz-bitmap.c
src/afl-fuzz-bitmap.c
@@ -250,20 +250,21 @@ inline u8 has_new_bits(afl_state_t *afl, u8 *virgin_map) {
inline u8 has_new_bits_unclassified(afl_state_t *afl, u8 *virgin_map) {
/* Handle the hot path first: no new coverage */
+ u32 off;
u8 *end = afl->fsrv.trace_bits + afl->fsrv.map_size;
#ifdef WORD_SIZE_64
- if (!skim((u64 *)virgin_map, (u64 *)afl->fsrv.trace_bits, (u64 *)end))
+ if (!(off = skim((u64 *)virgin_map, (u64 *)afl->fsrv.trace_bits, (u64 *)end)))
return 0;
#else
- if (!skim((u32 *)virgin_map, (u32 *)afl->fsrv.trace_bits, (u32 *)end))
+ if (!(off = skim((u32 *)virgin_map, (u32 *)afl->fsrv.trace_bits, (u32 *)end)))
return 0;
#endif /* ^WORD_SIZE_64 */
- classify_counts(&afl->fsrv);
+ classify_counts_off(&afl->fsrv, off);
return has_new_bits(afl, virgin_map);
}

MODIFY
src/afl-fuzz-queue.c
src/afl-fuzz-queue.c
@@ -769,11 +769,7 @@ void cull_queue(afl_state_t *afl) {
afl->top_rated[i]->favored = 1;
++afl->queued_favored;
- if (!afl->top_rated[i]->was_fuzzed) {
-
- ++afl->pending_favored;
-
- }
+ if (!afl->top_rated[i]->was_fuzzed) { ++afl->pending_favored; }
}

fix classify counts