Coverage Report

Created: 2025-07-11 06:33

/src/zstd/tests/fuzz/fuzz_helpers.h
Line
Count
Source (jump to first uncovered line)
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 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
 * 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 "debug.h"
19
#include "fuzz.h"
20
#include "xxhash.h"
21
#include "zstd.h"
22
#include "fuzz_data_producer.h"
23
#include <stdint.h>
24
#include <stdio.h>
25
#include <stdlib.h>
26
27
#ifdef __cplusplus
28
extern "C" {
29
#endif
30
31
0
#define MIN(a, b) ((a) < (b) ? (a) : (b))
32
19.4k
#define MAX(a, b) ((a) > (b) ? (a) : (b))
33
34
0
#define FUZZ_QUOTE_IMPL(str) #str
35
0
#define FUZZ_QUOTE(str) FUZZ_QUOTE_IMPL(str)
36
37
/**
38
 * Asserts for fuzzing that are always enabled.
39
 */
40
#define FUZZ_ASSERT_MSG(cond, msg)                                             \
41
1.31M
  ((cond) ? (void)0                                                            \
42
1.31M
          : (fprintf(stderr, "%s: %u: Assertion: `%s' failed. %s\n", __FILE__, \
43
0
                     __LINE__, FUZZ_QUOTE(cond), (msg)),                       \
44
0
             abort()))
45
1.30M
#define FUZZ_ASSERT(cond) FUZZ_ASSERT_MSG((cond), "");
46
#define FUZZ_ZASSERT(code)                                                     \
47
5.02k
  FUZZ_ASSERT_MSG(!ZSTD_isError(code), ZSTD_getErrorName(code))
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
 * malloc except return NULL for zero sized data and FUZZ_ASSERT
62
 * that malloc doesn't fail.
63
 */
64
void* FUZZ_malloc(size_t size);
65
66
/**
67
 * malloc except returns random pointer for zero sized data and FUZZ_ASSERT
68
 * that malloc doesn't fail.
69
 * WARNING: Only free the returned pointer if size > 0!
70
 */
71
void* FUZZ_malloc_rand(size_t size,  FUZZ_dataProducer_t *producer);
72
73
/**
74
 * memcmp but accepts NULL.
75
 */
76
int FUZZ_memcmp(void const* lhs, void const* rhs, size_t size);
77
78
#ifdef __cplusplus
79
}
80
#endif
81
82
#endif