Skip to content
This repository was archived by the owner on May 21, 2019. It is now read-only.

Commit a3a0560

Browse files
[ASan] Move GetMacosVersion() to sanitizer_common.
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@200700 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 220d6ba commit a3a0560

File tree

5 files changed

+76
-50
lines changed

5 files changed

+76
-50
lines changed

lib/asan/asan_mac.cc

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "asan_thread.h"
2424
#include "sanitizer_common/sanitizer_atomic.h"
2525
#include "sanitizer_common/sanitizer_libc.h"
26+
#include "sanitizer_common/sanitizer_mac.h"
2627

2728
#include <crt_externs.h> // for _NSGetArgv
2829
#include <dlfcn.h> // for dladdr()
@@ -53,43 +54,6 @@ void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
5354
# endif // SANITIZER_WORDSIZE
5455
}
5556

56-
MacosVersion cached_macos_version = MACOS_VERSION_UNINITIALIZED;
57-
58-
MacosVersion GetMacosVersionInternal() {
59-
int mib[2] = { CTL_KERN, KERN_OSRELEASE };
60-
char version[100];
61-
uptr len = 0, maxlen = sizeof(version) / sizeof(version[0]);
62-
for (uptr i = 0; i < maxlen; i++) version[i] = '\0';
63-
// Get the version length.
64-
CHECK_NE(sysctl(mib, 2, 0, &len, 0, 0), -1);
65-
CHECK_LT(len, maxlen);
66-
CHECK_NE(sysctl(mib, 2, version, &len, 0, 0), -1);
67-
switch (version[0]) {
68-
case '9': return MACOS_VERSION_LEOPARD;
69-
case '1': {
70-
switch (version[1]) {
71-
case '0': return MACOS_VERSION_SNOW_LEOPARD;
72-
case '1': return MACOS_VERSION_LION;
73-
case '2': return MACOS_VERSION_MOUNTAIN_LION;
74-
case '3': return MACOS_VERSION_MAVERICKS;
75-
default: return MACOS_VERSION_UNKNOWN;
76-
}
77-
}
78-
default: return MACOS_VERSION_UNKNOWN;
79-
}
80-
}
81-
82-
MacosVersion GetMacosVersion() {
83-
atomic_uint32_t *cache =
84-
reinterpret_cast<atomic_uint32_t*>(&cached_macos_version);
85-
MacosVersion result =
86-
static_cast<MacosVersion>(atomic_load(cache, memory_order_acquire));
87-
if (result == MACOS_VERSION_UNINITIALIZED) {
88-
result = GetMacosVersionInternal();
89-
atomic_store(cache, result, memory_order_release);
90-
}
91-
return result;
92-
}
9357

9458
bool PlatformHasDifferentMemcpyAndMemmove() {
9559
// On OS X 10.7 memcpy() and memmove() are both resolved

lib/asan/asan_mac.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,12 @@ typedef struct __CFRuntimeBase {
3636
#endif
3737
} CFRuntimeBase;
3838

39-
enum MacosVersion {
40-
MACOS_VERSION_UNINITIALIZED = 0,
41-
MACOS_VERSION_UNKNOWN,
42-
MACOS_VERSION_LEOPARD,
43-
MACOS_VERSION_SNOW_LEOPARD,
44-
MACOS_VERSION_LION,
45-
MACOS_VERSION_MOUNTAIN_LION,
46-
MACOS_VERSION_MAVERICKS
47-
};
4839

4940
// Used by asan_malloc_mac.cc and asan_mac.cc
5041
extern "C" void __CFInitialize();
5142

5243
namespace __asan {
5344

54-
MacosVersion GetMacosVersion();
5545
void MaybeReplaceCFAllocator();
5646

5747
} // namespace __asan

lib/asan/asan_malloc_mac.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "asan_report.h"
2929
#include "asan_stack.h"
3030
#include "asan_stats.h"
31+
#include "sanitizer_common/sanitizer_mac.h"
3132

3233
// Similar code is used in Google Perftools,
3334
// http://code.google.com/p/google-perftools.

lib/sanitizer_common/sanitizer_mac.cc

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
//
88
//===----------------------------------------------------------------------===//
99
//
10-
// This file is shared between AddressSanitizer and ThreadSanitizer
11-
// run-time libraries and implements mac-specific functions from
12-
// sanitizer_libc.h.
10+
// This file is shared between various sanitizers' runtime libraries and
11+
// implements OSX-specific functions.
1312
//===----------------------------------------------------------------------===//
1413

1514
#include "sanitizer_platform.h"
@@ -26,6 +25,7 @@
2625
#include "sanitizer_flags.h"
2726
#include "sanitizer_internal_defs.h"
2827
#include "sanitizer_libc.h"
28+
#include "sanitizer_mac.h"
2929
#include "sanitizer_placement_new.h"
3030
#include "sanitizer_procmaps.h"
3131

@@ -37,6 +37,7 @@
3737
#include <sys/mman.h>
3838
#include <sys/resource.h>
3939
#include <sys/stat.h>
40+
#include <sys/sysctl.h>
4041
#include <sys/types.h>
4142
#include <unistd.h>
4243
#include <libkern/OSAtomic.h>
@@ -249,6 +250,44 @@ bool IsDeadlySignal(int signum) {
249250
return (signum == SIGSEGV || signum == SIGBUS) && common_flags()->handle_segv;
250251
}
251252

253+
MacosVersion cached_macos_version = MACOS_VERSION_UNINITIALIZED;
254+
255+
MacosVersion GetMacosVersionInternal() {
256+
int mib[2] = { CTL_KERN, KERN_OSRELEASE };
257+
char version[100];
258+
uptr len = 0, maxlen = sizeof(version) / sizeof(version[0]);
259+
for (uptr i = 0; i < maxlen; i++) version[i] = '\0';
260+
// Get the version length.
261+
CHECK_NE(sysctl(mib, 2, 0, &len, 0, 0), -1);
262+
CHECK_LT(len, maxlen);
263+
CHECK_NE(sysctl(mib, 2, version, &len, 0, 0), -1);
264+
switch (version[0]) {
265+
case '9': return MACOS_VERSION_LEOPARD;
266+
case '1': {
267+
switch (version[1]) {
268+
case '0': return MACOS_VERSION_SNOW_LEOPARD;
269+
case '1': return MACOS_VERSION_LION;
270+
case '2': return MACOS_VERSION_MOUNTAIN_LION;
271+
case '3': return MACOS_VERSION_MAVERICKS;
272+
default: return MACOS_VERSION_UNKNOWN;
273+
}
274+
}
275+
default: return MACOS_VERSION_UNKNOWN;
276+
}
277+
}
278+
279+
MacosVersion GetMacosVersion() {
280+
atomic_uint32_t *cache =
281+
reinterpret_cast<atomic_uint32_t*>(&cached_macos_version);
282+
MacosVersion result =
283+
static_cast<MacosVersion>(atomic_load(cache, memory_order_acquire));
284+
if (result == MACOS_VERSION_UNINITIALIZED) {
285+
result = GetMacosVersionInternal();
286+
atomic_store(cache, result, memory_order_release);
287+
}
288+
return result;
289+
}
290+
252291
} // namespace __sanitizer
253292

254293
#endif // SANITIZER_MAC

lib/sanitizer_common/sanitizer_mac.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===-- sanitizer_mac.h -----------------------------------------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This file is shared between various sanitizers' runtime libraries and
11+
// provides definitions for OSX-specific functions.
12+
//===----------------------------------------------------------------------===//
13+
#ifndef SANITIZER_MAC_H
14+
#define SANITIZER_MAC_H
15+
16+
namespace __sanitizer {
17+
18+
enum MacosVersion {
19+
MACOS_VERSION_UNINITIALIZED = 0,
20+
MACOS_VERSION_UNKNOWN,
21+
MACOS_VERSION_LEOPARD,
22+
MACOS_VERSION_SNOW_LEOPARD,
23+
MACOS_VERSION_LION,
24+
MACOS_VERSION_MOUNTAIN_LION,
25+
MACOS_VERSION_MAVERICKS
26+
};
27+
28+
MacosVersion GetMacosVersion();
29+
30+
} // namespace __sanitizer
31+
32+
#endif // SANITIZER_MAC_H

0 commit comments

Comments
 (0)