Coverage Report

Created: 2025-07-11 06:34

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