Do not check for __CFRuntimeClassTableSize on non-10.6 systems, where this symbol is private.
This change may cause http://code.google.com/p/address-sanitizer/issues/detail?id=87 to re-appear on Lion.


git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@159819 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/asan_mac.cc b/lib/asan/asan_mac.cc
index f58852c..740c011 100644
--- a/lib/asan/asan_mac.cc
+++ b/lib/asan/asan_mac.cc
@@ -16,6 +16,7 @@
 
 #include "asan_interceptors.h"
 #include "asan_internal.h"
+#include "asan_mac.h"
 #include "asan_mapping.h"
 #include "asan_stack.h"
 #include "asan_thread.h"
@@ -51,14 +52,7 @@
 # endif  // __WORDSIZE
 }
 
-enum {
-  MACOS_VERSION_UNKNOWN = 0,
-  MACOS_VERSION_LEOPARD,
-  MACOS_VERSION_SNOW_LEOPARD,
-  MACOS_VERSION_LION
-};
-
-static int GetMacosVersion() {
+int GetMacosVersion() {
   int mib[2] = { CTL_KERN, KERN_OSRELEASE };
   char version[100];
   uptr len = 0, maxlen = sizeof(version) / sizeof(version[0]);
diff --git a/lib/asan/asan_mac.h b/lib/asan/asan_mac.h
new file mode 100644
index 0000000..4917b20
--- /dev/null
+++ b/lib/asan/asan_mac.h
@@ -0,0 +1,30 @@
+//===-- asan_mac.h ----------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file is a part of AddressSanitizer, an address sanity checker.
+//
+// Mac-specific ASan definitions.
+//===----------------------------------------------------------------------===//
+#ifndef ASAN_MAC_H
+#define ASAN_MAC_H
+
+enum {
+  MACOS_VERSION_UNKNOWN = 0,
+  MACOS_VERSION_LEOPARD,
+  MACOS_VERSION_SNOW_LEOPARD,
+  MACOS_VERSION_LION
+};
+
+namespace __asan {
+
+int GetMacosVersion();
+
+}  // namespace __asan
+
+#endif  // ASAN_MAC_H
diff --git a/lib/asan/asan_malloc_mac.cc b/lib/asan/asan_malloc_mac.cc
index 75f7788..5770324 100644
--- a/lib/asan/asan_malloc_mac.cc
+++ b/lib/asan/asan_malloc_mac.cc
@@ -16,12 +16,14 @@
 
 #include <AvailabilityMacros.h>
 #include <CoreFoundation/CFBase.h>
+#include <dlfcn.h>
 #include <malloc/malloc.h>
 #include <setjmp.h>
 
 #include "asan_allocator.h"
 #include "asan_interceptors.h"
 #include "asan_internal.h"
+#include "asan_mac.h"
 #include "asan_stack.h"
 
 // Similar code is used in Google Perftools,
@@ -394,9 +396,16 @@
     cf_asan = CFAllocatorCreate(kCFAllocatorUseContext, &asan_context);
     // If __CFInitialize() hasn't been called yet, cf_asan will be installed
     // as the default allocator after __CFInitialize() finishes (see the
-    // interceptor for __CFInitialize() above). Otherwise (if
-    // __CFRuntimeClassTableSize is initialized) install cf_asan right now.
-    if (__CFRuntimeClassTableSize) CFAllocatorSetDefault(cf_asan);
+    // interceptor for __CFInitialize() above). Otherwise install cf_asan right
+    // now. On Snow Leopard we can check for __CFRuntimeClassTableSize, but on
+    // Lion it is private, so we can't.
+    if (GetMacosVersion() == MACOS_VERSION_SNOW_LEOPARD) {
+      int *cf_rcts = (int*)dlsym(RTLD_SELF, "__CFRuntimeClassTableSize");
+      if (cf_rcts && *cf_rcts) CFAllocatorSetDefault(cf_asan);
+    } else {
+      // FIXME: how can we check __CFInitialize() has been called already?
+      CFAllocatorSetDefault(cf_asan);
+    }
   }
 }
 }  // namespace __asan