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: 58 64 90.6 %
Date: 2019-05-05 22:32:45 Branches: 33 60 55.0 %

Line Branch Exec Source
1
#include "env-inl.h"
2
#include "node_internals.h"
3
#include "node_options-inl.h"
4
#include "node_metadata.h"
5
#include "node_process.h"
6
#include "node_revert.h"
7
#include "util-inl.h"
8
9
#include <climits>  // PATH_MAX
10
11
namespace node {
12
using v8::Context;
13
using v8::DEFAULT;
14
using v8::EscapableHandleScope;
15
using v8::Function;
16
using v8::FunctionCallbackInfo;
17
using v8::FunctionTemplate;
18
using v8::HandleScope;
19
using v8::Integer;
20
using v8::Isolate;
21
using v8::Just;
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
11
static void ProcessTitleGetter(Local<Name> property,
34
                               const PropertyCallbackInfo<Value>& info) {
35
  char buffer[512];
36
11
  uv_get_process_title(buffer, sizeof(buffer));
37
  info.GetReturnValue().Set(
38
11
      String::NewFromUtf8(info.GetIsolate(), buffer, NewStringType::kNormal)
39
33
          .ToLocalChecked());
40
11
}
41
42
88
static void ProcessTitleSetter(Local<Name> property,
43
                               Local<Value> value,
44
                               const PropertyCallbackInfo<void>& info) {
45
88
  node::Utf8Value title(info.GetIsolate(), value);
46

177
  TRACE_EVENT_METADATA1(
47
      "__metadata", "process_name", "name", TRACE_STR_COPY(*title));
48
88
  uv_set_process_title(*title);
49
88
}
50
51
84
static void DebugPortGetter(Local<Name> property,
52
                            const PropertyCallbackInfo<Value>& info) {
53
84
  Environment* env = Environment::GetCurrent(info);
54
84
  int port = env->inspector_host_port()->port();
55
168
  info.GetReturnValue().Set(port);
56
84
}
57
58
static void DebugPortSetter(Local<Name> property,
59
                            Local<Value> value,
60
                            const PropertyCallbackInfo<void>& info) {
61
  Environment* env = Environment::GetCurrent(info);
62
  int32_t port = value->Int32Value(env->context()).FromMaybe(0);
63
  env->inspector_host_port()->set_port(static_cast<int>(port));
64
}
65
66
6
static void GetParentProcessId(Local<Name> property,
67
                               const PropertyCallbackInfo<Value>& info) {
68
18
  info.GetReturnValue().Set(uv_os_getppid());
69
6
}
70
71
4652
MaybeLocal<Object> CreateProcessObject(
72
    Environment* env,
73
    const std::vector<std::string>& args,
74
    const std::vector<std::string>& exec_args) {
75
4652
  Isolate* isolate = env->isolate();
76
4652
  EscapableHandleScope scope(isolate);
77
4652
  Local<Context> context = env->context();
78
79
4652
  Local<FunctionTemplate> process_template = FunctionTemplate::New(isolate);
80
9304
  process_template->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "process"));
81
  Local<Function> process_ctor;
82
  Local<Object> process;
83


27912
  if (!process_template->GetFunction(context).ToLocal(&process_ctor) ||
84
13956
      !process_ctor->NewInstance(context).ToLocal(&process)) {
85
    return MaybeLocal<Object>();
86
  }
87
88
  // process.version
89
18608
  READONLY_PROPERTY(process,
90
                    "version",
91
                    FIXED_ONE_BYTE_STRING(env->isolate(), NODE_VERSION));
92
93
  // process.versions
94
4652
  Local<Object> versions = Object::New(env->isolate());
95
13956
  READONLY_PROPERTY(process, "versions", versions);
96
97
#define V(key)                                                                 \
98
  if (!per_process::metadata.versions.key.empty()) {                           \
99
    READONLY_STRING_PROPERTY(                                                  \
100
        versions, #key, per_process::metadata.versions.key);                   \
101
  }
102








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

18572
  CHECK(process
146
            ->SetAccessor(
147
                context,
148
                FIXED_ONE_BYTE_STRING(isolate, "title"),
149
                ProcessTitleGetter,
150
                env->owns_process_state() ? ProcessTitleSetter : nullptr,
151
                env->as_callback_data(),
152
                DEFAULT,
153
                None,
154
                SideEffectType::kHasNoSideEffect)
155
            .FromJust());
156
157
  // process.argv
158
  process->Set(context,
159
               FIXED_ONE_BYTE_STRING(isolate, "argv"),
160
18572
               ToV8Value(context, env->argv()).ToLocalChecked()).Check();
161
162
  // process.execArgv
163
  process->Set(context,
164
               FIXED_ONE_BYTE_STRING(isolate, "execArgv"),
165
4643
               ToV8Value(context, env->exec_argv())
166
18572
                   .ToLocalChecked()).Check();
167
168
18572
  READONLY_PROPERTY(process, "pid",
169
                    Integer::New(isolate, uv_os_getpid()));
170
171
13929
  CHECK(process->SetAccessor(context,
172
                             FIXED_ONE_BYTE_STRING(isolate, "ppid"),
173
                             GetParentProcessId).FromJust());
174
175
  // --security-revert flags
176
#define V(code, _, __)                                                        \
177
  do {                                                                        \
178
    if (IsReverted(SECURITY_REVERT_ ## code)) {                               \
179
      READONLY_PROPERTY(process, "REVERT_" #code, True(isolate));             \
180
    }                                                                         \
181
  } while (0);
182
  SECURITY_REVERSIONS(V)
183
#undef V
184
185
  // process.execPath
186
  {
187
    char exec_path_buf[2 * PATH_MAX];
188
4643
    size_t exec_path_len = sizeof(exec_path_buf);
189
4643
    std::string exec_path;
190
4643
    if (uv_exepath(exec_path_buf, &exec_path_len) == 0) {
191
4643
      exec_path = std::string(exec_path_buf, exec_path_len);
192
    } else {
193
      exec_path = env->argv()[0];
194
    }
195
    // On OpenBSD process.execPath will be relative unless we
196
    // get the full path before process.execPath is used.
197
#if defined(__OpenBSD__)
198
    uv_fs_t req;
199
    req.ptr = nullptr;
200
    if (0 ==
201
        uv_fs_realpath(env->event_loop(), &req, exec_path.c_str(), nullptr)) {
202
      CHECK_NOT_NULL(req.ptr);
203
      exec_path = std::string(static_cast<char*>(req.ptr));
204
    }
205
    uv_fs_req_cleanup(&req);
206
#endif
207
    process
208
        ->Set(context,
209
              FIXED_ONE_BYTE_STRING(isolate, "execPath"),
210
              String::NewFromUtf8(isolate,
211
                                  exec_path.c_str(),
212
                                  NewStringType::kInternalized,
213
4643
                                  exec_path.size())
214
18572
                  .ToLocalChecked())
215
13929
        .Check();
216
  }
217
218
  // process.debugPort
219

18572
  CHECK(process
220
            ->SetAccessor(context,
221
                          FIXED_ONE_BYTE_STRING(isolate, "debugPort"),
222
                          DebugPortGetter,
223
                          env->owns_process_state() ? DebugPortSetter : nullptr,
224
                          env->as_callback_data())
225
            .FromJust());
226
4643
}
227
228
}  // namespace node