generate_seed_corpus.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python
  2. # Copyright 2019 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. from __future__ import division
  6. from __future__ import print_function
  7. from array import array
  8. import os
  9. import random
  10. import shutil
  11. import sys
  12. MAX_INPUT_SIZE = 5000 # < 1 MB so we don't blow up fuzzer build sizes.
  13. MAX_FLOAT32 = 3.4028235e+38
  14. def IsValidSize(n):
  15. if n == 0:
  16. return False
  17. # PFFFT only supports transforms for inputs of length N of the form
  18. # N = (2^a)*(3^b)*(5^c) where a >= 5, b >=0, c >= 0.
  19. FACTORS = [2, 3, 5]
  20. factorization = [0, 0, 0]
  21. for i, factor in enumerate(FACTORS):
  22. while n % factor == 0:
  23. n = n // factor
  24. factorization[i] += 1
  25. return factorization[0] >= 5 and n == 1
  26. def WriteFloat32ArrayToFile(file_path, size, generator):
  27. """Generate an array of float32 values and writes to file."""
  28. with open(file_path, 'wb') as f:
  29. float_array = array('f', [generator() for _ in range(size)])
  30. float_array.tofile(f)
  31. def main():
  32. if len(sys.argv) < 2:
  33. print('Usage: %s <path to output directory>' % sys.argv[0])
  34. sys.exit(1)
  35. output_path = sys.argv[1]
  36. # Start with a clean output directory.
  37. if os.path.exists(output_path):
  38. shutil.rmtree(output_path)
  39. os.makedirs(output_path)
  40. # List of valid input sizes.
  41. N = [n for n in range(MAX_INPUT_SIZE) if IsValidSize(n)]
  42. # Set the seed to always generate the same random data.
  43. random.seed(0)
  44. # Generate different types of input arrays for each target length.
  45. for n in N:
  46. # Zeros.
  47. WriteFloat32ArrayToFile(
  48. os.path.join(output_path, 'zeros_%d' % n), n, lambda: 0)
  49. # Max float 32.
  50. WriteFloat32ArrayToFile(
  51. os.path.join(output_path, 'max_%d' % n), n, lambda: MAX_FLOAT32)
  52. # Random values in the s16 range.
  53. rnd_s16 = lambda: 32768.0 * 2.0 * (random.random() - 0.5)
  54. WriteFloat32ArrayToFile(
  55. os.path.join(output_path, 'rnd_s16_%d' % n), n, rnd_s16)
  56. sys.exit(0)
  57. if __name__ == '__main__':
  58. main()