• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2016 The WebM project authors. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS.  All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 #include <cassert>
9 #include <cstdint>
10 #include <cstdio>
11 #include <cstdlib>
12 #include <iostream>
13 #include <new>
14 #include <vector>
15 
16 #include "webm/buffer_reader.h"
17 #include "webm/callback.h"
18 #include "webm/file_reader.h"
19 #include "webm/reader.h"
20 #include "webm/status.h"
21 #include "webm/webm_parser.h"
22 
23 using webm::BufferReader;
24 using webm::Callback;
25 using webm::FileReader;
26 using webm::Reader;
27 using webm::Status;
28 using webm::WebmParser;
29 
Run(Reader * reader)30 static int Run(Reader* reader) {
31   Callback callback;
32   WebmParser parser;
33 
34 #if WEBM_FUZZER_SEEK_FIRST
35   parser.DidSeek();
36 #endif
37 
38   Status status(-1);
39   try {
40     status = parser.Feed(&callback, reader);
41   } catch (std::bad_alloc&) {
42     // Failed allocations are okay. MSan doesn't throw std::bad_alloc, but
43     // someday it might.
44   }
45 
46   // BufferReader/FileReader should never return either of the following codes,
47   // which means the parser never should too:
48   assert(status.code != Status::kWouldBlock);
49   assert(status.code != Status::kOkPartial);
50 
51   // Only the following ranges have status codes defined:
52   assert((-1031 <= status.code && status.code <= -1025) ||
53          (-3 <= status.code && status.code <= 0));
54 
55   return 0;
56 }
57 
LLVMFuzzerTestOneInput(const std::uint8_t * data,std::size_t size)58 extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data,
59                                       std::size_t size) {
60   BufferReader reader(std::vector<std::uint8_t>(data, data + size));
61   return Run(&reader);
62 }
63 
64 #if __AFL_COMPILER
main(int argc,char * argv[])65 int main(int argc, char* argv[]) {
66   FILE* file = (argc == 2) ? std::fopen(argv[1], "rb")
67                            : std::freopen(nullptr, "rb", stdin);
68   if (!file) {
69     std::cerr << "File cannot be opened\n";
70     return EXIT_FAILURE;
71   }
72 
73   FileReader reader(file);
74   return Run(&reader);
75 }
76 #endif
77