/src/node/test/fuzzers/fuzz_buffer_includes.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 buffer1 = new Buffer('"; |
78 | | //RANDOM1 |
79 | | std::string S2 = |
80 | | "');\n" |
81 | | "const checkStr = '"; |
82 | | //RANDOM2 |
83 | | std::string S3 = |
84 | | "';\n" |
85 | | "const _ = buffer1.includes(checkStr);\n"; |
86 | | |
87 | 115k | void EnvTest(v8::Isolate* isolate_, char* env_string) { |
88 | 115k | printf("%s\n", env_string); |
89 | 115k | const v8::HandleScope handle_scope(isolate_); |
90 | 115k | Argv argv; |
91 | | |
92 | 115k | node::EnvironmentFlags::Flags flags = node::EnvironmentFlags::kDefaultFlags; |
93 | 115k | auto isolate = handle_scope.GetIsolate(); |
94 | 115k | v8::Local<v8::Context> context_ = node::NewContext(isolate); |
95 | 115k | context_->Enter(); |
96 | | |
97 | 115k | node::IsolateData* isolate_data_ = node::CreateIsolateData(isolate, ¤t_loop, |
98 | 115k | platform.get()); |
99 | 115k | std::vector<std::string> args(*argv, *argv + 1); |
100 | 115k | std::vector<std::string> exec_args(*argv, *argv + 1); |
101 | 115k | node::Environment* environment_ = node::CreateEnvironment(isolate_data_, |
102 | 115k | context_, args, exec_args, flags); |
103 | 115k | node::Environment* envi = environment_; |
104 | 115k | SetProcessExitHandler(envi, [&](node::Environment* env_, int exit_code) { |
105 | 1.80k | node::Stop(envi); |
106 | 1.80k | }); |
107 | 115k | node::LoadEnvironment(envi, env_string); |
108 | | |
109 | | // Cleanup! |
110 | 115k | node::FreeEnvironment(environment_); |
111 | 115k | node::FreeIsolateData(isolate_data_); |
112 | 115k | context_->Exit(); |
113 | 115k | } |
114 | | |
115 | 23.6k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data2, size_t size) { |
116 | 23.6k | FuzzedDataProvider prov(data2, size); |
117 | 23.6k | std::string s1 = prov.ConsumeRandomLengthString(); |
118 | 23.6k | std::string s2 = prov.ConsumeRandomLengthString(); |
119 | 23.6k | if (hasUnescapedSingleQuotes(s1)) { |
120 | 110 | return 0; |
121 | 110 | } |
122 | 23.5k | if (hasUnescapedSingleQuotes(s2)) { |
123 | 68 | return 0; |
124 | 68 | } |
125 | 23.5k | std::stringstream stream; |
126 | 23.5k | stream << S1 << s1 << S2 << s2 << S3 << std::endl; |
127 | 23.5k | std::string js_code = stream.str(); |
128 | 23.5k | FuzzerFixtureHelper ffh; |
129 | 23.5k | EnvTest(ffh.isolate_, (char*)js_code.c_str()); |
130 | 23.5k | ffh.Teardown(); |
131 | 23.5k | return 0; |
132 | 23.5k | } |
133 | | |