GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/stream_wrap.cc Lines: 177 182 97.3 %
Date: 2020-06-24 22:13:30 Branches: 55 84 65.5 %

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 "stream_wrap.h"
23
#include "stream_base-inl.h"
24
25
#include "env-inl.h"
26
#include "handle_wrap.h"
27
#include "node_buffer.h"
28
#include "pipe_wrap.h"
29
#include "req_wrap-inl.h"
30
#include "tcp_wrap.h"
31
#include "udp_wrap.h"
32
#include "util-inl.h"
33
34
#include <cstring>  // memcpy()
35
#include <climits>  // INT_MAX
36
37
38
namespace node {
39
40
using v8::Context;
41
using v8::DontDelete;
42
using v8::EscapableHandleScope;
43
using v8::FunctionCallbackInfo;
44
using v8::FunctionTemplate;
45
using v8::HandleScope;
46
using v8::Local;
47
using v8::MaybeLocal;
48
using v8::Object;
49
using v8::PropertyAttribute;
50
using v8::ReadOnly;
51
using v8::Signature;
52
using v8::String;
53
using v8::Value;
54
55
56
4201
void LibuvStreamWrap::Initialize(Local<Object> target,
57
                                 Local<Value> unused,
58
                                 Local<Context> context,
59
                                 void* priv) {
60
4201
  Environment* env = Environment::GetCurrent(context);
61
62
  auto is_construct_call_callback =
63
727016
      [](const FunctionCallbackInfo<Value>& args) {
64
359307
    CHECK(args.IsConstructCall());
65
359307
    StreamReq::ResetObject(args.This());
66
727016
  };
67
  Local<FunctionTemplate> sw =
68
4201
      FunctionTemplate::New(env->isolate(), is_construct_call_callback);
69
8402
  sw->InstanceTemplate()->SetInternalFieldCount(StreamReq::kInternalFieldCount);
70
  Local<String> wrapString =
71
4201
      FIXED_ONE_BYTE_STRING(env->isolate(), "ShutdownWrap");
72
4201
  sw->SetClassName(wrapString);
73
74
  // we need to set handle and callback to null,
75
  // so that those fields are created and functions
76
  // do not become megamorphic
77
  // Fields:
78
  // - oncomplete
79
  // - callback
80
  // - handle
81
21005
  sw->InstanceTemplate()->Set(
82
      env->oncomplete_string(),
83
4201
      v8::Null(env->isolate()));
84
21005
  sw->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "callback"),
85
4201
      v8::Null(env->isolate()));
86
21005
  sw->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "handle"),
87
4201
      v8::Null(env->isolate()));
88
89
8402
  sw->Inherit(AsyncWrap::GetConstructorTemplate(env));
90
91
8402
  target->Set(env->context(),
92
              wrapString,
93
21005
              sw->GetFunction(env->context()).ToLocalChecked()).Check();
94
4201
  env->set_shutdown_wrap_template(sw->InstanceTemplate());
95
96
  Local<FunctionTemplate> ww =
97
4201
      FunctionTemplate::New(env->isolate(), is_construct_call_callback);
98
12603
  ww->InstanceTemplate()->SetInternalFieldCount(
99
4201
      StreamReq::kInternalFieldCount);
100
  Local<String> writeWrapString =
101
4201
      FIXED_ONE_BYTE_STRING(env->isolate(), "WriteWrap");
102
4201
  ww->SetClassName(writeWrapString);
103
8402
  ww->Inherit(AsyncWrap::GetConstructorTemplate(env));
104
8402
  target->Set(env->context(),
105
              writeWrapString,
106
21005
              ww->GetFunction(env->context()).ToLocalChecked()).Check();
107
4201
  env->set_write_wrap_template(ww->InstanceTemplate());
108
109
16804
  NODE_DEFINE_CONSTANT(target, kReadBytesOrError);
110
16804
  NODE_DEFINE_CONSTANT(target, kArrayBufferOffset);
111
16804
  NODE_DEFINE_CONSTANT(target, kBytesWritten);
112
16804
  NODE_DEFINE_CONSTANT(target, kLastWriteWasAsync);
113
8402
  target->Set(context, FIXED_ONE_BYTE_STRING(env->isolate(), "streamBaseState"),
114
16804
              env->stream_base_state().GetJSArray()).Check();
115
4201
}
116
117
118
16378
LibuvStreamWrap::LibuvStreamWrap(Environment* env,
119
                                 Local<Object> object,
120
                                 uv_stream_t* stream,
121
16378
                                 AsyncWrap::ProviderType provider)
122
    : HandleWrap(env,
123
                 object,
124
                 reinterpret_cast<uv_handle_t*>(stream),
125
                 provider),
126
      StreamBase(env),
127
16378
      stream_(stream) {
128
16378
  StreamBase::AttachToObject(object);
129
16378
}
130
131
132
12202
Local<FunctionTemplate> LibuvStreamWrap::GetConstructorTemplate(
133
    Environment* env) {
134
12202
  Local<FunctionTemplate> tmpl = env->libuv_stream_wrap_ctor_template();
135
12202
  if (tmpl.IsEmpty()) {
136
4176
    tmpl = env->NewFunctionTemplate(nullptr);
137
8352
    tmpl->SetClassName(
138
4176
        FIXED_ONE_BYTE_STRING(env->isolate(), "LibuvStreamWrap"));
139
8352
    tmpl->Inherit(HandleWrap::GetConstructorTemplate(env));
140
12528
    tmpl->InstanceTemplate()->SetInternalFieldCount(
141
4176
        StreamBase::kInternalFieldCount);
142
    Local<FunctionTemplate> get_write_queue_size =
143
        FunctionTemplate::New(env->isolate(),
144
                              GetWriteQueueSize,
145
                              Local<Value>(),
146
8352
                              Signature::New(env->isolate(), tmpl));
147
16704
    tmpl->PrototypeTemplate()->SetAccessorProperty(
148
        env->write_queue_size_string(),
149
        get_write_queue_size,
150
        Local<FunctionTemplate>(),
151
4176
        static_cast<PropertyAttribute>(ReadOnly | DontDelete));
152
4176
    env->SetProtoMethod(tmpl, "setBlocking", SetBlocking);
153
4176
    StreamBase::AddMethods(env, tmpl);
154
4176
    env->set_libuv_stream_wrap_ctor_template(tmpl);
155
  }
156
12202
  return tmpl;
157
}
158
159
160
3303
LibuvStreamWrap* LibuvStreamWrap::From(Environment* env, Local<Object> object) {
161
3303
  Local<FunctionTemplate> sw = env->libuv_stream_wrap_ctor_template();
162

6606
  CHECK(!sw.IsEmpty() && sw->HasInstance(object));
163
3303
  return Unwrap<LibuvStreamWrap>(object);
164
}
165
166
167
119
int LibuvStreamWrap::GetFD() {
168
#ifdef _WIN32
169
  return fd_;
170
#else
171
119
  int fd = -1;
172
119
  if (stream() != nullptr)
173
119
    uv_fileno(reinterpret_cast<uv_handle_t*>(stream()), &fd);
174
119
  return fd;
175
#endif
176
}
177
178
179
351393
bool LibuvStreamWrap::IsAlive() {
180
351393
  return HandleWrap::IsAlive(this);
181
}
182
183
184
3
bool LibuvStreamWrap::IsClosing() {
185
3
  return uv_is_closing(reinterpret_cast<uv_handle_t*>(stream()));
186
}
187
188
189
646715
AsyncWrap* LibuvStreamWrap::GetAsyncWrap() {
190
646715
  return static_cast<AsyncWrap*>(this);
191
}
192
193
194
271175
bool LibuvStreamWrap::IsIPCPipe() {
195
271175
  return is_named_pipe_ipc();
196
}
197
198
199
13562
int LibuvStreamWrap::ReadStart() {
200
27124
  return uv_read_start(stream(), [](uv_handle_t* handle,
201
                                    size_t suggested_size,
202
665034
                                    uv_buf_t* buf) {
203
325736
    static_cast<LibuvStreamWrap*>(handle->data)->OnUvAlloc(suggested_size, buf);
204
1330564
  }, [](uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
205
325984
    static_cast<LibuvStreamWrap*>(stream->data)->OnUvRead(nread, buf);
206
692569
  });
207
}
208
209
210
8487
int LibuvStreamWrap::ReadStop() {
211
8487
  return uv_read_stop(stream());
212
}
213
214
215
325736
void LibuvStreamWrap::OnUvAlloc(size_t suggested_size, uv_buf_t* buf) {
216
651472
  HandleScope scope(env()->isolate());
217
325736
  Context::Scope context_scope(env()->context());
218
219
325736
  *buf = EmitAlloc(suggested_size);
220
325736
}
221
222
template <class WrapType>
223
155
static MaybeLocal<Object> AcceptHandle(Environment* env,
224
                                       LibuvStreamWrap* parent) {
225
  static_assert(std::is_base_of<LibuvStreamWrap, WrapType>::value ||
226
                std::is_base_of<UDPWrap, WrapType>::value,
227
                "Can only accept stream handles");
228
229
155
  EscapableHandleScope scope(env->isolate());
230
  Local<Object> wrap_obj;
231
232

310
  if (!WrapType::Instantiate(env, parent, WrapType::SOCKET).ToLocal(&wrap_obj))
233
    return Local<Object>();
234
235
155
  HandleWrap* wrap = Unwrap<HandleWrap>(wrap_obj);
236

155
  CHECK_NOT_NULL(wrap);
237
155
  uv_stream_t* stream = reinterpret_cast<uv_stream_t*>(wrap->GetHandle());
238

155
  CHECK_NOT_NULL(stream);
239
240

155
  if (uv_accept(parent->stream(), stream))
241
    ABORT();
242
243
155
  return scope.Escape(wrap_obj);
244
}
245
246
247
325984
void LibuvStreamWrap::OnUvRead(ssize_t nread, const uv_buf_t* buf) {
248
651883
  HandleScope scope(env()->isolate());
249
325984
  Context::Scope context_scope(env()->context());
250
325984
  uv_handle_type type = UV_UNKNOWN_HANDLE;
251
252

327489
  if (is_named_pipe_ipc() &&
253
1505
      uv_pipe_pending_count(reinterpret_cast<uv_pipe_t*>(stream())) > 0) {
254
155
    type = uv_pipe_pending_type(reinterpret_cast<uv_pipe_t*>(stream()));
255
  }
256
257
  // We should not be getting this callback if someone has already called
258
  // uv_close() on the handle.
259
651968
  CHECK_EQ(persistent().IsEmpty(), false);
260
261
325984
  if (nread > 0) {
262
    MaybeLocal<Object> pending_obj;
263
264
320491
    if (type == UV_TCP) {
265
129
      pending_obj = AcceptHandle<TCPWrap>(env(), this);
266
320362
    } else if (type == UV_NAMED_PIPE) {
267
1
      pending_obj = AcceptHandle<PipeWrap>(env(), this);
268
320361
    } else if (type == UV_UDP) {
269
25
      pending_obj = AcceptHandle<UDPWrap>(env(), this);
270
    } else {
271
320336
      CHECK_EQ(type, UV_UNKNOWN_HANDLE);
272
    }
273
274
320491
    if (!pending_obj.IsEmpty()) {
275
310
      object()
276
310
          ->Set(env()->context(),
277
                env()->pending_handle_string(),
278
620
                pending_obj.ToLocalChecked())
279
          .Check();
280
    }
281
  }
282
283
325984
  EmitRead(nread, *buf);
284
325899
}
285
286
287
1249
void LibuvStreamWrap::GetWriteQueueSize(
288
    const FunctionCallbackInfo<Value>& info) {
289
  LibuvStreamWrap* wrap;
290
1249
  ASSIGN_OR_RETURN_UNWRAP(&wrap, info.This());
291
292
1249
  if (wrap->stream() == nullptr) {
293
    info.GetReturnValue().Set(0);
294
    return;
295
  }
296
297
1249
  uint32_t write_queue_size = wrap->stream()->write_queue_size;
298
2498
  info.GetReturnValue().Set(write_queue_size);
299
}
300
301
302
31
void LibuvStreamWrap::SetBlocking(const FunctionCallbackInfo<Value>& args) {
303
  LibuvStreamWrap* wrap;
304
31
  ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
305
306
31
  CHECK_GT(args.Length(), 0);
307
31
  if (!wrap->IsAlive())
308
    return args.GetReturnValue().Set(UV_EINVAL);
309
310
62
  bool enable = args[0]->IsTrue();
311
93
  args.GetReturnValue().Set(uv_stream_set_blocking(wrap->stream(), enable));
312
}
313
314
typedef SimpleShutdownWrap<ReqWrap<uv_shutdown_t>> LibuvShutdownWrap;
315
typedef SimpleWriteWrap<ReqWrap<uv_write_t>> LibuvWriteWrap;
316
317
6505
ShutdownWrap* LibuvStreamWrap::CreateShutdownWrap(Local<Object> object) {
318
6505
  return new LibuvShutdownWrap(this, object);
319
}
320
321
437
WriteWrap* LibuvStreamWrap::CreateWriteWrap(Local<Object> object) {
322
437
  return new LibuvWriteWrap(this, object);
323
}
324
325
326
6505
int LibuvStreamWrap::DoShutdown(ShutdownWrap* req_wrap_) {
327
6505
  LibuvShutdownWrap* req_wrap = static_cast<LibuvShutdownWrap*>(req_wrap_);
328
6505
  return req_wrap->Dispatch(uv_shutdown, stream(), AfterUvShutdown);
329
}
330
331
332
6502
void LibuvStreamWrap::AfterUvShutdown(uv_shutdown_t* req, int status) {
333
6502
  LibuvShutdownWrap* req_wrap = static_cast<LibuvShutdownWrap*>(
334
13004
      LibuvShutdownWrap::from_req(req));
335
6502
  CHECK_NOT_NULL(req_wrap);
336
13004
  HandleScope scope(req_wrap->env()->isolate());
337
6502
  Context::Scope context_scope(req_wrap->env()->context());
338
6502
  req_wrap->Done(status);
339
6502
}
340
341
342
// NOTE: Call to this function could change both `buf`'s and `count`'s
343
// values, shifting their base and decrementing their length. This is
344
// required in order to skip the data that was successfully written via
345
// uv_try_write().
346
361911
int LibuvStreamWrap::DoTryWrite(uv_buf_t** bufs, size_t* count) {
347
  int err;
348
  size_t written;
349
361911
  uv_buf_t* vbufs = *bufs;
350
361911
  size_t vcount = *count;
351
352
361911
  err = uv_try_write(stream(), vbufs, vcount);
353

361911
  if (err == UV_ENOSYS || err == UV_EAGAIN)
354
34
    return 0;
355
361877
  if (err < 0)
356
132
    return err;
357
358
  // Slice off the buffers: skip all written buffers and slice the one that
359
  // was partially written.
360
361745
  written = err;
361
1229701
  for (; vcount > 0; vbufs++, vcount--) {
362
    // Slice
363
434280
    if (vbufs[0].len > written) {
364
302
      vbufs[0].base += written;
365
302
      vbufs[0].len -= written;
366
302
      written = 0;
367
302
      break;
368
369
    // Discard
370
    } else {
371
433978
      written -= vbufs[0].len;
372
    }
373
  }
374
375
361745
  *bufs = vbufs;
376
361745
  *count = vcount;
377
378
361745
  return 0;
379
}
380
381
382
437
int LibuvStreamWrap::DoWrite(WriteWrap* req_wrap,
383
                             uv_buf_t* bufs,
384
                             size_t count,
385
                             uv_stream_t* send_handle) {
386
437
  LibuvWriteWrap* w = static_cast<LibuvWriteWrap*>(req_wrap);
387
437
  return w->Dispatch(uv_write2,
388
                     stream(),
389
                     bufs,
390
                     count,
391
                     send_handle,
392
437
                     AfterUvWrite);
393
}
394
395
396
397
436
void LibuvStreamWrap::AfterUvWrite(uv_write_t* req, int status) {
398
436
  LibuvWriteWrap* req_wrap = static_cast<LibuvWriteWrap*>(
399
872
      LibuvWriteWrap::from_req(req));
400
436
  CHECK_NOT_NULL(req_wrap);
401
871
  HandleScope scope(req_wrap->env()->isolate());
402
436
  Context::Scope context_scope(req_wrap->env()->context());
403
436
  req_wrap->Done(status);
404
435
}
405
406
}  // namespace node
407
408
4398
NODE_MODULE_CONTEXT_AWARE_INTERNAL(stream_wrap,
409
                                   node::LibuvStreamWrap::Initialize)