Coverage Report

Created: 2025-07-04 09:33

/src/node/test/fuzzers/fuzz_env.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
18
using node::AliasedBufferBase;
19
20
/* General set up */
21
using ArrayBufferUniquePtr = std::unique_ptr<node::ArrayBufferAllocator,
22
  decltype(&node::FreeArrayBufferAllocator)>;
23
using TracingAgentUniquePtr = std::unique_ptr<node::tracing::Agent>;
24
using NodePlatformUniquePtr = std::unique_ptr<node::NodePlatform>;
25
26
static TracingAgentUniquePtr tracing_agent;
27
static NodePlatformUniquePtr platform;
28
static uv_loop_t current_loop;
29
30
132k
extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) {
31
132k
  uv_os_unsetenv("NODE_OPTIONS");
32
132k
  std::vector<std::string> node_argv{ "fuzz_env" };
33
132k
  std::vector<std::string> exec_argv;
34
132k
  std::vector<std::string> errors;
35
36
132k
  node::InitializeNodeWithArgs(&node_argv, &exec_argv, &errors);
37
38
132k
  tracing_agent = std::make_unique<node::tracing::Agent>();
39
132k
  node::tracing::TraceEventHelper::SetAgent(tracing_agent.get());
40
132k
  node::tracing::TracingController* tracing_controller =
41
132k
    tracing_agent->GetTracingController();  
42
132k
  CHECK_EQ(0, uv_loop_init(&current_loop));
43
132k
  static constexpr int kV8ThreadPoolSize = 4;
44
132k
  platform.reset(
45
132k
    new node::NodePlatform(kV8ThreadPoolSize, tracing_controller));
46
132k
  v8::V8::InitializePlatform(platform.get());
47
132k
  cppgc::InitializeProcess(platform->GetPageAllocator());
48
132k
  v8::V8::Initialize();
49
132k
  return 0;
50
132k
}
51
52
class FuzzerFixtureHelper {
53
public:
54
  v8::Isolate* isolate_;
55
  ArrayBufferUniquePtr allocator;
56
57
  FuzzerFixtureHelper()
58
134k
    : allocator(ArrayBufferUniquePtr(node::CreateArrayBufferAllocator(),
59
134k
                                     &node::FreeArrayBufferAllocator)) {
60
134k
    isolate_ = NewIsolate(allocator.get(), &current_loop, platform.get());
61
134k
    CHECK_NOT_NULL(isolate_);
62
134k
    isolate_->Enter();
63
134k
  };
64
65
134k
  void Teardown() {
66
134k
    platform->DrainTasks(isolate_);
67
134k
    isolate_->Exit();
68
134k
    platform->UnregisterIsolate(isolate_);
69
134k
    isolate_->Dispose();
70
134k
    isolate_ = nullptr;
71
134k
  }
72
};
73
74
115k
void EnvTest(v8::Isolate* isolate_, char* env_string) {
75
115k
  const v8::HandleScope handle_scope(isolate_);
76
115k
  Argv argv;
77
78
115k
  node::EnvironmentFlags::Flags flags = node::EnvironmentFlags::kDefaultFlags;
79
115k
  auto isolate = handle_scope.GetIsolate();
80
115k
  v8::Local<v8::Context> context_ = node::NewContext(isolate);
81
115k
  context_->Enter();
82
83
115k
  node::IsolateData* isolate_data_ = node::CreateIsolateData(isolate, &current_loop,
84
115k
                                                             platform.get());
85
115k
  std::vector<std::string> args(*argv, *argv + 1);
86
115k
  std::vector<std::string> exec_args(*argv, *argv + 1);
87
115k
  node::Environment* environment_ = node::CreateEnvironment(isolate_data_,
88
115k
                                          context_, args, exec_args, flags);
89
115k
  node::Environment* envi = environment_;
90
115k
  SetProcessExitHandler(envi, [&](node::Environment* env_, int exit_code) {
91
6.60k
    node::Stop(envi);
92
6.60k
  });
93
115k
  node::LoadEnvironment(envi, env_string);
94
95
  // Cleanup!
96
115k
  node::FreeEnvironment(environment_);
97
115k
  node::FreeIsolateData(isolate_data_);
98
115k
  context_->Exit();
99
115k
}
100
101
36.0k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data2, size_t size) {
102
36.0k
  FuzzerFixtureHelper ffh;
103
36.0k
  std::string s(reinterpret_cast<const char*>(data2), size);
104
36.0k
  EnvTest(ffh.isolate_, (char*)s.c_str());
105
36.0k
  ffh.Teardown();
106
36.0k
  return 0;
107
36.0k
}
108