xref: /aosp_15_r20/external/zstd/lib/common/compiler.h (revision 01826a4963a0d8a59bc3812d29bdf0fb76416722)
1*01826a49SYabin Cui /*
2*01826a49SYabin Cui  * Copyright (c) Meta Platforms, Inc. and affiliates.
3*01826a49SYabin Cui  * All rights reserved.
4*01826a49SYabin Cui  *
5*01826a49SYabin Cui  * This source code is licensed under both the BSD-style license (found in the
6*01826a49SYabin Cui  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*01826a49SYabin Cui  * in the COPYING file in the root directory of this source tree).
8*01826a49SYabin Cui  * You may select, at your option, one of the above-listed licenses.
9*01826a49SYabin Cui  */
10*01826a49SYabin Cui 
11*01826a49SYabin Cui #ifndef ZSTD_COMPILER_H
12*01826a49SYabin Cui #define ZSTD_COMPILER_H
13*01826a49SYabin Cui 
14*01826a49SYabin Cui #include <stddef.h>
15*01826a49SYabin Cui 
16*01826a49SYabin Cui #include "portability_macros.h"
17*01826a49SYabin Cui 
18*01826a49SYabin Cui /*-*******************************************************
19*01826a49SYabin Cui *  Compiler specifics
20*01826a49SYabin Cui *********************************************************/
21*01826a49SYabin Cui /* force inlining */
22*01826a49SYabin Cui 
23*01826a49SYabin Cui #if !defined(ZSTD_NO_INLINE)
24*01826a49SYabin Cui #if (defined(__GNUC__) && !defined(__STRICT_ANSI__)) || defined(__cplusplus) || defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L   /* C99 */
25*01826a49SYabin Cui #  define INLINE_KEYWORD inline
26*01826a49SYabin Cui #else
27*01826a49SYabin Cui #  define INLINE_KEYWORD
28*01826a49SYabin Cui #endif
29*01826a49SYabin Cui 
30*01826a49SYabin Cui #if defined(__GNUC__) || defined(__ICCARM__)
31*01826a49SYabin Cui #  define FORCE_INLINE_ATTR __attribute__((always_inline))
32*01826a49SYabin Cui #elif defined(_MSC_VER)
33*01826a49SYabin Cui #  define FORCE_INLINE_ATTR __forceinline
34*01826a49SYabin Cui #else
35*01826a49SYabin Cui #  define FORCE_INLINE_ATTR
36*01826a49SYabin Cui #endif
37*01826a49SYabin Cui 
38*01826a49SYabin Cui #else
39*01826a49SYabin Cui 
40*01826a49SYabin Cui #define INLINE_KEYWORD
41*01826a49SYabin Cui #define FORCE_INLINE_ATTR
42*01826a49SYabin Cui 
43*01826a49SYabin Cui #endif
44*01826a49SYabin Cui 
45*01826a49SYabin Cui /**
46*01826a49SYabin Cui   On MSVC qsort requires that functions passed into it use the __cdecl calling conversion(CC).
47*01826a49SYabin Cui   This explicitly marks such functions as __cdecl so that the code will still compile
48*01826a49SYabin Cui   if a CC other than __cdecl has been made the default.
49*01826a49SYabin Cui */
50*01826a49SYabin Cui #if  defined(_MSC_VER)
51*01826a49SYabin Cui #  define WIN_CDECL __cdecl
52*01826a49SYabin Cui #else
53*01826a49SYabin Cui #  define WIN_CDECL
54*01826a49SYabin Cui #endif
55*01826a49SYabin Cui 
56*01826a49SYabin Cui /* UNUSED_ATTR tells the compiler it is okay if the function is unused. */
57*01826a49SYabin Cui #if defined(__GNUC__)
58*01826a49SYabin Cui #  define UNUSED_ATTR __attribute__((unused))
59*01826a49SYabin Cui #else
60*01826a49SYabin Cui #  define UNUSED_ATTR
61*01826a49SYabin Cui #endif
62*01826a49SYabin Cui 
63*01826a49SYabin Cui /**
64*01826a49SYabin Cui  * FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant
65*01826a49SYabin Cui  * parameters. They must be inlined for the compiler to eliminate the constant
66*01826a49SYabin Cui  * branches.
67*01826a49SYabin Cui  */
68*01826a49SYabin Cui #define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR UNUSED_ATTR
69*01826a49SYabin Cui /**
70*01826a49SYabin Cui  * HINT_INLINE is used to help the compiler generate better code. It is *not*
71*01826a49SYabin Cui  * used for "templates", so it can be tweaked based on the compilers
72*01826a49SYabin Cui  * performance.
73*01826a49SYabin Cui  *
74*01826a49SYabin Cui  * gcc-4.8 and gcc-4.9 have been shown to benefit from leaving off the
75*01826a49SYabin Cui  * always_inline attribute.
76*01826a49SYabin Cui  *
77*01826a49SYabin Cui  * clang up to 5.0.0 (trunk) benefit tremendously from the always_inline
78*01826a49SYabin Cui  * attribute.
79*01826a49SYabin Cui  */
80*01826a49SYabin Cui #if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
81*01826a49SYabin Cui #  define HINT_INLINE static INLINE_KEYWORD
82*01826a49SYabin Cui #else
83*01826a49SYabin Cui #  define HINT_INLINE FORCE_INLINE_TEMPLATE
84*01826a49SYabin Cui #endif
85*01826a49SYabin Cui 
86*01826a49SYabin Cui /* "soft" inline :
87*01826a49SYabin Cui  * The compiler is free to select if it's a good idea to inline or not.
88*01826a49SYabin Cui  * The main objective is to silence compiler warnings
89*01826a49SYabin Cui  * when a defined function in included but not used.
90*01826a49SYabin Cui  *
91*01826a49SYabin Cui  * Note : this macro is prefixed `MEM_` because it used to be provided by `mem.h` unit.
92*01826a49SYabin Cui  * Updating the prefix is probably preferable, but requires a fairly large codemod,
93*01826a49SYabin Cui  * since this name is used everywhere.
94*01826a49SYabin Cui  */
95*01826a49SYabin Cui #ifndef MEM_STATIC  /* already defined in Linux Kernel mem.h */
96*01826a49SYabin Cui #if defined(__GNUC__)
97*01826a49SYabin Cui #  define MEM_STATIC static __inline UNUSED_ATTR
98*01826a49SYabin Cui #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
99*01826a49SYabin Cui #  define MEM_STATIC static inline
100*01826a49SYabin Cui #elif defined(_MSC_VER)
101*01826a49SYabin Cui #  define MEM_STATIC static __inline
102*01826a49SYabin Cui #else
103*01826a49SYabin Cui #  define MEM_STATIC static  /* this version may generate warnings for unused static functions; disable the relevant warning */
104*01826a49SYabin Cui #endif
105*01826a49SYabin Cui #endif
106*01826a49SYabin Cui 
107*01826a49SYabin Cui /* force no inlining */
108*01826a49SYabin Cui #ifdef _MSC_VER
109*01826a49SYabin Cui #  define FORCE_NOINLINE static __declspec(noinline)
110*01826a49SYabin Cui #else
111*01826a49SYabin Cui #  if defined(__GNUC__) || defined(__ICCARM__)
112*01826a49SYabin Cui #    define FORCE_NOINLINE static __attribute__((__noinline__))
113*01826a49SYabin Cui #  else
114*01826a49SYabin Cui #    define FORCE_NOINLINE static
115*01826a49SYabin Cui #  endif
116*01826a49SYabin Cui #endif
117*01826a49SYabin Cui 
118*01826a49SYabin Cui 
119*01826a49SYabin Cui /* target attribute */
120*01826a49SYabin Cui #if defined(__GNUC__) || defined(__ICCARM__)
121*01826a49SYabin Cui #  define TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
122*01826a49SYabin Cui #else
123*01826a49SYabin Cui #  define TARGET_ATTRIBUTE(target)
124*01826a49SYabin Cui #endif
125*01826a49SYabin Cui 
126*01826a49SYabin Cui /* Target attribute for BMI2 dynamic dispatch.
127*01826a49SYabin Cui  * Enable lzcnt, bmi, and bmi2.
128*01826a49SYabin Cui  * We test for bmi1 & bmi2. lzcnt is included in bmi1.
129*01826a49SYabin Cui  */
130*01826a49SYabin Cui #define BMI2_TARGET_ATTRIBUTE TARGET_ATTRIBUTE("lzcnt,bmi,bmi2")
131*01826a49SYabin Cui 
132*01826a49SYabin Cui /* prefetch
133*01826a49SYabin Cui  * can be disabled, by declaring NO_PREFETCH build macro */
134*01826a49SYabin Cui #if defined(NO_PREFETCH)
135*01826a49SYabin Cui #  define PREFETCH_L1(ptr)  do { (void)(ptr); } while (0)  /* disabled */
136*01826a49SYabin Cui #  define PREFETCH_L2(ptr)  do { (void)(ptr); } while (0)  /* disabled */
137*01826a49SYabin Cui #else
138*01826a49SYabin Cui #  if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) && !defined(_M_ARM64EC)  /* _mm_prefetch() is not defined outside of x86/x64 */
139*01826a49SYabin Cui #    include <mmintrin.h>   /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
140*01826a49SYabin Cui #    define PREFETCH_L1(ptr)  _mm_prefetch((const char*)(ptr), _MM_HINT_T0)
141*01826a49SYabin Cui #    define PREFETCH_L2(ptr)  _mm_prefetch((const char*)(ptr), _MM_HINT_T1)
142*01826a49SYabin Cui #  elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
143*01826a49SYabin Cui #    define PREFETCH_L1(ptr)  __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
144*01826a49SYabin Cui #    define PREFETCH_L2(ptr)  __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)
145*01826a49SYabin Cui #  elif defined(__aarch64__)
146*01826a49SYabin Cui #    define PREFETCH_L1(ptr)  do { __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr))); } while (0)
147*01826a49SYabin Cui #    define PREFETCH_L2(ptr)  do { __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr))); } while (0)
148*01826a49SYabin Cui #  else
149*01826a49SYabin Cui #    define PREFETCH_L1(ptr) do { (void)(ptr); } while (0)  /* disabled */
150*01826a49SYabin Cui #    define PREFETCH_L2(ptr) do { (void)(ptr); } while (0)  /* disabled */
151*01826a49SYabin Cui #  endif
152*01826a49SYabin Cui #endif  /* NO_PREFETCH */
153*01826a49SYabin Cui 
154*01826a49SYabin Cui #define CACHELINE_SIZE 64
155*01826a49SYabin Cui 
156*01826a49SYabin Cui #define PREFETCH_AREA(p, s)                              \
157*01826a49SYabin Cui     do {                                                 \
158*01826a49SYabin Cui         const char* const _ptr = (const char*)(p);       \
159*01826a49SYabin Cui         size_t const _size = (size_t)(s);                \
160*01826a49SYabin Cui         size_t _pos;                                     \
161*01826a49SYabin Cui         for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \
162*01826a49SYabin Cui             PREFETCH_L2(_ptr + _pos);                    \
163*01826a49SYabin Cui         }                                                \
164*01826a49SYabin Cui     } while (0)
165*01826a49SYabin Cui 
166*01826a49SYabin Cui /* vectorization
167*01826a49SYabin Cui  * older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax,
168*01826a49SYabin Cui  * and some compilers, like Intel ICC and MCST LCC, do not support it at all. */
169*01826a49SYabin Cui #if !defined(__INTEL_COMPILER) && !defined(__clang__) && defined(__GNUC__) && !defined(__LCC__)
170*01826a49SYabin Cui #  if (__GNUC__ == 4 && __GNUC_MINOR__ > 3) || (__GNUC__ >= 5)
171*01826a49SYabin Cui #    define DONT_VECTORIZE __attribute__((optimize("no-tree-vectorize")))
172*01826a49SYabin Cui #  else
173*01826a49SYabin Cui #    define DONT_VECTORIZE _Pragma("GCC optimize(\"no-tree-vectorize\")")
174*01826a49SYabin Cui #  endif
175*01826a49SYabin Cui #else
176*01826a49SYabin Cui #  define DONT_VECTORIZE
177*01826a49SYabin Cui #endif
178*01826a49SYabin Cui 
179*01826a49SYabin Cui /* Tell the compiler that a branch is likely or unlikely.
180*01826a49SYabin Cui  * Only use these macros if it causes the compiler to generate better code.
181*01826a49SYabin Cui  * If you can remove a LIKELY/UNLIKELY annotation without speed changes in gcc
182*01826a49SYabin Cui  * and clang, please do.
183*01826a49SYabin Cui  */
184*01826a49SYabin Cui #if defined(__GNUC__)
185*01826a49SYabin Cui #define LIKELY(x) (__builtin_expect((x), 1))
186*01826a49SYabin Cui #define UNLIKELY(x) (__builtin_expect((x), 0))
187*01826a49SYabin Cui #else
188*01826a49SYabin Cui #define LIKELY(x) (x)
189*01826a49SYabin Cui #define UNLIKELY(x) (x)
190*01826a49SYabin Cui #endif
191*01826a49SYabin Cui 
192*01826a49SYabin Cui #if __has_builtin(__builtin_unreachable) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)))
193*01826a49SYabin Cui #  define ZSTD_UNREACHABLE do { assert(0), __builtin_unreachable(); } while (0)
194*01826a49SYabin Cui #else
195*01826a49SYabin Cui #  define ZSTD_UNREACHABLE do { assert(0); } while (0)
196*01826a49SYabin Cui #endif
197*01826a49SYabin Cui 
198*01826a49SYabin Cui /* disable warnings */
199*01826a49SYabin Cui #ifdef _MSC_VER    /* Visual Studio */
200*01826a49SYabin Cui #  include <intrin.h>                    /* For Visual 2005 */
201*01826a49SYabin Cui #  pragma warning(disable : 4100)        /* disable: C4100: unreferenced formal parameter */
202*01826a49SYabin Cui #  pragma warning(disable : 4127)        /* disable: C4127: conditional expression is constant */
203*01826a49SYabin Cui #  pragma warning(disable : 4204)        /* disable: C4204: non-constant aggregate initializer */
204*01826a49SYabin Cui #  pragma warning(disable : 4214)        /* disable: C4214: non-int bitfields */
205*01826a49SYabin Cui #  pragma warning(disable : 4324)        /* disable: C4324: padded structure */
206*01826a49SYabin Cui #endif
207*01826a49SYabin Cui 
208*01826a49SYabin Cui /*Like DYNAMIC_BMI2 but for compile time determination of BMI2 support*/
209*01826a49SYabin Cui #ifndef STATIC_BMI2
210*01826a49SYabin Cui #  if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86))
211*01826a49SYabin Cui #    ifdef __AVX2__  //MSVC does not have a BMI2 specific flag, but every CPU that supports AVX2 also supports BMI2
212*01826a49SYabin Cui #       define STATIC_BMI2 1
213*01826a49SYabin Cui #    endif
214*01826a49SYabin Cui #  elif defined(__BMI2__) && defined(__x86_64__) && defined(__GNUC__)
215*01826a49SYabin Cui #    define STATIC_BMI2 1
216*01826a49SYabin Cui #  endif
217*01826a49SYabin Cui #endif
218*01826a49SYabin Cui 
219*01826a49SYabin Cui #ifndef STATIC_BMI2
220*01826a49SYabin Cui     #define STATIC_BMI2 0
221*01826a49SYabin Cui #endif
222*01826a49SYabin Cui 
223*01826a49SYabin Cui /* compile time determination of SIMD support */
224*01826a49SYabin Cui #if !defined(ZSTD_NO_INTRINSICS)
225*01826a49SYabin Cui #  if defined(__SSE2__) || defined(_M_AMD64) || (defined (_M_IX86) && defined(_M_IX86_FP) && (_M_IX86_FP >= 2))
226*01826a49SYabin Cui #    define ZSTD_ARCH_X86_SSE2
227*01826a49SYabin Cui #  endif
228*01826a49SYabin Cui #  if defined(__ARM_NEON) || defined(_M_ARM64)
229*01826a49SYabin Cui #    define ZSTD_ARCH_ARM_NEON
230*01826a49SYabin Cui #  endif
231*01826a49SYabin Cui #
232*01826a49SYabin Cui #  if defined(ZSTD_ARCH_X86_SSE2)
233*01826a49SYabin Cui #    include <emmintrin.h>
234*01826a49SYabin Cui #  elif defined(ZSTD_ARCH_ARM_NEON)
235*01826a49SYabin Cui #    include <arm_neon.h>
236*01826a49SYabin Cui #  endif
237*01826a49SYabin Cui #endif
238*01826a49SYabin Cui 
239*01826a49SYabin Cui /* C-language Attributes are added in C23. */
240*01826a49SYabin Cui #if defined(__STDC_VERSION__) && (__STDC_VERSION__ > 201710L) && defined(__has_c_attribute)
241*01826a49SYabin Cui # define ZSTD_HAS_C_ATTRIBUTE(x) __has_c_attribute(x)
242*01826a49SYabin Cui #else
243*01826a49SYabin Cui # define ZSTD_HAS_C_ATTRIBUTE(x) 0
244*01826a49SYabin Cui #endif
245*01826a49SYabin Cui 
246*01826a49SYabin Cui /* Only use C++ attributes in C++. Some compilers report support for C++
247*01826a49SYabin Cui  * attributes when compiling with C.
248*01826a49SYabin Cui  */
249*01826a49SYabin Cui #if defined(__cplusplus) && defined(__has_cpp_attribute)
250*01826a49SYabin Cui # define ZSTD_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
251*01826a49SYabin Cui #else
252*01826a49SYabin Cui # define ZSTD_HAS_CPP_ATTRIBUTE(x) 0
253*01826a49SYabin Cui #endif
254*01826a49SYabin Cui 
255*01826a49SYabin Cui /* Define ZSTD_FALLTHROUGH macro for annotating switch case with the 'fallthrough' attribute.
256*01826a49SYabin Cui  * - C23: https://en.cppreference.com/w/c/language/attributes/fallthrough
257*01826a49SYabin Cui  * - CPP17: https://en.cppreference.com/w/cpp/language/attributes/fallthrough
258*01826a49SYabin Cui  * - Else: __attribute__((__fallthrough__))
259*01826a49SYabin Cui  */
260*01826a49SYabin Cui #ifndef ZSTD_FALLTHROUGH
261*01826a49SYabin Cui # if ZSTD_HAS_C_ATTRIBUTE(fallthrough)
262*01826a49SYabin Cui #  define ZSTD_FALLTHROUGH [[fallthrough]]
263*01826a49SYabin Cui # elif ZSTD_HAS_CPP_ATTRIBUTE(fallthrough)
264*01826a49SYabin Cui #  define ZSTD_FALLTHROUGH [[fallthrough]]
265*01826a49SYabin Cui # elif __has_attribute(__fallthrough__)
266*01826a49SYabin Cui /* Leading semicolon is to satisfy gcc-11 with -pedantic. Without the semicolon
267*01826a49SYabin Cui  * gcc complains about: a label can only be part of a statement and a declaration is not a statement.
268*01826a49SYabin Cui  */
269*01826a49SYabin Cui #  define ZSTD_FALLTHROUGH ; __attribute__((__fallthrough__))
270*01826a49SYabin Cui # else
271*01826a49SYabin Cui #  define ZSTD_FALLTHROUGH
272*01826a49SYabin Cui # endif
273*01826a49SYabin Cui #endif
274*01826a49SYabin Cui 
275*01826a49SYabin Cui /*-**************************************************************
276*01826a49SYabin Cui *  Alignment check
277*01826a49SYabin Cui *****************************************************************/
278*01826a49SYabin Cui 
279*01826a49SYabin Cui /* this test was initially positioned in mem.h,
280*01826a49SYabin Cui  * but this file is removed (or replaced) for linux kernel
281*01826a49SYabin Cui  * so it's now hosted in compiler.h,
282*01826a49SYabin Cui  * which remains valid for both user & kernel spaces.
283*01826a49SYabin Cui  */
284*01826a49SYabin Cui 
285*01826a49SYabin Cui #ifndef ZSTD_ALIGNOF
286*01826a49SYabin Cui # if defined(__GNUC__) || defined(_MSC_VER)
287*01826a49SYabin Cui /* covers gcc, clang & MSVC */
288*01826a49SYabin Cui /* note : this section must come first, before C11,
289*01826a49SYabin Cui  * due to a limitation in the kernel source generator */
290*01826a49SYabin Cui #  define ZSTD_ALIGNOF(T) __alignof(T)
291*01826a49SYabin Cui 
292*01826a49SYabin Cui # elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)
293*01826a49SYabin Cui /* C11 support */
294*01826a49SYabin Cui #  include <stdalign.h>
295*01826a49SYabin Cui #  define ZSTD_ALIGNOF(T) alignof(T)
296*01826a49SYabin Cui 
297*01826a49SYabin Cui # else
298*01826a49SYabin Cui /* No known support for alignof() - imperfect backup */
299*01826a49SYabin Cui #  define ZSTD_ALIGNOF(T) (sizeof(void*) < sizeof(T) ? sizeof(void*) : sizeof(T))
300*01826a49SYabin Cui 
301*01826a49SYabin Cui # endif
302*01826a49SYabin Cui #endif /* ZSTD_ALIGNOF */
303*01826a49SYabin Cui 
304*01826a49SYabin Cui /*-**************************************************************
305*01826a49SYabin Cui *  Sanitizer
306*01826a49SYabin Cui *****************************************************************/
307*01826a49SYabin Cui 
308*01826a49SYabin Cui /**
309*01826a49SYabin Cui  * Zstd relies on pointer overflow in its decompressor.
310*01826a49SYabin Cui  * We add this attribute to functions that rely on pointer overflow.
311*01826a49SYabin Cui  */
312*01826a49SYabin Cui #ifndef ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
313*01826a49SYabin Cui #  if __has_attribute(no_sanitize)
314*01826a49SYabin Cui #    if !defined(__clang__) && defined(__GNUC__) && __GNUC__ < 8
315*01826a49SYabin Cui        /* gcc < 8 only has signed-integer-overlow which triggers on pointer overflow */
316*01826a49SYabin Cui #      define ZSTD_ALLOW_POINTER_OVERFLOW_ATTR __attribute__((no_sanitize("signed-integer-overflow")))
317*01826a49SYabin Cui #    else
318*01826a49SYabin Cui        /* older versions of clang [3.7, 5.0) will warn that pointer-overflow is ignored. */
319*01826a49SYabin Cui #      define ZSTD_ALLOW_POINTER_OVERFLOW_ATTR __attribute__((no_sanitize("pointer-overflow")))
320*01826a49SYabin Cui #    endif
321*01826a49SYabin Cui #  else
322*01826a49SYabin Cui #    define ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
323*01826a49SYabin Cui #  endif
324*01826a49SYabin Cui #endif
325*01826a49SYabin Cui 
326*01826a49SYabin Cui /**
327*01826a49SYabin Cui  * Helper function to perform a wrapped pointer difference without trigging
328*01826a49SYabin Cui  * UBSAN.
329*01826a49SYabin Cui  *
330*01826a49SYabin Cui  * @returns lhs - rhs with wrapping
331*01826a49SYabin Cui  */
332*01826a49SYabin Cui MEM_STATIC
333*01826a49SYabin Cui ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
ZSTD_wrappedPtrDiff(unsigned char const * lhs,unsigned char const * rhs)334*01826a49SYabin Cui ptrdiff_t ZSTD_wrappedPtrDiff(unsigned char const* lhs, unsigned char const* rhs)
335*01826a49SYabin Cui {
336*01826a49SYabin Cui     return lhs - rhs;
337*01826a49SYabin Cui }
338*01826a49SYabin Cui 
339*01826a49SYabin Cui /**
340*01826a49SYabin Cui  * Helper function to perform a wrapped pointer add without triggering UBSAN.
341*01826a49SYabin Cui  *
342*01826a49SYabin Cui  * @return ptr + add with wrapping
343*01826a49SYabin Cui  */
344*01826a49SYabin Cui MEM_STATIC
345*01826a49SYabin Cui ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
ZSTD_wrappedPtrAdd(unsigned char const * ptr,ptrdiff_t add)346*01826a49SYabin Cui unsigned char const* ZSTD_wrappedPtrAdd(unsigned char const* ptr, ptrdiff_t add)
347*01826a49SYabin Cui {
348*01826a49SYabin Cui     return ptr + add;
349*01826a49SYabin Cui }
350*01826a49SYabin Cui 
351*01826a49SYabin Cui /**
352*01826a49SYabin Cui  * Helper function to perform a wrapped pointer subtraction without triggering
353*01826a49SYabin Cui  * UBSAN.
354*01826a49SYabin Cui  *
355*01826a49SYabin Cui  * @return ptr - sub with wrapping
356*01826a49SYabin Cui  */
357*01826a49SYabin Cui MEM_STATIC
358*01826a49SYabin Cui ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
ZSTD_wrappedPtrSub(unsigned char const * ptr,ptrdiff_t sub)359*01826a49SYabin Cui unsigned char const* ZSTD_wrappedPtrSub(unsigned char const* ptr, ptrdiff_t sub)
360*01826a49SYabin Cui {
361*01826a49SYabin Cui     return ptr - sub;
362*01826a49SYabin Cui }
363*01826a49SYabin Cui 
364*01826a49SYabin Cui /**
365*01826a49SYabin Cui  * Helper function to add to a pointer that works around C's undefined behavior
366*01826a49SYabin Cui  * of adding 0 to NULL.
367*01826a49SYabin Cui  *
368*01826a49SYabin Cui  * @returns `ptr + add` except it defines `NULL + 0 == NULL`.
369*01826a49SYabin Cui  */
370*01826a49SYabin Cui MEM_STATIC
ZSTD_maybeNullPtrAdd(unsigned char * ptr,ptrdiff_t add)371*01826a49SYabin Cui unsigned char* ZSTD_maybeNullPtrAdd(unsigned char* ptr, ptrdiff_t add)
372*01826a49SYabin Cui {
373*01826a49SYabin Cui     return add > 0 ? ptr + add : ptr;
374*01826a49SYabin Cui }
375*01826a49SYabin Cui 
376*01826a49SYabin Cui /* Issue #3240 reports an ASAN failure on an llvm-mingw build. Out of an
377*01826a49SYabin Cui  * abundance of caution, disable our custom poisoning on mingw. */
378*01826a49SYabin Cui #ifdef __MINGW32__
379*01826a49SYabin Cui #ifndef ZSTD_ASAN_DONT_POISON_WORKSPACE
380*01826a49SYabin Cui #define ZSTD_ASAN_DONT_POISON_WORKSPACE 1
381*01826a49SYabin Cui #endif
382*01826a49SYabin Cui #ifndef ZSTD_MSAN_DONT_POISON_WORKSPACE
383*01826a49SYabin Cui #define ZSTD_MSAN_DONT_POISON_WORKSPACE 1
384*01826a49SYabin Cui #endif
385*01826a49SYabin Cui #endif
386*01826a49SYabin Cui 
387*01826a49SYabin Cui #if ZSTD_MEMORY_SANITIZER && !defined(ZSTD_MSAN_DONT_POISON_WORKSPACE)
388*01826a49SYabin Cui /* Not all platforms that support msan provide sanitizers/msan_interface.h.
389*01826a49SYabin Cui  * We therefore declare the functions we need ourselves, rather than trying to
390*01826a49SYabin Cui  * include the header file... */
391*01826a49SYabin Cui #include <stddef.h>  /* size_t */
392*01826a49SYabin Cui #define ZSTD_DEPS_NEED_STDINT
393*01826a49SYabin Cui #include "zstd_deps.h"  /* intptr_t */
394*01826a49SYabin Cui 
395*01826a49SYabin Cui /* Make memory region fully initialized (without changing its contents). */
396*01826a49SYabin Cui void __msan_unpoison(const volatile void *a, size_t size);
397*01826a49SYabin Cui 
398*01826a49SYabin Cui /* Make memory region fully uninitialized (without changing its contents).
399*01826a49SYabin Cui    This is a legacy interface that does not update origin information. Use
400*01826a49SYabin Cui    __msan_allocated_memory() instead. */
401*01826a49SYabin Cui void __msan_poison(const volatile void *a, size_t size);
402*01826a49SYabin Cui 
403*01826a49SYabin Cui /* Returns the offset of the first (at least partially) poisoned byte in the
404*01826a49SYabin Cui    memory range, or -1 if the whole range is good. */
405*01826a49SYabin Cui intptr_t __msan_test_shadow(const volatile void *x, size_t size);
406*01826a49SYabin Cui 
407*01826a49SYabin Cui /* Print shadow and origin for the memory range to stderr in a human-readable
408*01826a49SYabin Cui    format. */
409*01826a49SYabin Cui void __msan_print_shadow(const volatile void *x, size_t size);
410*01826a49SYabin Cui #endif
411*01826a49SYabin Cui 
412*01826a49SYabin Cui #if ZSTD_ADDRESS_SANITIZER && !defined(ZSTD_ASAN_DONT_POISON_WORKSPACE)
413*01826a49SYabin Cui /* Not all platforms that support asan provide sanitizers/asan_interface.h.
414*01826a49SYabin Cui  * We therefore declare the functions we need ourselves, rather than trying to
415*01826a49SYabin Cui  * include the header file... */
416*01826a49SYabin Cui #include <stddef.h>  /* size_t */
417*01826a49SYabin Cui 
418*01826a49SYabin Cui /**
419*01826a49SYabin Cui  * Marks a memory region (<c>[addr, addr+size)</c>) as unaddressable.
420*01826a49SYabin Cui  *
421*01826a49SYabin Cui  * This memory must be previously allocated by your program. Instrumented
422*01826a49SYabin Cui  * code is forbidden from accessing addresses in this region until it is
423*01826a49SYabin Cui  * unpoisoned. This function is not guaranteed to poison the entire region -
424*01826a49SYabin Cui  * it could poison only a subregion of <c>[addr, addr+size)</c> due to ASan
425*01826a49SYabin Cui  * alignment restrictions.
426*01826a49SYabin Cui  *
427*01826a49SYabin Cui  * \note This function is not thread-safe because no two threads can poison or
428*01826a49SYabin Cui  * unpoison memory in the same memory region simultaneously.
429*01826a49SYabin Cui  *
430*01826a49SYabin Cui  * \param addr Start of memory region.
431*01826a49SYabin Cui  * \param size Size of memory region. */
432*01826a49SYabin Cui void __asan_poison_memory_region(void const volatile *addr, size_t size);
433*01826a49SYabin Cui 
434*01826a49SYabin Cui /**
435*01826a49SYabin Cui  * Marks a memory region (<c>[addr, addr+size)</c>) as addressable.
436*01826a49SYabin Cui  *
437*01826a49SYabin Cui  * This memory must be previously allocated by your program. Accessing
438*01826a49SYabin Cui  * addresses in this region is allowed until this region is poisoned again.
439*01826a49SYabin Cui  * This function could unpoison a super-region of <c>[addr, addr+size)</c> due
440*01826a49SYabin Cui  * to ASan alignment restrictions.
441*01826a49SYabin Cui  *
442*01826a49SYabin Cui  * \note This function is not thread-safe because no two threads can
443*01826a49SYabin Cui  * poison or unpoison memory in the same memory region simultaneously.
444*01826a49SYabin Cui  *
445*01826a49SYabin Cui  * \param addr Start of memory region.
446*01826a49SYabin Cui  * \param size Size of memory region. */
447*01826a49SYabin Cui void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
448*01826a49SYabin Cui #endif
449*01826a49SYabin Cui 
450*01826a49SYabin Cui #endif /* ZSTD_COMPILER_H */
451