GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_process_object.cc Lines: 77 78 98.7 %
Date: 2022-05-22 04:15:48 Branches: 40 64 62.5 %

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
52
static void ProcessTitleGetter(Local<Name> property,
34
                               const PropertyCallbackInfo<Value>& info) {
35
52
  std::string title = GetProcessTitle("node");
36
156
  info.GetReturnValue().Set(
37
52
      String::NewFromUtf8(info.GetIsolate(), title.data(),
38
104
                          NewStringType::kNormal, title.size())
39
      .ToLocalChecked());
40
52
}
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
147
static void DebugPortGetter(Local<Name> property,
52
                            const PropertyCallbackInfo<Value>& info) {
53
147
  Environment* env = Environment::GetCurrent(info);
54
147
  ExclusiveAccess<HostPort>::Scoped host_port(env->inspector_host_port());
55
147
  int port = host_port->port();
56
294
  info.GetReturnValue().Set(port);
57
147
}
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
46
static void GetParentProcessId(Local<Name> property,
76
                               const PropertyCallbackInfo<Value>& info) {
77
46
  info.GetReturnValue().Set(uv_os_getppid());
78
46
}
79
80
848
MaybeLocal<Object> CreateProcessObject(Environment* env) {
81
848
  Isolate* isolate = env->isolate();
82
848
  EscapableHandleScope scope(isolate);
83
848
  Local<Context> context = env->context();
84
85
848
  Local<FunctionTemplate> process_template = FunctionTemplate::New(isolate);
86
848
  process_template->SetClassName(env->process_string());
87
  Local<Function> process_ctor;
88
  Local<Object> process;
89
2544
  if (!process_template->GetFunction(context).ToLocal(&process_ctor) ||
90

2544
      !process_ctor->NewInstance(context).ToLocal(&process)) {
91
    return MaybeLocal<Object>();
92
  }
93
94
  // process.version
95
2544
  READONLY_PROPERTY(process,
96
                    "version",
97
                    FIXED_ONE_BYTE_STRING(env->isolate(), NODE_VERSION));
98
99
  // process.versions
100
848
  Local<Object> versions = Object::New(env->isolate());
101
1696
  READONLY_PROPERTY(process, "versions", versions);
102
103
#define V(key)                                                                 \
104
  if (!per_process::metadata.versions.key.empty()) {                           \
105
    READONLY_STRING_PROPERTY(                                                  \
106
        versions, #key, per_process::metadata.versions.key);                   \
107
  }
108








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

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

17973
  CHECK(process
204
            ->SetAccessor(context,
205
                          FIXED_ONE_BYTE_STRING(isolate, "debugPort"),
206
                          DebugPortGetter,
207
                          env->owns_process_state() ? DebugPortSetter : nullptr,
208
                          Local<Value>())
209
            .FromJust());
210
5991
}
211
212
5184
void RegisterProcessExternalReferences(ExternalReferenceRegistry* registry) {
213
5184
  registry->Register(RawDebug);
214
5184
  registry->Register(GetParentProcessId);
215
5184
  registry->Register(DebugPortSetter);
216
5184
  registry->Register(DebugPortGetter);
217
5184
  registry->Register(ProcessTitleSetter);
218
5184
  registry->Register(ProcessTitleGetter);
219
5184
}
220
221
}  // namespace node
222
223
5184
NODE_MODULE_EXTERNAL_REFERENCE(process_object,
224
                               node::RegisterProcessExternalReferences)