folly: #define UNDEFINED_SANITIZER in ubsan mode
[folly.git] / folly / CPortability.h
1 /*
2  * Copyright 2015 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 CPORTABILITY_H
18 #define CPORTABILITY_H
19
20 /* These definitions are in a separate file so that they
21  * may be included from C- as well as C++-based projects. */
22
23 /* Define a convenience macro to test when address sanitizer is being used
24  * across the different compilers (e.g. clang, gcc) */
25 #if defined(__clang__)
26 # if __has_feature(address_sanitizer)
27 #  define FOLLY_SANITIZE_ADDRESS 1
28 # endif
29 #elif defined (__GNUC__) && \
30       (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)) || (__GNUC__ >= 5)) && \
31       __SANITIZE_ADDRESS__
32 # define FOLLY_SANITIZE_ADDRESS 1
33 #endif
34
35 /* Define attribute wrapper for function attribute used to disable
36  * address sanitizer instrumentation. Unfortunately, this attribute
37  * has issues when inlining is used, so disable that as well. */
38 #ifdef FOLLY_SANITIZE_ADDRESS
39 # if defined(__clang__)
40 #  if __has_attribute(__no_sanitize__)
41 #   define FOLLY_DISABLE_ADDRESS_SANITIZER \
42       __attribute__((__no_sanitize__("address"), __noinline__))
43 #  elif __has_attribute(__no_address_safety_analysis__)
44 #   define FOLLY_DISABLE_ADDRESS_SANITIZER \
45       __attribute__((__no_address_safety_analysis__, __noinline__))
46 #  elif __has_attribute(__no_sanitize_address__)
47 #   define FOLLY_DISABLE_ADDRESS_SANITIZER \
48       __attribute__((__no_sanitize_address__, __noinline__))
49 #  endif
50 # elif defined(__GNUC__)
51 #  define FOLLY_DISABLE_ADDRESS_SANITIZER \
52      __attribute__((__no_address_safety_analysis__, __noinline__))
53 # endif
54 #endif
55 #ifndef FOLLY_DISABLE_ADDRESS_SANITIZER
56 # define FOLLY_DISABLE_ADDRESS_SANITIZER
57 #endif
58
59
60 /**
61  * ASAN/MSAN/TSAN define pre-processor symbols:
62  * ADDRESS_SANITIZER/MEMORY_SANITIZER/THREAD_SANITIZER.
63  *
64  * UBSAN doesn't define anything and makes it hard to
65  * conditionally compile.
66  *
67  * The build system should define UNDEFINED_SANITIZER=1 when UBSAN is
68  * used as folly whitelists some functions.
69  */
70 #if UNDEFINED_SANITIZER
71 # define UBSAN_DISABLE(x) __attribute__((no_sanitize(x)))
72 #else
73 # define UBSAN_DISABLE(x)
74 #endif // UNDEFINED_SANITIZER
75
76 #endif // CPORTABILITY_H