Coverage Report

Created: 2025-08-28 09:57

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