1 //===-- FuzzerPlatform.h --------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // Common platform macros.
9 //===----------------------------------------------------------------------===//
10 
11 #ifndef LLVM_FUZZER_PLATFORM_H
12 #define LLVM_FUZZER_PLATFORM_H
13 
14 // Platform detection.
15 #ifdef __linux__
16 #define LIBFUZZER_APPLE 0
17 #define LIBFUZZER_FUCHSIA 0
18 #define LIBFUZZER_LINUX 1
19 #define LIBFUZZER_NETBSD 0
20 #define LIBFUZZER_FREEBSD 0
21 #define LIBFUZZER_WINDOWS 0
22 #define LIBFUZZER_EMSCRIPTEN 0
23 #elif __APPLE__
24 #define LIBFUZZER_APPLE 1
25 #define LIBFUZZER_FUCHSIA 0
26 #define LIBFUZZER_LINUX 0
27 #define LIBFUZZER_NETBSD 0
28 #define LIBFUZZER_FREEBSD 0
29 #define LIBFUZZER_WINDOWS 0
30 #define LIBFUZZER_EMSCRIPTEN 0
31 #elif __NetBSD__
32 #define LIBFUZZER_APPLE 0
33 #define LIBFUZZER_FUCHSIA 0
34 #define LIBFUZZER_LINUX 0
35 #define LIBFUZZER_NETBSD 1
36 #define LIBFUZZER_FREEBSD 0
37 #define LIBFUZZER_WINDOWS 0
38 #define LIBFUZZER_EMSCRIPTEN 0
39 #elif __FreeBSD__
40 #define LIBFUZZER_APPLE 0
41 #define LIBFUZZER_FUCHSIA 0
42 #define LIBFUZZER_LINUX 0
43 #define LIBFUZZER_NETBSD 0
44 #define LIBFUZZER_FREEBSD 1
45 #define LIBFUZZER_WINDOWS 0
46 #define LIBFUZZER_EMSCRIPTEN 0
47 #elif _WIN32
48 #define LIBFUZZER_APPLE 0
49 #define LIBFUZZER_FUCHSIA 0
50 #define LIBFUZZER_LINUX 0
51 #define LIBFUZZER_NETBSD 0
52 #define LIBFUZZER_FREEBSD 0
53 #define LIBFUZZER_WINDOWS 1
54 #define LIBFUZZER_EMSCRIPTEN 0
55 #elif __Fuchsia__
56 #define LIBFUZZER_APPLE 0
57 #define LIBFUZZER_FUCHSIA 1
58 #define LIBFUZZER_LINUX 0
59 #define LIBFUZZER_NETBSD 0
60 #define LIBFUZZER_FREEBSD 0
61 #define LIBFUZZER_WINDOWS 0
62 #define LIBFUZZER_EMSCRIPTEN 0
63 #elif __EMSCRIPTEN__
64 #define LIBFUZZER_APPLE 0
65 #define LIBFUZZER_FUCHSIA 0
66 #define LIBFUZZER_LINUX 0
67 #define LIBFUZZER_NETBSD 0
68 #define LIBFUZZER_FREEBSD 0
69 #define LIBFUZZER_WINDOWS 0
70 #define LIBFUZZER_EMSCRIPTEN 1
71 #else
72 #error "Support for your platform has not been implemented"
73 #endif
74 
75 #if defined(_MSC_VER) && !defined(__clang__)
76 // MSVC compiler is being used.
77 #define LIBFUZZER_MSVC 1
78 #else
79 #define LIBFUZZER_MSVC 0
80 #endif
81 
82 #ifndef __has_attribute
83 #define __has_attribute(x) 0
84 #endif
85 
86 #define LIBFUZZER_POSIX                                                        \
87   (LIBFUZZER_APPLE || LIBFUZZER_LINUX || LIBFUZZER_NETBSD ||                   \
88    LIBFUZZER_FREEBSD || LIBFUZZER_EMSCRIPTEN)
89 
90 #ifdef __x86_64
91 #if __has_attribute(target)
92 #define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt")))
93 #else
94 #define ATTRIBUTE_TARGET_POPCNT
95 #endif
96 #else
97 #define ATTRIBUTE_TARGET_POPCNT
98 #endif
99 
100 #ifdef __clang__ // avoid gcc warning.
101 #if __has_attribute(no_sanitize)
102 #define ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize("memory")))
103 #else
104 #define ATTRIBUTE_NO_SANITIZE_MEMORY
105 #endif
106 #define ALWAYS_INLINE __attribute__((always_inline))
107 #else
108 #define ATTRIBUTE_NO_SANITIZE_MEMORY
109 #define ALWAYS_INLINE
110 #endif // __clang__
111 
112 #if LIBFUZZER_WINDOWS
113 #define ATTRIBUTE_NO_SANITIZE_ADDRESS
114 #else
115 #define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
116 #endif
117 
118 #if LIBFUZZER_WINDOWS
119 #define ATTRIBUTE_ALIGNED(X) __declspec(align(X))
120 #define ATTRIBUTE_INTERFACE __declspec(dllexport)
121 // This is used for __sancov_lowest_stack which is needed for
122 // -fsanitize-coverage=stack-depth. That feature is not yet available on
123 // Windows, so make the symbol static to avoid linking errors.
124 #define ATTRIBUTES_INTERFACE_TLS_INITIAL_EXEC static
125 #define ATTRIBUTE_NOINLINE __declspec(noinline)
126 #else
127 #define ATTRIBUTE_ALIGNED(X) __attribute__((aligned(X)))
128 #define ATTRIBUTE_INTERFACE __attribute__((visibility("default")))
129 #define ATTRIBUTES_INTERFACE_TLS_INITIAL_EXEC                                  \
130   ATTRIBUTE_INTERFACE __attribute__((tls_model("initial-exec"))) thread_local
131 
132 #define ATTRIBUTE_NOINLINE __attribute__((noinline))
133 #endif
134 
135 #if defined(__has_feature)
136 #if __has_feature(address_sanitizer)
137 #define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_ADDRESS
138 #elif __has_feature(memory_sanitizer)
139 #define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_MEMORY
140 #else
141 #define ATTRIBUTE_NO_SANITIZE_ALL
142 #endif
143 #else
144 #define ATTRIBUTE_NO_SANITIZE_ALL
145 #endif
146 
147 #endif // LLVM_FUZZER_PLATFORM_H
148