GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/util.cc Lines: 95 109 87.2 %
Date: 2020-02-19 22:14:06 Branches: 24 42 57.1 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#include "util.h"  // NOLINT(build/include_inline)
23
#include "util-inl.h"
24
25
#include "debug_utils-inl.h"
26
#include "env-inl.h"
27
#include "node_buffer.h"
28
#include "node_errors.h"
29
#include "node_internals.h"
30
#include "string_bytes.h"
31
#include "uv.h"
32
33
#ifdef _WIN32
34
#include <io.h>  // _S_IREAD _S_IWRITE
35
#include <time.h>
36
#ifndef S_IRUSR
37
#define S_IRUSR _S_IREAD
38
#endif  // S_IRUSR
39
#ifndef S_IWUSR
40
#define S_IWUSR _S_IWRITE
41
#endif  // S_IWUSR
42
#else
43
#include <sys/time.h>
44
#include <sys/types.h>
45
#endif
46
47
#include <atomic>
48
#include <cstdio>
49
#include <cstring>
50
#include <iomanip>
51
#include <sstream>
52
53
static std::atomic_int seq = {0};  // Sequence number for diagnostic filenames.
54
55
namespace node {
56
57
using v8::ArrayBufferView;
58
using v8::Isolate;
59
using v8::Local;
60
using v8::String;
61
using v8::Value;
62
63
template <typename T>
64
3474924
static void MakeUtf8String(Isolate* isolate,
65
                           Local<Value> value,
66
                           T* target) {
67
  Local<String> string;
68

10424779
  if (!value->ToString(isolate->GetCurrentContext()).ToLocal(&string)) return;
69
70
  size_t storage;
71

6949850
  if (!StringBytes::StorageSize(isolate, string, UTF8).To(&storage)) return;
72
3474924
  storage += 1;
73
3474924
  target->AllocateSufficientStorage(storage);
74
  const int flags =
75
3474930
      String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8;
76
  const int length =
77
6949855
      string->WriteUtf8(isolate, target->out(), storage, nullptr, flags);
78
3474928
  target->SetLengthAndZeroTerminate(length);
79
}
80
81
3290971
Utf8Value::Utf8Value(Isolate* isolate, Local<Value> value) {
82
3290970
  if (value.IsEmpty())
83
1465
    return;
84
85
3289505
  MakeUtf8String(isolate, value, this);
86
}
87
88
89
2120
TwoByteValue::TwoByteValue(Isolate* isolate, Local<Value> value) {
90
2120
  if (value.IsEmpty()) {
91
    return;
92
  }
93
94
  Local<String> string;
95
6360
  if (!value->ToString(isolate->GetCurrentContext()).ToLocal(&string)) return;
96
97
  // Allocate enough space to include the null terminator
98
2120
  const size_t storage = string->Length() + 1;
99
2120
  AllocateSufficientStorage(storage);
100
101
2120
  const int flags = String::NO_NULL_TERMINATION;
102
4240
  const int length = string->Write(isolate, out(), 0, storage, flags);
103
2120
  SetLengthAndZeroTerminate(length);
104
}
105
106
188954
BufferValue::BufferValue(Isolate* isolate, Local<Value> value) {
107
  // Slightly different take on Utf8Value. If value is a String,
108
  // it will return a Utf8 encoded string. If value is a Buffer,
109
  // it will copy the data out of the Buffer as is.
110
188954
  if (value.IsEmpty()) {
111
    // Dereferencing this object will return nullptr.
112
    Invalidate();
113
    return;
114
  }
115
116
377908
  if (value->IsString()) {
117
185421
    MakeUtf8String(isolate, value, this);
118
3533
  } else if (value->IsArrayBufferView()) {
119
7066
    const size_t len = value.As<ArrayBufferView>()->ByteLength();
120
    // Leave place for the terminating '\0' byte.
121
3533
    AllocateSufficientStorage(len + 1);
122
10599
    value.As<ArrayBufferView>()->CopyContents(out(), len);
123
3533
    SetLengthAndZeroTerminate(len);
124
  } else {
125
    Invalidate();
126
  }
127
}
128
129
void LowMemoryNotification() {
130
  if (per_process::v8_initialized) {
131
    auto isolate = Isolate::GetCurrent();
132
    if (isolate != nullptr) {
133
      isolate->LowMemoryNotification();
134
    }
135
  }
136
}
137
138
4218
std::string GetProcessTitle(const char* default_title) {
139
8436
  std::string buf(16, '\0');
140
141
  for (;;) {
142
21211
    const int rc = uv_get_process_title(&buf[0], buf.size());
143
144
21211
    if (rc == 0)
145
4218
      break;
146
147
16993
    if (rc != UV_ENOBUFS)
148
      return default_title;
149
150
16993
    buf.resize(2 * buf.size());
151
16993
  }
152
153
  // Strip excess trailing nul bytes. Using strlen() here is safe,
154
  // uv_get_process_title() always zero-terminates the result.
155
4218
  buf.resize(strlen(&buf[0]));
156
157
4218
  return buf;
158
}
159
160
4137
std::string GetHumanReadableProcessName() {
161
4137
  return SPrintF("%s[%d]", GetProcessTitle("Node.js"), uv_os_getpid());
162
}
163
164
8371
std::vector<std::string> SplitString(const std::string& in, char delim) {
165
8371
  std::vector<std::string> out;
166
8371
  if (in.empty())
167
3
    return out;
168
16736
  std::istringstream in_stream(in);
169
41748
  while (in_stream.good()) {
170
33380
    std::string item;
171
16690
    std::getline(in_stream, item, delim);
172
16690
    if (item.empty()) continue;
173
16690
    out.emplace_back(std::move(item));
174
  }
175
8368
  return out;
176
}
177
178
void ThrowErrStringTooLong(Isolate* isolate) {
179
  isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
180
}
181
182
8499
double GetCurrentTimeInMicroseconds() {
183
8499
  constexpr double kMicrosecondsPerSecond = 1e6;
184
  uv_timeval64_t tv;
185
8499
  CHECK_EQ(0, uv_gettimeofday(&tv));
186
8499
  return kMicrosecondsPerSecond * tv.tv_sec + tv.tv_usec;
187
}
188
189
4333
int WriteFileSync(const char* path, uv_buf_t buf) {
190
  uv_fs_t req;
191
  int fd = uv_fs_open(nullptr,
192
                      &req,
193
                      path,
194
                      O_WRONLY | O_CREAT | O_TRUNC,
195
                      S_IWUSR | S_IRUSR,
196
4333
                      nullptr);
197
4333
  uv_fs_req_cleanup(&req);
198
4333
  if (fd < 0) {
199
    return fd;
200
  }
201
202
4333
  int err = uv_fs_write(nullptr, &req, fd, &buf, 1, 0, nullptr);
203
4333
  uv_fs_req_cleanup(&req);
204
4333
  if (err < 0) {
205
    return err;
206
  }
207
208
4333
  err = uv_fs_close(nullptr, &req, fd, nullptr);
209
4333
  uv_fs_req_cleanup(&req);
210
4333
  return err;
211
}
212
213
4333
int WriteFileSync(v8::Isolate* isolate,
214
                  const char* path,
215
                  v8::Local<v8::String> string) {
216
8666
  node::Utf8Value utf8(isolate, string);
217
4333
  uv_buf_t buf = uv_buf_init(utf8.out(), utf8.length());
218
8666
  return WriteFileSync(path, buf);
219
}
220
221
44
void DiagnosticFilename::LocalTime(TIME_TYPE* tm_struct) {
222
#ifdef _WIN32
223
  GetLocalTime(tm_struct);
224
#else  // UNIX, OSX
225
  struct timeval time_val;
226
44
  gettimeofday(&time_val, nullptr);
227
44
  localtime_r(&time_val.tv_sec, tm_struct);
228
#endif
229
44
}
230
231
// Defined in node_internals.h
232
25
std::string DiagnosticFilename::MakeFilename(
233
    uint64_t thread_id,
234
    const char* prefix,
235
    const char* ext) {
236
50
  std::ostringstream oss;
237
  TIME_TYPE tm_struct;
238
25
  LocalTime(&tm_struct);
239
25
  oss << prefix;
240
#ifdef _WIN32
241
  oss << "." << std::setfill('0') << std::setw(4) << tm_struct.wYear;
242
  oss << std::setfill('0') << std::setw(2) << tm_struct.wMonth;
243
  oss << std::setfill('0') << std::setw(2) << tm_struct.wDay;
244
  oss << "." << std::setfill('0') << std::setw(2) << tm_struct.wHour;
245
  oss << std::setfill('0') << std::setw(2) << tm_struct.wMinute;
246
  oss << std::setfill('0') << std::setw(2) << tm_struct.wSecond;
247
#else  // UNIX, OSX
248
  oss << "."
249
            << std::setfill('0')
250
25
            << std::setw(4)
251
50
            << tm_struct.tm_year + 1900;
252
  oss << std::setfill('0')
253
25
            << std::setw(2)
254
50
            << tm_struct.tm_mon + 1;
255
  oss << std::setfill('0')
256
25
            << std::setw(2)
257
50
            << tm_struct.tm_mday;
258
  oss << "."
259
            << std::setfill('0')
260
25
            << std::setw(2)
261
50
            << tm_struct.tm_hour;
262
  oss << std::setfill('0')
263
25
            << std::setw(2)
264
50
            << tm_struct.tm_min;
265
  oss << std::setfill('0')
266
25
            << std::setw(2)
267
50
            << tm_struct.tm_sec;
268
#endif
269
25
  oss << "." << uv_os_getpid();
270
25
  oss << "." << thread_id;
271
25
  oss << "." << std::setfill('0') << std::setw(3) << ++seq;
272
25
  oss << "." << ext;
273
50
  return oss.str();
274
}
275
276
}  // namespace node