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


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


37374
  if (*key_str != nullptr && key_str.length() > 0 && *value_str != nullptr) {
231
74748
    map_[std::string(*key_str, key_str.length())] =
232
112122
        std::string(*value_str, value_str.length());
233
  }
234
37374
}
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
6
std::shared_ptr<KVStore> MapKVStore::Clone(Isolate* isolate) const {
266
6
  return std::make_shared<MapKVStore>(*this);
267
}
268
269
542
std::shared_ptr<KVStore> KVStore::CreateMapKVStore() {
270
542
  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
477434
static void EnvGetter(Local<Name> property,
300
                      const PropertyCallbackInfo<Value>& info) {
301
477434
  Environment* env = Environment::GetCurrent(info);
302
477434
  CHECK(env->has_run_bootstrapping_code());
303
477434
  if (property->IsSymbol()) {
304
12960
    return info.GetReturnValue().SetUndefined();
305
  }
306
941908
  CHECK(property->IsString());
307
  MaybeLocal<String> value_string =
308
941908
      env->env_vars()->Get(env->isolate(), property.As<String>());
309
470954
  if (!value_string.IsEmpty()) {
310
854006
    info.GetReturnValue().Set(value_string.ToLocalChecked());
311
  }
312
}
313
314
6187
static void EnvSetter(Local<Name> property,
315
                      Local<Value> value,
316
                      const PropertyCallbackInfo<Value>& info) {
317
6187
  Environment* env = Environment::GetCurrent(info);
318
6187
  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


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

18564
      !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

30934
  if (!property->ToString(env->context()).ToLocal(&key) ||
339
24745
      !value->ToString(env->context()).ToLocal(&value_string)) {
340
2
    return;
341
  }
342
343
6185
  env->env_vars()->Set(env->isolate(), key, value_string);
344
345
  // Whether it worked or not, always return value.
346
12370
  info.GetReturnValue().Set(value);
347
}
348
349
590897
static void EnvQuery(Local<Name> property,
350
                     const PropertyCallbackInfo<Integer>& info) {
351
590897
  Environment* env = Environment::GetCurrent(info);
352
590897
  CHECK(env->has_run_bootstrapping_code());
353
1181794
  if (property->IsString()) {
354
1181792
    int32_t rc = env->env_vars()->Query(env->isolate(), property.As<String>());
355
1761524
    if (rc != -1) info.GetReturnValue().Set(rc);
356
  }
357
590897
}
358
359
774
static void EnvDeleter(Local<Name> property,
360
                       const PropertyCallbackInfo<Boolean>& info) {
361
774
  Environment* env = Environment::GetCurrent(info);
362
774
  CHECK(env->has_run_bootstrapping_code());
363
1548
  if (property->IsString()) {
364
1546
    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
1548
  info.GetReturnValue().Set(true);
370
774
}
371
372
6738
static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
373
6738
  Environment* env = Environment::GetCurrent(info);
374
6738
  CHECK(env->has_run_bootstrapping_code());
375
376
20214
  info.GetReturnValue().Set(
377
13476
      env->env_vars()->Enumerate(env->isolate()));
378
6738
}
379
380
4594
MaybeLocal<Object> CreateEnvVarProxy(Local<Context> context, Isolate* isolate) {
381
4594
  EscapableHandleScope scope(isolate);
382
4594
  Local<ObjectTemplate> env_proxy_template = ObjectTemplate::New(isolate);
383
13782
  env_proxy_template->SetHandler(NamedPropertyHandlerConfiguration(
384
      EnvGetter, EnvSetter, EnvQuery, EnvDeleter, EnvEnumerator, Local<Value>(),
385
4594
      PropertyHandlerFlags::kHasNoSideEffect));
386
9188
  return scope.EscapeMaybe(env_proxy_template->NewInstance(context));
387
}
388

12978
}  // namespace node