1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
media / test / pipeline_integration_fuzzertest.cc [blame]
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include <stddef.h>
#include <stdint.h>
#include <vector>
#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/task/bind_post_task.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/test_timeouts.h"
#include "media/base/eme_constants.h"
#include "media/base/media.h"
#include "media/base/media_switches.h"
#include "media/base/pipeline_status.h"
#include "media/base/test_data_util.h"
#include "media/media_buildflags.h"
#include "media/test/pipeline_integration_test_base.h"
#include "media/test/test_media_source.h"
#include "third_party/googletest/src/googletest/src/gtest-internal-inl.h"
namespace {
// Keep these aligned with BUILD.gn's pipeline_integration_fuzzer_variants
enum FuzzerVariant {
SRC,
WEBM_OPUS,
WEBM_VORBIS,
WEBM_VP8,
WEBM_VP9,
WEBM_OPUS_VP9,
#if BUILDFLAG(ENABLE_AV1_DECODER)
MP4_AV1,
#endif
MP4_FLAC,
MP4_OPUS,
MP3,
#if BUILDFLAG(USE_PROPRIETARY_CODECS)
ADTS,
MP4_AACLC,
MP4_AACSBR,
MP4_AVC1,
MP4_AACLC_AVC,
#if BUILDFLAG(ENABLE_MSE_MPEG2TS_STREAM_PARSER)
MP2T_AACLC,
MP2T_AACSBR,
MP2T_AVC,
MP2T_MP3,
MP2T_AACLC_AVC,
#endif // BUILDFLAG(ENABLE_MSE_MPEG2TS_STREAM_PARSER)
#endif // BUILDFLAG(USE_PROPRIETARY_CODECS)
};
std::string MseFuzzerVariantEnumToMimeTypeString(FuzzerVariant variant) {
switch (variant) {
case WEBM_OPUS:
return "audio/webm; codecs=\"opus\"";
case WEBM_VORBIS:
return "audio/webm; codecs=\"vorbis\"";
case WEBM_VP8:
return "video/webm; codecs=\"vp8\"";
case WEBM_VP9:
return "video/webm; codecs=\"vp9\"";
case WEBM_OPUS_VP9:
return "video/webm; codecs=\"opus,vp9\"";
#if BUILDFLAG(ENABLE_AV1_DECODER)
case MP4_AV1:
return "video/mp4; codecs=\"av01.0.04M.08\"";
#endif // BUILDFLAG(ENABLE_AV1_DECODER)
case MP4_FLAC:
return "audio/mp4; codecs=\"flac\"";
case MP4_OPUS:
return "audio/mp4; codecs=\"opus\"";
case MP3:
return "audio/mpeg";
#if BUILDFLAG(USE_PROPRIETARY_CODECS)
case ADTS:
return "audio/aac";
case MP4_AACLC:
return "audio/mp4; codecs=\"mp4a.40.2\"";
case MP4_AACSBR:
return "audio/mp4; codecs=\"mp4a.40.5\"";
case MP4_AVC1:
return "video/mp4; codecs=\"avc1.42E01E\"";
case MP4_AACLC_AVC:
return "video/mp4; codecs=\"mp4a.40.2,avc1.42E01E\"";
#if BUILDFLAG(ENABLE_MSE_MPEG2TS_STREAM_PARSER)
case MP2T_AACLC:
return "video/mp2t; codecs=\"mp4a.67\"";
case MP2T_AACSBR:
return "video/mp2t; codecs=\"mp4a.40.5\"";
case MP2T_AVC:
return "video/mp2t; codecs=\"avc1.42E01E\"";
case MP2T_MP3:
// Note, "mp4a.6B" appears to be an equivalent codec substring.
return "video/mp2t; codecs=\"mp4a.69\"";
case MP2T_AACLC_AVC:
return "video/mp2t; codecs=\"mp4a.40.2,avc1.42E01E\"";
#endif // BUILDFLAG(ENABLE_MSE_MPEG2TS_STREAM_PARSER)
#endif // BUILDFLAG(USE_PROPRIETARY_CODECS)
case SRC:
NOTREACHED() << "SRC is an invalid MSE fuzzer variant";
}
}
} // namespace
namespace media {
// Limit the amount of initial (or post-seek) audio silence padding allowed in
// rendering of fuzzed input.
constexpr base::TimeDelta kMaxPlayDelay = base::Seconds(10);
void OnEncryptedMediaInitData(media::PipelineIntegrationTestBase* test,
media::EmeInitDataType /* type */,
const std::vector<uint8_t>& /* init_data */) {
// Encrypted media is not supported in this test. For an encrypted media file,
// we will start demuxing the data but media pipeline will wait for a CDM to
// be available to start initialization, which will not happen in this case.
// To prevent the test timeout, we'll just fail the test immediately here.
// Note: Since the callback is on the media task runner but the test is on
// the main task runner, this must be posted.
// TODO(xhwang): Support encrypted media in this fuzzer test.
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&PipelineIntegrationTestBase::FailTest,
base::Unretained(test),
media::PIPELINE_ERROR_INITIALIZATION_FAILED));
}
void OnAudioPlayDelay(media::PipelineIntegrationTestBase* test,
base::TimeDelta play_delay) {
CHECK_GT(play_delay, base::TimeDelta());
if (play_delay > kMaxPlayDelay) {
// Note: Since the callback is on the media task runner but the test is on
// the main task runner, this must be posted.
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&PipelineIntegrationTestBase::FailTest,
base::Unretained(test),
media::PIPELINE_ERROR_INITIALIZATION_FAILED));
}
}
class ProgressivePipelineIntegrationFuzzerTest
: public PipelineIntegrationTestBase {
public:
ProgressivePipelineIntegrationFuzzerTest() {
set_encrypted_media_init_data_cb(
base::BindRepeating(&OnEncryptedMediaInitData, this));
set_audio_play_delay_cb(base::BindPostTaskToCurrentDefault(
base::BindRepeating(&OnAudioPlayDelay, this)));
}
~ProgressivePipelineIntegrationFuzzerTest() override = default;
void RunTest(const uint8_t* data, size_t size) {
if (PIPELINE_OK != Start(data, size, kUnreliableDuration | kFuzzing))
return;
Play();
if (PIPELINE_OK != WaitUntilEndedOrError())
return;
Seek(base::TimeDelta());
}
};
class MediaSourcePipelineIntegrationFuzzerTest
: public PipelineIntegrationTestBase {
public:
MediaSourcePipelineIntegrationFuzzerTest() {
set_encrypted_media_init_data_cb(
base::BindRepeating(&OnEncryptedMediaInitData, this));
set_audio_play_delay_cb(base::BindPostTaskToCurrentDefault(
base::BindRepeating(&OnAudioPlayDelay, this)));
}
~MediaSourcePipelineIntegrationFuzzerTest() override = default;
void RunTest(const uint8_t* data, size_t size, const std::string& mimetype) {
if (size == 0)
return;
auto external_memory =
std::make_unique<media::ExternalMemoryAdapterForTesting>(
base::span(data, size));
scoped_refptr<media::DecoderBuffer> buffer =
media::DecoderBuffer::FromExternalMemory(std::move(external_memory));
TestMediaSource source(buffer, mimetype, kAppendWholeFile);
// Prevent timeout in the case of not enough media appended to complete
// demuxer initialization, yet no error in the media appended. The
// following will trigger DEMUXER_ERROR_COULD_NOT_OPEN state transition in
// this case.
source.set_do_eos_after_next_append(true);
source.set_encrypted_media_init_data_cb(
base::BindRepeating(&OnEncryptedMediaInitData, this));
// Allow parsing to either pass or fail without emitting a gtest failure
// from TestMediaSource.
source.set_expected_append_result(
TestMediaSource::ExpectedAppendResult::kSuccessOrFailure);
// TODO(wolenetz): Vary the behavior (abort/remove/seek/endOfStream/Append
// in pieces/append near play-head/vary append mode/etc), perhaps using
// CustomMutator and Seed to insert/update the variation information into/in
// the |data| we process here. See https://crbug.com/750818.
// Use |kFuzzing| test type to allow pipeline start to either pass or fail
// without emitting a gtest failure.
if (PIPELINE_OK != StartPipelineWithMediaSource(&source, kFuzzing, nullptr))
return;
Play();
}
};
} // namespace media
// Disable noisy logging.
struct Environment {
Environment() {
base::CommandLine::Init(0, nullptr);
// |test| instances uses TaskEnvironment, which needs TestTimeouts.
TestTimeouts::Initialize();
media::InitializeMediaLibrary();
// Note, instead of LOGGING_FATAL, use a value at or below
// logging::LOGGING_VERBOSE here to assist local debugging.
logging::SetMinLogLevel(logging::LOGGING_FATAL);
}
};
Environment* env = new Environment();
// Entry point for LibFuzzer.
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
// Media pipeline starts new threads, which needs AtExitManager.
base::AtExitManager at_exit;
FuzzerVariant variant = PIPELINE_FUZZER_VARIANT;
// These tests use GoogleTest assertions without using the GoogleTest
// framework. While this is the case, tell GoogleTest's stack trace getter
// that GoogleTest is being left now so that there is a basis for traces
// collected upon assertion failure. TODO(crbug.com/40113640): use
// RUN_ALL_TESTS() and remove this code.
::testing::internal::GetUnitTestImpl()
->os_stack_trace_getter()
->UponLeavingGTest();
if (variant == SRC) {
media::ProgressivePipelineIntegrationFuzzerTest test;
test.RunTest(data, size);
} else {
media::MediaSourcePipelineIntegrationFuzzerTest test;
test.RunTest(data, size, MseFuzzerVariantEnumToMimeTypeString(variant));
}
return 0;
}