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 implementation for the unified fuzzing interface
8  */
9 
10 #include "nsIFile.h"
11 #include "nsIPrefService.h"
12 #include "nsIProperties.h"
13 
14 #include "FuzzingInterfaceStream.h"
15 
16 #include "mozilla/Assertions.h"
17 
18 #ifndef JS_STANDALONE
19 #  include "nsNetUtil.h"
20 #endif
21 
22 namespace mozilla {
23 
24 #ifdef AFLFUZZ
25 
afl_interface_stream(const char * testFile,FuzzingTestFuncStream testFunc)26 void afl_interface_stream(const char* testFile,
27                           FuzzingTestFuncStream testFunc) {
28   nsresult rv;
29   nsCOMPtr<nsIProperties> dirService =
30       do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID);
31   MOZ_RELEASE_ASSERT(dirService != nullptr);
32   nsCOMPtr<nsIFile> file;
33   rv = dirService->Get(NS_OS_CURRENT_WORKING_DIR, NS_GET_IID(nsIFile),
34                        getter_AddRefs(file));
35   MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
36   file->AppendNative(nsDependentCString(testFile));
37   while (__AFL_LOOP(1000)) {
38     nsCOMPtr<nsIInputStream> inputStream;
39     rv = NS_NewLocalFileInputStream(getter_AddRefs(inputStream), file);
40     MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
41     if (!NS_InputStreamIsBuffered(inputStream)) {
42       nsCOMPtr<nsIInputStream> bufStream;
43       rv = NS_NewBufferedInputStream(getter_AddRefs(bufStream),
44                                      inputStream.forget(), 1024);
45       MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
46       inputStream = bufStream;
47     }
48     testFunc(inputStream.forget());
49   }
50 }
51 
52 #endif
53 
54 }  // namespace mozilla
55