Skip to content

Commit fcb551e

Browse files
committed
feat: upgrade zstd version to v1.5.6
1 parent d3f94bd commit fcb551e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+4663
-2363
lines changed

allocations.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#define ZSTD_DEPS_NEED_MALLOC
1616
#include "zstd_deps.h" /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_memset */
1717

18-
#include "mem.h" /* MEM_STATIC */
18+
#include "compiler.h" /* MEM_STATIC */
1919
#define ZSTD_STATIC_LINKING_ONLY
2020
#include "zstd.h" /* ZSTD_customMem */
2121

bitstream.h

+49-29
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,20 @@ MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC);
9191
/*-********************************************
9292
* bitStream decoding API (read backward)
9393
**********************************************/
94+
typedef size_t BitContainerType;
9495
typedef struct {
95-
size_t bitContainer;
96+
BitContainerType bitContainer;
9697
unsigned bitsConsumed;
9798
const char* ptr;
9899
const char* start;
99100
const char* limitPtr;
100101
} BIT_DStream_t;
101102

102-
typedef enum { BIT_DStream_unfinished = 0,
103-
BIT_DStream_endOfBuffer = 1,
104-
BIT_DStream_completed = 2,
105-
BIT_DStream_overflow = 3 } BIT_DStream_status; /* result of BIT_reloadDStream() */
106-
/* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */
103+
typedef enum { BIT_DStream_unfinished = 0, /* fully refilled */
104+
BIT_DStream_endOfBuffer = 1, /* still some bits left in bitstream */
105+
BIT_DStream_completed = 2, /* bitstream entirely consumed, bit-exact */
106+
BIT_DStream_overflow = 3 /* user requested more bits than present in bitstream */
107+
} BIT_DStream_status; /* result of BIT_reloadDStream() */
107108

108109
MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize);
109110
MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits);
@@ -113,7 +114,7 @@ MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD);
113114

114115
/* Start by invoking BIT_initDStream().
115116
* A chunk of the bitStream is then stored into a local register.
116-
* Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t).
117+
* Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (BitContainerType).
117118
* You can then retrieve bitFields stored into the local register, **in reverse order**.
118119
* Local register is explicitly reloaded from memory by the BIT_reloadDStream() method.
119120
* A reload guarantee a minimum of ((8*sizeof(bitD->bitContainer))-7) bits when its result is BIT_DStream_unfinished.
@@ -163,7 +164,7 @@ MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC,
163164
return 0;
164165
}
165166

166-
MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits)
167+
FORCE_INLINE_TEMPLATE size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits)
167168
{
168169
#if defined(STATIC_BMI2) && STATIC_BMI2 == 1 && !defined(ZSTD_NO_INTRINSICS)
169170
return _bzhi_u64(bitContainer, nbBits);
@@ -268,22 +269,22 @@ MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, si
268269
bitD->bitContainer = *(const BYTE*)(bitD->start);
269270
switch(srcSize)
270271
{
271-
case 7: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16);
272+
case 7: bitD->bitContainer += (BitContainerType)(((const BYTE*)(srcBuffer))[6]) << (sizeof(bitD->bitContainer)*8 - 16);
272273
ZSTD_FALLTHROUGH;
273274

274-
case 6: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24);
275+
case 6: bitD->bitContainer += (BitContainerType)(((const BYTE*)(srcBuffer))[5]) << (sizeof(bitD->bitContainer)*8 - 24);
275276
ZSTD_FALLTHROUGH;
276277

277-
case 5: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32);
278+
case 5: bitD->bitContainer += (BitContainerType)(((const BYTE*)(srcBuffer))[4]) << (sizeof(bitD->bitContainer)*8 - 32);
278279
ZSTD_FALLTHROUGH;
279280

280-
case 4: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[3]) << 24;
281+
case 4: bitD->bitContainer += (BitContainerType)(((const BYTE*)(srcBuffer))[3]) << 24;
281282
ZSTD_FALLTHROUGH;
282283

283-
case 3: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[2]) << 16;
284+
case 3: bitD->bitContainer += (BitContainerType)(((const BYTE*)(srcBuffer))[2]) << 16;
284285
ZSTD_FALLTHROUGH;
285286

286-
case 2: bitD->bitContainer += (size_t)(((const BYTE*)(srcBuffer))[1]) << 8;
287+
case 2: bitD->bitContainer += (BitContainerType)(((const BYTE*)(srcBuffer))[1]) << 8;
287288
ZSTD_FALLTHROUGH;
288289

289290
default: break;
@@ -298,12 +299,12 @@ MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, si
298299
return srcSize;
299300
}
300301

301-
MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getUpperBits(size_t bitContainer, U32 const start)
302+
FORCE_INLINE_TEMPLATE size_t BIT_getUpperBits(BitContainerType bitContainer, U32 const start)
302303
{
303304
return bitContainer >> start;
304305
}
305306

306-
MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getMiddleBits(size_t bitContainer, U32 const start, U32 const nbBits)
307+
FORCE_INLINE_TEMPLATE size_t BIT_getMiddleBits(BitContainerType bitContainer, U32 const start, U32 const nbBits)
307308
{
308309
U32 const regMask = sizeof(bitContainer)*8 - 1;
309310
/* if start > regMask, bitstream is corrupted, and result is undefined */
@@ -326,7 +327,7 @@ MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getMiddleBits(size_t bitContainer, U32 c
326327
* On 32-bits, maxNbBits==24.
327328
* On 64-bits, maxNbBits==56.
328329
* @return : value extracted */
329-
MEM_STATIC FORCE_INLINE_ATTR size_t BIT_lookBits(const BIT_DStream_t* bitD, U32 nbBits)
330+
FORCE_INLINE_TEMPLATE size_t BIT_lookBits(const BIT_DStream_t* bitD, U32 nbBits)
330331
{
331332
/* arbitrate between double-shift and shift+mask */
332333
#if 1
@@ -349,7 +350,7 @@ MEM_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t* bitD, U32 nbBits)
349350
return (bitD->bitContainer << (bitD->bitsConsumed & regMask)) >> (((regMask+1)-nbBits) & regMask);
350351
}
351352

352-
MEM_STATIC FORCE_INLINE_ATTR void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
353+
FORCE_INLINE_TEMPLATE void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
353354
{
354355
bitD->bitsConsumed += nbBits;
355356
}
@@ -358,7 +359,7 @@ MEM_STATIC FORCE_INLINE_ATTR void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits)
358359
* Read (consume) next n bits from local register and update.
359360
* Pay attention to not read more than nbBits contained into local register.
360361
* @return : extracted value. */
361-
MEM_STATIC FORCE_INLINE_ATTR size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits)
362+
FORCE_INLINE_TEMPLATE size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits)
362363
{
363364
size_t const value = BIT_lookBits(bitD, nbBits);
364365
BIT_skipBits(bitD, nbBits);
@@ -375,6 +376,21 @@ MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits)
375376
return value;
376377
}
377378

379+
/*! BIT_reloadDStream_internal() :
380+
* Simple variant of BIT_reloadDStream(), with two conditions:
381+
* 1. bitstream is valid : bitsConsumed <= sizeof(bitD->bitContainer)*8
382+
* 2. look window is valid after shifted down : bitD->ptr >= bitD->start
383+
*/
384+
MEM_STATIC BIT_DStream_status BIT_reloadDStream_internal(BIT_DStream_t* bitD)
385+
{
386+
assert(bitD->bitsConsumed <= sizeof(bitD->bitContainer)*8);
387+
bitD->ptr -= bitD->bitsConsumed >> 3;
388+
assert(bitD->ptr >= bitD->start);
389+
bitD->bitsConsumed &= 7;
390+
bitD->bitContainer = MEM_readLEST(bitD->ptr);
391+
return BIT_DStream_unfinished;
392+
}
393+
378394
/*! BIT_reloadDStreamFast() :
379395
* Similar to BIT_reloadDStream(), but with two differences:
380396
* 1. bitsConsumed <= sizeof(bitD->bitContainer)*8 must hold!
@@ -385,31 +401,35 @@ MEM_STATIC BIT_DStream_status BIT_reloadDStreamFast(BIT_DStream_t* bitD)
385401
{
386402
if (UNLIKELY(bitD->ptr < bitD->limitPtr))
387403
return BIT_DStream_overflow;
388-
assert(bitD->bitsConsumed <= sizeof(bitD->bitContainer)*8);
389-
bitD->ptr -= bitD->bitsConsumed >> 3;
390-
bitD->bitsConsumed &= 7;
391-
bitD->bitContainer = MEM_readLEST(bitD->ptr);
392-
return BIT_DStream_unfinished;
404+
return BIT_reloadDStream_internal(bitD);
393405
}
394406

395407
/*! BIT_reloadDStream() :
396408
* Refill `bitD` from buffer previously set in BIT_initDStream() .
397-
* This function is safe, it guarantees it will not read beyond src buffer.
409+
* This function is safe, it guarantees it will not never beyond src buffer.
398410
* @return : status of `BIT_DStream_t` internal register.
399411
* when status == BIT_DStream_unfinished, internal register is filled with at least 25 or 57 bits */
400-
MEM_STATIC FORCE_INLINE_ATTR BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
412+
FORCE_INLINE_TEMPLATE BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD)
401413
{
402-
if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* overflow detected, like end of stream */
414+
/* note : once in overflow mode, a bitstream remains in this mode until it's reset */
415+
if (UNLIKELY(bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8))) {
416+
static const BitContainerType zeroFilled = 0;
417+
bitD->ptr = (const char*)&zeroFilled; /* aliasing is allowed for char */
418+
/* overflow detected, erroneous scenario or end of stream: no update */
403419
return BIT_DStream_overflow;
420+
}
421+
422+
assert(bitD->ptr >= bitD->start);
404423

405424
if (bitD->ptr >= bitD->limitPtr) {
406-
return BIT_reloadDStreamFast(bitD);
425+
return BIT_reloadDStream_internal(bitD);
407426
}
408427
if (bitD->ptr == bitD->start) {
428+
/* reached end of bitStream => no update */
409429
if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer;
410430
return BIT_DStream_completed;
411431
}
412-
/* start < ptr < limitPtr */
432+
/* start < ptr < limitPtr => cautious update */
413433
{ U32 nbBytes = bitD->bitsConsumed >> 3;
414434
BIT_DStream_status result = BIT_DStream_unfinished;
415435
if (bitD->ptr - nbBytes < bitD->start) {

compiler.h

+114-22
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#ifndef ZSTD_COMPILER_H
1313
#define ZSTD_COMPILER_H
1414

15+
#include <stddef.h>
16+
1517
#include "portability_macros.h"
1618

1719
/*-*******************************************************
@@ -52,12 +54,19 @@
5254
# define WIN_CDECL
5355
#endif
5456

57+
/* UNUSED_ATTR tells the compiler it is okay if the function is unused. */
58+
#if defined(__GNUC__)
59+
# define UNUSED_ATTR __attribute__((unused))
60+
#else
61+
# define UNUSED_ATTR
62+
#endif
63+
5564
/**
5665
* FORCE_INLINE_TEMPLATE is used to define C "templates", which take constant
5766
* parameters. They must be inlined for the compiler to eliminate the constant
5867
* branches.
5968
*/
60-
#define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR
69+
#define FORCE_INLINE_TEMPLATE static INLINE_KEYWORD FORCE_INLINE_ATTR UNUSED_ATTR
6170
/**
6271
* HINT_INLINE is used to help the compiler generate better code. It is *not*
6372
* used for "templates", so it can be tweaked based on the compilers
@@ -72,14 +81,28 @@
7281
#if !defined(__clang__) && defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8 && __GNUC__ < 5
7382
# define HINT_INLINE static INLINE_KEYWORD
7483
#else
75-
# define HINT_INLINE static INLINE_KEYWORD FORCE_INLINE_ATTR
84+
# define HINT_INLINE FORCE_INLINE_TEMPLATE
7685
#endif
7786

78-
/* UNUSED_ATTR tells the compiler it is okay if the function is unused. */
87+
/* "soft" inline :
88+
* The compiler is free to select if it's a good idea to inline or not.
89+
* The main objective is to silence compiler warnings
90+
* when a defined function in included but not used.
91+
*
92+
* Note : this macro is prefixed `MEM_` because it used to be provided by `mem.h` unit.
93+
* Updating the prefix is probably preferable, but requires a fairly large codemod,
94+
* since this name is used everywhere.
95+
*/
96+
#ifndef MEM_STATIC /* already defined in Linux Kernel mem.h */
7997
#if defined(__GNUC__)
80-
# define UNUSED_ATTR __attribute__((unused))
98+
# define MEM_STATIC static __inline UNUSED_ATTR
99+
#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
100+
# define MEM_STATIC static inline
101+
#elif defined(_MSC_VER)
102+
# define MEM_STATIC static __inline
81103
#else
82-
# define UNUSED_ATTR
104+
# define MEM_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
105+
#endif
83106
#endif
84107

85108
/* force no inlining */
@@ -110,35 +133,36 @@
110133
/* prefetch
111134
* can be disabled, by declaring NO_PREFETCH build macro */
112135
#if defined(NO_PREFETCH)
113-
# define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
114-
# define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
136+
# define PREFETCH_L1(ptr) do { (void)(ptr); } while (0) /* disabled */
137+
# define PREFETCH_L2(ptr) do { (void)(ptr); } while (0) /* disabled */
115138
#else
116-
# if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) /* _mm_prefetch() is not defined outside of x86/x64 */
139+
# if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_I86)) && !defined(_M_ARM64EC) /* _mm_prefetch() is not defined outside of x86/x64 */
117140
# include <mmintrin.h> /* https://msdn.microsoft.com/fr-fr/library/84szxsww(v=vs.90).aspx */
118141
# define PREFETCH_L1(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T0)
119142
# define PREFETCH_L2(ptr) _mm_prefetch((const char*)(ptr), _MM_HINT_T1)
120143
# elif defined(__GNUC__) && ( (__GNUC__ >= 4) || ( (__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) ) )
121144
# define PREFETCH_L1(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 3 /* locality */)
122145
# define PREFETCH_L2(ptr) __builtin_prefetch((ptr), 0 /* rw==read */, 2 /* locality */)
123146
# elif defined(__aarch64__)
124-
# define PREFETCH_L1(ptr) __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr)))
125-
# define PREFETCH_L2(ptr) __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr)))
147+
# define PREFETCH_L1(ptr) do { __asm__ __volatile__("prfm pldl1keep, %0" ::"Q"(*(ptr))); } while (0)
148+
# define PREFETCH_L2(ptr) do { __asm__ __volatile__("prfm pldl2keep, %0" ::"Q"(*(ptr))); } while (0)
126149
# else
127-
# define PREFETCH_L1(ptr) (void)(ptr) /* disabled */
128-
# define PREFETCH_L2(ptr) (void)(ptr) /* disabled */
150+
# define PREFETCH_L1(ptr) do { (void)(ptr); } while (0) /* disabled */
151+
# define PREFETCH_L2(ptr) do { (void)(ptr); } while (0) /* disabled */
129152
# endif
130153
#endif /* NO_PREFETCH */
131154

132155
#define CACHELINE_SIZE 64
133156

134-
#define PREFETCH_AREA(p, s) { \
135-
const char* const _ptr = (const char*)(p); \
136-
size_t const _size = (size_t)(s); \
137-
size_t _pos; \
138-
for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \
139-
PREFETCH_L2(_ptr + _pos); \
140-
} \
141-
}
157+
#define PREFETCH_AREA(p, s) \
158+
do { \
159+
const char* const _ptr = (const char*)(p); \
160+
size_t const _size = (size_t)(s); \
161+
size_t _pos; \
162+
for (_pos=0; _pos<_size; _pos+=CACHELINE_SIZE) { \
163+
PREFETCH_L2(_ptr + _pos); \
164+
} \
165+
} while (0)
142166

143167
/* vectorization
144168
* older GCC (pre gcc-4.3 picked as the cutoff) uses a different syntax,
@@ -167,9 +191,9 @@
167191
#endif
168192

169193
#if __has_builtin(__builtin_unreachable) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)))
170-
# define ZSTD_UNREACHABLE { assert(0), __builtin_unreachable(); }
194+
# define ZSTD_UNREACHABLE do { assert(0), __builtin_unreachable(); } while (0)
171195
#else
172-
# define ZSTD_UNREACHABLE { assert(0); }
196+
# define ZSTD_UNREACHABLE do { assert(0); } while (0)
173197
#endif
174198

175199
/* disable warnings */
@@ -282,6 +306,74 @@
282306
* Sanitizer
283307
*****************************************************************/
284308

309+
/**
310+
* Zstd relies on pointer overflow in its decompressor.
311+
* We add this attribute to functions that rely on pointer overflow.
312+
*/
313+
#ifndef ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
314+
# if __has_attribute(no_sanitize)
315+
# if !defined(__clang__) && defined(__GNUC__) && __GNUC__ < 8
316+
/* gcc < 8 only has signed-integer-overlow which triggers on pointer overflow */
317+
# define ZSTD_ALLOW_POINTER_OVERFLOW_ATTR __attribute__((no_sanitize("signed-integer-overflow")))
318+
# else
319+
/* older versions of clang [3.7, 5.0) will warn that pointer-overflow is ignored. */
320+
# define ZSTD_ALLOW_POINTER_OVERFLOW_ATTR __attribute__((no_sanitize("pointer-overflow")))
321+
# endif
322+
# else
323+
# define ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
324+
# endif
325+
#endif
326+
327+
/**
328+
* Helper function to perform a wrapped pointer difference without trigging
329+
* UBSAN.
330+
*
331+
* @returns lhs - rhs with wrapping
332+
*/
333+
MEM_STATIC
334+
ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
335+
ptrdiff_t ZSTD_wrappedPtrDiff(unsigned char const* lhs, unsigned char const* rhs)
336+
{
337+
return lhs - rhs;
338+
}
339+
340+
/**
341+
* Helper function to perform a wrapped pointer add without triggering UBSAN.
342+
*
343+
* @return ptr + add with wrapping
344+
*/
345+
MEM_STATIC
346+
ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
347+
unsigned char const* ZSTD_wrappedPtrAdd(unsigned char const* ptr, ptrdiff_t add)
348+
{
349+
return ptr + add;
350+
}
351+
352+
/**
353+
* Helper function to perform a wrapped pointer subtraction without triggering
354+
* UBSAN.
355+
*
356+
* @return ptr - sub with wrapping
357+
*/
358+
MEM_STATIC
359+
ZSTD_ALLOW_POINTER_OVERFLOW_ATTR
360+
unsigned char const* ZSTD_wrappedPtrSub(unsigned char const* ptr, ptrdiff_t sub)
361+
{
362+
return ptr - sub;
363+
}
364+
365+
/**
366+
* Helper function to add to a pointer that works around C's undefined behavior
367+
* of adding 0 to NULL.
368+
*
369+
* @returns `ptr + add` except it defines `NULL + 0 == NULL`.
370+
*/
371+
MEM_STATIC
372+
unsigned char* ZSTD_maybeNullPtrAdd(unsigned char* ptr, ptrdiff_t add)
373+
{
374+
return add > 0 ? ptr + add : ptr;
375+
}
376+
285377
/* Issue #3240 reports an ASAN failure on an llvm-mingw build. Out of an
286378
* abundance of caution, disable our custom poisoning on mingw. */
287379
#ifdef __MINGW32__

0 commit comments

Comments
 (0)