GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_process_object.cc Lines: 81 83 97.6 %
Date: 2022-08-12 04:19:25 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
100
static void ProcessTitleGetter(Local<Name> property,
34
                               const PropertyCallbackInfo<Value>& info) {
35
100
  std::string title = GetProcessTitle("node");
36
300
  info.GetReturnValue().Set(
37
100
      String::NewFromUtf8(info.GetIsolate(), title.data(),
38
200
                          NewStringType::kNormal, title.size())
39
      .ToLocalChecked());
40
100
}
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
195
static void DebugPortGetter(Local<Name> property,
52
                            const PropertyCallbackInfo<Value>& info) {
53
195
  Environment* env = Environment::GetCurrent(info);
54
195
  ExclusiveAccess<HostPort>::Scoped host_port(env->inspector_host_port());
55
195
  int port = host_port->port();
56
390
  info.GetReturnValue().Set(port);
57
195
}
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
94
static void GetParentProcessId(Local<Name> property,
76
                               const PropertyCallbackInfo<Value>& info) {
77
94
  info.GetReturnValue().Set(uv_os_getppid());
78
94
}
79
80
771
MaybeLocal<Object> CreateProcessObject(Environment* env) {
81
771
  Isolate* isolate = env->isolate();
82
771
  EscapableHandleScope scope(isolate);
83
771
  Local<Context> context = env->context();
84
85
771
  Local<FunctionTemplate> process_template = FunctionTemplate::New(isolate);
86
771
  process_template->SetClassName(env->process_string());
87
  Local<Function> process_ctor;
88
  Local<Object> process;
89
2313
  if (!process_template->GetFunction(context).ToLocal(&process_ctor) ||
90

2313
      !process_ctor->NewInstance(context).ToLocal(&process)) {
91
    return MaybeLocal<Object>();
92
  }
93
94
  // process[exiting_aliased_Uint32Array]
95
771
  if (process
96
771
          ->SetPrivate(context,
97
                       env->exiting_aliased_Uint32Array(),
98
1542
                       env->exiting().GetJSArray())
99
771
          .IsNothing()) {
100
    return {};
101
  }
102
103
  // process.version
104
2313
  READONLY_PROPERTY(process,
105
                    "version",
106
                    FIXED_ONE_BYTE_STRING(env->isolate(), NODE_VERSION));
107
108
  // process.versions
109
771
  Local<Object> versions = Object::New(env->isolate());
110
1542
  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








53199
  NODE_VERSIONS_KEYS(V)
118
#undef V
119
120
  // process.arch
121
3084
  READONLY_STRING_PROPERTY(process, "arch", per_process::metadata.arch);
122
123
  // process.platform
124
2313
  READONLY_STRING_PROPERTY(process, "platform", per_process::metadata.platform);
125
126
  // process.release
127
771
  Local<Object> release = Object::New(env->isolate());
128
2313
  READONLY_PROPERTY(process, "release", release);
129
2313
  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
771
  SetMethod(context, process, "_rawDebug", RawDebug);
148
149
771
  return scope.Escape(process);
150
}
151
152
6065
void PatchProcessObject(const FunctionCallbackInfo<Value>& args) {
153
6065
  Isolate* isolate = args.GetIsolate();
154
6065
  Local<Context> context = isolate->GetCurrentContext();
155
6065
  Environment* env = Environment::GetCurrent(context);
156
6065
  CHECK(args[0]->IsObject());
157
12130
  Local<Object> process = args[0].As<Object>();
158
159
  // process.title
160

18195
  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
6065
  process->Set(context,
174
               FIXED_ONE_BYTE_STRING(isolate, "argv"),
175
24260
               ToV8Value(context, env->argv()).ToLocalChecked()).Check();
176
177
  // process.execArgv
178
6065
  process->Set(context,
179
               FIXED_ONE_BYTE_STRING(isolate, "execArgv"),
180
6065
               ToV8Value(context, env->exec_argv())
181
24260
                   .ToLocalChecked()).Check();
182
183
24260
  READONLY_PROPERTY(process, "pid",
184
                    Integer::New(isolate, uv_os_getpid()));
185
186
24260
  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
6065
      ->Set(context,
203
            FIXED_ONE_BYTE_STRING(isolate, "execPath"),
204
6065
            String::NewFromUtf8(isolate,
205
6065
                                env->exec_path().c_str(),
206
                                NewStringType::kInternalized,
207
6065
                                env->exec_path().size())
208
24260
                .ToLocalChecked())
209
      .Check();
210
211
  // process.debugPort
212

18195
  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
6065
}
220
221
5337
void RegisterProcessExternalReferences(ExternalReferenceRegistry* registry) {
222
5337
  registry->Register(RawDebug);
223
5337
  registry->Register(GetParentProcessId);
224
5337
  registry->Register(DebugPortSetter);
225
5337
  registry->Register(DebugPortGetter);
226
5337
  registry->Register(ProcessTitleSetter);
227
5337
  registry->Register(ProcessTitleGetter);
228
5337
}
229
230
}  // namespace node
231
232
5337
NODE_MODULE_EXTERNAL_REFERENCE(process_object,
233
                               node::RegisterProcessExternalReferences)