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: 177 225 78.7 %
Date: 2020-05-27 22:15:15 Branches: 45 100 45.0 %

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
550
  ~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
606
  ~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
273
SerializerContext::SerializerContext(Environment* env, Local<Object> wrap)
98
  : BaseObject(env, wrap),
99
273
    serializer_(env->isolate(), this) {
100
274
  MakeWeak();
101
275
}
102
103
2
void SerializerContext::ThrowDataCloneError(Local<String> message) {
104
4
  Local<Value> args[1] = { message };
105
  Local<Value> get_data_clone_error =
106
6
      object()->Get(env()->context(),
107
8
                    env()->get_data_clone_error_string())
108
2
                      .ToLocalChecked();
109
110
2
  CHECK(get_data_clone_error->IsFunction());
111
  MaybeLocal<Value> error =
112
4
      get_data_clone_error.As<Function>()->Call(env()->context(),
113
                                                object(),
114
2
                                                arraysize(args),
115
8
                                                args);
116
117
2
  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
35
Maybe<bool> SerializerContext::WriteHostObject(Isolate* isolate,
147
                                               Local<Object> input) {
148
  MaybeLocal<Value> ret;
149
70
  Local<Value> args[1] = { input };
150
151
  Local<Value> write_host_object =
152
105
      object()->Get(env()->context(),
153
175
                    env()->write_host_object_string()).ToLocalChecked();
154
155
35
  if (!write_host_object->IsFunction()) {
156
    return ValueSerializer::Delegate::WriteHostObject(isolate, input);
157
  }
158
159
70
  ret = write_host_object.As<Function>()->Call(env()->context(),
160
                                               object(),
161
35
                                               arraysize(args),
162
140
                                               args);
163
164
35
  if (ret.IsEmpty())
165
2
    return Nothing<bool>();
166
167
33
  return Just(true);
168
}
169
170
273
void SerializerContext::New(const FunctionCallbackInfo<Value>& args) {
171
273
  Environment* env = Environment::GetCurrent(args);
172
173
275
  new SerializerContext(env, args.This());
174
274
}
175
176
274
void SerializerContext::WriteHeader(const FunctionCallbackInfo<Value>& args) {
177
  SerializerContext* ctx;
178
274
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
179
274
  ctx->serializer_.WriteHeader();
180
}
181
182
282
void SerializerContext::WriteValue(const FunctionCallbackInfo<Value>& args) {
183
  SerializerContext* ctx;
184
282
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
185
  Maybe<bool> ret =
186
282
      ctx->serializer_.WriteValue(ctx->env()->context(), args[0]);
187
188
1116
  if (ret.IsJust()) args.GetReturnValue().Set(ret.FromJust());
189
}
190
191
273
void SerializerContext::SetTreatArrayBufferViewsAsHostObjects(
192
    const FunctionCallbackInfo<Value>& args) {
193
  SerializerContext* ctx;
194
273
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
195
196
819
  bool value = args[0]->BooleanValue(ctx->env()->isolate());
197
274
  ctx->serializer_.SetTreatArrayBufferViewsAsHostObjects(value);
198
}
199
200
270
void SerializerContext::ReleaseBuffer(const FunctionCallbackInfo<Value>& args) {
201
  SerializerContext* ctx;
202
270
  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
270
  std::pair<uint8_t*, size_t> ret = ctx->serializer_.Release();
207
  auto buf = Buffer::New(ctx->env(),
208
270
                         reinterpret_cast<char*>(ret.first),
209
                         ret.second,
210
540
                         true /* uses_malloc */);
211
212
270
  if (!buf.IsEmpty()) {
213
540
    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
67
void SerializerContext::WriteUint32(const FunctionCallbackInfo<Value>& args) {
235
  SerializerContext* ctx;
236
67
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
237
238
201
  Maybe<uint32_t> value = args[0]->Uint32Value(ctx->env()->context());
239
67
  if (value.IsNothing()) return;
240
241
67
  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
34
void SerializerContext::WriteRawBytes(const FunctionCallbackInfo<Value>& args) {
269
  SerializerContext* ctx;
270
35
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
271
272
68
  if (!args[0]->IsArrayBufferView()) {
273
1
    return node::THROW_ERR_INVALID_ARG_TYPE(
274
1
        ctx->env(), "source must be a TypedArray or a DataView");
275
  }
276
277
33
  ArrayBufferViewContents<char> bytes(args[0]);
278
33
  ctx->serializer_.WriteRawBytes(bytes.data(), bytes.length());
279
}
280
281
302
DeserializerContext::DeserializerContext(Environment* env,
282
                                         Local<Object> wrap,
283
302
                                         Local<Value> buffer)
284
  : BaseObject(env, wrap),
285
303
    data_(reinterpret_cast<const uint8_t*>(Buffer::Data(buffer))),
286
303
    length_(Buffer::Length(buffer)),
287
908
    deserializer_(env->isolate(), data_, length_, this) {
288
1212
  object()->Set(env->context(), env->buffer_string(), buffer).Check();
289
290
303
  MakeWeak();
291
303
}
292
293
43
MaybeLocal<Object> DeserializerContext::ReadHostObject(Isolate* isolate) {
294
  Local<Value> read_host_object =
295
129
      object()->Get(env()->context(),
296
215
                    env()->read_host_object_string()).ToLocalChecked();
297
298
43
  if (!read_host_object->IsFunction()) {
299
    return ValueDeserializer::Delegate::ReadHostObject(isolate);
300
  }
301
302
86
  Isolate::AllowJavascriptExecutionScope allow_js(isolate);
303
  MaybeLocal<Value> ret =
304
86
      read_host_object.As<Function>()->Call(env()->context(),
305
                                            object(),
306
                                            0,
307
172
                                            nullptr);
308
309
43
  if (ret.IsEmpty())
310
    return MaybeLocal<Object>();
311
312
43
  Local<Value> return_value = ret.ToLocalChecked();
313
43
  if (!return_value->IsObject()) {
314
    env()->ThrowTypeError("readHostObject must return an object");
315
    return MaybeLocal<Object>();
316
  }
317
318
43
  return return_value.As<Object>();
319
}
320
321
304
void DeserializerContext::New(const FunctionCallbackInfo<Value>& args) {
322
304
  Environment* env = Environment::GetCurrent(args);
323
324
610
  if (!args[0]->IsArrayBufferView()) {
325
    return node::THROW_ERR_INVALID_ARG_TYPE(
326
2
        env, "buffer must be a TypedArray or a DataView");
327
  }
328
329
604
  new DeserializerContext(env, args.This(), args[0]);
330
}
331
332
303
void DeserializerContext::ReadHeader(const FunctionCallbackInfo<Value>& args) {
333
  DeserializerContext* ctx;
334
303
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
335
336
303
  Maybe<bool> ret = ctx->deserializer_.ReadHeader(ctx->env()->context());
337
338
1212
  if (ret.IsJust()) args.GetReturnValue().Set(ret.FromJust());
339
}
340
341
311
void DeserializerContext::ReadValue(const FunctionCallbackInfo<Value>& args) {
342
  DeserializerContext* ctx;
343
311
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
344
345
311
  MaybeLocal<Value> ret = ctx->deserializer_.ReadValue(ctx->env()->context());
346
347
933
  if (!ret.IsEmpty()) args.GetReturnValue().Set(ret.ToLocalChecked());
348
}
349
350
void DeserializerContext::TransferArrayBuffer(
351
    const FunctionCallbackInfo<Value>& args) {
352
  DeserializerContext* ctx;
353
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
354
355
  Maybe<uint32_t> id = args[0]->Uint32Value(ctx->env()->context());
356
  if (id.IsNothing()) return;
357
358
  if (args[1]->IsArrayBuffer()) {
359
    Local<ArrayBuffer> ab = args[1].As<ArrayBuffer>();
360
    ctx->deserializer_.TransferArrayBuffer(id.FromJust(), ab);
361
    return;
362
  }
363
364
  if (args[1]->IsSharedArrayBuffer()) {
365
    Local<SharedArrayBuffer> sab = args[1].As<SharedArrayBuffer>();
366
    ctx->deserializer_.TransferSharedArrayBuffer(id.FromJust(), sab);
367
    return;
368
  }
369
370
  return node::THROW_ERR_INVALID_ARG_TYPE(
371
      ctx->env(), "arrayBuffer must be an ArrayBuffer or SharedArrayBuffer");
372
}
373
374
1
void DeserializerContext::GetWireFormatVersion(
375
    const FunctionCallbackInfo<Value>& args) {
376
  DeserializerContext* ctx;
377
1
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
378
379
3
  args.GetReturnValue().Set(ctx->deserializer_.GetWireFormatVersion());
380
}
381
382
87
void DeserializerContext::ReadUint32(const FunctionCallbackInfo<Value>& args) {
383
  DeserializerContext* ctx;
384
87
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
385
386
  uint32_t value;
387
87
  bool ok = ctx->deserializer_.ReadUint32(&value);
388
87
  if (!ok) return ctx->env()->ThrowError("ReadUint32() failed");
389
261
  return args.GetReturnValue().Set(value);
390
}
391
392
1
void DeserializerContext::ReadUint64(const FunctionCallbackInfo<Value>& args) {
393
  DeserializerContext* ctx;
394
1
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
395
396
  uint64_t value;
397
1
  bool ok = ctx->deserializer_.ReadUint64(&value);
398
1
  if (!ok) return ctx->env()->ThrowError("ReadUint64() failed");
399
400
1
  uint32_t hi = static_cast<uint32_t>(value >> 32);
401
1
  uint32_t lo = static_cast<uint32_t>(value);
402
403
1
  Isolate* isolate = ctx->env()->isolate();
404
405
  Local<Value> ret[] = {
406
    Integer::NewFromUnsigned(isolate, hi),
407
    Integer::NewFromUnsigned(isolate, lo)
408
3
  };
409
3
  return args.GetReturnValue().Set(Array::New(isolate, ret, arraysize(ret)));
410
}
411
412
1
void DeserializerContext::ReadDouble(const FunctionCallbackInfo<Value>& args) {
413
  DeserializerContext* ctx;
414
1
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
415
416
  double value;
417
1
  bool ok = ctx->deserializer_.ReadDouble(&value);
418
1
  if (!ok) return ctx->env()->ThrowError("ReadDouble() failed");
419
3
  return args.GetReturnValue().Set(value);
420
}
421
422
43
void DeserializerContext::ReadRawBytes(
423
    const FunctionCallbackInfo<Value>& args) {
424
  DeserializerContext* ctx;
425
43
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
426
427
129
  Maybe<int64_t> length_arg = args[0]->IntegerValue(ctx->env()->context());
428
43
  if (length_arg.IsNothing()) return;
429
43
  size_t length = length_arg.FromJust();
430
431
  const void* data;
432
43
  bool ok = ctx->deserializer_.ReadRawBytes(length, &data);
433
43
  if (!ok) return ctx->env()->ThrowError("ReadRawBytes() failed");
434
435
43
  const uint8_t* position = reinterpret_cast<const uint8_t*>(data);
436
43
  CHECK_GE(position, ctx->data_);
437
43
  CHECK_LE(position + length, ctx->data_ + ctx->length_);
438
439
43
  const uint32_t offset = position - ctx->data_;
440
43
  CHECK_EQ(ctx->data_ + offset, position);
441
442
86
  args.GetReturnValue().Set(offset);
443
}
444
445
522
void Initialize(Local<Object> target,
446
                Local<Value> unused,
447
                Local<Context> context,
448
                void* priv) {
449
522
  Environment* env = Environment::GetCurrent(context);
450
  Local<FunctionTemplate> ser =
451
522
      env->NewFunctionTemplate(SerializerContext::New);
452
453
1566
  ser->InstanceTemplate()->SetInternalFieldCount(
454
522
      SerializerContext::kInternalFieldCount);
455
456
522
  env->SetProtoMethod(ser, "writeHeader", SerializerContext::WriteHeader);
457
522
  env->SetProtoMethod(ser, "writeValue", SerializerContext::WriteValue);
458
522
  env->SetProtoMethod(ser, "releaseBuffer", SerializerContext::ReleaseBuffer);
459
  env->SetProtoMethod(ser,
460
                      "transferArrayBuffer",
461
522
                      SerializerContext::TransferArrayBuffer);
462
522
  env->SetProtoMethod(ser, "writeUint32", SerializerContext::WriteUint32);
463
522
  env->SetProtoMethod(ser, "writeUint64", SerializerContext::WriteUint64);
464
522
  env->SetProtoMethod(ser, "writeDouble", SerializerContext::WriteDouble);
465
522
  env->SetProtoMethod(ser, "writeRawBytes", SerializerContext::WriteRawBytes);
466
  env->SetProtoMethod(ser,
467
                      "_setTreatArrayBufferViewsAsHostObjects",
468
522
                      SerializerContext::SetTreatArrayBufferViewsAsHostObjects);
469
470
  Local<String> serializerString =
471
522
      FIXED_ONE_BYTE_STRING(env->isolate(), "Serializer");
472
522
  ser->SetClassName(serializerString);
473
1044
  target->Set(env->context(),
474
              serializerString,
475
2610
              ser->GetFunction(env->context()).ToLocalChecked()).Check();
476
477
  Local<FunctionTemplate> des =
478
522
      env->NewFunctionTemplate(DeserializerContext::New);
479
480
1566
  des->InstanceTemplate()->SetInternalFieldCount(
481
522
      DeserializerContext::kInternalFieldCount);
482
483
522
  env->SetProtoMethod(des, "readHeader", DeserializerContext::ReadHeader);
484
522
  env->SetProtoMethod(des, "readValue", DeserializerContext::ReadValue);
485
  env->SetProtoMethod(des,
486
                      "getWireFormatVersion",
487
522
                      DeserializerContext::GetWireFormatVersion);
488
  env->SetProtoMethod(des,
489
                      "transferArrayBuffer",
490
522
                      DeserializerContext::TransferArrayBuffer);
491
522
  env->SetProtoMethod(des, "readUint32", DeserializerContext::ReadUint32);
492
522
  env->SetProtoMethod(des, "readUint64", DeserializerContext::ReadUint64);
493
522
  env->SetProtoMethod(des, "readDouble", DeserializerContext::ReadDouble);
494
522
  env->SetProtoMethod(des, "_readRawBytes", DeserializerContext::ReadRawBytes);
495
496
  Local<String> deserializerString =
497
522
      FIXED_ONE_BYTE_STRING(env->isolate(), "Deserializer");
498
522
  des->SetClassName(deserializerString);
499
1044
  target->Set(env->context(),
500
              deserializerString,
501
2610
              des->GetFunction(env->context()).ToLocalChecked()).Check();
502
522
}
503
504
}  // anonymous namespace
505
}  // namespace node
506
507
4325
NODE_MODULE_CONTEXT_AWARE_INTERNAL(serdes, node::Initialize)