blob: 0fb5d57299897ff9dd9a8f99f9b9f92aba6f4809 [file] [log] [blame]
karthik bharadwaj2ee244b2020-04-16 14:08:22 -07001// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15// This file implements a basic fuzz test for the TokenDatabase class
16// A database is created from fuzz data, and a random entry count (also
17// derived from the fuzz data) is set. We then run iterations and 'find'
18// operations on this database.
19
20#include <cstring>
21
22#include "pw_fuzzer/asan_interface.h"
23#include "pw_fuzzer/fuzzed_data_provider.h"
24#include "pw_preprocessor/util.h"
25#include "pw_span/span.h"
26#include "pw_tokenizer/token_database.h"
27
28namespace pw::tokenizer {
29namespace {
30
31enum FuzzTestType : uint8_t {
32 kValidHeader,
33 kRandomHeader,
34 kMaxValue = kRandomHeader,
35};
36
37constexpr size_t kTokenHeaderSize = 16;
38
39// The default max length in bytes of fuzzed data provided. Note that
40// this needs to change if the fuzzer executable is run with a
41// '-max_len' argument.
42constexpr size_t kFuzzDataSizeMax = 4096;
43
44// Location of the 'EntryCount' field in the token header.
45constexpr size_t kEntryCountOffset = 8;
46constexpr size_t kEntryCountSize = 4;
47
48void SetTokenEntryCountInBuffer(uint8_t* buffer, uint32_t count) {
49 memcpy(buffer + kEntryCountOffset, &count, kEntryCountSize);
50}
51
52void IterateOverDatabase(TokenDatabase* const database) {
53 for (TokenDatabase::Entry entry : *database) {
54 // Since we don't "use" the contents of the entry, we exercise
55 // the entry by extracting its contents into volatile variables
56 // to prevent it from being optimized out during compilation.
57 volatile const char* entry_string = entry.string;
58 volatile uint32_t entry_token = entry.token;
59 PW_UNUSED(entry_string);
60 PW_UNUSED(entry_token);
61 }
62}
karthik bharadwaj8f535362020-05-04 13:40:46 -070063
karthik bharadwaj2ee244b2020-04-16 14:08:22 -070064} // namespace
65
66extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
67 constexpr size_t kBufferSizeMax = kFuzzDataSizeMax + kTokenHeaderSize;
68 constexpr char kDefaultHeader[] = "TOKENS\0\0\0\0\0\0\0\0\0";
69 static uint8_t buffer[kBufferSizeMax];
70
71 if (size > kFuzzDataSizeMax) {
72 return 0;
73 }
74
75 FuzzedDataProvider provider(data, size);
76
77 // Initialize the token header with either a valid or invalid header
78 // based on a random enum consumed from the fuzz data.
79 switch (provider.ConsumeEnum<FuzzTestType>()) {
80 case kValidHeader:
81 memcpy(buffer, kDefaultHeader, kTokenHeaderSize);
82 break;
83
84 case kRandomHeader: {
85 std::vector<uint8_t> random_header =
86 provider.ConsumeBytes<uint8_t>(kTokenHeaderSize);
87 random_header.resize(kTokenHeaderSize);
88 memcpy(buffer, &random_header[0], kTokenHeaderSize);
89 break;
90 }
91 }
92
93 // Consume a 'test token' integer to look up later in the database.
94 uint32_t random_token = provider.ConsumeIntegral<uint32_t>();
95
96 // Consume a 'token count' integer to set as our database entry count.
97 uint32_t random_token_count =
98 provider.ConsumeIntegralInRange<uint32_t>(0, kFuzzDataSizeMax);
99
100 // Consume the remaining data. Note that the data corresponding to the
101 // string entries in the database are not explicitly null-terminated.
karthik bharadwaj8f535362020-05-04 13:40:46 -0700102 // TODO(karthikmb): Once OSS-Fuzz updates to Clang11.0, switch to
103 // provider.ConsumeData() to avoid extra memory and the memcpy call.
104 auto consumed_bytes =
105 provider.ConsumeBytes<uint8_t>(provider.remaining_bytes());
106 memcpy(buffer + kTokenHeaderSize, &consumed_bytes[0], consumed_bytes.size());
karthik bharadwaj2ee244b2020-04-16 14:08:22 -0700107
108 SetTokenEntryCountInBuffer(buffer, random_token_count);
109
110 // Poison the unused buffer space for this run of the fuzzer to
111 // prevent the token database creator from reading too far in.
karthik bharadwaj8f535362020-05-04 13:40:46 -0700112 size_t data_size = kTokenHeaderSize + consumed_bytes.size();
karthik bharadwaj2ee244b2020-04-16 14:08:22 -0700113 size_t poisoned_length = kBufferSizeMax - data_size;
114 void* poisoned = &buffer[data_size];
115
116 ASAN_POISON_MEMORY_REGION(poisoned, poisoned_length);
117
118 // We create a database from a span of the buffer since the string
119 // entries might not be null terminated, and the creation of a database
120 // from a raw buffer has an explicit null terminated string requirement
121 // specified in the API.
122 span<uint8_t> data_span(buffer, data_size);
123 auto token_database = TokenDatabase::Create<span<uint8_t>>(data_span);
124 volatile auto match = token_database.Find(random_token);
125 PW_UNUSED(match);
126
127 IterateOverDatabase(&token_database);
128
129 // Un-poison for the next iteration.
130 ASAN_UNPOISON_MEMORY_REGION(poisoned, poisoned_length);
131
132 return 0;
133}
134
135} // namespace pw::tokenizer