fuzz_data_helper.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef TEST_FUZZERS_FUZZ_DATA_HELPER_H_
  11. #define TEST_FUZZERS_FUZZ_DATA_HELPER_H_
  12. #include <limits>
  13. #include "api/array_view.h"
  14. #include "modules/rtp_rtcp/source/byte_io.h"
  15. namespace webrtc {
  16. namespace test {
  17. // Helper class to take care of the fuzzer input, read from it, and keep track
  18. // of when the end of the data has been reached.
  19. class FuzzDataHelper {
  20. public:
  21. explicit FuzzDataHelper(rtc::ArrayView<const uint8_t> data);
  22. // Returns true if n bytes can be read.
  23. bool CanReadBytes(size_t n) const { return data_ix_ + n <= data_.size(); }
  24. // Reads and returns data of type T.
  25. template <typename T>
  26. T Read() {
  27. RTC_CHECK(CanReadBytes(sizeof(T)));
  28. T x = ByteReader<T>::ReadLittleEndian(&data_[data_ix_]);
  29. data_ix_ += sizeof(T);
  30. return x;
  31. }
  32. // Reads and returns data of type T. Returns default_value if not enough
  33. // fuzzer input remains to read a T.
  34. template <typename T>
  35. T ReadOrDefaultValue(T default_value) {
  36. if (!CanReadBytes(sizeof(T))) {
  37. return default_value;
  38. }
  39. return Read<T>();
  40. }
  41. // Like ReadOrDefaultValue, but replaces the value 0 with default_value.
  42. template <typename T>
  43. T ReadOrDefaultValueNotZero(T default_value) {
  44. static_assert(std::is_integral<T>::value, "");
  45. T x = ReadOrDefaultValue(default_value);
  46. return x == 0 ? default_value : x;
  47. }
  48. // Returns one of the elements from the provided input array. The selection
  49. // is based on the fuzzer input data. If not enough fuzzer data is available,
  50. // the method will return the first element in the input array. The reason for
  51. // not flagging this as an error is to allow the method to be called from
  52. // class constructors, and in constructors we typically do not handle
  53. // errors. The code will work anyway, and the fuzzer will likely see that
  54. // providing more data will actually make this method return something else.
  55. template <typename T, size_t N>
  56. T SelectOneOf(const T (&select_from)[N]) {
  57. static_assert(N <= std::numeric_limits<uint8_t>::max(), "");
  58. // Read an index between 0 and select_from.size() - 1 from the fuzzer data.
  59. uint8_t index = ReadOrDefaultValue<uint8_t>(0) % N;
  60. return select_from[index];
  61. }
  62. rtc::ArrayView<const uint8_t> ReadByteArray(size_t bytes) {
  63. if (!CanReadBytes(bytes)) {
  64. return rtc::ArrayView<const uint8_t>(nullptr, 0);
  65. }
  66. const size_t index_to_return = data_ix_;
  67. data_ix_ += bytes;
  68. return data_.subview(index_to_return, bytes);
  69. }
  70. // If sizeof(T) > BytesLeft then the remaining bytes will be used and the rest
  71. // of the object will be zero initialized.
  72. template <typename T>
  73. void CopyTo(T* object) {
  74. memset(object, 0, sizeof(T));
  75. size_t bytes_to_copy = std::min(BytesLeft(), sizeof(T));
  76. memcpy(object, data_.data() + data_ix_, bytes_to_copy);
  77. data_ix_ += bytes_to_copy;
  78. }
  79. size_t BytesRead() const { return data_ix_; }
  80. size_t BytesLeft() const { return data_.size() - data_ix_; }
  81. private:
  82. rtc::ArrayView<const uint8_t> data_;
  83. size_t data_ix_ = 0;
  84. };
  85. } // namespace test
  86. } // namespace webrtc
  87. #endif // TEST_FUZZERS_FUZZ_DATA_HELPER_H_