/work/obj-fuzz/dist/include/FuzzingInterfaceStream.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
3 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
5 | | |
6 | | /* |
7 | | * Interface definitions for the unified fuzzing interface with streaming support |
8 | | */ |
9 | | |
10 | | #ifndef FuzzingInterfaceStream_h__ |
11 | | #define FuzzingInterfaceStream_h__ |
12 | | |
13 | | #ifdef JS_STANDALONE |
14 | | #error "FuzzingInterfaceStream.h cannot be used in JS standalone builds." |
15 | | #endif |
16 | | |
17 | | #include "gtest/gtest.h" |
18 | | #include "nsComponentManagerUtils.h" |
19 | | #include "nsCOMPtr.h" |
20 | | #include "nsIInputStream.h" |
21 | | |
22 | | #include "nsDirectoryServiceDefs.h" |
23 | | #include "nsIDirectoryService.h" |
24 | | #include "nsIFile.h" |
25 | | #include "nsStreamUtils.h" |
26 | | #include "nsStringStream.h" |
27 | | |
28 | | #include <fstream> |
29 | | |
30 | | #include "FuzzingInterface.h" |
31 | | |
32 | | namespace mozilla { |
33 | | |
34 | | typedef int(*FuzzingTestFuncStream)(nsCOMPtr<nsIInputStream>); |
35 | | |
36 | | #ifdef __AFL_COMPILER |
37 | | void afl_interface_stream(const char* testFile, FuzzingTestFuncStream testFunc); |
38 | | |
39 | | #define MOZ_AFL_INTERFACE_COMMON(initFunc) \ |
40 | | if (initFunc) initFunc(NULL, NULL); \ |
41 | | char* testFilePtr = getenv("MOZ_FUZZ_TESTFILE"); \ |
42 | | if (!testFilePtr) { \ |
43 | | fprintf(stderr, "Must specify testfile in MOZ_FUZZ_TESTFILE environment variable.\n"); \ |
44 | | return; \ |
45 | | } \ |
46 | | /* Make a copy of testFilePtr so the testing function can safely call getenv */ \ |
47 | | std::string testFile(testFilePtr); |
48 | | |
49 | | #define MOZ_AFL_INTERFACE_STREAM(initFunc, testFunc, moduleName) \ |
50 | | TEST(AFL, moduleName) { \ |
51 | | MOZ_AFL_INTERFACE_COMMON(initFunc); \ |
52 | | ::mozilla::afl_interface_stream(testFile.c_str(), testFunc); \ |
53 | | } |
54 | | #else |
55 | | #define MOZ_AFL_INTERFACE_STREAM(initFunc, testFunc, moduleName) /* Nothing */ |
56 | | #endif |
57 | | |
58 | | #ifdef LIBFUZZER |
59 | | #define MOZ_LIBFUZZER_INTERFACE_STREAM(initFunc, testFunc, moduleName) \ |
60 | 0 | static int LibFuzzerTest##moduleName (const uint8_t *data, size_t size) { \ |
61 | 0 | if (size > INT32_MAX) \ |
62 | 0 | return 0; \ |
63 | 0 | nsCOMPtr<nsIInputStream> stream; \ |
64 | 0 | nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream), \ |
65 | 0 | (const char*)data, size, NS_ASSIGNMENT_DEPEND); \ |
66 | 0 | MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv)); \ |
67 | 0 | testFunc(stream.forget()); \ |
68 | 0 | return 0; \ |
69 | 0 | } \ |
70 | 3 | static void __attribute__ ((constructor)) LibFuzzerRegister() { \ |
71 | 3 | ::mozilla::FuzzerRegistry::getInstance().registerModule( \ |
72 | 3 | #moduleName, initFunc, LibFuzzerTest##moduleName \ |
73 | 3 | ); \ |
74 | 3 | } |
75 | | #else |
76 | | #define MOZ_LIBFUZZER_INTERFACE_STREAM(initFunc, testFunc, moduleName) /* Nothing */ |
77 | | #endif |
78 | | |
79 | | #define MOZ_FUZZING_INTERFACE_STREAM(initFunc, testFunc, moduleName) \ |
80 | | MOZ_LIBFUZZER_INTERFACE_STREAM(initFunc, testFunc, moduleName); \ |
81 | | MOZ_AFL_INTERFACE_STREAM(initFunc, testFunc, moduleName); |
82 | | |
83 | | } // namespace mozilla |
84 | | |
85 | | #endif // FuzzingInterfaceStream_h__ |