[compiler-rt] a4e537d - [libFuzzer] Fix endianness issue in ForEachNonZeroByte()

Ilya Leoshkevich via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 4 12:53:36 PDT 2020


Author: Ilya Leoshkevich
Date: 2020-08-04T21:53:27+02:00
New Revision: a4e537d9c47aa378a24636e2d90d208389ad93ab

URL: https://github.com/llvm/llvm-project/commit/a4e537d9c47aa378a24636e2d90d208389ad93ab
DIFF: https://github.com/llvm/llvm-project/commit/a4e537d9c47aa378a24636e2d90d208389ad93ab.diff

LOG: [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.

Added: 
    

Modified: 
    compiler-rt/lib/fuzzer/FuzzerTracePC.h
    compiler-rt/lib/fuzzer/FuzzerUtil.h

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/fuzzer/FuzzerTracePC.h b/compiler-rt/lib/fuzzer/FuzzerTracePC.h
index 501f3b544971..4601300cb9dc 100644
--- a/compiler-rt/lib/fuzzer/FuzzerTracePC.h
+++ b/compiler-rt/lib/fuzzer/FuzzerTracePC.h
@@ -194,10 +194,12 @@ size_t ForEachNonZeroByte(const uint8_t *Begin, const uint8_t *End,
 
   // Iterate by Step bytes at a time.
   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)
         if (uint8_t V = Bundle & 0xff)
           Handle8bitCounter(FirstFeature, P - Begin + I, V);
+    }
 
   // Iterate by 1 byte until the end.
   for (; P < End; P++)

diff  --git a/compiler-rt/lib/fuzzer/FuzzerUtil.h b/compiler-rt/lib/fuzzer/FuzzerUtil.h
index 4ae35838306d..e90be085008e 100644
--- a/compiler-rt/lib/fuzzer/FuzzerUtil.h
+++ b/compiler-rt/lib/fuzzer/FuzzerUtil.h
@@ -106,6 +106,12 @@ inline uint8_t *RoundDownByPage(uint8_t *P) {
   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
 
 #endif  // LLVM_FUZZER_UTIL_H


        


More information about the llvm-commits mailing list