GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_process_object.cc Lines: 77 78 98.7 %
Date: 2021-10-02 04:13:01 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
34
static void ProcessTitleGetter(Local<Name> property,
34
                               const PropertyCallbackInfo<Value>& info) {
35
34
  std::string title = GetProcessTitle("node");
36
102
  info.GetReturnValue().Set(
37
34
      String::NewFromUtf8(info.GetIsolate(), title.data(),
38
68
                          NewStringType::kNormal, title.size())
39
      .ToLocalChecked());
40
34
}
41
42
99
static void ProcessTitleSetter(Local<Name> property,
43
                               Local<Value> value,
44
                               const PropertyCallbackInfo<void>& info) {
45
198
  node::Utf8Value title(info.GetIsolate(), value);
46

197
  TRACE_EVENT_METADATA1(
47
      "__metadata", "process_name", "name", TRACE_STR_COPY(*title));
48
99
  uv_set_process_title(*title);
49
99
}
50
51
127
static void DebugPortGetter(Local<Name> property,
52
                            const PropertyCallbackInfo<Value>& info) {
53
127
  Environment* env = Environment::GetCurrent(info);
54
127
  ExclusiveAccess<HostPort>::Scoped host_port(env->inspector_host_port());
55
127
  int port = host_port->port();
56
254
  info.GetReturnValue().Set(port);
57
127
}
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
26
static void GetParentProcessId(Local<Name> property,
76
                               const PropertyCallbackInfo<Value>& info) {
77
26
  info.GetReturnValue().Set(uv_os_getppid());
78
26
}
79
80
620
MaybeLocal<Object> CreateProcessObject(Environment* env) {
81
620
  Isolate* isolate = env->isolate();
82
620
  EscapableHandleScope scope(isolate);
83
620
  Local<Context> context = env->context();
84
85
620
  Local<FunctionTemplate> process_template = FunctionTemplate::New(isolate);
86
620
  process_template->SetClassName(env->process_string());
87
  Local<Function> process_ctor;
88
  Local<Object> process;
89
1860
  if (!process_template->GetFunction(context).ToLocal(&process_ctor) ||
90

1860
      !process_ctor->NewInstance(context).ToLocal(&process)) {
91
    return MaybeLocal<Object>();
92
  }
93
94
  // process.version
95
1860
  READONLY_PROPERTY(process,
96
                    "version",
97
                    FIXED_ONE_BYTE_STRING(env->isolate(), NODE_VERSION));
98
99
  // process.versions
100
620
  Local<Object> versions = Object::New(env->isolate());
101
1240
  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








42780
  NODE_VERSIONS_KEYS(V)
109
#undef V
110
111
  // process.arch
112
2480
  READONLY_STRING_PROPERTY(process, "arch", per_process::metadata.arch);
113
114
  // process.platform
115
1860
  READONLY_STRING_PROPERTY(process, "platform", per_process::metadata.platform);
116
117
  // process.release
118
620
  Local<Object> release = Object::New(env->isolate());
119
1860
  READONLY_PROPERTY(process, "release", release);
120
1860
  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
620
  env->SetMethod(process, "_rawDebug", RawDebug);
139
140
620
  return scope.Escape(process);
141
}
142
143
5430
void PatchProcessObject(const FunctionCallbackInfo<Value>& args) {
144
5430
  Isolate* isolate = args.GetIsolate();
145
5430
  Local<Context> context = isolate->GetCurrentContext();
146
5430
  Environment* env = Environment::GetCurrent(context);
147
5430
  CHECK(args[0]->IsObject());
148
10860
  Local<Object> process = args[0].As<Object>();
149
150
  // process.title
151

16290
  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
5430
  process->Set(context,
165
               FIXED_ONE_BYTE_STRING(isolate, "argv"),
166
21720
               ToV8Value(context, env->argv()).ToLocalChecked()).Check();
167
168
  // process.execArgv
169
5430
  process->Set(context,
170
               FIXED_ONE_BYTE_STRING(isolate, "execArgv"),
171
5430
               ToV8Value(context, env->exec_argv())
172
21720
                   .ToLocalChecked()).Check();
173
174
21720
  READONLY_PROPERTY(process, "pid",
175
                    Integer::New(isolate, uv_os_getpid()));
176
177
21720
  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
5430
      ->Set(context,
194
            FIXED_ONE_BYTE_STRING(isolate, "execPath"),
195
5430
            String::NewFromUtf8(isolate,
196
5430
                                env->exec_path().c_str(),
197
                                NewStringType::kInternalized,
198
5430
                                env->exec_path().size())
199
21720
                .ToLocalChecked())
200
      .Check();
201
202
  // process.debugPort
203

16290
  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
5430
}
211
212
4853
void RegisterProcessExternalReferences(ExternalReferenceRegistry* registry) {
213
4853
  registry->Register(RawDebug);
214
4853
  registry->Register(GetParentProcessId);
215
4853
  registry->Register(DebugPortSetter);
216
4853
  registry->Register(DebugPortGetter);
217
4853
  registry->Register(ProcessTitleSetter);
218
4853
  registry->Register(ProcessTitleGetter);
219
4853
}
220
221
}  // namespace node
222
223
4853
NODE_MODULE_EXTERNAL_REFERENCE(process_object,
224
                               node::RegisterProcessExternalReferences)