pffft_fuzzer.cc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include <algorithm>
  5. #include <array>
  6. #include <cassert>
  7. #include <cstring>
  8. #include "third_party/pffft/src/pffft.h"
  9. namespace {
  10. #if defined(TRANSFORM_REAL)
  11. // Real FFT.
  12. constexpr pffft_transform_t kTransform = PFFFT_REAL;
  13. constexpr size_t kSizeOfOneSample = sizeof(float);
  14. #elif defined(TRANSFORM_COMPLEX)
  15. // Complex FFT.
  16. constexpr pffft_transform_t kTransform = PFFFT_COMPLEX;
  17. constexpr size_t kSizeOfOneSample = 2 * sizeof(float); // Real plus imaginary.
  18. #else
  19. #error FFT transform type not defined.
  20. #endif
  21. bool IsValidSize(size_t n) {
  22. if (n == 0) {
  23. return false;
  24. }
  25. // PFFFT only supports transforms for inputs of length N of the form
  26. // N = (2^a)*(3^b)*(5^c) where a >= 5, b >=0, c >= 0.
  27. constexpr std::array<int, 3> kFactors = {2, 3, 5};
  28. std::array<int, kFactors.size()> factorization{};
  29. for (size_t i = 0; i < kFactors.size(); ++i) {
  30. const int factor = kFactors[i];
  31. while (n % factor == 0) {
  32. n /= factor;
  33. factorization[i]++;
  34. }
  35. }
  36. return factorization[0] >= 5 && n == 1;
  37. }
  38. float* AllocatePffftBuffer(size_t number_of_bytes) {
  39. return static_cast<float*>(pffft_aligned_malloc(number_of_bytes));
  40. }
  41. } // namespace
  42. // Entry point for LibFuzzer.
  43. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  44. // Set the number of FFT points to use |data| as input vector.
  45. // The latter is truncated if the number of bytes is not an integer
  46. // multiple of the size of one sample (which is either a real or a complex
  47. // floating point number).
  48. const size_t fft_size = size / kSizeOfOneSample;
  49. if (!IsValidSize(fft_size)) {
  50. return 0;
  51. }
  52. const size_t number_of_bytes = fft_size * kSizeOfOneSample;
  53. assert(number_of_bytes <= size);
  54. // Allocate input and output buffers.
  55. float* in = AllocatePffftBuffer(number_of_bytes);
  56. float* out = AllocatePffftBuffer(number_of_bytes);
  57. // Copy input data.
  58. std::memcpy(in, reinterpret_cast<const float*>(data), number_of_bytes);
  59. // Setup FFT.
  60. PFFFT_Setup* pffft_setup = pffft_new_setup(fft_size, kTransform);
  61. // Call different PFFFT functions to maximize the coverage.
  62. pffft_transform(pffft_setup, in, out, nullptr, PFFFT_FORWARD);
  63. pffft_zconvolve_accumulate(pffft_setup, out, out, out, 1.f);
  64. pffft_transform_ordered(pffft_setup, in, out, nullptr, PFFFT_BACKWARD);
  65. // Release memory.
  66. pffft_aligned_free(in);
  67. pffft_aligned_free(out);
  68. pffft_destroy_setup(pffft_setup);
  69. return 0;
  70. }