From d2dd35ae658e03d09487a70b157b36d0a4b2a3a1 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Thu, 4 Mar 2021 21:13:52 -0800 Subject: [PATCH 1/6] [contrib][linux-kernel] Fix unaligned.h Fix the `unaligned.h` shim in the tests that was causing corruption in the tests. Note that this is a problem with the test shim, not the kernel code. --- .../linux-kernel/test/include/asm/unaligned.h | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/contrib/linux-kernel/test/include/asm/unaligned.h b/contrib/linux-kernel/test/include/asm/unaligned.h index 6576b37ee93..02c2d74f301 100644 --- a/contrib/linux-kernel/test/include/asm/unaligned.h +++ b/contrib/linux-kernel/test/include/asm/unaligned.h @@ -4,13 +4,23 @@ #include #include -#define _LITTLE_ENDIAN 1 +#ifndef __LITTLE_ENDIAN +# if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || defined(__LITTLE_ENDIAN__) +# define __LITTLE_ENDIAN 1 +# endif +#endif + +#ifdef __LITTLE_ENDIAN +# define _IS_LITTLE_ENDIAN 1 +#else +# define _IS_LITTLE_ENDIAN 0 +#endif static unsigned _isLittleEndian(void) { const union { uint32_t u; uint8_t c[4]; } one = { 1 }; - assert(_LITTLE_ENDIAN == one.c[0]); - return _LITTLE_ENDIAN; + assert(_IS_LITTLE_ENDIAN == one.c[0]); + return _IS_LITTLE_ENDIAN; } static uint16_t _swap16(uint16_t in) @@ -165,7 +175,7 @@ extern void __bad_unaligned_access_size(void); (void)0; \ }) -#if _LITTLE_ENDIAN +#if _IS_LITTLE_ENDIAN # define get_unaligned __get_unaligned_le # define put_unaligned __put_unaligned_le #else From 49a9e070f5dc7fdb0e39fdfdbbf998451c75a68e Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Fri, 12 Mar 2021 15:31:38 -0800 Subject: [PATCH 2/6] [contrib][linux-kernel] Update test include stubs Update the test include stubs so they are able to run the current zstd version in the kernel, so I can compare stack usage. --- contrib/linux-kernel/test/Makefile | 7 ++++--- contrib/linux-kernel/test/include/linux/compiler.h | 4 ++++ contrib/linux-kernel/test/include/linux/kernel.h | 4 ++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/contrib/linux-kernel/test/Makefile b/contrib/linux-kernel/test/Makefile index 80bce74db43..7946713861f 100644 --- a/contrib/linux-kernel/test/Makefile +++ b/contrib/linux-kernel/test/Makefile @@ -7,9 +7,9 @@ CPPFLAGS += -I$(LINUX)/include -I$(LINUX_ZSTDLIB) -Iinclude -DNDEBUG CPPFLAGS += -DZSTD_ASAN_DONT_POISON_WORKSPACE LINUX_ZSTD_MODULE := $(wildcard $(LINUX_ZSTDLIB)/*.c) -LINUX_ZSTD_COMMON := $(wildcard $(LINUX_ZSTDLIB)/common/*.c) -LINUX_ZSTD_COMPRESS := $(wildcard $(LINUX_ZSTDLIB)/compress/*.c) -LINUX_ZSTD_DECOMPRESS := $(wildcard $(LINUX_ZSTDLIB)/decompress/*.c) +LINUX_ZSTD_COMMON := $(wildcard $(LINUX_ZSTDLIB)/common/*.c) +LINUX_ZSTD_COMPRESS := $(wildcard $(LINUX_ZSTDLIB)/compress/*.c) +LINUX_ZSTD_DECOMPRESS := $(wildcard $(LINUX_ZSTDLIB)/decompress/*.c) LINUX_ZSTD_FILES := $(LINUX_ZSTD_MODULE) $(LINUX_ZSTD_COMMON) $(LINUX_ZSTD_COMPRESS) $(LINUX_ZSTD_DECOMPRESS) LINUX_ZSTD_OBJECTS := $(LINUX_ZSTD_FILES:.c=.o) @@ -29,6 +29,7 @@ run-test: test static_test .PHONY: clean: + $(RM) -f $(LINUX_ZSTDLIB)/*.o $(RM) -f $(LINUX_ZSTDLIB)/**/*.o $(RM) -f *.o *.a $(RM) -f test diff --git a/contrib/linux-kernel/test/include/linux/compiler.h b/contrib/linux-kernel/test/include/linux/compiler.h index 58c71181681..ea3422ee316 100644 --- a/contrib/linux-kernel/test/include/linux/compiler.h +++ b/contrib/linux-kernel/test/include/linux/compiler.h @@ -14,4 +14,8 @@ #define inline __inline __attribute__((unused)) #endif +#ifndef noinline +#define noinline __attribute__((noinline)) +#endif + #endif diff --git a/contrib/linux-kernel/test/include/linux/kernel.h b/contrib/linux-kernel/test/include/linux/kernel.h index 9b481ef4747..1f702abac55 100644 --- a/contrib/linux-kernel/test/include/linux/kernel.h +++ b/contrib/linux-kernel/test/include/linux/kernel.h @@ -12,4 +12,8 @@ #define WARN_ON(x) +#define PTR_ALIGN(p, a) (typeof(p))ALIGN((unsigned long long)(p), (a)) +#define ALIGN(x, a) ALIGN_MASK((x), (a) - 1) +#define ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) + #endif From e4b914e6639699be519e86e96f576caf8cb3503a Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Thu, 3 Dec 2020 12:37:30 -0800 Subject: [PATCH 3/6] [contrib][linux] Expose zstd headers to avoid duplication Expose the zstd headers in `include/linux` to avoid struct duplication. This makes the member names not follow Kernel style guidelines, and exposes the zstd symbols. But, the LMKL reviewers are okay with that. --- contrib/linux-kernel/Makefile | 9 +- contrib/linux-kernel/linux_zstd.h | 163 ++++++++---------- contrib/linux-kernel/test/test.c | 20 +-- contrib/linux-kernel/zstd_compress_module.c | 137 ++------------- contrib/linux-kernel/zstd_decompress_module.c | 40 +---- 5 files changed, 113 insertions(+), 256 deletions(-) diff --git a/contrib/linux-kernel/Makefile b/contrib/linux-kernel/Makefile index 1725e7d46c9..1cbe3435f48 100644 --- a/contrib/linux-kernel/Makefile +++ b/contrib/linux-kernel/Makefile @@ -22,6 +22,8 @@ libzstd: --xxh64-prefix 'xxh64' \ --rewrite-include '=' \ --rewrite-include '=' \ + --rewrite-include '"\.\./zstd.h"=' \ + --rewrite-include '"(\.\./common/)?zstd_errors.h"=' \ -DZSTD_NO_INTRINSICS \ -DZSTD_NO_UNUSED_FUNCTIONS \ -DZSTD_LEGACY_SUPPORT=0 \ @@ -48,6 +50,8 @@ libzstd: -RZSTDERRORLIB_VISIBILITY= \ -DZSTD_HAVE_WEAK_SYMBOLS=0 \ -DZSTD_TRACE=0 + mv linux/lib/zstd/zstd.h linux/include/linux/zstd_lib.h + mv linux/lib/zstd/common/zstd_errors.h linux/include/linux/ cp linux_zstd.h linux/include/linux/zstd.h cp zstd_compress_module.c linux/lib/zstd cp zstd_decompress_module.c linux/lib/zstd @@ -62,15 +66,18 @@ import: libzstd rm -f $(LINUX)/include/linux/zstd_errors.h rm -rf $(LINUX)/lib/zstd cp linux/include/linux/zstd.h $(LINUX)/include/linux + cp linux/include/linux/zstd_lib.h $(LINUX)/include/linux + cp linux/include/linux/zstd_errors.h $(LINUX)/include/linux cp -r linux/lib/zstd $(LINUX)/lib import-upstream: rm -rf $(LINUX)/lib/zstd mkdir $(LINUX)/lib/zstd - cp ../../lib/zstd.h $(LINUX)/lib/zstd + cp ../../lib/zstd.h $(LINUX)/include/linux/zstd_lib.h cp -r ../../lib/common $(LINUX)/lib/zstd cp -r ../../lib/compress $(LINUX)/lib/zstd cp -r ../../lib/decompress $(LINUX)/lib/zstd + mv $(LINUX)/lib/zstd/common/zstd_errors.h $(LINUX)/include/linux rm $(LINUX)/lib/zstd/common/threading.* rm $(LINUX)/lib/zstd/common/pool.* rm $(LINUX)/lib/zstd/common/xxhash.* diff --git a/contrib/linux-kernel/linux_zstd.h b/contrib/linux-kernel/linux_zstd.h index dcd1ec18b64..fe64558709a 100644 --- a/contrib/linux-kernel/linux_zstd.h +++ b/contrib/linux-kernel/linux_zstd.h @@ -27,6 +27,8 @@ /* ====== Dependency ====== */ #include +#include +#include /* ====== Helper Functions ====== */ /** @@ -45,13 +47,18 @@ size_t zstd_compress_bound(size_t src_size); */ unsigned int zstd_is_error(size_t code); +/** + * enum zstd_error_code - zstd error codes + */ +typedef ZSTD_ErrorCode zstd_error_code; + /** * zstd_get_error_code() - translates an error function result to an error code * @code: The function result for which zstd_is_error(code) is true. * * Return: A unique error code for this error. */ -int zstd_get_error_code(size_t code); +zstd_error_code zstd_get_error_code(size_t code); /** * zstd_get_error_name() - translates an error function result to a string @@ -66,71 +73,48 @@ const char *zstd_get_error_name(size_t code); /** * enum zstd_strategy - zstd compression search strategy * - * From faster to stronger. + * From faster to stronger. See zstd_lib.h. */ -enum zstd_strategy { - zstd_fast = 1, - zstd_dfast = 2, - zstd_greedy = 3, - zstd_lazy = 4, - zstd_lazy2 = 5, - zstd_btlazy2 = 6, - zstd_btopt = 7, - zstd_btultra = 8, - zstd_btultra2 = 9 -}; +typedef ZSTD_strategy zstd_strategy; /** * struct zstd_compression_parameters - zstd compression parameters - * @window_log: Log of the largest match distance. Larger means more - * compression, and more memory needed during decompression. - * @chain_log: Fully searched segment. Larger means more compression, - * slower, and more memory (useless for fast). - * @hash_log: Dispatch table. Larger means more compression, - * slower, and more memory. - * @search_log: Number of searches. Larger means more compression and slower. - * @search_length: Match length searched. Larger means faster decompression, - * sometimes less compression. - * @target_length: Acceptable match size for optimal parser (only). Larger means - * more compression, and slower. - * @strategy: The zstd compression strategy. - */ -struct zstd_compression_parameters { - unsigned int window_log; - unsigned int chain_log; - unsigned int hash_log; - unsigned int search_log; - unsigned int search_length; - unsigned int target_length; - enum zstd_strategy strategy; -}; + * @windowLog: Log of the largest match distance. Larger means more + * compression, and more memory needed during decompression. + * @chainLog: Fully searched segment. Larger means more compression, + * slower, and more memory (useless for fast). + * @hashLog: Dispatch table. Larger means more compression, + * slower, and more memory. + * @searchLog: Number of searches. Larger means more compression and slower. + * @searchLength: Match length searched. Larger means faster decompression, + * sometimes less compression. + * @targetLength: Acceptable match size for optimal parser (only). Larger means + * more compression, and slower. + * @strategy: The zstd compression strategy. + * + * See zstd_lib.h. + */ +typedef ZSTD_compressionParameters zstd_compression_parameters; /** * struct zstd_frame_parameters - zstd frame parameters - * @content_size_flag: Controls whether content size will be present in the - * frame header (when known). - * @checksum_flag: Controls whether a 32-bit checksum is generated at the - * end of the frame for error detection. - * @no_dict_id_flag: Controls whether dictID will be saved into the frame - * header when using dictionary compression. + * @contentSizeFlag: Controls whether content size will be present in the + * frame header (when known). + * @checksumFlag: Controls whether a 32-bit checksum is generated at the + * end of the frame for error detection. + * @noDictIDFlag: Controls whether dictID will be saved into the frame + * header when using dictionary compression. * - * The default value is all fields set to 0. + * The default value is all fields set to 0. See zstd_lib.h. */ -struct zstd_frame_parameters { - unsigned int content_size_flag; - unsigned int checksum_flag; - unsigned int no_dict_id_flag; -}; +typedef ZSTD_frameParameters zstd_frame_parameters; /** * struct zstd_parameters - zstd parameters - * @cparams: The compression parameters. - * @fparams: The frame parameters. + * @cParams: The compression parameters. + * @fParams: The frame parameters. */ -struct zstd_parameters { - struct zstd_compression_parameters cparams; - struct zstd_frame_parameters fparams; -}; +typedef ZSTD_parameters zstd_parameters; /** * zstd_get_params() - returns zstd_parameters for selected level @@ -140,12 +124,12 @@ struct zstd_parameters { * * Return: The selected zstd_parameters. */ -struct zstd_parameters zstd_get_params(int level, +zstd_parameters zstd_get_params(int level, unsigned long long estimated_src_size); /* ====== Single-pass Compression ====== */ -typedef struct ZSTD_CCtx_s zstd_cctx; +typedef ZSTD_CCtx zstd_cctx; /** * zstd_cctx_workspace_bound() - max memory needed to initialize a zstd_cctx @@ -158,8 +142,7 @@ typedef struct ZSTD_CCtx_s zstd_cctx; * Return: A lower bound on the size of the workspace that is passed to * zstd_init_cctx(). */ -size_t zstd_cctx_workspace_bound( - const struct zstd_compression_parameters *parameters); +size_t zstd_cctx_workspace_bound(const zstd_compression_parameters *parameters); /** * zstd_init_cctx() - initialize a zstd compression context @@ -186,11 +169,11 @@ zstd_cctx *zstd_init_cctx(void *workspace, size_t workspace_size); * zstd_is_error(). */ size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity, - const void *src, size_t src_size, const struct zstd_parameters *parameters); + const void *src, size_t src_size, const zstd_parameters *parameters); /* ====== Single-pass Decompression ====== */ -typedef struct ZSTD_DCtx_s zstd_dctx; +typedef ZSTD_DCtx zstd_dctx; /** * zstd_dctx_workspace_bound() - max memory needed to initialize a zstd_dctx @@ -236,12 +219,10 @@ size_t zstd_decompress_dctx(zstd_dctx *dctx, void *dst, size_t dst_capacity, * @size: Size of the input buffer. * @pos: Position where reading stopped. Will be updated. * Necessarily 0 <= pos <= size. + * + * See zstd_lib.h. */ -struct zstd_in_buffer { - const void *src; - size_t size; - size_t pos; -}; +typedef ZSTD_inBuffer zstd_in_buffer; /** * struct zstd_out_buffer - output buffer for streaming @@ -249,16 +230,14 @@ struct zstd_in_buffer { * @size: Size of the output buffer. * @pos: Position where writing stopped. Will be updated. * Necessarily 0 <= pos <= size. + * + * See zstd_lib.h. */ -struct zstd_out_buffer { - void *dst; - size_t size; - size_t pos; -}; +typedef ZSTD_outBuffer zstd_out_buffer; /* ====== Streaming Compression ====== */ -typedef struct ZSTD_CCtx_s zstd_cstream; +typedef ZSTD_CStream zstd_cstream; /** * zstd_cstream_workspace_bound() - memory needed to initialize a zstd_cstream @@ -267,8 +246,7 @@ typedef struct ZSTD_CCtx_s zstd_cstream; * Return: A lower bound on the size of the workspace that is passed to * zstd_init_cstream(). */ -size_t zstd_cstream_workspace_bound( - const struct zstd_compression_parameters *cparams); +size_t zstd_cstream_workspace_bound(const zstd_compression_parameters *cparams); /** * zstd_init_cstream() - initialize a zstd streaming compression context @@ -285,7 +263,7 @@ size_t zstd_cstream_workspace_bound( * * Return: The zstd streaming compression context or NULL on error. */ -zstd_cstream *zstd_init_cstream(const struct zstd_parameters *parameters, +zstd_cstream *zstd_init_cstream(const zstd_parameters *parameters, unsigned long long pledged_src_size, void *workspace, size_t workspace_size); /** @@ -320,8 +298,8 @@ size_t zstd_reset_cstream(zstd_cstream *cstream, * function call or an error, which can be checked using * zstd_is_error(). */ -size_t zstd_compress_stream(zstd_cstream *cstream, - struct zstd_out_buffer *output, struct zstd_in_buffer *input); +size_t zstd_compress_stream(zstd_cstream *cstream, zstd_out_buffer *output, + zstd_in_buffer *input); /** * zstd_flush_stream() - flush internal buffers into output @@ -336,7 +314,7 @@ size_t zstd_compress_stream(zstd_cstream *cstream, * Return: The number of bytes still present within internal buffers or an * error, which can be checked using zstd_is_error(). */ -size_t zstd_flush_stream(zstd_cstream *cstream, struct zstd_out_buffer *output); +size_t zstd_flush_stream(zstd_cstream *cstream, zstd_out_buffer *output); /** * zstd_end_stream() - flush internal buffers into output and end the frame @@ -350,11 +328,11 @@ size_t zstd_flush_stream(zstd_cstream *cstream, struct zstd_out_buffer *output); * Return: The number of bytes still present within internal buffers or an * error, which can be checked using zstd_is_error(). */ -size_t zstd_end_stream(zstd_cstream *cstream, struct zstd_out_buffer *output); +size_t zstd_end_stream(zstd_cstream *cstream, zstd_out_buffer *output); /* ====== Streaming Decompression ====== */ -typedef struct ZSTD_DCtx_s zstd_dstream; +typedef ZSTD_DStream zstd_dstream; /** * zstd_dstream_workspace_bound() - memory needed to initialize a zstd_dstream @@ -411,8 +389,8 @@ size_t zstd_reset_dstream(zstd_dstream *dstream); * using zstd_is_error(). The size hint will never load more than the * frame. */ -size_t zstd_decompress_stream(zstd_dstream *dstream, - struct zstd_out_buffer *output, struct zstd_in_buffer *input); +size_t zstd_decompress_stream(zstd_dstream *dstream, zstd_out_buffer *output, + zstd_in_buffer *input); /* ====== Frame Inspection Functions ====== */ @@ -431,20 +409,21 @@ size_t zstd_find_frame_compressed_size(const void *src, size_t src_size); /** * struct zstd_frame_params - zstd frame parameters stored in the frame header - * @frame_content_size: The frame content size, or 0 if not present. - * @window_size: The window size, or 0 if the frame is a skippable frame. - * @dict_id: The dictionary id, or 0 if not present. - * @checksum_flag: Whether a checksum was used. + * @frameContentSize: The frame content size, or ZSTD_CONTENTSIZE_UNKNOWN if not + * present. + * @windowSize: The window size, or 0 if the frame is a skippable frame. + * @blockSizeMax: The maximum block size. + * @frameType: The frame type (zstd or skippable) + * @headerSize: The size of the frame header. + * @dictID: The dictionary id, or 0 if not present. + * @checksumFlag: Whether a checksum was used. + * + * See zstd_lib.h. */ -struct zstd_frame_params { - unsigned long long frame_content_size; - unsigned int window_size; - unsigned int dict_id; - unsigned int checksum_flag; -}; +typedef ZSTD_frameHeader zstd_frame_header; /** - * zstd_get_frame_params() - extracts parameters from a zstd or skippable frame + * zstd_get_frame_header() - extracts parameters from a zstd or skippable frame * @params: On success the frame parameters are written here. * @src: The source buffer. It must point to a zstd or skippable frame. * @src_size: The size of the source buffer. @@ -453,7 +432,7 @@ struct zstd_frame_params { * must be provided to make forward progress. Otherwise it returns * an error, which can be checked using zstd_is_error(). */ -size_t zstd_get_frame_params(struct zstd_frame_params *params, const void *src, +size_t zstd_get_frame_header(zstd_frame_header *params, const void *src, size_t src_size); #endif /* LINUX_ZSTD_H */ diff --git a/contrib/linux-kernel/test/test.c b/contrib/linux-kernel/test/test.c index 47af82f19a2..0a33a9a76b4 100644 --- a/contrib/linux-kernel/test/test.c +++ b/contrib/linux-kernel/test/test.c @@ -57,10 +57,10 @@ static void test_btrfs(test_data_t const *data) { fprintf(stderr, "testing btrfs use cases... "); size_t const size = MIN(data->dataSize, 128 * 1024); for (int level = -1; level < 16; ++level) { - struct zstd_parameters params = zstd_get_params(level, size); - CONTROL(params.cparams.window_log <= 17); + zstd_parameters params = zstd_get_params(level, size); + CONTROL(params.cParams.windowLog <= 17); size_t const workspaceSize = - MAX(zstd_cstream_workspace_bound(¶ms.cparams), + MAX(zstd_cstream_workspace_bound(¶ms.cParams), zstd_dstream_workspace_bound(size)); void *workspace = malloc(workspaceSize); CONTROL(workspace != NULL); @@ -72,8 +72,8 @@ static void test_btrfs(test_data_t const *data) { { zstd_cstream *cctx = zstd_init_cstream(¶ms, size, workspace, workspaceSize); CONTROL(cctx != NULL); - struct zstd_out_buffer out = {NULL, 0, 0}; - struct zstd_in_buffer in = {NULL, 0, 0}; + zstd_out_buffer out = {NULL, 0, 0}; + zstd_in_buffer in = {NULL, 0, 0}; for (;;) { if (in.pos == in.size) { in.src = ip; @@ -107,10 +107,10 @@ static void test_btrfs(test_data_t const *data) { op = data->data2; oend = op + size; { - zstd_dstream *dctx = zstd_init_dstream(1ULL << params.cparams.window_log, workspace, workspaceSize); + zstd_dstream *dctx = zstd_init_dstream(1ULL << params.cParams.windowLog, workspace, workspaceSize); CONTROL(dctx != NULL); - struct zstd_out_buffer out = {NULL, 0, 0}; - struct zstd_in_buffer in = {NULL, 0, 0}; + zstd_out_buffer out = {NULL, 0, 0}; + zstd_in_buffer in = {NULL, 0, 0}; for (;;) { if (in.pos == in.size) { in.src = ip; @@ -144,8 +144,8 @@ static void test_decompress_unzstd(test_data_t const *data) { fprintf(stderr, "Testing decompress unzstd... "); size_t cSize; { - struct zstd_parameters params = zstd_get_params(19, 0); - size_t const wkspSize = zstd_cctx_workspace_bound(¶ms.cparams); + zstd_parameters params = zstd_get_params(19, 0); + size_t const wkspSize = zstd_cctx_workspace_bound(¶ms.cParams); void* wksp = malloc(wkspSize); CONTROL(wksp != NULL); zstd_cctx* cctx = zstd_init_cctx(wksp, wkspSize); diff --git a/contrib/linux-kernel/zstd_compress_module.c b/contrib/linux-kernel/zstd_compress_module.c index bab79afb0f0..0a29abb5379 100644 --- a/contrib/linux-kernel/zstd_compress_module.c +++ b/contrib/linux-kernel/zstd_compress_module.c @@ -5,98 +5,25 @@ #include #include -#include "zstd.h" #include "common/zstd_deps.h" #include "common/zstd_internal.h" -static void zstd_check_structs(void) { - /* Check that the structs have the same size. */ - ZSTD_STATIC_ASSERT(sizeof(ZSTD_parameters) == - sizeof(struct zstd_parameters)); - ZSTD_STATIC_ASSERT(sizeof(ZSTD_compressionParameters) == - sizeof(struct zstd_compression_parameters)); - ZSTD_STATIC_ASSERT(sizeof(ZSTD_frameParameters) == - sizeof(struct zstd_frame_parameters)); - /* Zstd guarantees that the layout of the structs never change. Verify it. */ - ZSTD_STATIC_ASSERT(offsetof(ZSTD_parameters, cParams) == - offsetof(struct zstd_parameters, cparams)); - ZSTD_STATIC_ASSERT(offsetof(ZSTD_parameters, fParams) == - offsetof(struct zstd_parameters, fparams)); - ZSTD_STATIC_ASSERT(offsetof(ZSTD_compressionParameters, windowLog) == - offsetof(struct zstd_compression_parameters, window_log)); - ZSTD_STATIC_ASSERT(offsetof(ZSTD_compressionParameters, chainLog) == - offsetof(struct zstd_compression_parameters, chain_log)); - ZSTD_STATIC_ASSERT(offsetof(ZSTD_compressionParameters, hashLog) == - offsetof(struct zstd_compression_parameters, hash_log)); - ZSTD_STATIC_ASSERT(offsetof(ZSTD_compressionParameters, searchLog) == - offsetof(struct zstd_compression_parameters, search_log)); - ZSTD_STATIC_ASSERT(offsetof(ZSTD_compressionParameters, minMatch) == - offsetof(struct zstd_compression_parameters, search_length)); - ZSTD_STATIC_ASSERT(offsetof(ZSTD_compressionParameters, targetLength) == - offsetof(struct zstd_compression_parameters, target_length)); - ZSTD_STATIC_ASSERT(offsetof(ZSTD_compressionParameters, strategy) == - offsetof(struct zstd_compression_parameters, strategy)); - ZSTD_STATIC_ASSERT(offsetof(ZSTD_frameParameters, contentSizeFlag) == - offsetof(struct zstd_frame_parameters, content_size_flag)); - ZSTD_STATIC_ASSERT(offsetof(ZSTD_frameParameters, checksumFlag) == - offsetof(struct zstd_frame_parameters, checksum_flag)); - ZSTD_STATIC_ASSERT(offsetof(ZSTD_frameParameters, noDictIDFlag) == - offsetof(struct zstd_frame_parameters, no_dict_id_flag)); - /* Check that the strategies are the same. This can change. */ - ZSTD_STATIC_ASSERT((int)ZSTD_fast == (int)zstd_fast); - ZSTD_STATIC_ASSERT((int)ZSTD_dfast == (int)zstd_dfast); - ZSTD_STATIC_ASSERT((int)ZSTD_greedy == (int)zstd_greedy); - ZSTD_STATIC_ASSERT((int)ZSTD_lazy == (int)zstd_lazy); - ZSTD_STATIC_ASSERT((int)ZSTD_lazy2 == (int)zstd_lazy2); - ZSTD_STATIC_ASSERT((int)ZSTD_btlazy2 == (int)zstd_btlazy2); - ZSTD_STATIC_ASSERT((int)ZSTD_btopt == (int)zstd_btopt); - ZSTD_STATIC_ASSERT((int)ZSTD_btultra == (int)zstd_btultra); - ZSTD_STATIC_ASSERT((int)ZSTD_btultra2 == (int)zstd_btultra2); - /* Check input buffer */ - ZSTD_STATIC_ASSERT(sizeof(ZSTD_inBuffer) == sizeof(struct zstd_in_buffer)); - ZSTD_STATIC_ASSERT(offsetof(ZSTD_inBuffer, src) == - offsetof(struct zstd_in_buffer, src)); - ZSTD_STATIC_ASSERT(offsetof(ZSTD_inBuffer, size) == - offsetof(struct zstd_in_buffer, size)); - ZSTD_STATIC_ASSERT(offsetof(ZSTD_inBuffer, pos) == - offsetof(struct zstd_in_buffer, pos)); - /* Check output buffer */ - ZSTD_STATIC_ASSERT(sizeof(ZSTD_outBuffer) == - sizeof(struct zstd_out_buffer)); - ZSTD_STATIC_ASSERT(offsetof(ZSTD_outBuffer, dst) == - offsetof(struct zstd_out_buffer, dst)); - ZSTD_STATIC_ASSERT(offsetof(ZSTD_outBuffer, size) == - offsetof(struct zstd_out_buffer, size)); - ZSTD_STATIC_ASSERT(offsetof(ZSTD_outBuffer, pos) == - offsetof(struct zstd_out_buffer, pos)); -} - size_t zstd_compress_bound(size_t src_size) { return ZSTD_compressBound(src_size); } EXPORT_SYMBOL(zstd_compress_bound); -struct zstd_parameters zstd_get_params(int level, +zstd_parameters zstd_get_params(int level, unsigned long long estimated_src_size) { - const ZSTD_parameters params = ZSTD_getParams(level, estimated_src_size, 0); - struct zstd_parameters out; - - /* no-op */ - zstd_check_structs(); - ZSTD_memcpy(&out, ¶ms, sizeof(out)); - return out; + return ZSTD_getParams(level, estimated_src_size, 0); } EXPORT_SYMBOL(zstd_get_params); -size_t zstd_cctx_workspace_bound( - const struct zstd_compression_parameters *cparams) +size_t zstd_cctx_workspace_bound(const zstd_compression_parameters *cparams) { - ZSTD_compressionParameters p; - - ZSTD_memcpy(&p, cparams, sizeof(p)); - return ZSTD_estimateCCtxSize_usingCParams(p); + return ZSTD_estimateCCtxSize_usingCParams(*cparams); } EXPORT_SYMBOL(zstd_cctx_workspace_bound); @@ -109,29 +36,21 @@ zstd_cctx *zstd_init_cctx(void *workspace, size_t workspace_size) EXPORT_SYMBOL(zstd_init_cctx); size_t zstd_compress_cctx(zstd_cctx *cctx, void *dst, size_t dst_capacity, - const void *src, size_t src_size, const struct zstd_parameters *parameters) + const void *src, size_t src_size, const zstd_parameters *parameters) { - ZSTD_parameters p; - - ZSTD_memcpy(&p, parameters, sizeof(p)); - return ZSTD_compress_advanced(cctx, dst, dst_capacity, src, src_size, NULL, 0, p); + return ZSTD_compress_advanced(cctx, dst, dst_capacity, src, src_size, NULL, 0, *parameters); } EXPORT_SYMBOL(zstd_compress_cctx); -size_t zstd_cstream_workspace_bound( - const struct zstd_compression_parameters *cparams) +size_t zstd_cstream_workspace_bound(const zstd_compression_parameters *cparams) { - ZSTD_compressionParameters p; - - ZSTD_memcpy(&p, cparams, sizeof(p)); - return ZSTD_estimateCStreamSize_usingCParams(p); + return ZSTD_estimateCStreamSize_usingCParams(*cparams); } EXPORT_SYMBOL(zstd_cstream_workspace_bound); -zstd_cstream *zstd_init_cstream(const struct zstd_parameters *parameters, +zstd_cstream *zstd_init_cstream(const zstd_parameters *parameters, unsigned long long pledged_src_size, void *workspace, size_t workspace_size) { - ZSTD_parameters p; zstd_cstream *cstream; size_t ret; @@ -146,8 +65,7 @@ zstd_cstream *zstd_init_cstream(const struct zstd_parameters *parameters, if (pledged_src_size == 0) pledged_src_size = ZSTD_CONTENTSIZE_UNKNOWN; - ZSTD_memcpy(&p, parameters, sizeof(p)); - ret = ZSTD_initCStream_advanced(cstream, NULL, 0, p, pledged_src_size); + ret = ZSTD_initCStream_advanced(cstream, NULL, 0, *parameters, pledged_src_size); if (ZSTD_isError(ret)) return NULL; @@ -162,43 +80,22 @@ size_t zstd_reset_cstream(zstd_cstream *cstream, } EXPORT_SYMBOL(zstd_reset_cstream); -size_t zstd_compress_stream(zstd_cstream *cstream, - struct zstd_out_buffer *output, struct zstd_in_buffer *input) +size_t zstd_compress_stream(zstd_cstream *cstream, zstd_out_buffer *output, + zstd_in_buffer *input) { - ZSTD_outBuffer o; - ZSTD_inBuffer i; - size_t ret; - - ZSTD_memcpy(&o, output, sizeof(o)); - ZSTD_memcpy(&i, input, sizeof(i)); - ret = ZSTD_compressStream(cstream, &o, &i); - ZSTD_memcpy(output, &o, sizeof(o)); - ZSTD_memcpy(input, &i, sizeof(i)); - return ret; + return ZSTD_compressStream(cstream, output, input); } EXPORT_SYMBOL(zstd_compress_stream); -size_t zstd_flush_stream(zstd_cstream *cstream, struct zstd_out_buffer *output) +size_t zstd_flush_stream(zstd_cstream *cstream, zstd_out_buffer *output) { - ZSTD_outBuffer o; - size_t ret; - - ZSTD_memcpy(&o, output, sizeof(o)); - ret = ZSTD_flushStream(cstream, &o); - ZSTD_memcpy(output, &o, sizeof(o)); - return ret; + return ZSTD_flushStream(cstream, output); } EXPORT_SYMBOL(zstd_flush_stream); -size_t zstd_end_stream(zstd_cstream *cstream, struct zstd_out_buffer *output) +size_t zstd_end_stream(zstd_cstream *cstream, zstd_out_buffer *output) { - ZSTD_outBuffer o; - size_t ret; - - ZSTD_memcpy(&o, output, sizeof(o)); - ret = ZSTD_endStream(cstream, &o); - ZSTD_memcpy(output, &o, sizeof(o)); - return ret; + return ZSTD_endStream(cstream, output); } EXPORT_SYMBOL(zstd_end_stream); diff --git a/contrib/linux-kernel/zstd_decompress_module.c b/contrib/linux-kernel/zstd_decompress_module.c index 988fdb57200..bac348daab7 100644 --- a/contrib/linux-kernel/zstd_decompress_module.c +++ b/contrib/linux-kernel/zstd_decompress_module.c @@ -5,9 +5,7 @@ #include #include -#include "zstd.h" #include "common/zstd_deps.h" -#include "common/zstd_errors.h" /* Common symbols. zstd_compress must depend on zstd_decompress. */ @@ -17,7 +15,7 @@ unsigned int zstd_is_error(size_t code) } EXPORT_SYMBOL(zstd_is_error); -int zstd_get_error_code(size_t code) +zstd_error_code zstd_get_error_code(size_t code) { return ZSTD_getErrorCode(code); } @@ -74,19 +72,10 @@ size_t zstd_reset_dstream(zstd_dstream *dstream) } EXPORT_SYMBOL(zstd_reset_dstream); -size_t zstd_decompress_stream(zstd_dstream *dstream, - struct zstd_out_buffer *output, struct zstd_in_buffer *input) +size_t zstd_decompress_stream(zstd_dstream *dstream, zstd_out_buffer *output, + zstd_in_buffer *input) { - ZSTD_outBuffer o; - ZSTD_inBuffer i; - size_t ret; - - ZSTD_memcpy(&o, output, sizeof(o)); - ZSTD_memcpy(&i, input, sizeof(i)); - ret = ZSTD_decompressStream(dstream, &o, &i); - ZSTD_memcpy(output, &o, sizeof(o)); - ZSTD_memcpy(input, &i, sizeof(i)); - return ret; + return ZSTD_decompressStream(dstream, output, input); } EXPORT_SYMBOL(zstd_decompress_stream); @@ -96,27 +85,12 @@ size_t zstd_find_frame_compressed_size(const void *src, size_t src_size) } EXPORT_SYMBOL(zstd_find_frame_compressed_size); -size_t zstd_get_frame_params(struct zstd_frame_params *params, const void *src, +size_t zstd_get_frame_header(zstd_frame_header *header, const void *src, size_t src_size) { - ZSTD_frameHeader h; - const size_t ret = ZSTD_getFrameHeader(&h, src, src_size); - - if (ret != 0) - return ret; - - if (h.frameContentSize != ZSTD_CONTENTSIZE_UNKNOWN) - params->frame_content_size = h.frameContentSize; - else - params->frame_content_size = 0; - - params->window_size = h.windowSize; - params->dict_id = h.dictID; - params->checksum_flag = h.checksumFlag; - - return ret; + return ZSTD_getFrameHeader(header, src, src_size); } -EXPORT_SYMBOL(zstd_get_frame_params); +EXPORT_SYMBOL(zstd_get_frame_header); MODULE_LICENSE("Dual BSD/GPL"); MODULE_DESCRIPTION("Zstd Decompressor"); From 7222614a191e1dc0ade80a5b295a3fbb04e5696b Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Mon, 15 Mar 2021 11:19:21 -0700 Subject: [PATCH 4/6] [contrib][freestanding] Remove tracing support Remove tracing support from `freestanding.py` to keep things simple. --- contrib/freestanding_lib/freestanding.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/contrib/freestanding_lib/freestanding.py b/contrib/freestanding_lib/freestanding.py index 32d4fcbea86..ca3f0ac8be7 100755 --- a/contrib/freestanding_lib/freestanding.py +++ b/contrib/freestanding_lib/freestanding.py @@ -27,6 +27,7 @@ "common/pool.h", "common/threading.c", "common/threading.h", + "common/zstd_trace.c", "compress/zstdmt_compress.h", "compress/zstdmt_compress.c", ] @@ -471,7 +472,7 @@ def _copy_file(self, lib_path): dst_path = os.path.join(self._dst_lib, lib_path) self._log(f"\tCopying: {src_path} -> {dst_path}") shutil.copyfile(src_path, dst_path) - + def _copy_source_lib(self): self._log("Copying source library into output library") @@ -481,14 +482,14 @@ def _copy_source_lib(self): for subdir in INCLUDED_SUBDIRS: src_dir = os.path.join(self._src_lib, subdir) dst_dir = os.path.join(self._dst_lib, subdir) - + assert os.path.exists(src_dir) os.makedirs(dst_dir, exist_ok=True) for filename in os.listdir(src_dir): lib_path = os.path.join(subdir, filename) self._copy_file(lib_path) - + def _copy_zstd_deps(self): dst_zstd_deps = os.path.join(self._dst_lib, "common", "zstd_deps.h") self._log(f"Copying zstd_deps: {self._zstd_deps} -> {dst_zstd_deps}") @@ -508,7 +509,7 @@ def _hardwire_preprocessor(self, name: str, value: Optional[str] = None, undef=F assert not (undef and value is not None) for filepath in self._dst_lib_file_paths(): file = FileLines(filepath) - + def _hardwire_defines(self): self._log("Hardwiring macros") partial_preprocessor = PartialPreprocessor(self._defs, self._replaces, self._undefs) @@ -536,7 +537,7 @@ def _remove_excludes(self): skipped.append(line) if end_re.search(line) is not None: assert begin_re.search(line) is None - self._log(f"\t\tRemoving excluded section: {exclude}") + self._log(f"\t\tRemoving excluded section: {exclude}") for s in skipped: self._log(f"\t\t\t- {s}") emit = True @@ -559,12 +560,12 @@ def _rewrite_include(self, original, rewritten): e = match.end('include') file.lines[i] = line[:s] + rewritten + line[e:] file.write() - + def _rewrite_includes(self): self._log("Rewriting includes") for original, rewritten in self._rewritten_includes: self._rewrite_include(original, rewritten) - + def _replace_xxh64_prefix(self): if self._xxh64_prefix is None: return @@ -656,6 +657,10 @@ def main(name, args): if name in args.undefs: raise RuntimeError(f"{name} is both defined and undefined!") + # Always set tracing to 0 + if "ZSTD_TRACE" not in (arg[0] for arg in args.defs): + args.defs.append(("ZSTD_TRACE", "0")) + args.replaces = parse_pair(args.replaces) for name, _ in args.replaces: if name in args.undefs or name in args.defs: From cd1551d2611028540a4d68363fb3e791e3bf8561 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Mon, 15 Mar 2021 14:46:26 -0700 Subject: [PATCH 5/6] [lib][tracing] Add ZSTD_NO_TRACE macro When defined, it disables tracing, and avoids including the header. --- contrib/freestanding_lib/freestanding.py | 4 +++- contrib/linux-kernel/Makefile | 3 ++- lib/common/zstd_internal.h | 7 ++++++- lib/compress/zstd_compress.c | 1 - lib/compress/zstd_compress_internal.h | 1 - lib/decompress/zstd_decompress.c | 1 - lib/decompress/zstd_decompress_internal.h | 1 - 7 files changed, 11 insertions(+), 7 deletions(-) diff --git a/contrib/freestanding_lib/freestanding.py b/contrib/freestanding_lib/freestanding.py index ca3f0ac8be7..a5bcf100f72 100755 --- a/contrib/freestanding_lib/freestanding.py +++ b/contrib/freestanding_lib/freestanding.py @@ -28,6 +28,7 @@ "common/threading.c", "common/threading.h", "common/zstd_trace.c", + "common/zstd_trace.h", "compress/zstdmt_compress.h", "compress/zstdmt_compress.c", ] @@ -658,7 +659,8 @@ def main(name, args): raise RuntimeError(f"{name} is both defined and undefined!") # Always set tracing to 0 - if "ZSTD_TRACE" not in (arg[0] for arg in args.defs): + if "ZSTD_NO_TRACE" not in (arg[0] for arg in args.defs): + args.defs.append(("ZSTD_NO_TRACE", None)) args.defs.append(("ZSTD_TRACE", "0")) args.replaces = parse_pair(args.replaces) diff --git a/contrib/linux-kernel/Makefile b/contrib/linux-kernel/Makefile index 1cbe3435f48..0fefc2bf20d 100644 --- a/contrib/linux-kernel/Makefile +++ b/contrib/linux-kernel/Makefile @@ -49,7 +49,8 @@ libzstd: -RZSTDLIB_VISIBILITY= \ -RZSTDERRORLIB_VISIBILITY= \ -DZSTD_HAVE_WEAK_SYMBOLS=0 \ - -DZSTD_TRACE=0 + -DZSTD_TRACE=0 \ + -DZSTD_NO_TRACE mv linux/lib/zstd/zstd.h linux/include/linux/zstd_lib.h mv linux/lib/zstd/common/zstd_errors.h linux/include/linux/ cp linux_zstd.h linux/include/linux/zstd.h diff --git a/lib/common/zstd_internal.h b/lib/common/zstd_internal.h index 53a982c7341..1d1c91ffd8f 100644 --- a/lib/common/zstd_internal.h +++ b/lib/common/zstd_internal.h @@ -36,6 +36,11 @@ # define XXH_STATIC_LINKING_ONLY /* XXH64_state_t */ #endif #include "xxhash.h" /* XXH_reset, update, digest */ +#ifndef ZSTD_NO_TRACE +# include "zstd_trace.h" +#else +# define ZSTD_TRACE 0 +#endif #if defined (__cplusplus) extern "C" { @@ -365,7 +370,7 @@ typedef struct { /* longLengthPos and longLengthID to allow us to represent either a single litLength or matchLength * in the seqStore that has a value larger than U16 (if it exists). To do so, we increment - * the existing value of the litLength or matchLength by 0x10000. + * the existing value of the litLength or matchLength by 0x10000. */ U32 longLengthID; /* 0 == no longLength; 1 == Represent the long literal; 2 == Represent the long match; */ U32 longLengthPos; /* Index of the sequence to apply long length modification to */ diff --git a/lib/compress/zstd_compress.c b/lib/compress/zstd_compress.c index 93c4075c521..5973497127c 100644 --- a/lib/compress/zstd_compress.c +++ b/lib/compress/zstd_compress.c @@ -14,7 +14,6 @@ #include "../common/zstd_deps.h" /* INT_MAX, ZSTD_memset, ZSTD_memcpy */ #include "../common/cpu.h" #include "../common/mem.h" -#include "../common/zstd_trace.h" #include "hist.h" /* HIST_countFast_wksp */ #define FSE_STATIC_LINKING_ONLY /* FSE_encodeSymbol */ #include "../common/fse.h" diff --git a/lib/compress/zstd_compress_internal.h b/lib/compress/zstd_compress_internal.h index 6083ed66418..bfe83e2149c 100644 --- a/lib/compress/zstd_compress_internal.h +++ b/lib/compress/zstd_compress_internal.h @@ -19,7 +19,6 @@ * Dependencies ***************************************/ #include "../common/zstd_internal.h" -#include "../common/zstd_trace.h" /* ZSTD_TraceCtx */ #include "zstd_cwksp.h" #ifdef ZSTD_MULTITHREAD # include "zstdmt_compress.h" diff --git a/lib/decompress/zstd_decompress.c b/lib/decompress/zstd_decompress.c index 15139501bea..10b6a88e155 100644 --- a/lib/decompress/zstd_decompress.c +++ b/lib/decompress/zstd_decompress.c @@ -58,7 +58,6 @@ #include "../common/zstd_deps.h" /* ZSTD_memcpy, ZSTD_memmove, ZSTD_memset */ #include "../common/cpu.h" /* bmi2 */ #include "../common/mem.h" /* low level memory routines */ -#include "../common/zstd_trace.h" #define FSE_STATIC_LINKING_ONLY #include "../common/fse.h" #define HUF_STATIC_LINKING_ONLY diff --git a/lib/decompress/zstd_decompress_internal.h b/lib/decompress/zstd_decompress_internal.h index 3fcec6c5667..40d82023185 100644 --- a/lib/decompress/zstd_decompress_internal.h +++ b/lib/decompress/zstd_decompress_internal.h @@ -21,7 +21,6 @@ *********************************************************/ #include "../common/mem.h" /* BYTE, U16, U32 */ #include "../common/zstd_internal.h" /* ZSTD_seqSymbol */ -#include "../common/zstd_trace.h" /* ZSTD_TraceCtx */ From ea288e0d8e56dc3fd7a3e6f057aa67b841db95e5 Mon Sep 17 00:00:00 2001 From: Nick Terrell Date: Mon, 15 Mar 2021 17:48:56 -0700 Subject: [PATCH 6/6] [lib] Bump zstd version number --- lib/zstd.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/zstd.h b/lib/zstd.h index 222339d71a3..054793eff37 100644 --- a/lib/zstd.h +++ b/lib/zstd.h @@ -72,7 +72,7 @@ extern "C" { /*------ Version ------*/ #define ZSTD_VERSION_MAJOR 1 #define ZSTD_VERSION_MINOR 4 -#define ZSTD_VERSION_RELEASE 9 +#define ZSTD_VERSION_RELEASE 10 #define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE) /*! ZSTD_versionNumber() :