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_OPENBSD 0
22#define LIBFUZZER_WINDOWS 0
23#define LIBFUZZER_EMSCRIPTEN 0
24#elif __APPLE__
25#define LIBFUZZER_APPLE 1
26#define LIBFUZZER_FUCHSIA 0
27#define LIBFUZZER_LINUX 0
28#define LIBFUZZER_NETBSD 0
29#define LIBFUZZER_FREEBSD 0
30#define LIBFUZZER_OPENBSD 0
31#define LIBFUZZER_WINDOWS 0
32#define LIBFUZZER_EMSCRIPTEN 0
33#elif __NetBSD__
34#define LIBFUZZER_APPLE 0
35#define LIBFUZZER_FUCHSIA 0
36#define LIBFUZZER_LINUX 0
37#define LIBFUZZER_NETBSD 1
38#define LIBFUZZER_FREEBSD 0
39#define LIBFUZZER_OPENBSD 0
40#define LIBFUZZER_WINDOWS 0
41#define LIBFUZZER_EMSCRIPTEN 0
42#elif __FreeBSD__
43#define LIBFUZZER_APPLE 0
44#define LIBFUZZER_FUCHSIA 0
45#define LIBFUZZER_LINUX 0
46#define LIBFUZZER_NETBSD 0
47#define LIBFUZZER_FREEBSD 1
48#define LIBFUZZER_OPENBSD 0
49#define LIBFUZZER_WINDOWS 0
50#define LIBFUZZER_EMSCRIPTEN 0
51#elif __OpenBSD__
52#define LIBFUZZER_APPLE 0
53#define LIBFUZZER_FUCHSIA 0
54#define LIBFUZZER_LINUX 0
55#define LIBFUZZER_NETBSD 0
56#define LIBFUZZER_FREEBSD 0
57#define LIBFUZZER_OPENBSD 1
58#define LIBFUZZER_WINDOWS 0
59#define LIBFUZZER_EMSCRIPTEN 0
60#elif _WIN32
61#define LIBFUZZER_APPLE 0
62#define LIBFUZZER_FUCHSIA 0
63#define LIBFUZZER_LINUX 0
64#define LIBFUZZER_NETBSD 0
65#define LIBFUZZER_FREEBSD 0
66#define LIBFUZZER_OPENBSD 0
67#define LIBFUZZER_WINDOWS 1
68#define LIBFUZZER_EMSCRIPTEN 0
69#elif __Fuchsia__
70#define LIBFUZZER_APPLE 0
71#define LIBFUZZER_FUCHSIA 1
72#define LIBFUZZER_LINUX 0
73#define LIBFUZZER_NETBSD 0
74#define LIBFUZZER_FREEBSD 0
75#define LIBFUZZER_OPENBSD 0
76#define LIBFUZZER_WINDOWS 0
77#define LIBFUZZER_EMSCRIPTEN 0
78#elif __EMSCRIPTEN__
79#define LIBFUZZER_APPLE 0
80#define LIBFUZZER_FUCHSIA 0
81#define LIBFUZZER_LINUX 0
82#define LIBFUZZER_NETBSD 0
83#define LIBFUZZER_FREEBSD 0
84#define LIBFUZZER_OPENBSD 0
85#define LIBFUZZER_WINDOWS 0
86#define LIBFUZZER_EMSCRIPTEN 1
87#else
88#error "Support for your platform has not been implemented"
89#endif
90
91#if defined(_MSC_VER) && !defined(__clang__)
92// MSVC compiler is being used.
93#define LIBFUZZER_MSVC 1
94#else
95#define LIBFUZZER_MSVC 0
96#endif
97
98#ifndef __has_attribute
99#define __has_attribute(x) 0
100#endif
101
102#define LIBFUZZER_POSIX                                                        \
103  (LIBFUZZER_APPLE || LIBFUZZER_LINUX || LIBFUZZER_NETBSD ||                   \
104   LIBFUZZER_FREEBSD || LIBFUZZER_OPENBSD || LIBFUZZER_EMSCRIPTEN)
105
106#ifdef __x86_64
107#if __has_attribute(target)
108#define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt")))
109#else
110#define ATTRIBUTE_TARGET_POPCNT
111#endif
112#else
113#define ATTRIBUTE_TARGET_POPCNT
114#endif
115
116#ifdef __clang__ // avoid gcc warning.
117#if __has_attribute(no_sanitize)
118#define ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize("memory")))
119#else
120#define ATTRIBUTE_NO_SANITIZE_MEMORY
121#endif
122#define ALWAYS_INLINE __attribute__((always_inline))
123#else
124#define ATTRIBUTE_NO_SANITIZE_MEMORY
125#define ALWAYS_INLINE
126#endif // __clang__
127
128#if LIBFUZZER_WINDOWS
129#define ATTRIBUTE_NO_SANITIZE_ADDRESS
130#else
131#define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
132#endif
133
134#if LIBFUZZER_WINDOWS
135#define ATTRIBUTE_ALIGNED(X) __declspec(align(X))
136#define ATTRIBUTE_INTERFACE __declspec(dllexport)
137// This is used for __sancov_lowest_stack which is needed for
138// -fsanitize-coverage=stack-depth. That feature is not yet available on
139// Windows, so make the symbol static to avoid linking errors.
140#define ATTRIBUTES_INTERFACE_TLS_INITIAL_EXEC static
141#define ATTRIBUTE_NOINLINE __declspec(noinline)
142#else
143#define ATTRIBUTE_ALIGNED(X) __attribute__((aligned(X)))
144#define ATTRIBUTE_INTERFACE __attribute__((visibility("default")))
145#define ATTRIBUTES_INTERFACE_TLS_INITIAL_EXEC                                  \
146  ATTRIBUTE_INTERFACE __attribute__((tls_model("initial-exec"))) thread_local
147
148#define ATTRIBUTE_NOINLINE __attribute__((noinline))
149#endif
150
151#if defined(__has_feature)
152#if __has_feature(address_sanitizer)
153#define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_ADDRESS
154#elif __has_feature(memory_sanitizer)
155#define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_MEMORY
156#else
157#define ATTRIBUTE_NO_SANITIZE_ALL
158#endif
159#else
160#define ATTRIBUTE_NO_SANITIZE_ALL
161#endif
162
163#endif // LLVM_FUZZER_PLATFORM_H
164