[llvm-commits] [compiler-rt] r149130 - in /compiler-rt/trunk/lib/asan: asan_allocator.cc asan_interceptors.cc asan_interceptors.h asan_interface.h asan_internal.h asan_printf.cc asan_rtl.cc

Alexander Potapenko glider at google.com
Fri Jan 27 07:15:04 PST 2012


Author: glider
Date: Fri Jan 27 09:15:04 2012
New Revision: 149130

URL: http://llvm.org/viewvc/llvm-project?rev=149130&view=rev
Log:
Make compiler-rt/trunk/lib/asan compileable with Visual Studio 2008 on Windows.
Patch by Timur Iskhodzhanov (timurrrr at google.com)

To test:
$ cl /c *.c*
in the asan directory.

The code fails to link if you omit the "/c" part but that's one of the
next steps,
as well as a few TODO's I've put into the Windows-specific code.


Modified:
    compiler-rt/trunk/lib/asan/asan_allocator.cc
    compiler-rt/trunk/lib/asan/asan_interceptors.cc
    compiler-rt/trunk/lib/asan/asan_interceptors.h
    compiler-rt/trunk/lib/asan/asan_interface.h
    compiler-rt/trunk/lib/asan/asan_internal.h
    compiler-rt/trunk/lib/asan/asan_printf.cc
    compiler-rt/trunk/lib/asan/asan_rtl.cc

Modified: compiler-rt/trunk/lib/asan/asan_allocator.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_allocator.cc?rev=149130&r1=149129&r2=149130&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_allocator.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_allocator.cc Fri Jan 27 09:15:04 2012
@@ -59,15 +59,44 @@
   return (a & (alignment - 1)) == 0;
 }
 
+#ifdef _WIN32
+#include <intrin.h>
+#endif
+
 static inline size_t Log2(size_t x) {
   CHECK(IsPowerOfTwo(x));
+#if defined(_WIN64)
+  unsigned long ret;  // NOLINT
+  _BitScanForward64(&ret, x);
+  return ret;
+#elif defined(_WIN32)
+  unsigned long ret;  // NOLINT
+  _BitScanForward(&ret, x);
+  return ret;
+#else
   return __builtin_ctzl(x);
+#endif
+}
+
+static inline size_t clz(size_t x) {
+#if defined(_WIN64)
+  unsigned long ret;  // NOLINT
+  _BitScanReverse64(&ret, x);
+  return ret;
+#elif defined(_WIN32)
+  unsigned long ret;  // NOLINT
+  _BitScanReverse(&ret, x);
+  return ret;
+#else
+  return __builtin_clzl(x);
+#endif
 }
 
 static inline size_t RoundUpToPowerOfTwo(size_t size) {
   CHECK(size);
   if (IsPowerOfTwo(size)) return size;
-  size_t up = __WORDSIZE - __builtin_clzl(size);
+
+  size_t up = __WORDSIZE - clz(size);
   CHECK(size < (1ULL << up));
   CHECK(size > (1ULL << (up - 1)));
   return 1UL << up;

Modified: compiler-rt/trunk/lib/asan/asan_interceptors.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_interceptors.cc?rev=149130&r1=149129&r2=149130&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_interceptors.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_interceptors.cc Fri Jan 27 09:15:04 2012
@@ -24,9 +24,11 @@
 
 #include <new>
 #include <ctype.h>
-#include <dlfcn.h>
 
+#ifndef _WIN32
+#include <dlfcn.h>
 #include <pthread.h>
+#endif
 
 // To replace weak system functions on Linux we just need to declare functions
 // with same names in our library and then obtain the real function pointers
@@ -41,7 +43,7 @@
 // After interception, the calls to system functions will be substituted by
 // calls to our interceptors. We store pointers to system function f()
 // in __asan::real_f().
-#ifdef __APPLE__
+#if defined(__APPLE__)
 // Include the declarations of the original functions.
 #include <string.h>
 #include <strings.h>
@@ -71,6 +73,17 @@
 #define INTERCEPT_FUNCTION_IF_EXISTS(func)                              \
   OVERRIDE_FUNCTION_IF_EXISTS(func, WRAP(func))
 
+#elif defined(_WIN32)
+// TODO(timurrrr): change these macros once we decide how to intercept
+// functions on Windows.
+#define WRAPPER_NAME(x) #x
+
+#define INTERCEPT_FUNCTION(func)                                        \
+  do { } while (0)
+
+#define INTERCEPT_FUNCTION_IF_EXISTS(func)                              \
+  do { } while (0)
+
 #else  // __linux__
 #define WRAPPER_NAME(x) #x
 
@@ -287,6 +300,7 @@
   return t->ThreadStart();
 }
 
+#ifndef _WIN32
 extern "C"
 #ifndef __APPLE__
 __attribute__((visibility("default")))
@@ -318,6 +332,7 @@
   }
   return 0;
 }
+#endif  // _WIN32
 
 
 static void UnpoisonStackFromHereToTop() {

Modified: compiler-rt/trunk/lib/asan/asan_interceptors.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_interceptors.h?rev=149130&r1=149129&r2=149130&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_interceptors.h (original)
+++ compiler-rt/trunk/lib/asan/asan_interceptors.h Fri Jan 27 09:15:04 2012
@@ -16,7 +16,10 @@
 
 #include "asan_internal.h"
 
-#ifdef __APPLE__
+#if defined(__APPLE__)
+# define WRAP(x) wrap_##x
+#elif defined(_WIN32)
+// TODO(timurrrr): we're likely to use something else later on Windows.
 # define WRAP(x) wrap_##x
 #else
 # define WRAP(x) x

Modified: compiler-rt/trunk/lib/asan/asan_interface.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_interface.h?rev=149130&r1=149129&r2=149130&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_interface.h (original)
+++ compiler-rt/trunk/lib/asan/asan_interface.h Fri Jan 27 09:15:04 2012
@@ -15,7 +15,12 @@
 #ifndef ASAN_INTERFACE_H
 #define ASAN_INTERFACE_H
 
+#if !defined(_WIN32)
 #include <stdint.h>  // for __WORDSIZE
+#else
+// The __attribute__ keyword is not understood by Visual Studio.
+#define __attribute__(x)
+#endif
 #include <stdlib.h>  // for size_t
 
 // This header should NOT include any other headers from ASan runtime.

Modified: compiler-rt/trunk/lib/asan/asan_internal.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_internal.h?rev=149130&r1=149129&r2=149130&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_internal.h (original)
+++ compiler-rt/trunk/lib/asan/asan_internal.h Fri Jan 27 09:15:04 2012
@@ -14,12 +14,35 @@
 #ifndef ASAN_INTERNAL_H
 #define ASAN_INTERNAL_H
 
-#if !defined(__linux__) && !defined(__APPLE__)
+#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
 # error "This operating system is not supported by AddressSanitizer"
 #endif
 
+#include <stdlib.h>  // for size_t, uintptr_t, etc.
+
+#if !defined(_WIN32)
 #include <stdint.h>  // for __WORDSIZE
-#include <stdlib.h>  // for size_t
+#else
+// There's no <stdint.h> in Visual Studio 9, so we have to define [u]int*_t.
+typedef unsigned __int8  uint8_t;
+typedef unsigned __int16 uint16_t;
+typedef unsigned __int32 uint32_t;
+typedef unsigned __int64 uint64_t;
+typedef __int8           int8_t;
+typedef __int16          int16_t;
+typedef __int32          int32_t;
+typedef __int64          int64_t;
+
+// Visual Studio does not define ssize_t.
+#ifdef _WIN64
+typedef int64_t ssize_t;
+#define __WORDSIZE 64
+#else
+typedef int32_t ssize_t;
+#define __WORDSIZE 32
+#endif
+
+#endif  // _WIN32
 
 // If __WORDSIZE was undefined by the platform, define it in terms of the
 // compiler built-in __LP64__.
@@ -185,8 +208,14 @@
 const size_t kPageSizeBits = 12;
 const size_t kPageSize = 1UL << kPageSizeBits;
 
+#ifndef _WIN32
 #define GET_CALLER_PC() (uintptr_t)__builtin_return_address(0)
 #define GET_CURRENT_FRAME() (uintptr_t)__builtin_frame_address(0)
+#else
+// TODO(timurrrr): implement.
+#define GET_CALLER_PC() (uintptr_t)0
+#define GET_CURRENT_FRAME() (uintptr_t)0
+#endif
 
 #define GET_BP_PC_SP \
   uintptr_t bp = GET_CURRENT_FRAME();              \

Modified: compiler-rt/trunk/lib/asan/asan_printf.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_printf.cc?rev=149130&r1=149129&r2=149130&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_printf.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_printf.cc Fri Jan 27 09:15:04 2012
@@ -180,11 +180,16 @@
 }
 
 int SScanf(const char *str, const char *format, ...) {
+#ifndef _WIN32
   va_list args;
   va_start(args, format);
   int res = vsscanf(str, format, args);
   va_end(args);
   return res;
+#else
+  UNIMPLEMENTED();
+  return -1;
+#endif
 }
 
 }  // namespace __asan

Modified: compiler-rt/trunk/lib/asan/asan_rtl.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_rtl.cc?rev=149130&r1=149129&r2=149130&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_rtl.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_rtl.cc Fri Jan 27 09:15:04 2012
@@ -245,6 +245,14 @@
 }
 
 // -------------------------- Init ------------------- {{{1
+#if defined(_WIN32)
+// atoll is not defined on Windows.
+int64_t atoll(const char *str) {
+  UNIMPLEMENTED();
+  return -1;
+}
+#endif
+
 static int64_t IntFlagValue(const char *flags, const char *flag,
                             int64_t default_val) {
   if (!flags) return default_val;





More information about the llvm-commits mailing list