folly: add a FOLLY_SANITIZE_ADDRESS macro to Portability.h
[folly.git] / folly / Portability.h
1 /*
2  * Copyright 2013 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef FOLLY_PORTABILITY_H_
18 #define FOLLY_PORTABILITY_H_
19
20 #ifndef FOLLY_NO_CONFIG
21 #include "folly-config.h"
22 #endif
23
24 #ifdef FOLLY_HAVE_FEATURES_H
25 #include <features.h>
26 #endif
27
28
29 #ifdef FOLLY_HAVE_SCHED_H
30  #include <sched.h>
31  #ifndef FOLLY_HAVE_PTHREAD_YIELD
32   #define pthread_yield sched_yield
33  #endif
34 #endif
35
36
37 // MaxAlign: max_align_t isn't supported by gcc
38 #ifdef __GNUC__
39 struct MaxAlign { char c; } __attribute__((aligned));
40 #else /* !__GNUC__ */
41 # error Cannot define MaxAlign on this platform
42 #endif
43
44
45 // noreturn
46 #if defined(__clang__) || defined(__GNUC__)
47 # define FOLLY_NORETURN __attribute__((noreturn))
48 #else
49 # define FOLLY_NORETURN
50 #endif
51
52
53 // portable version check
54 #ifndef __GNUC_PREREQ
55 # if defined __GNUC__ && defined __GNUC_MINOR__
56 #  define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
57                                    ((maj) << 16) + (min))
58 # else
59 #  define __GNUC_PREREQ(maj, min) 0
60 # endif
61 #endif
62
63
64 /* Define macro wrappers for C++11's "final" and "override" keywords, which
65  * are supported in gcc 4.7 but not gcc 4.6. */
66 #if !defined(FOLLY_FINAL) && !defined(FOLLY_OVERRIDE)
67 # if defined(__clang__) || __GNUC_PREREQ(4, 7)
68 #  define FOLLY_FINAL final
69 #  define FOLLY_OVERRIDE override
70 # else
71 #  define FOLLY_FINAL /**/
72 #  define FOLLY_OVERRIDE /**/
73 # endif
74 #endif
75
76
77 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
78 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
79 # if defined(__GLIBC_PREREQ)
80 #  if __GLIBC_PREREQ(2, 10)
81 #   define FOLLY_HAVE_PREADV 1
82 #   define FOLLY_HAVE_PWRITEV 1
83 #  endif
84 # endif
85 #endif
86
87
88 /* Define a convenience macro to test when address sanitizer is being used
89  * across the different compilers (e.g. clang, gcc) */
90 #if defined(__clang__)
91 # if __has_feature(address_sanitizer)
92 #  define FOLLY_SANITIZE_ADDRESS 1
93 # endif
94 #elif defined (__GNUC__) && \
95       (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)) || (__GNUC__ >= 5)) && \
96       __SANITIZE_ADDRESS__
97 # define FOLLY_SANITIZE_ADDRESS 1
98 #endif
99
100 /* Define attribute wrapper for function attribute used to disable
101  * address sanitizer instrumentation. Unfortunately, this attribute
102  * has issues when inlining is used, so disable that as well. */
103 #ifdef FOLLY_SANITIZE_ADDRESS
104 # if defined(__clang__)
105 #  if __has_attribute(__no_address_safety_analysis__)
106 #   define FOLLY_DISABLE_ADDRESS_SANITIZER \
107       __attribute__((__no_address_safety_analysis__, __noinline__))
108 #  elif __has_attribute(__no_sanitize_address__)
109 #   define FOLLY_DISABLE_ADDRESS_SANITIZER \
110       __attribute__((__no_sanitize_address__, __noinline__))
111 #  endif
112 # elif defined(__GNUC__)
113 #  define FOLLY_DISABLE_ADDRESS_SANITIZER \
114      __attribute__((__no_address_safety_analysis__, __noinline__))
115 # endif
116 #endif
117 #ifndef FOLLY_DISABLE_ADDRESS_SANITIZER
118 # define FOLLY_DISABLE_ADDRESS_SANITIZER
119 #endif
120
121 #endif // FOLLY_PORTABILITY_H_