Coverage Report

Created: 2025-08-28 09:57

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