GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_process_object.cc Lines: 97 98 99.0 %
Date: 2021-05-28 04:11:57 Branches: 40 66 60.6 %

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
31
static void ProcessTitleGetter(Local<Name> property,
34
                               const PropertyCallbackInfo<Value>& info) {
35
62
  std::string title = GetProcessTitle("node");
36
62
  info.GetReturnValue().Set(
37
62
      String::NewFromUtf8(info.GetIsolate(), title.data(),
38
62
                          NewStringType::kNormal, title.size())
39
      .ToLocalChecked());
40
31
}
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
  TRACE_EVENT_METADATA1(
47
101
      "__metadata", "process_name", "name", TRACE_STR_COPY(*title));
48
101
  uv_set_process_title(*title);
49
199
}
50
101
51
123
static void DebugPortGetter(Local<Name> property,
52
2
                            const PropertyCallbackInfo<Value>& info) {
53
123
  Environment* env = Environment::GetCurrent(info);
54
246
  ExclusiveAccess<HostPort>::Scoped host_port(env->inspector_host_port());
55
123
  int port = host_port->port();
56
246
  info.GetReturnValue().Set(port);
57
123
}
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
84
  int32_t port = value->Int32Value(env->context()).FromMaybe(0);
64
65

28
  if ((port != 0 && port < 1024) || port > 65535) {
66
    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
22
static void GetParentProcessId(Local<Name> property,
76
                               const PropertyCallbackInfo<Value>& info) {
77
66
  info.GetReturnValue().Set(uv_os_getppid());
78
22
}
79
80
468
MaybeLocal<Object> CreateProcessObject(Environment* env) {
81
468
  Isolate* isolate = env->isolate();
82
468
  EscapableHandleScope scope(isolate);
83
468
  Local<Context> context = env->context();
84
85
468
  Local<FunctionTemplate> process_template = FunctionTemplate::New(isolate);
86
936
  process_template->SetClassName(env->process_string());
87
  Local<Function> process_ctor;
88
  Local<Object> process;
89

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

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

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

15603
  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
5201
}
211
212
4774
void RegisterProcessExternalReferences(ExternalReferenceRegistry* registry) {
213
4774
  registry->Register(RawDebug);
214
4774
}
215
216
}  // namespace node
217
218

19294
NODE_MODULE_EXTERNAL_REFERENCE(process_object,
219
                               node::RegisterProcessExternalReferences)