GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_env_var.cc Lines: 179 190 94.2 %
Date: 2020-07-19 22:14:24 Branches: 75 104 72.1 %

Line Branch Exec Source
1
#include "debug_utils-inl.h"
2
#include "env-inl.h"
3
#include "node_errors.h"
4
#include "node_external_reference.h"
5
#include "node_process.h"
6
7
#include <time.h>  // tzset(), _tzset()
8
9
namespace node {
10
using v8::Array;
11
using v8::Boolean;
12
using v8::Context;
13
using v8::EscapableHandleScope;
14
using v8::HandleScope;
15
using v8::Integer;
16
using v8::Isolate;
17
using v8::Just;
18
using v8::Local;
19
using v8::Maybe;
20
using v8::MaybeLocal;
21
using v8::Name;
22
using v8::NamedPropertyHandlerConfiguration;
23
using v8::NewStringType;
24
using v8::Nothing;
25
using v8::Object;
26
using v8::ObjectTemplate;
27
using v8::PropertyCallbackInfo;
28
using v8::PropertyHandlerFlags;
29
using v8::String;
30
using v8::Value;
31
32
9482
class RealEnvStore final : public KVStore {
33
 public:
34
  MaybeLocal<String> Get(Isolate* isolate, Local<String> key) const override;
35
  Maybe<std::string> Get(const char* key) const override;
36
  void Set(Isolate* isolate, Local<String> key, Local<String> value) override;
37
  int32_t Query(Isolate* isolate, Local<String> key) const override;
38
  int32_t Query(const char* key) const override;
39
  void Delete(Isolate* isolate, Local<String> key) override;
40
  Local<Array> Enumerate(Isolate* isolate) const override;
41
};
42
43
562
class MapKVStore final : public KVStore {
44
 public:
45
  MaybeLocal<String> Get(Isolate* isolate, Local<String> key) const override;
46
  Maybe<std::string> Get(const char* key) const override;
47
  void Set(Isolate* isolate, Local<String> key, Local<String> value) override;
48
  int32_t Query(Isolate* isolate, Local<String> key) const override;
49
  int32_t Query(const char* key) const override;
50
  void Delete(Isolate* isolate, Local<String> key) override;
51
  Local<Array> Enumerate(Isolate* isolate) const override;
52
53
  std::shared_ptr<KVStore> Clone(Isolate* isolate) const override;
54
55
557
  MapKVStore() = default;
56
7
  MapKVStore(const MapKVStore& other) : KVStore(), map_(other.map_) {}
57
58
 private:
59
  mutable Mutex mutex_;
60
  std::unordered_map<std::string, std::string> map_;
61
};
62
63
namespace per_process {
64
4990
Mutex env_var_mutex;
65
4990
std::shared_ptr<KVStore> system_environment = std::make_shared<RealEnvStore>();
66
}  // namespace per_process
67
68
template <typename T>
69
10375
void DateTimeConfigurationChangeNotification(Isolate* isolate, const T& key) {
70


10375
  if (key.length() == 2 && key[0] == 'T' && key[1] == 'Z') {
71
#ifdef __POSIX__
72
4
    tzset();
73
#else
74
    _tzset();
75
#endif
76
4
    auto constexpr time_zone_detection = Isolate::TimeZoneDetection::kRedetect;
77
4
    isolate->DateTimeConfigurationChangeNotification(time_zone_detection);
78
  }
79
10375
}
80
81
680388
Maybe<std::string> RealEnvStore::Get(const char* key) const {
82
1360776
  Mutex::ScopedLock lock(per_process::env_var_mutex);
83
84
680388
  size_t init_sz = 256;
85
1360776
  MaybeStackBuffer<char, 256> val;
86
680388
  int ret = uv_os_getenv(key, *val, &init_sz);
87
88
680388
  if (ret == UV_ENOBUFS) {
89
    // Buffer is not large enough, reallocate to the updated init_sz
90
    // and fetch env value again.
91
    val.AllocateSufficientStorage(init_sz);
92
    ret = uv_os_getenv(key, *val, &init_sz);
93
  }
94
95
680388
  if (ret >= 0) {  // Env key value fetch success.
96
622840
    return v8::Just(std::string(*val, init_sz));
97
  }
98
99
57548
  return v8::Nothing<std::string>();
100
}
101
102
680388
MaybeLocal<String> RealEnvStore::Get(Isolate* isolate,
103
                                     Local<String> property) const {
104
1360776
  node::Utf8Value key(isolate, property);
105
1360776
  Maybe<std::string> value = Get(*key);
106
107
680388
  if (value.IsJust()) {
108
622840
    std::string val = value.FromJust();
109
    return String::NewFromUtf8(
110
622840
        isolate, val.data(), NewStringType::kNormal, val.size());
111
  }
112
113
57548
  return MaybeLocal<String>();
114
}
115
116
8565
void RealEnvStore::Set(Isolate* isolate,
117
                       Local<String> property,
118
                       Local<String> value) {
119
17130
  Mutex::ScopedLock lock(per_process::env_var_mutex);
120
121
17130
  node::Utf8Value key(isolate, property);
122
17130
  node::Utf8Value val(isolate, value);
123
124
#ifdef _WIN32
125
  if (key.length() > 0 && key[0] == '=') return;
126
#endif
127
8565
  uv_os_setenv(*key, *val);
128
8565
  DateTimeConfigurationChangeNotification(isolate, key);
129
8565
}
130
131
804617
int32_t RealEnvStore::Query(const char* key) const {
132
1609234
  Mutex::ScopedLock lock(per_process::env_var_mutex);
133
134
  char val[2];
135
804617
  size_t init_sz = sizeof(val);
136
804617
  int ret = uv_os_getenv(key, val, &init_sz);
137
138
804617
  if (ret == UV_ENOENT) {
139
5621
    return -1;
140
  }
141
142
#ifdef _WIN32
143
  if (key[0] == '=') {
144
    return static_cast<int32_t>(v8::ReadOnly) |
145
           static_cast<int32_t>(v8::DontDelete) |
146
           static_cast<int32_t>(v8::DontEnum);
147
  }
148
#endif
149
150
798996
  return 0;
151
}
152
153
804617
int32_t RealEnvStore::Query(Isolate* isolate, Local<String> property) const {
154
1609234
  node::Utf8Value key(isolate, property);
155
1609234
  return Query(*key);
156
}
157
158
1810
void RealEnvStore::Delete(Isolate* isolate, Local<String> property) {
159
3620
  Mutex::ScopedLock lock(per_process::env_var_mutex);
160
161
3620
  node::Utf8Value key(isolate, property);
162
1810
  uv_os_unsetenv(*key);
163
1810
  DateTimeConfigurationChangeNotification(isolate, key);
164
1810
}
165
166
8606
Local<Array> RealEnvStore::Enumerate(Isolate* isolate) const {
167
17212
  Mutex::ScopedLock lock(per_process::env_var_mutex);
168
  uv_env_item_t* items;
169
  int count;
170
171
25818
  auto cleanup = OnScopeLeave([&]() { uv_os_free_environ(items, count); });
172
8606
  CHECK_EQ(uv_os_environ(&items, &count), 0);
173
174
17212
  MaybeStackBuffer<Local<Value>, 256> env_v(count);
175
8606
  int env_v_index = 0;
176
611563
  for (int i = 0; i < count; i++) {
177
#ifdef _WIN32
178
    // If the key starts with '=' it is a hidden environment variable.
179
    // The '\0' check is a workaround for the bug behind
180
    // https://github.com/libuv/libuv/pull/2473 and can be removed later.
181
    if (items[i].name[0] == '=' || items[i].name[0] == '\0') continue;
182
#endif
183
602957
    MaybeLocal<String> str = String::NewFromUtf8(isolate, items[i].name);
184
602957
    if (str.IsEmpty()) {
185
      isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
186
      return Local<Array>();
187
    }
188
1205914
    env_v[env_v_index++] = str.ToLocalChecked();
189
  }
190
191
8606
  return Array::New(isolate, env_v.out(), env_v_index);
192
}
193
194
533
std::shared_ptr<KVStore> KVStore::Clone(v8::Isolate* isolate) const {
195
1066
  HandleScope handle_scope(isolate);
196
533
  Local<Context> context = isolate->GetCurrentContext();
197
198
533
  std::shared_ptr<KVStore> copy = KVStore::CreateMapKVStore();
199
533
  Local<Array> keys = Enumerate(isolate);
200
533
  uint32_t keys_length = keys->Length();
201
37351
  for (uint32_t i = 0; i < keys_length; i++) {
202
73636
    Local<Value> key = keys->Get(context, i).ToLocalChecked();
203
73636
    CHECK(key->IsString());
204
36818
    copy->Set(isolate,
205
              key.As<String>(),
206
110454
              Get(isolate, key.As<String>()).ToLocalChecked());
207
  }
208
1066
  return copy;
209
}
210
211
29480
Maybe<std::string> MapKVStore::Get(const char* key) const {
212
58960
  Mutex::ScopedLock lock(mutex_);
213
29480
  auto it = map_.find(key);
214
58960
  return it == map_.end() ? v8::Nothing<std::string>() : v8::Just(it->second);
215
}
216
217
29308
MaybeLocal<String> MapKVStore::Get(Isolate* isolate, Local<String> key) const {
218
58616
  Utf8Value str(isolate, key);
219
58616
  Maybe<std::string> value = Get(*str);
220
31107
  if (value.IsNothing()) return Local<String>();
221
27509
  std::string val = value.FromJust();
222
  return String::NewFromUtf8(
223
27509
      isolate, val.data(), NewStringType::kNormal, val.size());
224
}
225
226
38414
void MapKVStore::Set(Isolate* isolate, Local<String> key, Local<String> value) {
227
76828
  Mutex::ScopedLock lock(mutex_);
228
76828
  Utf8Value key_str(isolate, key);
229
76828
  Utf8Value value_str(isolate, value);
230


38414
  if (*key_str != nullptr && key_str.length() > 0 && *value_str != nullptr) {
231
76828
    map_[std::string(*key_str, key_str.length())] =
232
115242
        std::string(*value_str, value_str.length());
233
  }
234
38414
}
235
236
51417
int32_t MapKVStore::Query(const char* key) const {
237
102834
  Mutex::ScopedLock lock(mutex_);
238
102834
  return map_.find(key) == map_.end() ? -1 : 0;
239
}
240
241
51417
int32_t MapKVStore::Query(Isolate* isolate, Local<String> key) const {
242
102834
  Utf8Value str(isolate, key);
243
102834
  return Query(*str);
244
}
245
246
void MapKVStore::Delete(Isolate* isolate, Local<String> key) {
247
  Mutex::ScopedLock lock(mutex_);
248
  Utf8Value str(isolate, key);
249
  map_.erase(std::string(*str, str.length()));
250
}
251
252
720
Local<Array> MapKVStore::Enumerate(Isolate* isolate) const {
253
1440
  Mutex::ScopedLock lock(mutex_);
254
1440
  std::vector<Local<Value>> values;
255
720
  values.reserve(map_.size());
256
51565
  for (const auto& pair : map_) {
257
    values.emplace_back(
258
101690
        String::NewFromUtf8(isolate, pair.first.data(),
259
50845
                            NewStringType::kNormal, pair.first.size())
260
50845
            .ToLocalChecked());
261
  }
262
1440
  return Array::New(isolate, values.data(), values.size());
263
}
264
265
7
std::shared_ptr<KVStore> MapKVStore::Clone(Isolate* isolate) const {
266
7
  return std::make_shared<MapKVStore>(*this);
267
}
268
269
557
std::shared_ptr<KVStore> KVStore::CreateMapKVStore() {
270
557
  return std::make_shared<MapKVStore>();
271
}
272
273
24
Maybe<bool> KVStore::AssignFromObject(Local<Context> context,
274
                                      Local<Object> entries) {
275
24
  Isolate* isolate = context->GetIsolate();
276
48
  HandleScope handle_scope(isolate);
277
  Local<Array> keys;
278
48
  if (!entries->GetOwnPropertyNames(context).ToLocal(&keys))
279
    return Nothing<bool>();
280
24
  uint32_t keys_length = keys->Length();
281
1614
  for (uint32_t i = 0; i < keys_length; i++) {
282
    Local<Value> key;
283
3180
    if (!keys->Get(context, i).ToLocal(&key))
284
      return Nothing<bool>();
285
3180
    if (!key->IsString()) continue;
286
287
    Local<Value> value;
288
    Local<String> value_string;
289

6360
    if (!entries->Get(context, key).ToLocal(&value) ||
290
4770
        !value->ToString(context).ToLocal(&value_string)) {
291
      return Nothing<bool>();
292
    }
293
294
3180
    Set(isolate, key.As<String>(), value_string);
295
  }
296
24
  return Just(true);
297
}
298
299
657818
static void EnvGetter(Local<Name> property,
300
                      const PropertyCallbackInfo<Value>& info) {
301
657818
  Environment* env = Environment::GetCurrent(info);
302
657818
  CHECK(env->has_run_bootstrapping_code());
303
657818
  if (property->IsSymbol()) {
304
12960
    return info.GetReturnValue().SetUndefined();
305
  }
306
1302676
  CHECK(property->IsString());
307
  MaybeLocal<String> value_string =
308
1302676
      env->env_vars()->Get(env->isolate(), property.As<String>());
309
651338
  if (!value_string.IsEmpty()) {
310
1205576
    info.GetReturnValue().Set(value_string.ToLocalChecked());
311
  }
312
}
313
314
8573
static void EnvSetter(Local<Name> property,
315
                      Local<Value> value,
316
                      const PropertyCallbackInfo<Value>& info) {
317
8573
  Environment* env = Environment::GetCurrent(info);
318
8573
  CHECK(env->has_run_bootstrapping_code());
319
  // calling env->EmitProcessEnvWarning() sets a variable indicating that
320
  // warnings have been emitted. It should be called last after other
321
  // conditions leading to a warning have been met.
322


25756
  if (env->options()->pending_deprecation && !value->IsString() &&
323

25722
      !value->IsNumber() && !value->IsBoolean() &&
324
1
      env->EmitProcessEnvWarning()) {
325
2
    if (ProcessEmitDeprecationWarning(
326
            env,
327
            "Assigning any value other than a string, number, or boolean to a "
328
            "process.env property is deprecated. Please make sure to convert "
329
            "the "
330
            "value to a string before setting process.env with it.",
331
            "DEP0104")
332
            .IsNothing())
333
2
      return;
334
  }
335
336
  Local<String> key;
337
  Local<String> value_string;
338

42864
  if (!property->ToString(env->context()).ToLocal(&key) ||
339
34289
      !value->ToString(env->context()).ToLocal(&value_string)) {
340
2
    return;
341
  }
342
343
8571
  env->env_vars()->Set(env->isolate(), key, value_string);
344
345
  // Whether it worked or not, always return value.
346
17142
  info.GetReturnValue().Set(value);
347
}
348
349
856035
static void EnvQuery(Local<Name> property,
350
                     const PropertyCallbackInfo<Integer>& info) {
351
856035
  Environment* env = Environment::GetCurrent(info);
352
856035
  CHECK(env->has_run_bootstrapping_code());
353
1712070
  if (property->IsString()) {
354
1712068
    int32_t rc = env->env_vars()->Query(env->isolate(), property.As<String>());
355
2556136
    if (rc != -1) info.GetReturnValue().Set(rc);
356
  }
357
856035
}
358
359
1811
static void EnvDeleter(Local<Name> property,
360
                       const PropertyCallbackInfo<Boolean>& info) {
361
1811
  Environment* env = Environment::GetCurrent(info);
362
1811
  CHECK(env->has_run_bootstrapping_code());
363
3622
  if (property->IsString()) {
364
3620
    env->env_vars()->Delete(env->isolate(), property.As<String>());
365
  }
366
367
  // process.env never has non-configurable properties, so always
368
  // return true like the tc39 delete operator.
369
3622
  info.GetReturnValue().Set(true);
370
1811
}
371
372
8793
static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
373
8793
  Environment* env = Environment::GetCurrent(info);
374
8793
  CHECK(env->has_run_bootstrapping_code());
375
376
26379
  info.GetReturnValue().Set(
377
17586
      env->env_vars()->Enumerate(env->isolate()));
378
8793
}
379
380
384
MaybeLocal<Object> CreateEnvVarProxy(Local<Context> context, Isolate* isolate) {
381
384
  EscapableHandleScope scope(isolate);
382
384
  Local<ObjectTemplate> env_proxy_template = ObjectTemplate::New(isolate);
383
1152
  env_proxy_template->SetHandler(NamedPropertyHandlerConfiguration(
384
      EnvGetter, EnvSetter, EnvQuery, EnvDeleter, EnvEnumerator, Local<Value>(),
385
384
      PropertyHandlerFlags::kHasNoSideEffect));
386
768
  return scope.EscapeMaybe(env_proxy_template->NewInstance(context));
387
}
388
389
4920
void RegisterEnvVarExternalReferences(ExternalReferenceRegistry* registry) {
390
4920
  registry->Register(EnvGetter);
391
4920
  registry->Register(EnvSetter);
392
4920
  registry->Register(EnvQuery);
393
4920
  registry->Register(EnvDeleter);
394
4920
  registry->Register(EnvEnumerator);
395
4920
}
396
}  // namespace node
397
398

19890
NODE_MODULE_EXTERNAL_REFERENCE(env_var, node::RegisterEnvVarExternalReferences)