GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_serdes.cc Lines: 168 213 78.9 %
Date: 2019-10-05 22:32:21 Branches: 48 104 46.2 %

Line Branch Exec Source
1
#include "node_internals.h"
2
#include "node_buffer.h"
3
#include "node_errors.h"
4
#include "util-inl.h"
5
#include "base_object-inl.h"
6
7
namespace node {
8
9
using v8::Array;
10
using v8::ArrayBuffer;
11
using v8::Context;
12
using v8::Function;
13
using v8::FunctionCallbackInfo;
14
using v8::FunctionTemplate;
15
using v8::Integer;
16
using v8::Isolate;
17
using v8::Just;
18
using v8::Local;
19
using v8::Maybe;
20
using v8::MaybeLocal;
21
using v8::Nothing;
22
using v8::Object;
23
using v8::SharedArrayBuffer;
24
using v8::String;
25
using v8::Value;
26
using v8::ValueDeserializer;
27
using v8::ValueSerializer;
28
29
namespace {
30
31
class SerializerContext : public BaseObject,
32
                          public ValueSerializer::Delegate {
33
 public:
34
  SerializerContext(Environment* env,
35
                    Local<Object> wrap);
36
37
626
  ~SerializerContext() override = default;
38
39
  void ThrowDataCloneError(Local<String> message) override;
40
  Maybe<bool> WriteHostObject(Isolate* isolate, Local<Object> object) override;
41
  Maybe<uint32_t> GetSharedArrayBufferId(
42
      Isolate* isolate, Local<SharedArrayBuffer> shared_array_buffer) override;
43
44
  static void SetTreatArrayBufferViewsAsHostObjects(
45
      const FunctionCallbackInfo<Value>& args);
46
47
  static void New(const FunctionCallbackInfo<Value>& args);
48
  static void WriteHeader(const FunctionCallbackInfo<Value>& args);
49
  static void WriteValue(const FunctionCallbackInfo<Value>& args);
50
  static void ReleaseBuffer(const FunctionCallbackInfo<Value>& args);
51
  static void TransferArrayBuffer(const FunctionCallbackInfo<Value>& args);
52
  static void WriteUint32(const FunctionCallbackInfo<Value>& args);
53
  static void WriteUint64(const FunctionCallbackInfo<Value>& args);
54
  static void WriteDouble(const FunctionCallbackInfo<Value>& args);
55
  static void WriteRawBytes(const FunctionCallbackInfo<Value>& args);
56
57
  SET_NO_MEMORY_INFO()
58
  SET_MEMORY_INFO_NAME(SerializerContext)
59
  SET_SELF_SIZE(SerializerContext)
60
61
 private:
62
  ValueSerializer serializer_;
63
};
64
65
class DeserializerContext : public BaseObject,
66
                            public ValueDeserializer::Delegate {
67
 public:
68
  DeserializerContext(Environment* env,
69
                      Local<Object> wrap,
70
                      Local<Value> buffer);
71
72
686
  ~DeserializerContext() override = default;
73
74
  MaybeLocal<Object> ReadHostObject(Isolate* isolate) override;
75
76
  static void New(const FunctionCallbackInfo<Value>& args);
77
  static void ReadHeader(const FunctionCallbackInfo<Value>& args);
78
  static void ReadValue(const FunctionCallbackInfo<Value>& args);
79
  static void TransferArrayBuffer(const FunctionCallbackInfo<Value>& args);
80
  static void GetWireFormatVersion(const FunctionCallbackInfo<Value>& args);
81
  static void ReadUint32(const FunctionCallbackInfo<Value>& args);
82
  static void ReadUint64(const FunctionCallbackInfo<Value>& args);
83
  static void ReadDouble(const FunctionCallbackInfo<Value>& args);
84
  static void ReadRawBytes(const FunctionCallbackInfo<Value>& args);
85
86
  SET_NO_MEMORY_INFO()
87
  SET_MEMORY_INFO_NAME(DeserializerContext)
88
  SET_SELF_SIZE(DeserializerContext)
89
90
 private:
91
  const uint8_t* data_;
92
  const size_t length_;
93
94
  ValueDeserializer deserializer_;
95
};
96
97
312
SerializerContext::SerializerContext(Environment* env, Local<Object> wrap)
98
  : BaseObject(env, wrap),
99
312
    serializer_(env->isolate(), this) {
100
312
  MakeWeak();
101
311
}
102
103
2
void SerializerContext::ThrowDataCloneError(Local<String> message) {
104
4
  Local<Value> args[1] = { message };
105
  Local<Value> get_data_clone_error =
106
2
      object()->Get(env()->context(),
107
10
                    env()->get_data_clone_error_string())
108
4
                      .ToLocalChecked();
109
110
2
  CHECK(get_data_clone_error->IsFunction());
111
  MaybeLocal<Value> error =
112
      get_data_clone_error.As<Function>()->Call(env()->context(),
113
                                                object(),
114
2
                                                arraysize(args),
115
8
                                                args);
116
117
4
  if (error.IsEmpty()) return;
118
119
2
  env()->isolate()->ThrowException(error.ToLocalChecked());
120
}
121
122
Maybe<uint32_t> SerializerContext::GetSharedArrayBufferId(
123
    Isolate* isolate, Local<SharedArrayBuffer> shared_array_buffer) {
124
  Local<Value> args[1] = { shared_array_buffer };
125
  Local<Value> get_shared_array_buffer_id =
126
      object()->Get(env()->context(),
127
                    env()->get_shared_array_buffer_id_string())
128
                      .ToLocalChecked();
129
130
  if (!get_shared_array_buffer_id->IsFunction()) {
131
    return ValueSerializer::Delegate::GetSharedArrayBufferId(
132
        isolate, shared_array_buffer);
133
  }
134
135
  MaybeLocal<Value> id =
136
      get_shared_array_buffer_id.As<Function>()->Call(env()->context(),
137
                                                      object(),
138
                                                      arraysize(args),
139
                                                      args);
140
141
  if (id.IsEmpty()) return Nothing<uint32_t>();
142
143
  return id.ToLocalChecked()->Uint32Value(env()->context());
144
}
145
146
29
Maybe<bool> SerializerContext::WriteHostObject(Isolate* isolate,
147
                                               Local<Object> input) {
148
  MaybeLocal<Value> ret;
149
58
  Local<Value> args[1] = { input };
150
151
  Local<Value> write_host_object =
152
29
      object()->Get(env()->context(),
153
145
                    env()->write_host_object_string()).ToLocalChecked();
154
155
29
  if (!write_host_object->IsFunction()) {
156
    return ValueSerializer::Delegate::WriteHostObject(isolate, input);
157
  }
158
159
  ret = write_host_object.As<Function>()->Call(env()->context(),
160
                                               object(),
161
29
                                               arraysize(args),
162
116
                                               args);
163
164
29
  if (ret.IsEmpty())
165
2
    return Nothing<bool>();
166
167
27
  return Just(true);
168
}
169
170
312
void SerializerContext::New(const FunctionCallbackInfo<Value>& args) {
171
312
  Environment* env = Environment::GetCurrent(args);
172
173
313
  new SerializerContext(env, args.This());
174
312
}
175
176
312
void SerializerContext::WriteHeader(const FunctionCallbackInfo<Value>& args) {
177
  SerializerContext* ctx;
178
625
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
179
313
  ctx->serializer_.WriteHeader();
180
}
181
182
320
void SerializerContext::WriteValue(const FunctionCallbackInfo<Value>& args) {
183
  SerializerContext* ctx;
184
640
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
185
  Maybe<bool> ret =
186
321
      ctx->serializer_.WriteValue(ctx->env()->context(), args[0]);
187
188
1268
  if (ret.IsJust()) args.GetReturnValue().Set(ret.FromJust());
189
}
190
191
311
void SerializerContext::SetTreatArrayBufferViewsAsHostObjects(
192
    const FunctionCallbackInfo<Value>& args) {
193
  SerializerContext* ctx;
194
622
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
195
196
934
  bool value = args[0]->BooleanValue(ctx->env()->isolate());
197
311
  ctx->serializer_.SetTreatArrayBufferViewsAsHostObjects(value);
198
}
199
200
308
void SerializerContext::ReleaseBuffer(const FunctionCallbackInfo<Value>& args) {
201
  SerializerContext* ctx;
202
615
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
203
204
  // Note: Both ValueSerializer and this Buffer::New() variant use malloc()
205
  // as the underlying allocator.
206
308
  std::pair<uint8_t*, size_t> ret = ctx->serializer_.Release();
207
  auto buf = Buffer::New(ctx->env(),
208
                         reinterpret_cast<char*>(ret.first),
209
                         ret.second,
210
308
                         true /* uses_malloc */);
211
212
308
  if (!buf.IsEmpty()) {
213
614
    args.GetReturnValue().Set(buf.ToLocalChecked());
214
  }
215
}
216
217
void SerializerContext::TransferArrayBuffer(
218
    const FunctionCallbackInfo<Value>& args) {
219
  SerializerContext* ctx;
220
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
221
222
  Maybe<uint32_t> id = args[0]->Uint32Value(ctx->env()->context());
223
  if (id.IsNothing()) return;
224
225
  if (!args[1]->IsArrayBuffer())
226
    return node::THROW_ERR_INVALID_ARG_TYPE(
227
        ctx->env(), "arrayBuffer must be an ArrayBuffer");
228
229
  Local<ArrayBuffer> ab = args[1].As<ArrayBuffer>();
230
  ctx->serializer_.TransferArrayBuffer(id.FromJust(), ab);
231
  return;
232
}
233
234
49
void SerializerContext::WriteUint32(const FunctionCallbackInfo<Value>& args) {
235
  SerializerContext* ctx;
236
49
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
237
238
147
  Maybe<uint32_t> value = args[0]->Uint32Value(ctx->env()->context());
239
49
  if (value.IsNothing()) return;
240
241
49
  ctx->serializer_.WriteUint32(value.FromJust());
242
}
243
244
1
void SerializerContext::WriteUint64(const FunctionCallbackInfo<Value>& args) {
245
  SerializerContext* ctx;
246
1
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
247
248
3
  Maybe<uint32_t> arg0 = args[0]->Uint32Value(ctx->env()->context());
249
3
  Maybe<uint32_t> arg1 = args[1]->Uint32Value(ctx->env()->context());
250

2
  if (arg0.IsNothing() || arg1.IsNothing())
251
    return;
252
253
1
  uint64_t hi = arg0.FromJust();
254
1
  uint64_t lo = arg1.FromJust();
255
1
  ctx->serializer_.WriteUint64((hi << 32) | lo);
256
}
257
258
1
void SerializerContext::WriteDouble(const FunctionCallbackInfo<Value>& args) {
259
  SerializerContext* ctx;
260
1
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
261
262
3
  Maybe<double> value = args[0]->NumberValue(ctx->env()->context());
263
1
  if (value.IsNothing()) return;
264
265
1
  ctx->serializer_.WriteDouble(value.FromJust());
266
}
267
268
28
void SerializerContext::WriteRawBytes(const FunctionCallbackInfo<Value>& args) {
269
  SerializerContext* ctx;
270
29
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
271
272
56
  if (!args[0]->IsArrayBufferView()) {
273
    return node::THROW_ERR_INVALID_ARG_TYPE(
274
1
        ctx->env(), "source must be a TypedArray or a DataView");
275
  }
276
277
27
  ArrayBufferViewContents<char> bytes(args[0]);
278
27
  ctx->serializer_.WriteRawBytes(bytes.data(), bytes.length());
279
}
280
281
343
DeserializerContext::DeserializerContext(Environment* env,
282
                                         Local<Object> wrap,
283
                                         Local<Value> buffer)
284
  : BaseObject(env, wrap),
285
343
    data_(reinterpret_cast<const uint8_t*>(Buffer::Data(buffer))),
286
344
    length_(Buffer::Length(buffer)),
287
1031
    deserializer_(env->isolate(), data_, length_, this) {
288
1375
  object()->Set(env->context(), env->buffer_string(), buffer).Check();
289
344
  deserializer_.SetExpectInlineWasm(true);
290
291
344
  MakeWeak();
292
343
}
293
294
37
MaybeLocal<Object> DeserializerContext::ReadHostObject(Isolate* isolate) {
295
  Local<Value> read_host_object =
296
37
      object()->Get(env()->context(),
297
185
                    env()->read_host_object_string()).ToLocalChecked();
298
299
37
  if (!read_host_object->IsFunction()) {
300
    return ValueDeserializer::Delegate::ReadHostObject(isolate);
301
  }
302
303
37
  Isolate::AllowJavascriptExecutionScope allow_js(isolate);
304
  MaybeLocal<Value> ret =
305
      read_host_object.As<Function>()->Call(env()->context(),
306
                                            object(),
307
                                            0,
308
148
                                            nullptr);
309
310
37
  if (ret.IsEmpty())
311
    return MaybeLocal<Object>();
312
313
37
  Local<Value> return_value = ret.ToLocalChecked();
314
37
  if (!return_value->IsObject()) {
315
    env()->ThrowTypeError("readHostObject must return an object");
316
    return MaybeLocal<Object>();
317
  }
318
319
37
  return return_value.As<Object>();
320
}
321
322
345
void DeserializerContext::New(const FunctionCallbackInfo<Value>& args) {
323
345
  Environment* env = Environment::GetCurrent(args);
324
325
690
  if (!args[0]->IsArrayBufferView()) {
326
    return node::THROW_ERR_INVALID_ARG_TYPE(
327
347
        env, "buffer must be a TypedArray or a DataView");
328
  }
329
330
686
  new DeserializerContext(env, args.This(), args[0]);
331
}
332
333
343
void DeserializerContext::ReadHeader(const FunctionCallbackInfo<Value>& args) {
334
  DeserializerContext* ctx;
335
686
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
336
337
344
  Maybe<bool> ret = ctx->deserializer_.ReadHeader(ctx->env()->context());
338
339
1373
  if (ret.IsJust()) args.GetReturnValue().Set(ret.FromJust());
340
}
341
342
352
void DeserializerContext::ReadValue(const FunctionCallbackInfo<Value>& args) {
343
  DeserializerContext* ctx;
344
705
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
345
346
353
  MaybeLocal<Value> ret = ctx->deserializer_.ReadValue(ctx->env()->context());
347
348
1058
  if (!ret.IsEmpty()) args.GetReturnValue().Set(ret.ToLocalChecked());
349
}
350
351
void DeserializerContext::TransferArrayBuffer(
352
    const FunctionCallbackInfo<Value>& args) {
353
  DeserializerContext* ctx;
354
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
355
356
  Maybe<uint32_t> id = args[0]->Uint32Value(ctx->env()->context());
357
  if (id.IsNothing()) return;
358
359
  if (args[1]->IsArrayBuffer()) {
360
    Local<ArrayBuffer> ab = args[1].As<ArrayBuffer>();
361
    ctx->deserializer_.TransferArrayBuffer(id.FromJust(), ab);
362
    return;
363
  }
364
365
  if (args[1]->IsSharedArrayBuffer()) {
366
    Local<SharedArrayBuffer> sab = args[1].As<SharedArrayBuffer>();
367
    ctx->deserializer_.TransferSharedArrayBuffer(id.FromJust(), sab);
368
    return;
369
  }
370
371
  return node::THROW_ERR_INVALID_ARG_TYPE(
372
      ctx->env(), "arrayBuffer must be an ArrayBuffer or SharedArrayBuffer");
373
}
374
375
1
void DeserializerContext::GetWireFormatVersion(
376
    const FunctionCallbackInfo<Value>& args) {
377
  DeserializerContext* ctx;
378
2
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
379
380
3
  args.GetReturnValue().Set(ctx->deserializer_.GetWireFormatVersion());
381
}
382
383
69
void DeserializerContext::ReadUint32(const FunctionCallbackInfo<Value>& args) {
384
  DeserializerContext* ctx;
385
69
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
386
387
  uint32_t value;
388
69
  bool ok = ctx->deserializer_.ReadUint32(&value);
389
69
  if (!ok) return ctx->env()->ThrowError("ReadUint32() failed");
390
207
  return args.GetReturnValue().Set(value);
391
}
392
393
1
void DeserializerContext::ReadUint64(const FunctionCallbackInfo<Value>& args) {
394
  DeserializerContext* ctx;
395
1
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
396
397
  uint64_t value;
398
1
  bool ok = ctx->deserializer_.ReadUint64(&value);
399
1
  if (!ok) return ctx->env()->ThrowError("ReadUint64() failed");
400
401
1
  uint32_t hi = static_cast<uint32_t>(value >> 32);
402
1
  uint32_t lo = static_cast<uint32_t>(value);
403
404
1
  Isolate* isolate = ctx->env()->isolate();
405
406
  Local<Value> ret[] = {
407
    Integer::NewFromUnsigned(isolate, hi),
408
    Integer::NewFromUnsigned(isolate, lo)
409
3
  };
410
3
  return args.GetReturnValue().Set(Array::New(isolate, ret, arraysize(ret)));
411
}
412
413
1
void DeserializerContext::ReadDouble(const FunctionCallbackInfo<Value>& args) {
414
  DeserializerContext* ctx;
415
1
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
416
417
  double value;
418
1
  bool ok = ctx->deserializer_.ReadDouble(&value);
419
1
  if (!ok) return ctx->env()->ThrowError("ReadDouble() failed");
420
3
  return args.GetReturnValue().Set(value);
421
}
422
423
37
void DeserializerContext::ReadRawBytes(
424
    const FunctionCallbackInfo<Value>& args) {
425
  DeserializerContext* ctx;
426
37
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
427
428
111
  Maybe<int64_t> length_arg = args[0]->IntegerValue(ctx->env()->context());
429
37
  if (length_arg.IsNothing()) return;
430
37
  size_t length = length_arg.FromJust();
431
432
  const void* data;
433
37
  bool ok = ctx->deserializer_.ReadRawBytes(length, &data);
434
37
  if (!ok) return ctx->env()->ThrowError("ReadRawBytes() failed");
435
436
37
  const uint8_t* position = reinterpret_cast<const uint8_t*>(data);
437
37
  CHECK_GE(position, ctx->data_);
438
37
  CHECK_LE(position + length, ctx->data_ + ctx->length_);
439
440
37
  const uint32_t offset = position - ctx->data_;
441
37
  CHECK_EQ(ctx->data_ + offset, position);
442
443
74
  args.GetReturnValue().Set(offset);
444
}
445
446
71
void Initialize(Local<Object> target,
447
                Local<Value> unused,
448
                Local<Context> context,
449
                void* priv) {
450
71
  Environment* env = Environment::GetCurrent(context);
451
  Local<FunctionTemplate> ser =
452
71
      env->NewFunctionTemplate(SerializerContext::New);
453
454
142
  ser->InstanceTemplate()->SetInternalFieldCount(1);
455
456
71
  env->SetProtoMethod(ser, "writeHeader", SerializerContext::WriteHeader);
457
71
  env->SetProtoMethod(ser, "writeValue", SerializerContext::WriteValue);
458
71
  env->SetProtoMethod(ser, "releaseBuffer", SerializerContext::ReleaseBuffer);
459
  env->SetProtoMethod(ser,
460
                      "transferArrayBuffer",
461
71
                      SerializerContext::TransferArrayBuffer);
462
71
  env->SetProtoMethod(ser, "writeUint32", SerializerContext::WriteUint32);
463
71
  env->SetProtoMethod(ser, "writeUint64", SerializerContext::WriteUint64);
464
71
  env->SetProtoMethod(ser, "writeDouble", SerializerContext::WriteDouble);
465
71
  env->SetProtoMethod(ser, "writeRawBytes", SerializerContext::WriteRawBytes);
466
  env->SetProtoMethod(ser,
467
                      "_setTreatArrayBufferViewsAsHostObjects",
468
71
                      SerializerContext::SetTreatArrayBufferViewsAsHostObjects);
469
470
  Local<String> serializerString =
471
71
      FIXED_ONE_BYTE_STRING(env->isolate(), "Serializer");
472
71
  ser->SetClassName(serializerString);
473
  target->Set(env->context(),
474
              serializerString,
475
355
              ser->GetFunction(env->context()).ToLocalChecked()).Check();
476
477
  Local<FunctionTemplate> des =
478
71
      env->NewFunctionTemplate(DeserializerContext::New);
479
480
142
  des->InstanceTemplate()->SetInternalFieldCount(1);
481
482
71
  env->SetProtoMethod(des, "readHeader", DeserializerContext::ReadHeader);
483
71
  env->SetProtoMethod(des, "readValue", DeserializerContext::ReadValue);
484
  env->SetProtoMethod(des,
485
                      "getWireFormatVersion",
486
71
                      DeserializerContext::GetWireFormatVersion);
487
  env->SetProtoMethod(des,
488
                      "transferArrayBuffer",
489
71
                      DeserializerContext::TransferArrayBuffer);
490
71
  env->SetProtoMethod(des, "readUint32", DeserializerContext::ReadUint32);
491
71
  env->SetProtoMethod(des, "readUint64", DeserializerContext::ReadUint64);
492
71
  env->SetProtoMethod(des, "readDouble", DeserializerContext::ReadDouble);
493
71
  env->SetProtoMethod(des, "_readRawBytes", DeserializerContext::ReadRawBytes);
494
495
  Local<String> deserializerString =
496
71
      FIXED_ONE_BYTE_STRING(env->isolate(), "Deserializer");
497
71
  des->SetClassName(deserializerString);
498
  target->Set(env->context(),
499
              deserializerString,
500
355
              des->GetFunction(env->context()).ToLocalChecked()).Check();
501
71
}
502
503
}  // anonymous namespace
504
}  // namespace node
505
506
4943
NODE_MODULE_CONTEXT_AWARE_INTERNAL(serdes, node::Initialize)