fuzz_helpers.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2016-2021, 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. * You may select, at your option, one of the above-listed licenses.
  9. */
  10. /**
  11. * Helper functions for fuzzing.
  12. */
  13. #ifndef FUZZ_HELPERS_H
  14. #define FUZZ_HELPERS_H
  15. #include "debug.h"
  16. #include "fuzz.h"
  17. #include "xxhash.h"
  18. #include "zstd.h"
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  26. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  27. #define FUZZ_QUOTE_IMPL(str) #str
  28. #define FUZZ_QUOTE(str) FUZZ_QUOTE_IMPL(str)
  29. /**
  30. * Asserts for fuzzing that are always enabled.
  31. */
  32. #define FUZZ_ASSERT_MSG(cond, msg) \
  33. ((cond) ? (void)0 \
  34. : (fprintf(stderr, "%s: %u: Assertion: `%s' failed. %s\n", __FILE__, \
  35. __LINE__, FUZZ_QUOTE(cond), (msg)), \
  36. abort()))
  37. #define FUZZ_ASSERT(cond) FUZZ_ASSERT_MSG((cond), "");
  38. #define FUZZ_ZASSERT(code) \
  39. FUZZ_ASSERT_MSG(!ZSTD_isError(code), ZSTD_getErrorName(code))
  40. #if defined(__GNUC__)
  41. #define FUZZ_STATIC static __inline __attribute__((unused))
  42. #elif defined(__cplusplus) || \
  43. (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
  44. #define FUZZ_STATIC static inline
  45. #elif defined(_MSC_VER)
  46. #define FUZZ_STATIC static __inline
  47. #else
  48. #define FUZZ_STATIC static
  49. #endif
  50. /**
  51. * malloc except return NULL for zero sized data and FUZZ_ASSERT
  52. * that malloc doesn't fail.
  53. */
  54. void* FUZZ_malloc(size_t size);
  55. /**
  56. * memcmp but accepts NULL.
  57. */
  58. int FUZZ_memcmp(void const* lhs, void const* rhs, size_t size);
  59. #ifdef __cplusplus
  60. }
  61. #endif
  62. #endif