/src/node/test/fuzzers/fuzz_stream1.cc
Line | Count | Source |
1 | | /* |
2 | | * A fuzzer focused on the node::LoadEnvironment() function. |
3 | | * |
4 | | * Code here has been inspired by the cctest test case. |
5 | | */ |
6 | | |
7 | | #include <stdlib.h> |
8 | | #include "node.h" |
9 | | #include "node_platform.h" |
10 | | #include "node_internals.h" |
11 | | #include "env-inl.h" |
12 | | #include "util-inl.h" |
13 | | #include "v8.h" |
14 | | #include "libplatform/libplatform.h" |
15 | | #include "aliased_buffer.h" |
16 | | #include "fuzz_helper.h" |
17 | | #include <fuzzer/FuzzedDataProvider.h> |
18 | | #include <cstdio> |
19 | | |
20 | | using node::AliasedBufferBase; |
21 | | |
22 | | /* General set up */ |
23 | | using ArrayBufferUniquePtr = std::unique_ptr<node::ArrayBufferAllocator, |
24 | | decltype(&node::FreeArrayBufferAllocator)>; |
25 | | using TracingAgentUniquePtr = std::unique_ptr<node::tracing::Agent>; |
26 | | using NodePlatformUniquePtr = std::unique_ptr<node::NodePlatform>; |
27 | | |
28 | | static TracingAgentUniquePtr tracing_agent; |
29 | | static NodePlatformUniquePtr platform; |
30 | | static uv_loop_t current_loop; |
31 | | |
32 | 132k | extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) { |
33 | 132k | uv_os_unsetenv("NODE_OPTIONS"); |
34 | 132k | std::vector<std::string> node_argv{ "fuzz_env" }; |
35 | 132k | std::vector<std::string> exec_argv; |
36 | 132k | std::vector<std::string> errors; |
37 | | |
38 | 132k | node::InitializeNodeWithArgs(&node_argv, &exec_argv, &errors); |
39 | | |
40 | 132k | tracing_agent = std::make_unique<node::tracing::Agent>(); |
41 | 132k | node::tracing::TraceEventHelper::SetAgent(tracing_agent.get()); |
42 | 132k | node::tracing::TracingController* tracing_controller = |
43 | 132k | tracing_agent->GetTracingController(); |
44 | 132k | CHECK_EQ(0, uv_loop_init(¤t_loop)); |
45 | 132k | static constexpr int kV8ThreadPoolSize = 4; |
46 | 132k | platform.reset( |
47 | 132k | new node::NodePlatform(kV8ThreadPoolSize, tracing_controller)); |
48 | 132k | v8::V8::InitializePlatform(platform.get()); |
49 | 132k | cppgc::InitializeProcess(platform->GetPageAllocator()); |
50 | 132k | v8::V8::Initialize(); |
51 | 132k | return 0; |
52 | 132k | } |
53 | | |
54 | | class FuzzerFixtureHelper { |
55 | | public: |
56 | | v8::Isolate* isolate_; |
57 | | ArrayBufferUniquePtr allocator; |
58 | | |
59 | | FuzzerFixtureHelper() |
60 | 134k | : allocator(ArrayBufferUniquePtr(node::CreateArrayBufferAllocator(), |
61 | 134k | &node::FreeArrayBufferAllocator)) { |
62 | 134k | isolate_ = NewIsolate(allocator.get(), ¤t_loop, platform.get()); |
63 | 134k | CHECK_NOT_NULL(isolate_); |
64 | 134k | isolate_->Enter(); |
65 | 134k | }; |
66 | | |
67 | 134k | void Teardown() { |
68 | 134k | platform->DrainTasks(isolate_); |
69 | 134k | isolate_->Exit(); |
70 | 134k | platform->UnregisterIsolate(isolate_); |
71 | 134k | isolate_->Dispose(); |
72 | 134k | isolate_ = nullptr; |
73 | 134k | } |
74 | | }; |
75 | | |
76 | | std::string S1 = |
77 | | "const Stream = require('stream');\n" |
78 | | "const readableStream = new Stream.Readable();\n" |
79 | | |
80 | | "readableStream.push('"; |
81 | | |
82 | | std::string S2 = |
83 | | "');\n" |
84 | | "readableStream.push(null);\n" |
85 | | |
86 | | "async function getContentReadStream(readable) {\n" |
87 | | " for await (const chunk of readable) {\n" |
88 | | " var _ = chunk;\n" |
89 | | " var _ = chunk.toString();\n" |
90 | | " }\n" |
91 | | "}\n" |
92 | | "getContentReadStream(readableStream);\n"; |
93 | | |
94 | 115k | void EnvTest(v8::Isolate* isolate_, char* env_string) { |
95 | 115k | const v8::HandleScope handle_scope(isolate_); |
96 | 115k | Argv argv; |
97 | | |
98 | 115k | node::EnvironmentFlags::Flags flags = node::EnvironmentFlags::kDefaultFlags; |
99 | 115k | auto isolate = handle_scope.GetIsolate(); |
100 | 115k | v8::Local<v8::Context> context_ = node::NewContext(isolate); |
101 | 115k | context_->Enter(); |
102 | | |
103 | 115k | node::IsolateData* isolate_data_ = node::CreateIsolateData(isolate, ¤t_loop, |
104 | 115k | platform.get()); |
105 | 115k | std::vector<std::string> args(*argv, *argv + 1); |
106 | 115k | std::vector<std::string> exec_args(*argv, *argv + 1); |
107 | 115k | node::Environment* environment_ = node::CreateEnvironment(isolate_data_, |
108 | 115k | context_, args, exec_args, flags); |
109 | 115k | node::Environment* envi = environment_; |
110 | 115k | SetProcessExitHandler(envi, [&](node::Environment* env_, int exit_code) { |
111 | 2.03k | node::Stop(envi); |
112 | 2.03k | }); |
113 | 115k | node::LoadEnvironment(envi, env_string); |
114 | | |
115 | | // Cleanup! |
116 | 115k | node::FreeEnvironment(environment_); |
117 | 115k | node::FreeIsolateData(isolate_data_); |
118 | 115k | context_->Exit(); |
119 | 115k | } |
120 | | |
121 | 66.7k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data2, size_t size) { |
122 | 66.7k | FuzzedDataProvider prov(data2, size); |
123 | 66.7k | std::string r1 = prov.ConsumeRemainingBytesAsString(); |
124 | 66.7k | if (hasUnescapedSingleQuotes(r1)) { |
125 | 243 | return 0; |
126 | 243 | } |
127 | 66.4k | std::stringstream stream; |
128 | 66.4k | stream << S1 << r1 << S2 << std::endl; |
129 | 66.4k | FuzzerFixtureHelper ffh; |
130 | 66.4k | std::string js_code = stream.str(); |
131 | 66.4k | EnvTest(ffh.isolate_, (char*)js_code.c_str()); |
132 | 66.4k | ffh.Teardown(); |
133 | 66.4k | return 0; |
134 | 66.7k | } |
135 | | |