Coverage Report

Created: 2025-06-15 00:57

/src/cmp_tool/test/fuzz/fuzz_data_producer.h
Line
Count
Source
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.BSD-3.Zstandard file in the 3rdparty_licenses directory) and the GPLv2
7
 * (found in the LICENSE.GPL-2 file in the 3rdparty_licenses directory).
8
 * You may select, at your option, one of the above-listed licenses.
9
 */
10
11
/**
12
 * Modifications made by
13
 * @author Dominik Loidolt (dominik.loidolt@univie.ac.at)
14
 * @date 2024
15
 *
16
 * - Added function providing a cmp_par struct
17
 *
18
 * Modifications are also licensed under the same license for consistency
19
 */
20
21
/**
22
 * Helper APIs for generating random data from input data stream.
23
 The producer reads bytes from the end of the input and appends them together
24
 to generate  a random number in the requested range. If it runs out of input
25
 data, it will keep returning the same value (min) over and over again.
26
27
 */
28
29
#ifndef FUZZ_DATA_PRODUCER_H
30
#define FUZZ_DATA_PRODUCER_H
31
32
33
#include <stddef.h>
34
#include <stdint.h>
35
#include <stdlib.h>
36
37
#include <cmp_chunk.h>
38
39
40
#ifdef __APPLE__
41
#  define FUZZ_TMP_DIR "/tmp"
42
#else
43
8.78k
#  define FUZZ_TMP_DIR "/dev/shm"
44
#endif
45
46
47
/* Struct used for maintaining the state of the data */
48
typedef struct FUZZ_dataProducer_s FUZZ_dataProducer_t;
49
50
/* Returns a data producer state struct. Use for producer initialization. */
51
FUZZ_dataProducer_t *FUZZ_dataProducer_create(const uint8_t *data, size_t size);
52
53
/* Frees the data producer */
54
void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer);
55
56
/* Returns value between [min, max] */
57
uint32_t FUZZ_dataProducer_uint32Range(FUZZ_dataProducer_t *producer, uint32_t min,
58
          uint32_t max);
59
60
/* Returns a uint32 value */
61
uint32_t FUZZ_dataProducer_uint32(FUZZ_dataProducer_t *producer);
62
63
/* Returns a signed value between [min, max] */
64
int32_t FUZZ_dataProducer_int32Range(FUZZ_dataProducer_t *producer,
65
            int32_t min, int32_t max);
66
67
/* Provides compression parameters */
68
void FUZZ_dataProducer_cmp_par(FUZZ_dataProducer_t *producer, struct cmp_par *cmp_par);
69
70
/* Returns the size of the remaining bytes of data in the producer */
71
size_t FUZZ_dataProducer_remainingBytes(FUZZ_dataProducer_t *producer);
72
73
/* Rolls back the data producer state to have remainingBytes remaining */
74
void FUZZ_dataProducer_rollBack(FUZZ_dataProducer_t *producer, size_t remainingBytes);
75
76
/* Returns true if the data producer is out of bytes */
77
int FUZZ_dataProducer_empty(FUZZ_dataProducer_t *producer);
78
79
/* Restricts the producer to only the last newSize bytes of data.
80
 * If newSize > current data size, nothing happens. Returns the number of bytes
81
 * the producer won't use anymore, after contracting.
82
 */
83
size_t FUZZ_dataProducer_contract(FUZZ_dataProducer_t *producer, size_t newSize);
84
85
/* Restricts the producer to use only the last X bytes of data, where X is a
86
 * random number in the interval [0, data_size]. Returns the size of the
87
 * remaining data the producer won't use anymore (the prefix).
88
 */
89
size_t FUZZ_dataProducer_reserveDataPrefix(FUZZ_dataProducer_t *producer);
90
91
92
#endif // FUZZ_DATA_PRODUCER_H