template_url_parser_fuzzer.cc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2016 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 <random>
  7. #include <string>
  8. #include "libxml/parser.h"
  9. #include "base/at_exit.h"
  10. #include "base/bind.h"
  11. #include "base/command_line.h"
  12. #include "base/i18n/icu_util.h"
  13. #include "base/run_loop.h"
  14. #include "base/task/single_thread_task_executor.h"
  15. #include "components/search_engines/search_terms_data.h"
  16. #include "components/search_engines/template_url.h"
  17. #include "components/search_engines/template_url_parser.h"
  18. #include "mojo/core/embedder/embedder.h"
  19. #include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
  20. #include "testing/libfuzzer/libfuzzer_exports.h"
  21. bool PseudoRandomFilter(std::mt19937* generator,
  22. std::uniform_int_distribution<uint16_t>* pool,
  23. const std::string&,
  24. const std::string&) {
  25. // Return true 254/255 times, ie: as if pool only returned uint8_t.
  26. return (*pool)(*generator) % (UINT8_MAX + 1);
  27. }
  28. struct FuzzerFixedParams {
  29. uint32_t seed_;
  30. };
  31. base::AtExitManager at_exit_manager; // used by ICU integration
  32. extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) {
  33. CHECK(base::i18n::InitializeICU());
  34. CHECK(base::CommandLine::Init(*argc, *argv));
  35. return 0;
  36. }
  37. void ignore(void* ctx, const char* msg, ...) {
  38. // Error handler to avoid error message spam from libxml parser.
  39. }
  40. class Env {
  41. public:
  42. Env() : executor_(base::MessagePumpType::IO) {
  43. mojo::core::Init();
  44. xmlSetGenericErrorFunc(nullptr, &ignore);
  45. }
  46. private:
  47. base::SingleThreadTaskExecutor executor_;
  48. data_decoder::test::InProcessDataDecoder data_decoder_;
  49. };
  50. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  51. static Env env;
  52. if (size < sizeof(FuzzerFixedParams)) {
  53. return 0;
  54. }
  55. const FuzzerFixedParams* params =
  56. reinterpret_cast<const FuzzerFixedParams*>(data);
  57. size -= sizeof(FuzzerFixedParams);
  58. std::mt19937 generator(params->seed_);
  59. // Use a uint16_t here instead of uint8_t because uniform_int_distribution
  60. // does not support 8 bit types on Windows.
  61. std::uniform_int_distribution<uint16_t> pool(0, 1);
  62. base::RunLoop run_loop;
  63. SearchTermsData search_terms_data;
  64. std::string string_data(reinterpret_cast<const char*>(params + 1), size);
  65. TemplateURLParser::ParameterFilter filter =
  66. base::BindRepeating(&PseudoRandomFilter, base::Unretained(&generator),
  67. base::Unretained(&pool));
  68. TemplateURLParser::Parse(&search_terms_data, string_data, filter,
  69. base::BindOnce(
  70. [](base::OnceClosure quit_closure,
  71. std::unique_ptr<TemplateURL> ignored) {
  72. std::move(quit_closure).Run();
  73. },
  74. run_loop.QuitClosure()));
  75. run_loop.Run();
  76. return 0;
  77. }