1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
7 #pragma allow_unsafe_buffers
8 #endif
9
10 #include <stddef.h>
11 #include <stdint.h>
12
13 #include <random>
14 #include <string>
15
16 #include "libxml/parser.h"
17
18 #include "base/at_exit.h"
19 #include "base/command_line.h"
20 #include "base/functional/bind.h"
21 #include "base/i18n/icu_util.h"
22 #include "base/run_loop.h"
23 #include "base/task/single_thread_task_executor.h"
24 #include "components/search_engines/search_terms_data.h"
25 #include "components/search_engines/template_url.h"
26 #include "components/search_engines/template_url_parser.h"
27 #include "mojo/core/embedder/embedder.h"
28 #include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
29 #include "testing/libfuzzer/libfuzzer_exports.h"
30
PseudoRandomFilter(std::mt19937 * generator,std::uniform_int_distribution<uint16_t> * pool,const std::string &,const std::string &)31 bool PseudoRandomFilter(std::mt19937* generator,
32 std::uniform_int_distribution<uint16_t>* pool,
33 const std::string&,
34 const std::string&) {
35 // Return true 254/255 times, ie: as if pool only returned uint8_t.
36 return (*pool)(*generator) % (UINT8_MAX + 1);
37 }
38
39 struct FuzzerFixedParams {
40 uint32_t seed_;
41 };
42
43 base::AtExitManager at_exit_manager; // used by ICU integration
44
LLVMFuzzerInitialize(int * argc,char *** argv)45 extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) {
46 CHECK(base::i18n::InitializeICU());
47 CHECK(base::CommandLine::Init(*argc, *argv));
48 return 0;
49 }
50
ignore(void * ctx,const char * msg,...)51 void ignore(void* ctx, const char* msg, ...) {
52 // Error handler to avoid error message spam from libxml parser.
53 }
54
55 class Env {
56 public:
Env()57 Env() : executor_(base::MessagePumpType::IO) {
58 mojo::core::Init();
59 xmlSetGenericErrorFunc(nullptr, &ignore);
60 }
61
62 private:
63 base::SingleThreadTaskExecutor executor_;
64 data_decoder::test::InProcessDataDecoder data_decoder_;
65 };
66
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)67 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
68 static Env env;
69 if (size < sizeof(FuzzerFixedParams)) {
70 return 0;
71 }
72
73 const FuzzerFixedParams* params =
74 reinterpret_cast<const FuzzerFixedParams*>(data);
75 size -= sizeof(FuzzerFixedParams);
76
77 std::mt19937 generator(params->seed_);
78 // Use a uint16_t here instead of uint8_t because uniform_int_distribution
79 // does not support 8 bit types on Windows.
80 std::uniform_int_distribution<uint16_t> pool(0, 1);
81
82 base::RunLoop run_loop;
83
84 SearchTermsData search_terms_data;
85 std::string string_data(reinterpret_cast<const char*>(params + 1), size);
86 TemplateURLParser::ParameterFilter filter =
87 base::BindRepeating(&PseudoRandomFilter, base::Unretained(&generator),
88 base::Unretained(&pool));
89 TemplateURLParser::Parse(&search_terms_data, string_data, filter,
90 base::BindOnce(
91 [](base::OnceClosure quit_closure,
92 std::unique_ptr<TemplateURL> ignored) {
93 std::move(quit_closure).Run();
94 },
95 run_loop.QuitClosure()));
96
97 run_loop.Run();
98
99 return 0;
100 }
101