[libFuzzer] use bits instead of bytes for memcmp/strcmp value profile -- the fuzzer reaches the goal much faster, at least on the simple puzzles

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@280054 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Kostya Serebryany 2016-08-30 03:05:50 +00:00
parent 23b4641779
commit c7c6f45c22
2 changed files with 21 additions and 13 deletions

View File

@ -552,26 +552,34 @@ static void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
size_t n) {
if (!n) return;
size_t Len = std::min(n, (size_t)32);
const char *A1 = reinterpret_cast<const char *>(s1);
const char *A2 = reinterpret_cast<const char *>(s2);
size_t LastSameByte = 0;
for (; LastSameByte < Len; LastSameByte++)
if (A1[LastSameByte] != A2[LastSameByte])
const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
size_t I = 0;
for (; I < Len; I++)
if (A1[I] != A2[I])
break;
size_t PC = reinterpret_cast<size_t>(caller_pc);
VP.AddValue((PC & 4095) | (LastSameByte << 12));
size_t Idx = I * 8;
if (I < Len)
Idx += __builtin_popcountl((A1[I] ^ A2[I])) - 1;
VP.AddValue((PC & 4095) | (Idx << 12));
}
static void AddValueForStrcmp(void *caller_pc, const char *s1, const char *s2,
size_t n) {
if (!n) return;
size_t Len = std::min(n, (size_t)32);
size_t LastSameByte = 0;
for (; LastSameByte < Len; LastSameByte++)
if (s1[LastSameByte] != s2[LastSameByte] || s1[LastSameByte] == 0)
const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
size_t I = 0;
for (; I < Len; I++)
if (A1[I] != A2[I] || A1[I] == 0)
break;
size_t PC = reinterpret_cast<size_t>(caller_pc);
VP.AddValue((PC & 4095) | (LastSameByte << 12));
size_t Idx = I * 8;
if (I < Len && A1[I])
Idx += __builtin_popcountl((A1[I] ^ A2[I])) - 1;
VP.AddValue((PC & 4095) | (Idx << 12));
}
ATTRIBUTE_TARGET_POPCNT

View File

@ -1,4 +1,4 @@
CHECK: BINGO
RUN: not LLVMFuzzer-SingleMemcmpTest -seed=1 -use_memcmp=0 -use_value_profile=1 -runs=10000000 2>&1 | FileCheck %s
RUN: not LLVMFuzzer-SingleStrcmpTest -seed=1 -use_memcmp=0 -use_value_profile=1 -runs=10000000 2>&1 | FileCheck %s
RUN: not LLVMFuzzer-SingleStrncmpTest -seed=1 -use_memcmp=0 -use_value_profile=1 -runs=10000000 2>&1 | FileCheck %s
RUN: not LLVMFuzzer-SingleMemcmpTest -seed=1 -use_memcmp=0 -use_value_profile=1 -runs=1000000 2>&1 | FileCheck %s
RUN: not LLVMFuzzer-SingleStrcmpTest -seed=1 -use_memcmp=0 -use_value_profile=1 -runs=1000000 2>&1 | FileCheck %s
RUN: not LLVMFuzzer-SingleStrncmpTest -seed=1 -use_memcmp=0 -use_value_profile=1 -runs=1000000 2>&1 | FileCheck %s