[libFuzzer] Fix endianness issue in ForEachNonZeroByte()

The usage pattern of Bundle variable assumes the machine is little
endian, which is not the case on SystemZ. Fix by converting Bundle to
little-endian when necessary.
This commit is contained in:
Ilya Leoshkevich 2020-07-30 20:07:11 +02:00
parent 47f7174ffa
commit a4e537d9c4
2 changed files with 9 additions and 1 deletions

View File

@ -194,10 +194,12 @@ size_t ForEachNonZeroByte(const uint8_t *Begin, const uint8_t *End,
// Iterate by Step bytes at a time. // Iterate by Step bytes at a time.
for (; P < End; P += Step) for (; P < End; P += Step)
if (LargeType Bundle = *reinterpret_cast<const LargeType *>(P)) if (LargeType Bundle = *reinterpret_cast<const LargeType *>(P)) {
Bundle = HostToLE(Bundle);
for (size_t I = 0; I < Step; I++, Bundle >>= 8) for (size_t I = 0; I < Step; I++, Bundle >>= 8)
if (uint8_t V = Bundle & 0xff) if (uint8_t V = Bundle & 0xff)
Handle8bitCounter(FirstFeature, P - Begin + I, V); Handle8bitCounter(FirstFeature, P - Begin + I, V);
}
// Iterate by 1 byte until the end. // Iterate by 1 byte until the end.
for (; P < End; P++) for (; P < End; P++)

View File

@ -106,6 +106,12 @@ inline uint8_t *RoundDownByPage(uint8_t *P) {
return reinterpret_cast<uint8_t *>(X); return reinterpret_cast<uint8_t *>(X);
} }
#if __BYTE_ORDER == __LITTLE_ENDIAN
template <typename T> T HostToLE(T X) { return X; }
#else
template <typename T> T HostToLE(T X) { return Bswap(X); }
#endif
} // namespace fuzzer } // namespace fuzzer
#endif // LLVM_FUZZER_UTIL_H #endif // LLVM_FUZZER_UTIL_H