GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_process_object.cc Lines: 81 83 97.6 %
Date: 2022-08-05 04:16:08 Branches: 41 66 62.1 %

Line Branch Exec Source
1
#include "env-inl.h"
2
#include "node_errors.h"
3
#include "node_external_reference.h"
4
#include "node_internals.h"
5
#include "node_metadata.h"
6
#include "node_options-inl.h"
7
#include "node_process-inl.h"
8
#include "node_revert.h"
9
#include "util-inl.h"
10
11
#include <climits>  // PATH_MAX
12
13
namespace node {
14
using v8::Context;
15
using v8::DEFAULT;
16
using v8::EscapableHandleScope;
17
using v8::Function;
18
using v8::FunctionCallbackInfo;
19
using v8::FunctionTemplate;
20
using v8::Integer;
21
using v8::Isolate;
22
using v8::Local;
23
using v8::MaybeLocal;
24
using v8::Name;
25
using v8::NewStringType;
26
using v8::None;
27
using v8::Object;
28
using v8::PropertyCallbackInfo;
29
using v8::SideEffectType;
30
using v8::String;
31
using v8::Value;
32
33
98
static void ProcessTitleGetter(Local<Name> property,
34
                               const PropertyCallbackInfo<Value>& info) {
35
98
  std::string title = GetProcessTitle("node");
36
294
  info.GetReturnValue().Set(
37
98
      String::NewFromUtf8(info.GetIsolate(), title.data(),
38
196
                          NewStringType::kNormal, title.size())
39
      .ToLocalChecked());
40
98
}
41
42
101
static void ProcessTitleSetter(Local<Name> property,
43
                               Local<Value> value,
44
                               const PropertyCallbackInfo<void>& info) {
45
202
  node::Utf8Value title(info.GetIsolate(), value);
46

201
  TRACE_EVENT_METADATA1(
47
      "__metadata", "process_name", "name", TRACE_STR_COPY(*title));
48
101
  uv_set_process_title(*title);
49
101
}
50
51
193
static void DebugPortGetter(Local<Name> property,
52
                            const PropertyCallbackInfo<Value>& info) {
53
193
  Environment* env = Environment::GetCurrent(info);
54
193
  ExclusiveAccess<HostPort>::Scoped host_port(env->inspector_host_port());
55
193
  int port = host_port->port();
56
386
  info.GetReturnValue().Set(port);
57
193
}
58
59
28
static void DebugPortSetter(Local<Name> property,
60
                            Local<Value> value,
61
                            const PropertyCallbackInfo<void>& info) {
62
28
  Environment* env = Environment::GetCurrent(info);
63
28
  int32_t port = value->Int32Value(env->context()).FromMaybe(0);
64
65

28
  if ((port != 0 && port < 1024) || port > 65535) {
66
9
    return THROW_ERR_OUT_OF_RANGE(
67
      env,
68
9
      "process.debugPort must be 0 or in range 1024 to 65535");
69
  }
70
71
38
  ExclusiveAccess<HostPort>::Scoped host_port(env->inspector_host_port());
72
19
  host_port->set_port(static_cast<int>(port));
73
}
74
75
92
static void GetParentProcessId(Local<Name> property,
76
                               const PropertyCallbackInfo<Value>& info) {
77
92
  info.GetReturnValue().Set(uv_os_getppid());
78
92
}
79
80
1305
MaybeLocal<Object> CreateProcessObject(Environment* env) {
81
1305
  Isolate* isolate = env->isolate();
82
1305
  EscapableHandleScope scope(isolate);
83
1305
  Local<Context> context = env->context();
84
85
1305
  Local<FunctionTemplate> process_template = FunctionTemplate::New(isolate);
86
1305
  process_template->SetClassName(env->process_string());
87
  Local<Function> process_ctor;
88
  Local<Object> process;
89
3915
  if (!process_template->GetFunction(context).ToLocal(&process_ctor) ||
90

3915
      !process_ctor->NewInstance(context).ToLocal(&process)) {
91
    return MaybeLocal<Object>();
92
  }
93
94
  // process[exiting_aliased_Uint32Array]
95
1305
  if (process
96
1305
          ->SetPrivate(context,
97
                       env->exiting_aliased_Uint32Array(),
98
2610
                       env->exiting().GetJSArray())
99
1305
          .IsNothing()) {
100
    return {};
101
  }
102
103
  // process.version
104
3915
  READONLY_PROPERTY(process,
105
                    "version",
106
                    FIXED_ONE_BYTE_STRING(env->isolate(), NODE_VERSION));
107
108
  // process.versions
109
1305
  Local<Object> versions = Object::New(env->isolate());
110
2610
  READONLY_PROPERTY(process, "versions", versions);
111
112
#define V(key)                                                                 \
113
  if (!per_process::metadata.versions.key.empty()) {                           \
114
    READONLY_STRING_PROPERTY(                                                  \
115
        versions, #key, per_process::metadata.versions.key);                   \
116
  }
117








90045
  NODE_VERSIONS_KEYS(V)
118
#undef V
119
120
  // process.arch
121
5220
  READONLY_STRING_PROPERTY(process, "arch", per_process::metadata.arch);
122
123
  // process.platform
124
3915
  READONLY_STRING_PROPERTY(process, "platform", per_process::metadata.platform);
125
126
  // process.release
127
1305
  Local<Object> release = Object::New(env->isolate());
128
3915
  READONLY_PROPERTY(process, "release", release);
129
3915
  READONLY_STRING_PROPERTY(release, "name", per_process::metadata.release.name);
130
#if NODE_VERSION_IS_LTS
131
  READONLY_STRING_PROPERTY(release, "lts", per_process::metadata.release.lts);
132
#endif  // NODE_VERSION_IS_LTS
133
134
#ifdef NODE_HAS_RELEASE_URLS
135
  READONLY_STRING_PROPERTY(
136
      release, "sourceUrl", per_process::metadata.release.source_url);
137
  READONLY_STRING_PROPERTY(
138
      release, "headersUrl", per_process::metadata.release.headers_url);
139
#ifdef _WIN32
140
  READONLY_STRING_PROPERTY(
141
      release, "libUrl", per_process::metadata.release.lib_url);
142
#endif  // _WIN32
143
#endif  // NODE_HAS_RELEASE_URLS
144
145
  // process._rawDebug: may be overwritten later in JS land, but should be
146
  // available from the beginning for debugging purposes
147
1305
  SetMethod(context, process, "_rawDebug", RawDebug);
148
149
1305
  return scope.Escape(process);
150
}
151
152
6579
void PatchProcessObject(const FunctionCallbackInfo<Value>& args) {
153
6579
  Isolate* isolate = args.GetIsolate();
154
6579
  Local<Context> context = isolate->GetCurrentContext();
155
6579
  Environment* env = Environment::GetCurrent(context);
156
6579
  CHECK(args[0]->IsObject());
157
13158
  Local<Object> process = args[0].As<Object>();
158
159
  // process.title
160

19737
  CHECK(process
161
            ->SetAccessor(
162
                context,
163
                FIXED_ONE_BYTE_STRING(isolate, "title"),
164
                ProcessTitleGetter,
165
                env->owns_process_state() ? ProcessTitleSetter : nullptr,
166
                Local<Value>(),
167
                DEFAULT,
168
                None,
169
                SideEffectType::kHasNoSideEffect)
170
            .FromJust());
171
172
  // process.argv
173
6579
  process->Set(context,
174
               FIXED_ONE_BYTE_STRING(isolate, "argv"),
175
26316
               ToV8Value(context, env->argv()).ToLocalChecked()).Check();
176
177
  // process.execArgv
178
6579
  process->Set(context,
179
               FIXED_ONE_BYTE_STRING(isolate, "execArgv"),
180
6579
               ToV8Value(context, env->exec_argv())
181
26316
                   .ToLocalChecked()).Check();
182
183
26316
  READONLY_PROPERTY(process, "pid",
184
                    Integer::New(isolate, uv_os_getpid()));
185
186
26316
  CHECK(process->SetAccessor(context,
187
                             FIXED_ONE_BYTE_STRING(isolate, "ppid"),
188
                             GetParentProcessId).FromJust());
189
190
  // --security-revert flags
191
#define V(code, _, __)                                                        \
192
  do {                                                                        \
193
    if (IsReverted(SECURITY_REVERT_ ## code)) {                               \
194
      READONLY_PROPERTY(process, "REVERT_" #code, True(isolate));             \
195
    }                                                                         \
196
  } while (0);
197
  SECURITY_REVERSIONS(V)
198
#undef V
199
200
  // process.execPath
201
  process
202
6579
      ->Set(context,
203
            FIXED_ONE_BYTE_STRING(isolate, "execPath"),
204
6579
            String::NewFromUtf8(isolate,
205
6579
                                env->exec_path().c_str(),
206
                                NewStringType::kInternalized,
207
6579
                                env->exec_path().size())
208
26316
                .ToLocalChecked())
209
      .Check();
210
211
  // process.debugPort
212

19737
  CHECK(process
213
            ->SetAccessor(context,
214
                          FIXED_ONE_BYTE_STRING(isolate, "debugPort"),
215
                          DebugPortGetter,
216
                          env->owns_process_state() ? DebugPortSetter : nullptr,
217
                          Local<Value>())
218
            .FromJust());
219
6579
}
220
221
5314
void RegisterProcessExternalReferences(ExternalReferenceRegistry* registry) {
222
5314
  registry->Register(RawDebug);
223
5314
  registry->Register(GetParentProcessId);
224
5314
  registry->Register(DebugPortSetter);
225
5314
  registry->Register(DebugPortGetter);
226
5314
  registry->Register(ProcessTitleSetter);
227
5314
  registry->Register(ProcessTitleGetter);
228
5314
}
229
230
}  // namespace node
231
232
5314
NODE_MODULE_EXTERNAL_REFERENCE(process_object,
233
                               node::RegisterProcessExternalReferences)