File: generate_sequences.c

package info (click to toggle)
libzstd 1.5.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,352 kB
  • sloc: ansic: 89,030; sh: 3,788; python: 3,466; cpp: 2,927; makefile: 2,329; asm: 390
file content (88 lines) | stat: -rw-r--r-- 3,196 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 * All rights reserved.
 *
 * This source code is licensed under both the BSD-style license (found in the
 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
 * in the COPYING file in the root directory of this source tree).
 * You may select, at your option, one of the above-listed licenses.
 */

#define ZSTD_STATIC_LINKING_ONLY

#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>

#include "fuzz_data_producer.h"
#include "fuzz_helpers.h"
#include "zstd_helpers.h"

/**
 * This fuzz target ensures that ZSTD_generateSequences() does not crash and
 * if it succeeds that ZSTD_compressSequences() round trips.
 */

static void testRoundTrip(ZSTD_CCtx* cctx, ZSTD_Sequence const* seqs, size_t nbSeqs, const void* src, size_t srcSize) {
  /* Compress the sequences with block delimiters */
  const size_t compressBound = ZSTD_compressBound(srcSize);
  void* dst = FUZZ_malloc(compressBound);
  FUZZ_ASSERT(dst);

  size_t compressedSize = ZSTD_compressSequences(cctx, dst, compressBound, seqs, nbSeqs, src, srcSize);
  FUZZ_ZASSERT(compressedSize);

  void* decompressed = FUZZ_malloc(srcSize);
  FUZZ_ASSERT(srcSize == 0 || decompressed);
  size_t decompressedSize = ZSTD_decompress(decompressed, srcSize, dst, compressedSize);
  FUZZ_ZASSERT(decompressedSize);
  FUZZ_ASSERT(decompressedSize == srcSize);
  if (srcSize != 0) {
    FUZZ_ASSERT(!memcmp(src, decompressed, srcSize));
  }

  free(decompressed);
  free(dst);
}

int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {

  FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size);
  size = FUZZ_dataProducer_reserveDataPrefix(producer);

  ZSTD_CCtx* cctx = ZSTD_createCCtx();
  FUZZ_ASSERT(cctx);

  const size_t seqsCapacity = FUZZ_dataProducer_uint32Range(producer, 0, 2 * ZSTD_sequenceBound(size));
  ZSTD_Sequence* seqs = (ZSTD_Sequence*)FUZZ_malloc(sizeof(ZSTD_Sequence) * seqsCapacity);
  FUZZ_ASSERT(seqsCapacity == 0 || seqs);

  FUZZ_setRandomParameters(cctx, size, producer);
  FUZZ_ZASSERT(ZSTD_CCtx_setParameter(cctx, ZSTD_c_targetCBlockSize, 0));
  FUZZ_ZASSERT(ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, 0));

  const size_t nbSeqs = ZSTD_generateSequences(cctx, seqs, seqsCapacity, data, size);
  if (ZSTD_isError(nbSeqs)) {
    /* Allowed to error if the destination is too small */
    if (ZSTD_getErrorCode(nbSeqs) == ZSTD_error_dstSize_tooSmall) {
        FUZZ_ASSERT(seqsCapacity < ZSTD_sequenceBound(size));
    }
  } else {
    /* Ensure we round trip with and without block delimiters*/

    FUZZ_ZASSERT(ZSTD_CCtx_setParameter(cctx, ZSTD_c_blockDelimiters, ZSTD_sf_explicitBlockDelimiters));
    testRoundTrip(cctx, seqs, nbSeqs, data, size);

    const size_t nbMergedSeqs = ZSTD_mergeBlockDelimiters(seqs, nbSeqs);
    FUZZ_ASSERT(nbMergedSeqs <= nbSeqs);
    FUZZ_ZASSERT(ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only));
    FUZZ_ZASSERT(ZSTD_CCtx_setParameter(cctx, ZSTD_c_blockDelimiters, ZSTD_sf_noBlockDelimiters));
    testRoundTrip(cctx, seqs, nbMergedSeqs, data, size);
  }

  free(seqs);
  ZSTD_freeCCtx(cctx);
  FUZZ_dataProducer_free(producer);
  return 0;
}