Coverage Report

Created: 2023-03-26 06:13

/src/lz4/ossfuzz/fuzz_helpers.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2016-present, Facebook, Inc.
3
 * All rights reserved.
4
 *
5
 * This source code is licensed under both the BSD-style license (found in the
6
 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7
 * in the COPYING file in the root directory of this source tree),
8
 * meaning you may select, at your option, one of the above-listed licenses.
9
 */
10
11
/**
12
 * Helper functions for fuzzing.
13
 */
14
15
#ifndef FUZZ_HELPERS_H
16
#define FUZZ_HELPERS_H
17
18
#include "fuzz.h"
19
#include "xxhash.h"
20
#include <stdint.h>
21
#include <stdio.h>
22
#include <stdlib.h>
23
24
#ifdef __cplusplus
25
extern "C" {
26
#endif
27
28
#define LZ4_COMMONDEFS_ONLY
29
#ifndef LZ4_SRC_INCLUDED
30
#include "lz4.c"   /* LZ4_count, constants, mem */
31
#endif
32
33
#define MIN(a,b)   ( (a) < (b) ? (a) : (b) )
34
1.04k
#define MAX(a,b)   ( (a) > (b) ? (a) : (b) )
35
36
0
#define FUZZ_QUOTE_IMPL(str) #str
37
0
#define FUZZ_QUOTE(str) FUZZ_QUOTE_IMPL(str)
38
39
/**
40
 * Asserts for fuzzing that are always enabled.
41
 */
42
#define FUZZ_ASSERT_MSG(cond, msg)                                             \
43
3.13k
  ((cond) ? (void)0                                                            \
44
3.13k
          : (fprintf(stderr, "%s: %u: Assertion: `%s' failed. %s\n", __FILE__, \
45
0
                     __LINE__, FUZZ_QUOTE(cond), (msg)),                       \
46
0
             abort()))
47
3.13k
#define FUZZ_ASSERT(cond) FUZZ_ASSERT_MSG((cond), "");
48
49
#if defined(__GNUC__)
50
#define FUZZ_STATIC static __inline __attribute__((unused))
51
#elif defined(__cplusplus) ||                                                  \
52
    (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
53
#define FUZZ_STATIC static inline
54
#elif defined(_MSC_VER)
55
#define FUZZ_STATIC static __inline
56
#else
57
#define FUZZ_STATIC static
58
#endif
59
60
/**
61
 * Deterministically constructs a seed based on the fuzz input.
62
 * Consumes up to the first FUZZ_RNG_SEED_SIZE bytes of the input.
63
 */
64
0
FUZZ_STATIC uint32_t FUZZ_seed(uint8_t const **src, size_t* size) {
65
0
    uint8_t const *data = *src;
66
0
    size_t const toHash = MIN(FUZZ_RNG_SEED_SIZE, *size);
67
0
    *size -= toHash;
68
0
    *src += toHash;
69
0
    return XXH32(data, toHash, 0);
70
0
}
Unexecuted instantiation: decompress_fuzzer.c:FUZZ_seed
Unexecuted instantiation: lz4_helpers.c:FUZZ_seed
Unexecuted instantiation: fuzz_data_producer.c:FUZZ_seed
71
72
0
#define FUZZ_rotl32(x, r) (((x) << (r)) | ((x) >> (32 - (r))))
73
74
0
FUZZ_STATIC uint32_t FUZZ_rand(uint32_t *state) {
75
0
    static const uint32_t prime1 = 2654435761U;
76
0
    static const uint32_t prime2 = 2246822519U;
77
0
    uint32_t rand32 = *state;
78
0
    rand32 *= prime1;
79
0
    rand32 += prime2;
80
0
    rand32 = FUZZ_rotl32(rand32, 13);
81
0
    *state = rand32;
82
0
    return rand32 >> 5;
83
0
}
Unexecuted instantiation: decompress_fuzzer.c:FUZZ_rand
Unexecuted instantiation: lz4_helpers.c:FUZZ_rand
Unexecuted instantiation: fuzz_data_producer.c:FUZZ_rand
84
85
/* Returns a random number in the range [min, max]. */
86
0
FUZZ_STATIC uint32_t FUZZ_rand32(uint32_t *state, uint32_t min, uint32_t max) {
87
0
    uint32_t random = FUZZ_rand(state);
88
0
    return min + (random % (max - min + 1));
89
0
}
Unexecuted instantiation: decompress_fuzzer.c:FUZZ_rand32
Unexecuted instantiation: lz4_helpers.c:FUZZ_rand32
Unexecuted instantiation: fuzz_data_producer.c:FUZZ_rand32
90
91
#ifdef __cplusplus
92
}
93
#endif
94
95
#endif