Debian Codegraph @

tools/fuzzing/interface/FuzzingInterfaceStream.h

raw
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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
8 * support
9 */
10
11 #ifndef FuzzingInterfaceStream_h__
12 #define FuzzingInterfaceStream_h__
13
14 #ifdef JS_STANDALONE
15 # error "FuzzingInterfaceStream.h cannot be used in JS standalone builds."
16 #endif
17
18 #include "gtest/gtest.h"
19 #include "nsComponentManagerUtils.h"
20 #include "nsCOMPtr.h"
21 #include "nsIInputStream.h"
22
23 #include "nsDirectoryServiceDefs.h"
24 #include "nsStreamUtils.h"
25 #include "nsStringStream.h"
26
27 #include <fstream>
28
29 #include "FuzzingInterface.h"
30
31 typedef int (*FuzzingTestFuncStream)(nsCOMPtr<nsIInputStream>);
32
33 #ifdef AFLFUZZ
34 # define MOZ_AFL_INTERFACE_STREAM(initFunc, testFunc, moduleName) \
35 static int afl_fuzz_inner_##moduleName(const uint8_t* data, size_t size) { \
36 if (size > INT32_MAX) return 0; \
37 nsCOMPtr<nsIInputStream> stream; \
38 nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream), \
39 Span((const char*)data, size), \
40 NS_ASSIGNMENT_DEPEND); \
41 MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv)); \
42 return testFunc(stream.forget()); \
43 } \
44 static int afl_fuzz_##moduleName(const uint8_t* data, size_t size) { \
45 return afl_interface_raw(afl_fuzz_inner_##moduleName); \
46 } \
47 static void __attribute__((constructor)) AFLRegister##moduleName() { \
48 ::mozilla::FuzzerRegistry::getInstance().registerModule( \
49 #moduleName, initFunc, afl_fuzz_##moduleName); \
50 }
51 #else
52 # define MOZ_AFL_INTERFACE_STREAM(initFunc, testFunc, moduleName) /* Nothing \
53 */
54 #endif
55
56 #ifdef LIBFUZZER
57 # define MOZ_LIBFUZZER_INTERFACE_STREAM(initFunc, testFunc, moduleName) \
58 static int LibFuzzerTest##moduleName(const uint8_t* data, size_t size) { \
59 if (size > INT32_MAX) return 0; \
60 nsCOMPtr<nsIInputStream> stream; \
61 nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream), \
62 Span((const char*)data, size), \
63 NS_ASSIGNMENT_DEPEND); \
64 MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv)); \
65 testFunc(stream.forget()); \
66 return 0; \
67 } \
68 static void __attribute__((constructor)) LibFuzzerRegister##moduleName() { \
69 ::mozilla::FuzzerRegistry::getInstance().registerModule( \
70 #moduleName, initFunc, LibFuzzerTest##moduleName); \
71 }
72 #else
73 # define MOZ_LIBFUZZER_INTERFACE_STREAM(initFunc, testFunc, \
74 moduleName) /* Nothing */
75 #endif
76
77 #define MOZ_FUZZING_INTERFACE_STREAM(initFunc, testFunc, moduleName) \
78 MOZ_LIBFUZZER_INTERFACE_STREAM(initFunc, testFunc, moduleName); \
79 MOZ_AFL_INTERFACE_STREAM(initFunc, testFunc, moduleName);
80
81 #endif // FuzzingInterfaceStream_h__