hpack_decoder_fuzzer.cc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2017 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 <stddef.h>
  5. #include <stdint.h>
  6. #include <fuzzer/FuzzedDataProvider.h>
  7. #include <list>
  8. #include <vector>
  9. #include "net/third_party/quiche/src/quiche/http2/hpack/decoder/hpack_decoder.h"
  10. // Entry point for LibFuzzer.
  11. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  12. // At least 4 bytes of fuzz data are needed to generate a max string size.
  13. if (size < 4)
  14. return 0;
  15. FuzzedDataProvider fuzzed_data_provider(data, size);
  16. size_t max_string_size =
  17. fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 10 * size);
  18. http2::HpackDecoder decoder(http2::HpackDecoderNoOpListener::NoOpListener(),
  19. max_string_size);
  20. decoder.StartDecodingBlock();
  21. // Store all chunks in a function scope list, as the API requires the caller
  22. // to make sure the fragment chunks data is accessible during the whole
  23. // decoding process. |http2::DecodeBuffer| does not copy the data, it is just
  24. // a wrapper for the chunk provided in its constructor.
  25. std::list<std::vector<char>> all_chunks;
  26. while (fuzzed_data_provider.remaining_bytes() > 0) {
  27. size_t chunk_size = fuzzed_data_provider.ConsumeIntegralInRange(1, 32);
  28. all_chunks.emplace_back(
  29. fuzzed_data_provider.ConsumeBytes<char>(chunk_size));
  30. const auto& chunk = all_chunks.back();
  31. // http2::DecodeBuffer constructor does not accept nullptr buffer.
  32. if (chunk.data() == nullptr)
  33. continue;
  34. http2::DecodeBuffer fragment(chunk.data(), chunk.size());
  35. decoder.DecodeFragment(&fragment);
  36. }
  37. decoder.EndDecodingBlock();
  38. return 0;
  39. }