1 // Copyright 2020 The libgav1 Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://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,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <cstddef>
16 #include <cstdint>
17 #include <deque>
18 #include <memory>
19 #include <vector>
20
21 #include "examples/file_reader.h"
22 #include "examples/file_reader_constants.h"
23 #include "examples/file_reader_interface.h"
24 #include "src/gav1/decoder.h"
25 #include "src/gav1/status_code.h"
26 #include "tests/fuzzer/fuzzer_temp_file.h"
27
28 namespace {
29
30 #if defined(LIBGAV1_EXHAUSTIVE_FUZZING)
31 // Set a large upper bound to give more coverage of a single input; this value
32 // should be larger than most of the frame counts in the corpus.
33 constexpr size_t kMaxDataSize = 400 * 1024;
34 #else
35 constexpr size_t kMaxDataSize = 200 * 1024;
36 #endif
37
38 using InputBuffer = std::vector<uint8_t>;
39
40 struct InputBuffers {
~InputBuffers__anon424c2c270111::InputBuffers41 ~InputBuffers() {
42 for (auto& buffer : free_buffers) {
43 delete buffer;
44 }
45 }
46 std::deque<InputBuffer*> free_buffers;
47 };
48
ReleaseInputBuffer(void * callback_private_data,void * buffer_private_data)49 void ReleaseInputBuffer(void* callback_private_data,
50 void* buffer_private_data) {
51 auto* const test = static_cast<InputBuffers*>(callback_private_data);
52 test->free_buffers.push_back(static_cast<InputBuffer*>(buffer_private_data));
53 }
54
55 } // namespace
56
57 // Always returns 0. Nonzero return values are reserved by libFuzzer for future
58 // use.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)59 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
60 // Reject large chunks of data to improve fuzzer throughput.
61 if (size > kMaxDataSize) return 0;
62
63 // Note that |input_buffers| has to outlive the |decoder| object since the
64 // |release_input_buffer| callback could be called on the |decoder|'s
65 // destructor.
66 InputBuffers input_buffers;
67
68 libgav1::Decoder decoder;
69 libgav1::DecoderSettings settings = {};
70 // Use the 33 + low byte of the width to seed the number of threads. This
71 // ensures that we will trigger the frame parallel path in most cases.
72 // We use both nibbles of the lower byte as this results in values != 1 much
73 // more quickly than using the lower nibble alone.
74 settings.threads =
75 33 + ((size >= 13) ? ((data[12] >> 4 | data[12]) & 0xF) + 1 : 1);
76
77 settings.frame_parallel = true;
78 settings.blocking_dequeue = true;
79 settings.callback_private_data = &input_buffers;
80 settings.release_input_buffer = ReleaseInputBuffer;
81 if (decoder.Init(&settings) != libgav1::kStatusOk) return 0;
82
83 FuzzerTemporaryFile tempfile(data, size);
84 auto file_reader =
85 libgav1::FileReader::Open(tempfile.filename(), /*error_tolerant=*/true);
86 if (file_reader == nullptr) return 0;
87
88 InputBuffer* input_buffer = nullptr;
89 bool dequeue_finished = false;
90
91 do {
92 if (input_buffer == nullptr && !file_reader->IsEndOfFile()) {
93 if (input_buffers.free_buffers.empty()) {
94 auto* const buffer = new (std::nothrow) InputBuffer();
95 if (buffer == nullptr) {
96 break;
97 }
98 input_buffers.free_buffers.push_back(buffer);
99 }
100 input_buffer = input_buffers.free_buffers.front();
101 input_buffers.free_buffers.pop_front();
102 if (!file_reader->ReadTemporalUnit(input_buffer, nullptr)) {
103 break;
104 }
105 }
106
107 if (input_buffer != nullptr) {
108 libgav1::StatusCode status =
109 decoder.EnqueueFrame(input_buffer->data(), input_buffer->size(),
110 /*user_private_data=*/0,
111 /*buffer_private_data=*/input_buffer);
112 if (status == libgav1::kStatusOk) {
113 input_buffer = nullptr;
114 // Continue to enqueue frames until we get a kStatusTryAgain status.
115 continue;
116 }
117 if (status != libgav1::kStatusTryAgain) {
118 break;
119 }
120 }
121
122 const libgav1::DecoderBuffer* buffer;
123 libgav1::StatusCode status = decoder.DequeueFrame(&buffer);
124 if (status == libgav1::kStatusNothingToDequeue) {
125 dequeue_finished = true;
126 } else if (status == libgav1::kStatusOk) {
127 dequeue_finished = false;
128 } else {
129 break;
130 }
131 } while (input_buffer != nullptr || !file_reader->IsEndOfFile() ||
132 !dequeue_finished);
133
134 if (input_buffer != nullptr) {
135 input_buffers.free_buffers.push_back(input_buffer);
136 }
137
138 return 0;
139 }
140