Bug 1829125 - Align the PHC area to the jemalloc chunk size r=glandium
[gecko.git] / tools / fuzzing / interface / FuzzingInterfaceStream.h
blob1542020794de4fd74ca6ddc6cf5eae40747717ff
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/. */
6 /*
7 * Interface definitions for the unified fuzzing interface with streaming
8 * support
9 */
11 #ifndef FuzzingInterfaceStream_h__
12 #define FuzzingInterfaceStream_h__
14 #ifdef JS_STANDALONE
15 # error "FuzzingInterfaceStream.h cannot be used in JS standalone builds."
16 #endif
18 #include "gtest/gtest.h"
19 #include "nsComponentManagerUtils.h"
20 #include "nsCOMPtr.h"
21 #include "nsIInputStream.h"
23 #include "nsDirectoryServiceDefs.h"
24 #include "nsStreamUtils.h"
25 #include "nsStringStream.h"
27 #include <fstream>
29 #include "FuzzingInterface.h"
31 namespace mozilla {
33 typedef int (*FuzzingTestFuncStream)(nsCOMPtr<nsIInputStream>);
35 #ifdef AFLFUZZ
36 void afl_interface_stream(const char* testFile, FuzzingTestFuncStream testFunc);
38 # define MOZ_AFL_INTERFACE_COMMON(initFunc) \
39 if (initFunc) initFunc(NULL, NULL); \
40 char* testFilePtr = getenv("MOZ_FUZZ_TESTFILE"); \
41 if (!testFilePtr) { \
42 fprintf(stderr, \
43 "Must specify testfile in MOZ_FUZZ_TESTFILE environment " \
44 "variable.\n"); \
45 return; \
46 } \
47 /* Make a copy of testFilePtr so the testing function can safely call \
48 * getenv \
49 */ \
50 std::string testFile(testFilePtr);
52 # define MOZ_AFL_INTERFACE_STREAM(initFunc, testFunc, moduleName) \
53 TEST(AFL, moduleName) \
54 { \
55 MOZ_AFL_INTERFACE_COMMON(initFunc); \
56 ::mozilla::afl_interface_stream(testFile.c_str(), testFunc); \
58 #else
59 # define MOZ_AFL_INTERFACE_STREAM(initFunc, testFunc, moduleName) /* Nothing \
61 #endif
63 #ifdef LIBFUZZER
64 # define MOZ_LIBFUZZER_INTERFACE_STREAM(initFunc, testFunc, moduleName) \
65 static int LibFuzzerTest##moduleName(const uint8_t* data, size_t size) { \
66 if (size > INT32_MAX) return 0; \
67 nsCOMPtr<nsIInputStream> stream; \
68 nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream), \
69 Span((const char*)data, size), \
70 NS_ASSIGNMENT_DEPEND); \
71 MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv)); \
72 testFunc(stream.forget()); \
73 return 0; \
74 } \
75 static void __attribute__((constructor)) LibFuzzerRegister##moduleName() { \
76 ::mozilla::FuzzerRegistry::getInstance().registerModule( \
77 #moduleName, initFunc, LibFuzzerTest##moduleName); \
79 #else
80 # define MOZ_LIBFUZZER_INTERFACE_STREAM(initFunc, testFunc, \
81 moduleName) /* Nothing */
82 #endif
84 #define MOZ_FUZZING_INTERFACE_STREAM(initFunc, testFunc, moduleName) \
85 MOZ_LIBFUZZER_INTERFACE_STREAM(initFunc, testFunc, moduleName); \
86 MOZ_AFL_INTERFACE_STREAM(initFunc, testFunc, moduleName);
88 } // namespace mozilla
90 #endif // FuzzingInterfaceStream_h__