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: 101 113 89.4 %
Date: 2019-09-25 22:36:03 Branches: 21 38 55.3 %

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 "env-inl.h"
26
#include "node_buffer.h"
27
#include "node_errors.h"
28
#include "node_internals.h"
29
#include "string_bytes.h"
30
#include "uv.h"
31
32
#ifdef _WIN32
33
#include <io.h>  // _S_IREAD _S_IWRITE
34
#include <time.h>
35
#ifndef S_IRUSR
36
#define S_IRUSR _S_IREAD
37
#endif  // S_IRUSR
38
#ifndef S_IWUSR
39
#define S_IWUSR _S_IWRITE
40
#endif  // S_IWUSR
41
#else
42
#include <sys/time.h>
43
#include <sys/types.h>
44
#endif
45
46
#include <atomic>
47
#include <cstdio>
48
#include <iomanip>
49
#include <sstream>
50
51
static std::atomic_int seq = {0};  // Sequence number for diagnostic filenames.
52
53
namespace node {
54
55
using v8::ArrayBufferView;
56
using v8::Isolate;
57
using v8::Local;
58
using v8::String;
59
using v8::Value;
60
61
template <typename T>
62
6355546
static void MakeUtf8String(Isolate* isolate,
63
                           Local<Value> value,
64
                           T* target) {
65
  Local<String> string;
66

19066648
  if (!value->ToString(isolate->GetCurrentContext()).ToLocal(&string)) return;
67
68
  size_t storage;
69

12711097
  if (!StringBytes::StorageSize(isolate, string, UTF8).To(&storage)) return;
70
6355546
  storage += 1;
71
6355546
  target->AllocateSufficientStorage(storage);
72
  const int flags =
73
6355547
      String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8;
74
  const int length =
75
12711091
      string->WriteUtf8(isolate, target->out(), storage, nullptr, flags);
76
6355547
  target->SetLengthAndZeroTerminate(length);
77
}
78
79
6120624
Utf8Value::Utf8Value(Isolate* isolate, Local<Value> value) {
80
6120627
  if (value.IsEmpty())
81
6122740
    return;
82
83
6118516
  MakeUtf8String(isolate, value, this);
84
}
85
86
87
1555
TwoByteValue::TwoByteValue(Isolate* isolate, Local<Value> value) {
88
1555
  if (value.IsEmpty()) {
89
    return;
90
  }
91
92
  Local<String> string;
93
4665
  if (!value->ToString(isolate->GetCurrentContext()).ToLocal(&string)) return;
94
95
  // Allocate enough space to include the null terminator
96
1555
  const size_t storage = string->Length() + 1;
97
1555
  AllocateSufficientStorage(storage);
98
99
1555
  const int flags = String::NO_NULL_TERMINATION;
100
3110
  const int length = string->Write(isolate, out(), 0, storage, flags);
101
1555
  SetLengthAndZeroTerminate(length);
102
}
103
104
255669
BufferValue::BufferValue(Isolate* isolate, Local<Value> value) {
105
  // Slightly different take on Utf8Value. If value is a String,
106
  // it will return a Utf8 encoded string. If value is a Buffer,
107
  // it will copy the data out of the Buffer as is.
108
255669
  if (value.IsEmpty()) {
109
    // Dereferencing this object will return nullptr.
110
    Invalidate();
111
255669
    return;
112
  }
113
114
511338
  if (value->IsString()) {
115
237032
    MakeUtf8String(isolate, value, this);
116
18637
  } else if (value->IsArrayBufferView()) {
117
37274
    const size_t len = value.As<ArrayBufferView>()->ByteLength();
118
    // Leave place for the terminating '\0' byte.
119
18637
    AllocateSufficientStorage(len + 1);
120
55911
    value.As<ArrayBufferView>()->CopyContents(out(), len);
121
18637
    SetLengthAndZeroTerminate(len);
122
  } else {
123
    Invalidate();
124
  }
125
}
126
127
void LowMemoryNotification() {
128
  if (per_process::v8_initialized) {
129
    auto isolate = Isolate::GetCurrent();
130
    if (isolate != nullptr) {
131
      isolate->LowMemoryNotification();
132
    }
133
  }
134
}
135
136
4964
std::string GetHumanReadableProcessName() {
137
  char name[1024];
138
4964
  GetHumanReadableProcessName(&name);
139
4964
  return name;
140
}
141
142
4964
void GetHumanReadableProcessName(char (*name)[1024]) {
143
  // Leave room after title for pid, which can be up to 20 digits for 64 bit.
144
4964
  char title[1000] = "Node.js";
145
4964
  uv_get_process_title(title, sizeof(title));
146
4964
  snprintf(*name, sizeof(*name), "%s[%d]", title, uv_os_getpid());
147
4964
}
148
149
9934
std::vector<std::string> SplitString(const std::string& in, char delim) {
150
9934
  std::vector<std::string> out;
151
9934
  if (in.empty())
152
3
    return out;
153
19862
  std::istringstream in_stream(in);
154
39663
  while (in_stream.good()) {
155
19801
    std::string item;
156
19801
    std::getline(in_stream, item, delim);
157
19801
    if (item.empty()) continue;
158
19801
    out.emplace_back(std::move(item));
159
19801
  }
160
9931
  return out;
161
}
162
163
void ThrowErrStringTooLong(Isolate* isolate) {
164
  isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
165
}
166
167
10123
double GetCurrentTimeInMicroseconds() {
168
10123
  constexpr double kMicrosecondsPerSecond = 1e6;
169
  uv_timeval64_t tv;
170
10123
  CHECK_EQ(0, uv_gettimeofday(&tv));
171
10123
  return kMicrosecondsPerSecond * tv.tv_sec + tv.tv_usec;
172
}
173
174
5132
int WriteFileSync(const char* path, uv_buf_t buf) {
175
  uv_fs_t req;
176
  int fd = uv_fs_open(nullptr,
177
                      &req,
178
                      path,
179
                      O_WRONLY | O_CREAT | O_TRUNC,
180
                      S_IWUSR | S_IRUSR,
181
5132
                      nullptr);
182
5132
  uv_fs_req_cleanup(&req);
183
5132
  if (fd < 0) {
184
    return fd;
185
  }
186
187
5132
  int err = uv_fs_write(nullptr, &req, fd, &buf, 1, 0, nullptr);
188
5132
  uv_fs_req_cleanup(&req);
189
5132
  if (err < 0) {
190
    return err;
191
  }
192
193
5132
  err = uv_fs_close(nullptr, &req, fd, nullptr);
194
5132
  uv_fs_req_cleanup(&req);
195
5132
  return err;
196
}
197
198
5132
int WriteFileSync(v8::Isolate* isolate,
199
                  const char* path,
200
                  v8::Local<v8::String> string) {
201
5132
  node::Utf8Value utf8(isolate, string);
202
5132
  uv_buf_t buf = uv_buf_init(utf8.out(), utf8.length());
203
5132
  return WriteFileSync(path, buf);
204
}
205
206
40
void DiagnosticFilename::LocalTime(TIME_TYPE* tm_struct) {
207
#ifdef _WIN32
208
  GetLocalTime(tm_struct);
209
#else  // UNIX, OSX
210
  struct timeval time_val;
211
40
  gettimeofday(&time_val, nullptr);
212
40
  localtime_r(&time_val.tv_sec, tm_struct);
213
#endif
214
40
}
215
216
// Defined in node_internals.h
217
25
std::string DiagnosticFilename::MakeFilename(
218
    uint64_t thread_id,
219
    const char* prefix,
220
    const char* ext) {
221
25
  std::ostringstream oss;
222
  TIME_TYPE tm_struct;
223
25
  LocalTime(&tm_struct);
224
25
  oss << prefix;
225
#ifdef _WIN32
226
  oss << "." << std::setfill('0') << std::setw(4) << tm_struct.wYear;
227
  oss << std::setfill('0') << std::setw(2) << tm_struct.wMonth;
228
  oss << std::setfill('0') << std::setw(2) << tm_struct.wDay;
229
  oss << "." << std::setfill('0') << std::setw(2) << tm_struct.wHour;
230
  oss << std::setfill('0') << std::setw(2) << tm_struct.wMinute;
231
  oss << std::setfill('0') << std::setw(2) << tm_struct.wSecond;
232
#else  // UNIX, OSX
233
25
  oss << "."
234
50
            << std::setfill('0')
235
50
            << std::setw(4)
236
50
            << tm_struct.tm_year + 1900;
237
25
  oss << std::setfill('0')
238
50
            << std::setw(2)
239
50
            << tm_struct.tm_mon + 1;
240
25
  oss << std::setfill('0')
241
50
            << std::setw(2)
242
50
            << tm_struct.tm_mday;
243
25
  oss << "."
244
50
            << std::setfill('0')
245
50
            << std::setw(2)
246
50
            << tm_struct.tm_hour;
247
25
  oss << std::setfill('0')
248
50
            << std::setw(2)
249
50
            << tm_struct.tm_min;
250
25
  oss << std::setfill('0')
251
50
            << std::setw(2)
252
50
            << tm_struct.tm_sec;
253
#endif
254
25
  oss << "." << uv_os_getpid();
255
25
  oss << "." << thread_id;
256
25
  oss << "." << std::setfill('0') << std::setw(3) << ++seq;
257
25
  oss << "." << ext;
258
25
  return oss.str();
259
}
260
261
}  // namespace node