[libFuzzer][MSVC] Enable building libFuzzer with MSVC

Summary:
Enable building libFuzzer with MSVC.

* Don't try to include <endian.h> in FuzzerSHA1.cpp. MSVC
  doesn't have this header, and WINDOWS is always little
  endian (even on ARM)

Subscribers: srhines, mgorny, javed.absar, kristof.beyls

Differential Revision: https://reviews.llvm.org/D56510

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk/lib/fuzzer@351855 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/CMakeLists.txt b/CMakeLists.txt
index caea973..f6c5b76 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -71,8 +71,14 @@
   list(APPEND LIBFUZZER_CFLAGS -fno-sanitize-coverage=trace-pc-guard,edge,trace-cmp,indirect-calls,8bit-counters)
 endif()
 
-if(NOT HAS_THREAD_LOCAL)
-  list(APPEND LIBFUZZER_CFLAGS -Dthread_local=__thread)
+if(OS_NAME MATCHES "Windows")
+  # Silence warnings with /Ehsc and avoid an error by unecessarily defining
+  # thread_local when it isn't even used on Windows.
+  list(APPEND LIBFUZZER_CFLAGS /EHsc)
+else()
+  if(NOT HAS_THREAD_LOCAL)
+    list(APPEND LIBFUZZER_CFLAGS -Dthread_local=__thread)
+  endif()
 endif()
 
 set(FUZZER_SUPPORTED_OS ${SANITIZER_COMMON_SUPPORTED_OS})
diff --git a/FuzzerBuiltinsMsvc.h b/FuzzerBuiltinsMsvc.h
index 20defc4..82709cf 100644
--- a/FuzzerBuiltinsMsvc.h
+++ b/FuzzerBuiltinsMsvc.h
@@ -24,7 +24,7 @@
 
 // __builtin_return_address() cannot be compiled with MSVC. Use the equivalent
 // from <intrin.h>
-#define GET_CALLER_PC() reinterpret_cast<uintptr_t>(_ReturnAddress())
+#define GET_CALLER_PC() _ReturnAddress()
 
 namespace fuzzer {
 
diff --git a/FuzzerSHA1.cpp b/FuzzerSHA1.cpp
index 99c075f..43e5e78 100644
--- a/FuzzerSHA1.cpp
+++ b/FuzzerSHA1.cpp
@@ -31,7 +31,8 @@
 
 #ifdef __BIG_ENDIAN__
 # define SHA_BIG_ENDIAN
-#elif defined __LITTLE_ENDIAN__
+// Windows is always little endian and MSVC doesn't have <endian.h>
+#elif defined __LITTLE_ENDIAN__ || LIBFUZZER_WINDOWS
 /* override */
 #elif defined __BYTE_ORDER
 # if __BYTE_ORDER__ ==  __ORDER_BIG_ENDIAN__
diff --git a/FuzzerValueBitMap.h b/FuzzerValueBitMap.h
index 03fb747..bc039f1 100644
--- a/FuzzerValueBitMap.h
+++ b/FuzzerValueBitMap.h
@@ -34,7 +34,7 @@
     uintptr_t WordIdx = Idx / kBitsInWord;
     uintptr_t BitIdx = Idx % kBitsInWord;
     uintptr_t Old = Map[WordIdx];
-    uintptr_t New = Old | (1UL << BitIdx);
+    uintptr_t New = Old | (1ULL << BitIdx);
     Map[WordIdx] = New;
     return New != Old;
   }
@@ -48,7 +48,7 @@
     assert(Idx < kMapSizeInBits);
     uintptr_t WordIdx = Idx / kBitsInWord;
     uintptr_t BitIdx = Idx % kBitsInWord;
-    return Map[WordIdx] & (1UL << BitIdx);
+    return Map[WordIdx] & (1ULL << BitIdx);
   }
 
   size_t SizeInBits() const { return kMapSizeInBits; }