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 |
|
2872869 |
static void MakeUtf8String(Isolate* isolate, |
65 |
|
|
Local<Value> value, |
66 |
|
|
MaybeStackBuffer<T>* target) { |
67 |
|
|
Local<String> string; |
68 |
✗✓ |
5745738 |
if (!value->ToString(isolate->GetCurrentContext()).ToLocal(&string)) return; |
69 |
|
|
|
70 |
|
|
size_t storage; |
71 |
✗✓ |
5745738 |
if (!StringBytes::StorageSize(isolate, string, UTF8).To(&storage)) return; |
72 |
|
2872869 |
storage += 1; |
73 |
|
2872869 |
target->AllocateSufficientStorage(storage); |
74 |
|
2872869 |
const int flags = |
75 |
|
|
String::NO_NULL_TERMINATION | String::REPLACE_INVALID_UTF8; |
76 |
|
2872869 |
const int length = |
77 |
|
|
string->WriteUtf8(isolate, target->out(), storage, nullptr, flags); |
78 |
|
2872869 |
target->SetLengthAndZeroTerminate(length); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
2668507 |
Utf8Value::Utf8Value(Isolate* isolate, Local<Value> value) { |
82 |
✓✓ |
2668507 |
if (value.IsEmpty()) |
83 |
|
1745 |
return; |
84 |
|
|
|
85 |
|
2666762 |
MakeUtf8String(isolate, value, this); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
|
89 |
|
1197491 |
TwoByteValue::TwoByteValue(Isolate* isolate, Local<Value> value) { |
90 |
✗✓ |
1197491 |
if (value.IsEmpty()) { |
91 |
|
|
return; |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
Local<String> string; |
95 |
✗✓ |
2394982 |
if (!value->ToString(isolate->GetCurrentContext()).ToLocal(&string)) return; |
96 |
|
|
|
97 |
|
|
// Allocate enough space to include the null terminator |
98 |
|
1197491 |
const size_t storage = string->Length() + 1; |
99 |
|
1197491 |
AllocateSufficientStorage(storage); |
100 |
|
|
|
101 |
|
1197491 |
const int flags = String::NO_NULL_TERMINATION; |
102 |
|
1197491 |
const int length = string->Write(isolate, out(), 0, storage, flags); |
103 |
|
1197491 |
SetLengthAndZeroTerminate(length); |
104 |
|
|
} |
105 |
|
|
|
106 |
|
213542 |
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 |
✗✓ |
213542 |
if (value.IsEmpty()) { |
111 |
|
|
// Dereferencing this object will return nullptr. |
112 |
|
|
Invalidate(); |
113 |
|
|
return; |
114 |
|
|
} |
115 |
|
|
|
116 |
✓✓ |
427084 |
if (value->IsString()) { |
117 |
|
206107 |
MakeUtf8String(isolate, value, this); |
118 |
✓✗ |
7435 |
} else if (value->IsArrayBufferView()) { |
119 |
|
7435 |
const size_t len = value.As<ArrayBufferView>()->ByteLength(); |
120 |
|
|
// Leave place for the terminating '\0' byte. |
121 |
|
7435 |
AllocateSufficientStorage(len + 1); |
122 |
|
7435 |
value.As<ArrayBufferView>()->CopyContents(out(), len); |
123 |
|
7435 |
SetLengthAndZeroTerminate(len); |
124 |
|
|
} else { |
125 |
|
|
Invalidate(); |
126 |
|
|
} |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
void LowMemoryNotification() { |
130 |
|
|
if (per_process::v8_initialized) { |
131 |
|
|
auto isolate = Isolate::TryGetCurrent(); |
132 |
|
|
if (isolate != nullptr) { |
133 |
|
|
isolate->LowMemoryNotification(); |
134 |
|
|
} |
135 |
|
|
} |
136 |
|
|
} |
137 |
|
|
|
138 |
|
5273 |
std::string GetProcessTitle(const char* default_title) { |
139 |
|
15819 |
std::string buf(16, '\0'); |
140 |
|
|
|
141 |
|
|
for (;;) { |
142 |
|
14233 |
const int rc = uv_get_process_title(&buf[0], buf.size()); |
143 |
|
|
|
144 |
✓✓ |
14233 |
if (rc == 0) |
145 |
|
5233 |
break; |
146 |
|
|
|
147 |
|
|
// If uv_setup_args() was not called, `uv_get_process_title()` will always |
148 |
|
|
// return `UV_ENOBUFS`, no matter the input size. Guard against a possible |
149 |
|
|
// infinite loop by limiting the buffer size. |
150 |
✓✗✓✓ ✓✓ |
9000 |
if (rc != UV_ENOBUFS || buf.size() >= 1024 * 1024) |
151 |
|
40 |
return default_title; |
152 |
|
|
|
153 |
|
8960 |
buf.resize(2 * buf.size()); |
154 |
|
8960 |
} |
155 |
|
|
|
156 |
|
|
// Strip excess trailing nul bytes. Using strlen() here is safe, |
157 |
|
|
// uv_get_process_title() always zero-terminates the result. |
158 |
|
5233 |
buf.resize(strlen(&buf[0])); |
159 |
|
|
|
160 |
|
5233 |
return buf; |
161 |
|
|
} |
162 |
|
|
|
163 |
|
5165 |
std::string GetHumanReadableProcessName() { |
164 |
|
5165 |
return SPrintF("%s[%d]", GetProcessTitle("Node.js"), uv_os_getpid()); |
165 |
|
|
} |
166 |
|
|
|
167 |
|
10806 |
std::vector<std::string> SplitString(const std::string& in, char delim) { |
168 |
|
10806 |
std::vector<std::string> out; |
169 |
✓✓ |
10806 |
if (in.empty()) |
170 |
|
2 |
return out; |
171 |
|
21608 |
std::istringstream in_stream(in); |
172 |
✓✓ |
32366 |
while (in_stream.good()) { |
173 |
|
21562 |
std::string item; |
174 |
|
21562 |
std::getline(in_stream, item, delim); |
175 |
✗✓ |
21562 |
if (item.empty()) continue; |
176 |
|
21562 |
out.emplace_back(std::move(item)); |
177 |
|
|
} |
178 |
|
10804 |
return out; |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
void ThrowErrStringTooLong(Isolate* isolate) { |
182 |
|
|
isolate->ThrowException(ERR_STRING_TOO_LONG(isolate)); |
183 |
|
|
} |
184 |
|
|
|
185 |
|
11065 |
double GetCurrentTimeInMicroseconds() { |
186 |
|
11065 |
constexpr double kMicrosecondsPerSecond = 1e6; |
187 |
|
|
uv_timeval64_t tv; |
188 |
✗✓ |
11065 |
CHECK_EQ(0, uv_gettimeofday(&tv)); |
189 |
|
11065 |
return kMicrosecondsPerSecond * tv.tv_sec + tv.tv_usec; |
190 |
|
|
} |
191 |
|
|
|
192 |
|
5914 |
int WriteFileSync(const char* path, uv_buf_t buf) { |
193 |
|
|
uv_fs_t req; |
194 |
|
5914 |
int fd = uv_fs_open(nullptr, |
195 |
|
|
&req, |
196 |
|
|
path, |
197 |
|
|
O_WRONLY | O_CREAT | O_TRUNC, |
198 |
|
|
S_IWUSR | S_IRUSR, |
199 |
|
|
nullptr); |
200 |
|
5914 |
uv_fs_req_cleanup(&req); |
201 |
✓✓ |
5914 |
if (fd < 0) { |
202 |
|
5 |
return fd; |
203 |
|
|
} |
204 |
|
|
|
205 |
|
5909 |
int err = uv_fs_write(nullptr, &req, fd, &buf, 1, 0, nullptr); |
206 |
|
5909 |
uv_fs_req_cleanup(&req); |
207 |
✗✓ |
5909 |
if (err < 0) { |
208 |
|
|
return err; |
209 |
|
|
} |
210 |
|
|
|
211 |
|
5909 |
err = uv_fs_close(nullptr, &req, fd, nullptr); |
212 |
|
5909 |
uv_fs_req_cleanup(&req); |
213 |
|
5909 |
return err; |
214 |
|
|
} |
215 |
|
|
|
216 |
|
5914 |
int WriteFileSync(v8::Isolate* isolate, |
217 |
|
|
const char* path, |
218 |
|
|
v8::Local<v8::String> string) { |
219 |
|
11828 |
node::Utf8Value utf8(isolate, string); |
220 |
|
5914 |
uv_buf_t buf = uv_buf_init(utf8.out(), utf8.length()); |
221 |
|
5914 |
return WriteFileSync(path, buf); |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
int ReadFileSync(std::string* result, const char* path) { |
225 |
|
|
uv_fs_t req; |
226 |
|
|
auto defer_req_cleanup = OnScopeLeave([&req]() { |
227 |
|
|
uv_fs_req_cleanup(&req); |
228 |
|
|
}); |
229 |
|
|
|
230 |
|
|
uv_file file = uv_fs_open(nullptr, &req, path, O_RDONLY, 0, nullptr); |
231 |
|
|
if (req.result < 0) { |
232 |
|
|
// req will be cleaned up by scope leave. |
233 |
|
|
return req.result; |
234 |
|
|
} |
235 |
|
|
uv_fs_req_cleanup(&req); |
236 |
|
|
|
237 |
|
|
auto defer_close = OnScopeLeave([file]() { |
238 |
|
|
uv_fs_t close_req; |
239 |
|
|
CHECK_EQ(0, uv_fs_close(nullptr, &close_req, file, nullptr)); |
240 |
|
|
uv_fs_req_cleanup(&close_req); |
241 |
|
|
}); |
242 |
|
|
|
243 |
|
|
*result = std::string(""); |
244 |
|
|
char buffer[4096]; |
245 |
|
|
uv_buf_t buf = uv_buf_init(buffer, sizeof(buffer)); |
246 |
|
|
|
247 |
|
|
while (true) { |
248 |
|
|
const int r = |
249 |
|
|
uv_fs_read(nullptr, &req, file, &buf, 1, result->length(), nullptr); |
250 |
|
|
if (req.result < 0) { |
251 |
|
|
// req will be cleaned up by scope leave. |
252 |
|
|
return req.result; |
253 |
|
|
} |
254 |
|
|
uv_fs_req_cleanup(&req); |
255 |
|
|
if (r <= 0) { |
256 |
|
|
break; |
257 |
|
|
} |
258 |
|
|
result->append(buf.base, r); |
259 |
|
|
} |
260 |
|
|
return 0; |
261 |
|
|
} |
262 |
|
|
|
263 |
|
54 |
void DiagnosticFilename::LocalTime(TIME_TYPE* tm_struct) { |
264 |
|
|
#ifdef _WIN32 |
265 |
|
|
GetLocalTime(tm_struct); |
266 |
|
|
#else // UNIX, OSX |
267 |
|
|
struct timeval time_val; |
268 |
|
54 |
gettimeofday(&time_val, nullptr); |
269 |
|
54 |
localtime_r(&time_val.tv_sec, tm_struct); |
270 |
|
|
#endif |
271 |
|
54 |
} |
272 |
|
|
|
273 |
|
|
// Defined in node_internals.h |
274 |
|
30 |
std::string DiagnosticFilename::MakeFilename( |
275 |
|
|
uint64_t thread_id, |
276 |
|
|
const char* prefix, |
277 |
|
|
const char* ext) { |
278 |
|
60 |
std::ostringstream oss; |
279 |
|
|
TIME_TYPE tm_struct; |
280 |
|
30 |
LocalTime(&tm_struct); |
281 |
|
30 |
oss << prefix; |
282 |
|
|
#ifdef _WIN32 |
283 |
|
|
oss << "." << std::setfill('0') << std::setw(4) << tm_struct.wYear; |
284 |
|
|
oss << std::setfill('0') << std::setw(2) << tm_struct.wMonth; |
285 |
|
|
oss << std::setfill('0') << std::setw(2) << tm_struct.wDay; |
286 |
|
|
oss << "." << std::setfill('0') << std::setw(2) << tm_struct.wHour; |
287 |
|
|
oss << std::setfill('0') << std::setw(2) << tm_struct.wMinute; |
288 |
|
|
oss << std::setfill('0') << std::setw(2) << tm_struct.wSecond; |
289 |
|
|
#else // UNIX, OSX |
290 |
|
|
oss << "." |
291 |
|
|
<< std::setfill('0') |
292 |
|
30 |
<< std::setw(4) |
293 |
|
30 |
<< tm_struct.tm_year + 1900; |
294 |
|
|
oss << std::setfill('0') |
295 |
|
30 |
<< std::setw(2) |
296 |
|
30 |
<< tm_struct.tm_mon + 1; |
297 |
|
|
oss << std::setfill('0') |
298 |
|
30 |
<< std::setw(2) |
299 |
|
30 |
<< tm_struct.tm_mday; |
300 |
|
|
oss << "." |
301 |
|
|
<< std::setfill('0') |
302 |
|
30 |
<< std::setw(2) |
303 |
|
30 |
<< tm_struct.tm_hour; |
304 |
|
|
oss << std::setfill('0') |
305 |
|
30 |
<< std::setw(2) |
306 |
|
30 |
<< tm_struct.tm_min; |
307 |
|
|
oss << std::setfill('0') |
308 |
|
30 |
<< std::setw(2) |
309 |
|
30 |
<< tm_struct.tm_sec; |
310 |
|
|
#endif |
311 |
|
30 |
oss << "." << uv_os_getpid(); |
312 |
|
30 |
oss << "." << thread_id; |
313 |
|
30 |
oss << "." << std::setfill('0') << std::setw(3) << ++seq; |
314 |
|
30 |
oss << "." << ext; |
315 |
|
30 |
return oss.str(); |
316 |
|
|
} |
317 |
|
|
|
318 |
|
|
} // namespace node |