This commit is contained in:
jinyaoguo
2025-06-21 12:57:12 -04:00
30 changed files with 1223 additions and 310 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ Usage: zstd *OPTIONS...* *INPUT... | -* *-o OUTPUT*
Options:
-o OUTPUT Write output to a single file, OUTPUT.
-k, --keep Preserve INPUT file(s). *Default*
--rm Remove INPUT file(s) after successful (de)compression.
--rm Remove INPUT file(s) after successful (de)compression to file.
-# Desired compression level, where `#` is a number between 1 and 19;
lower numbers provide faster compression, higher numbers yield
+1 -1
View File
@@ -640,7 +640,7 @@ if __name__ == "__main__":
help="Preserve the scratch directory TEST_DIR/scratch/ for debugging purposes."
)
parser.add_argument("--verbose", action="store_true", help="Verbose test output.")
parser.add_argument("--timeout", default=200, type=int, help="Test case timeout in seconds. Set to 0 to disable timeouts.")
parser.add_argument("--timeout", default=800, type=int, help="Test case timeout in seconds. Set to 0 to disable timeouts.")
parser.add_argument(
"--exec-prefix",
default=None,
+1
View File
@@ -890,6 +890,7 @@ static int benchMem(unsigned scenarioID,
if (!BMK_isSuccessful_runOutcome(bOutcome)) {
DISPLAY("ERROR: Scenario %u: %s \n", scenarioID, ZSTD_getErrorName(BMK_extract_errorResult(bOutcome)));
errorcode = 1;
BMK_freeTimedFnState(tfs);
goto _cleanOut;
}
+2 -3
View File
@@ -31,12 +31,11 @@ void* FUZZ_malloc_rand(size_t size, FUZZ_dataProducer_t *producer)
return mem;
} else {
uintptr_t ptr = 0;
/* Add +- 1M 50% of the time */
/* Return junk pointer 50% of the time */
if (FUZZ_dataProducer_uint32Range(producer, 0, 1))
FUZZ_dataProducer_int32Range(producer, -1000000, 1000000);
ptr += FUZZ_dataProducer_int32Range(producer, -1000000, 1000000);
return (void*)ptr;
}
}
int FUZZ_memcmp(void const* lhs, void const* rhs, size_t size)
+1
View File
@@ -66,6 +66,7 @@ void* FUZZ_malloc(size_t size);
/**
* malloc except returns random pointer for zero sized data and FUZZ_ASSERT
* that malloc doesn't fail.
* WARNING: Only free the returned pointer if size > 0!
*/
void* FUZZ_malloc_rand(size_t size, FUZZ_dataProducer_t *producer);
+126
View File
@@ -44,6 +44,13 @@
/* must be included after util.h, due to ERROR macro redefinition issue on Visual Studio */
#include "zstd_internal.h" /* ZSTD_WORKSPACETOOLARGE_MAXDURATION, ZSTD_WORKSPACETOOLARGE_FACTOR, KB, MB */
#include "threading.h" /* ZSTD_pthread_create, ZSTD_pthread_join */
#include "compress/hist.h" /* HIST_count_wksp */
/*-************************************
* Macros
**************************************/
#define COUNTOF(array) (sizeof(array) / sizeof(*(array)))
/*-************************************
@@ -567,6 +574,123 @@ static void test_decompressBound(unsigned tnb)
DISPLAYLEVEL(3, "OK \n");
}
static unsigned test_histCountWksp(unsigned seed, unsigned testNb)
{
static const unsigned symLowLimits[] = { 0, 27, 0, 0, 27, 42, 0, 0, 27, 42, 27, 42 };
static const unsigned symHighLimits[] = { 255, 255, 210, 110, 42, 42, 210, 110, 42, 42, 42, 42 };
static const unsigned symMaxLimits[] = { 255, 255, 255, 255, 255, 255, 230, 130, 99, 99, 42, 42 };
static const size_t inputSizes[] = { 3367, 1761, 893, 117 };
unsigned workspace[HIST_WKSP_SIZE_U32];
size_t res, i, is, il;
DISPLAYLEVEL(3, "test%3u : HIST_count_wksp with empty source : ", testNb++);
{
/* With NULL source UBSan of older Clang could fail: applying zero offset to null pointer. */
static const unsigned char source[4] = { 0 };
unsigned count[1] = { 0 };
unsigned maxSym = 0;
res = HIST_count_wksp(count, &maxSym, source, 0, workspace, sizeof(workspace));
CHECK_EQ(res, 0);
CHECK_EQ(maxSym, 0);
CHECK_EQ(count[0], 0);
}
DISPLAYLEVEL(3, "OK \n");
#if HIST_WKSP_SIZE_U32
DISPLAYLEVEL(3, "test%3u : HIST_count_wksp with small workspace : ", testNb++);
{
unsigned count[1] = { 0 };
unsigned maxSym = 0;
res = HIST_count_wksp(count, &maxSym, NULL, 0, workspace, sizeof(workspace) - 1);
CHECK_EQ(res, ERROR(workSpace_tooSmall));
CHECK_EQ(maxSym, 0);
CHECK_EQ(count[0], 0);
}
DISPLAYLEVEL(3, "OK \n");
DISPLAYLEVEL(3, "test%3u : HIST_count_wksp with wrong workspace alignment : ", testNb++);
{
unsigned count[1] = { 0 };
unsigned maxSym = 0;
res = HIST_count_wksp(count, &maxSym, NULL, 0, (unsigned*)(void*)((char*)workspace + 1), sizeof(workspace));
CHECK_EQ(res, ERROR(GENERIC));
CHECK_EQ(maxSym, 0);
CHECK_EQ(count[0], 0);
}
DISPLAYLEVEL(3, "OK \n");
#endif
DISPLAYLEVEL(3, "test%3u : HIST_count_wksp with symbol out of range, small size : ", testNb++);
{
/* For less elements HIST_count_parallel_wksp would fail. */
static const unsigned char source[4] = { 1, 4, 0, 2 };
static const unsigned expected[6] = { 0 };
unsigned count[6] = { 0 };
unsigned maxSym = 2;
res = HIST_count_wksp(count, &maxSym, source, sizeof(source), workspace, sizeof(workspace));
CHECK_EQ(res, ERROR(maxSymbolValue_tooSmall));
CHECK_EQ(maxSym, 2);
for (i = 0; i < COUNTOF(expected); ++i) CHECK_EQ(count[i], expected[i]);
}
DISPLAYLEVEL(3, "OK \n");
DISPLAYLEVEL(3, "test%3u : HIST_count_wksp with symbol out of range, medium size : ", testNb++);
{
unsigned char source[3407];
unsigned count[6] = { 0 };
unsigned maxSym = 2;
for (i = 0; i < COUNTOF(source); ++i) {
source[i] = (48271 * (i + 1)) & 3;
}
res = HIST_count_wksp(count, &maxSym, source, sizeof(source), workspace, sizeof(workspace));
CHECK_EQ(res, ERROR(maxSymbolValue_tooSmall));
CHECK_EQ(maxSym, 2);
for (i = 0; i < COUNTOF(count); ++i) CHECK_EQ(count[i], 0);
}
DISPLAYLEVEL(3, "OK \n");
for (il = 0; il < COUNTOF(symMaxLimits); ++il) {
unsigned symMax = symMaxLimits[il];
unsigned symLow = symLowLimits[il];
unsigned symHigh = symHighLimits[il];
unsigned symRange = symHigh - symLow + 1;
for (is = 0; is < COUNTOF(inputSizes); ++is) {
unsigned char source[4000];
size_t inputSize = inputSizes[is];
assert(inputSize <= sizeof(source));
DISPLAYLEVEL(3, "test%3u : HIST_count_wksp test in [%u..%u], symMax: %u, inputSize: %u : ",
testNb++, symLow, symHigh, symMax, (unsigned)inputSize);
{
unsigned count[260] = { 0 };
unsigned expected[COUNTOF(count)] = { 0 };
unsigned maxSym = symMax;
unsigned realMaxSym = symMax;
unsigned maxCount = 0;
for (i = 0; i < inputSize; ++i) {
unsigned prng = (48271 * (i + seed)) % symRange + symLow;
source[i] = (unsigned char)prng;
++expected[prng];
}
/* for basic buffer overwrite checks */
for (i = maxSym + 1; i < COUNTOF(count); ++i) expected[i] = count[i] = ~0u;
for (i = 0; i <= maxSym; ++i) maxCount = MAX(maxCount, expected[i]);
for (i = realMaxSym; i > 0; --i) {
if (expected[i]) break;
--realMaxSym;
}
res = HIST_count_wksp(count, &maxSym, source, inputSize, workspace, sizeof(workspace));
CHECK_EQ(res, maxCount);
CHECK_EQ(maxSym, realMaxSym);
for (i = 0; i < COUNTOF(expected); ++i) CHECK_EQ(count[i], expected[i]);
}
DISPLAYLEVEL(3, "OK \n");
}
}
return testNb;
}
static void test_setCParams(unsigned tnb)
{
ZSTD_CCtx* const cctx = ZSTD_createCCtx();
@@ -712,6 +836,8 @@ static int basicUnitTests(U32 const seed, double compressibility)
}
DISPLAYLEVEL(3, "OK \n");
testNb = test_histCountWksp(seed, testNb);
DISPLAYLEVEL(3, "test%3u : compress %u bytes : ", testNb++, (unsigned)CNBuffSize);
{ ZSTD_CCtx* const cctx = ZSTD_createCCtx();
if (cctx==NULL) goto _output_error;
+2
View File
@@ -73,6 +73,7 @@ int main(int argc, const char** argv)
int _exit_code = 1;
(void)argc;
(void)argv;
int _exit_code = 0;
if (!buffer || !out || !roundtrip || !cctx || !dctx) {
fprintf(stderr, "Allocation failure\n");
@@ -127,4 +128,5 @@ cleanup:
ZSTD_freeDCtx(dctx);
ZSTD_freeCCtx(cctx);
return _exit_code;
return _exit_code;
}
+2
View File
@@ -56,6 +56,7 @@ static int testSimpleAPI(void)
} else {
DISPLAY("ERROR: %s\n", ZSTD_getErrorName(ret));
}
free(output);
return 1;
}
if (ret != size) {
@@ -64,6 +65,7 @@ static int testSimpleAPI(void)
}
if (memcmp(EXPECTED, output, size) != 0) {
DISPLAY("ERROR: Wrong decoded output produced\n");
free(output);
return 1;
}
+38 -17
View File
@@ -39,24 +39,39 @@ int main(int argc, const char** argv)
unsigned windowLog = 18;
(void)argc;
(void)argv;
int _exit_code = 0;
/* Create stream */
ctx = ZSTD_createCCtx();
if (!ctx) { return 1; }
/* Set parameters */
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_windowLog, windowLog)))
return 2;
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_chainLog, 13)))
return 2;
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_hashLog, 14)))
return 2;
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_searchLog, 1)))
return 2;
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_minMatch, 7)))
return 2;
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_targetLength, 16)))
return 2;
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_strategy, ZSTD_fast)))
return 2;
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_windowLog, windowLog))) {
_exit_code = 2;
goto _clean_ctx;
}
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_chainLog, 13))) {
_exit_code = 2;
goto _clean_ctx;
}
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_hashLog, 14))) {
_exit_code = 2;
goto _clean_ctx;
}
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_searchLog, 1))) {
_exit_code = 2;
goto _clean_ctx;
}
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_minMatch, 7))) {
_exit_code = 2;
goto _clean_ctx;
}
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_targetLength, 16))) {
_exit_code = 2;
goto _clean_ctx;
}
if (ZSTD_isError(ZSTD_CCtx_setParameter(ctx, ZSTD_c_strategy, ZSTD_fast))) {
_exit_code = 2;
goto _clean_ctx;
}
{
U64 compressed = 0;
const U64 toCompress = ((U64)1) << 33;
@@ -81,22 +96,28 @@ int main(int argc, const char** argv)
}
printf("Compressing, trying to generate a segfault \n");
if (compress(ctx, out, srcBuffer, size)) {
return 1;
_exit_code = 1;
goto _clean_buffer;
}
compressed += size;
while (compressed < toCompress) {
const size_t block = rand() % (size - pos + 1);
if (pos == size) { pos = 0; }
if (compress(ctx, out, srcBuffer + pos, block)) {
return 1;
_exit_code = 1;
goto _clean_buffer;
}
pos += block;
compressed += block;
}
printf("Compression completed successfully (no error triggered)\n");
_clean_buffer:
free(srcBuffer);
free(dstBuffer);
}
_clean_ctx:
ZSTD_freeCCtx(ctx);
return 0;
return _exit_code;
}