GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_crypto_bio.cc Lines: 234 267 87.6 %
Date: 2020-07-19 22:14:24 Branches: 126 180 70.0 %

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 "base_object-inl.h"
23
#include "memory_tracker-inl.h"
24
#include "allocated_buffer-inl.h"  // Inlined functions needed by node_crypto.h.
25
#include "node_crypto_bio.h"
26
#include "openssl/bio.h"
27
#include "util-inl.h"
28
#include <climits>
29
#include <cstring>
30
31
namespace node {
32
namespace crypto {
33
34
60899
BIOPointer NodeBIO::New(Environment* env) {
35
60899
  BIOPointer bio(BIO_new(GetMethod()));
36

60899
  if (bio && env != nullptr)
37
24418
    NodeBIO::FromBIO(bio.get())->env_ = env;
38
60899
  return bio;
39
}
40
41
42
36481
BIOPointer NodeBIO::NewFixed(const char* data, size_t len, Environment* env) {
43
72962
  BIOPointer bio = New(env);
44
45

109443
  if (!bio ||
46
36481
      len > INT_MAX ||
47

109443
      BIO_write(bio.get(), data, len) != static_cast<int>(len) ||
48
36481
      BIO_set_mem_eof_return(bio.get(), 0) != 1) {
49
    return BIOPointer();
50
  }
51
52
36481
  return bio;
53
}
54
55
56
60899
int NodeBIO::New(BIO* bio) {
57
60899
  BIO_set_data(bio, new NodeBIO());
58
60899
  BIO_set_init(bio, 1);
59
60
60899
  return 1;
61
}
62
63
64
60873
int NodeBIO::Free(BIO* bio) {
65
60873
  if (bio == nullptr)
66
    return 0;
67
68
60873
  if (BIO_get_shutdown(bio)) {
69

60873
    if (BIO_get_init(bio) && BIO_get_data(bio) != nullptr) {
70
60873
      delete FromBIO(bio);
71
60873
      BIO_set_data(bio, nullptr);
72
    }
73
  }
74
75
60873
  return 1;
76
}
77
78
79
37357
int NodeBIO::Read(BIO* bio, char* out, int len) {
80
37357
  BIO_clear_retry_flags(bio);
81
82
37357
  NodeBIO* nbio = FromBIO(bio);
83
37357
  int bytes = nbio->Read(out, len);
84
85
37357
  if (bytes == 0) {
86
7430
    bytes = nbio->eof_return();
87
7430
    if (bytes != 0) {
88
7428
      BIO_set_retry_read(bio);
89
    }
90
  }
91
92
37357
  return bytes;
93
}
94
95
96
20
char* NodeBIO::Peek(size_t* size) {
97
20
  *size = read_head_->write_pos_ - read_head_->read_pos_;
98
20
  return read_head_->data_ + read_head_->read_pos_;
99
}
100
101
102
7945
size_t NodeBIO::PeekMultiple(char** out, size_t* size, size_t* count) {
103
7945
  Buffer* pos = read_head_;
104
7945
  size_t max = *count;
105
7945
  size_t total = 0;
106
107
  size_t i;
108
8503
  for (i = 0; i < max; i++) {
109
8503
    size[i] = pos->write_pos_ - pos->read_pos_;
110
8503
    total += size[i];
111
8503
    out[i] = pos->data_ + pos->read_pos_;
112
113
    /* Don't get past write head */
114
8503
    if (pos == write_head_)
115
7945
      break;
116
    else
117
558
      pos = pos->next_;
118
  }
119
120
7945
  if (i == max)
121
    *count = i;
122
  else
123
7945
    *count = i + 1;
124
125
7945
  return total;
126
}
127
128
129
46591
int NodeBIO::Write(BIO* bio, const char* data, int len) {
130
46591
  BIO_clear_retry_flags(bio);
131
132
46591
  FromBIO(bio)->Write(data, len);
133
134
46591
  return len;
135
}
136
137
138
int NodeBIO::Puts(BIO* bio, const char* str) {
139
  return Write(bio, str, strlen(str));
140
}
141
142
143
839907
int NodeBIO::Gets(BIO* bio, char* out, int size) {
144
839907
  NodeBIO* nbio = FromBIO(bio);
145
146
839907
  if (nbio->Length() == 0)
147
1402
    return 0;
148
149
838505
  int i = nbio->IndexOf('\n', size);
150
151
  // Include '\n', if it's there.  If not, don't read off the end.
152


838505
  if (i < size && i >= 0 && static_cast<size_t>(i) < nbio->Length())
153
804276
    i++;
154
155
  // Shift `i` a bit to nullptr-terminate string later
156
838505
  if (size == i)
157
    i--;
158
159
  // Flush read data
160
838505
  nbio->Read(out, i);
161
162
838505
  out[i] = 0;
163
164
838505
  return i;
165
}
166
167
168
66346
long NodeBIO::Ctrl(BIO* bio, int cmd, long num,  // NOLINT(runtime/int)
169
                   void* ptr) {
170
  NodeBIO* nbio;
171
  long ret;  // NOLINT(runtime/int)
172
173
66346
  nbio = FromBIO(bio);
174
66346
  ret = 1;
175
176



66346
  switch (cmd) {
177
    case BIO_CTRL_RESET:
178
      nbio->Reset();
179
      break;
180
    case BIO_CTRL_EOF:
181
      ret = nbio->Length() == 0;
182
      break;
183
    case BIO_C_SET_BUF_MEM_EOF_RETURN:
184
36481
      nbio->set_eof_return(num);
185
36481
      break;
186
    case BIO_CTRL_INFO:
187
      ret = nbio->Length();
188
      if (ptr != nullptr)
189
        *reinterpret_cast<void**>(ptr) = nullptr;
190
      break;
191
    case BIO_C_SET_BUF_MEM:
192
      CHECK(0 && "Can't use SET_BUF_MEM_PTR with NodeBIO");
193
      break;
194
    case BIO_C_GET_BUF_MEM_PTR:
195
      CHECK(0 && "Can't use GET_BUF_MEM_PTR with NodeBIO");
196
      ret = 0;
197
      break;
198
    case BIO_CTRL_GET_CLOSE:
199
      ret = BIO_get_shutdown(bio);
200
      break;
201
    case BIO_CTRL_SET_CLOSE:
202
      BIO_set_shutdown(bio, num);
203
      break;
204
    case BIO_CTRL_WPENDING:
205
      ret = 0;
206
      break;
207
    case BIO_CTRL_PENDING:
208
18271
      ret = nbio->Length();
209
18271
      break;
210
    case BIO_CTRL_DUP:
211
    case BIO_CTRL_FLUSH:
212
5659
      ret = 1;
213
5659
      break;
214
    case BIO_CTRL_PUSH:
215
    case BIO_CTRL_POP:
216
    default:
217
5935
      ret = 0;
218
5935
      break;
219
  }
220
66346
  return ret;
221
}
222
223
224
61543
const BIO_METHOD* NodeBIO::GetMethod() {
225
  // This is called from InitCryptoOnce() to avoid race conditions during
226
  // initialization.
227
  static BIO_METHOD* method = nullptr;
228
229
61543
  if (method == nullptr) {
230
645
    method = BIO_meth_new(BIO_TYPE_MEM, "node.js SSL buffer");
231
645
    BIO_meth_set_write(method, Write);
232
645
    BIO_meth_set_read(method, Read);
233
645
    BIO_meth_set_puts(method, Puts);
234
645
    BIO_meth_set_gets(method, Gets);
235
645
    BIO_meth_set_ctrl(method, Ctrl);
236
645
    BIO_meth_set_create(method, New);
237
645
    BIO_meth_set_destroy(method, Free);
238
  }
239
240
61543
  return method;
241
}
242
243
244
929277
void NodeBIO::TryMoveReadHead() {
245
  // `read_pos_` and `write_pos_` means the position of the reader and writer
246
  // inside the buffer, respectively. When they're equal - its safe to reset
247
  // them, because both reader and writer will continue doing their stuff
248
  // from new (zero) positions.
249

1856839
  while (read_head_->read_pos_ != 0 &&
250
876845
         read_head_->read_pos_ == read_head_->write_pos_) {
251
    // Reset positions
252
50717
    read_head_->read_pos_ = 0;
253
50717
    read_head_->write_pos_ = 0;
254
255
    // Move read_head_ forward, just in case if there're still some data to
256
    // read in the next buffer.
257
50717
    if (read_head_ != write_head_)
258
1188
      read_head_ = read_head_->next_;
259
  }
260
878560
}
261
262
263
883727
size_t NodeBIO::Read(char* out, size_t size) {
264
883727
  size_t bytes_read = 0;
265
883727
  size_t expected = Length() > size ? size : Length();
266
883727
  size_t offset = 0;
267
883727
  size_t left = size;
268
269
2637417
  while (bytes_read < expected) {
270
876845
    CHECK_LE(read_head_->read_pos_, read_head_->write_pos_);
271
876845
    size_t avail = read_head_->write_pos_ - read_head_->read_pos_;
272
876845
    if (avail > left)
273
826128
      avail = left;
274
275
    // Copy data
276
876845
    if (out != nullptr)
277
868432
      memcpy(out + offset, read_head_->data_ + read_head_->read_pos_, avail);
278
876845
    read_head_->read_pos_ += avail;
279
280
    // Move pointers
281
876845
    bytes_read += avail;
282
876845
    offset += avail;
283
876845
    left -= avail;
284
285
876845
    TryMoveReadHead();
286
  }
287
883727
  CHECK_EQ(expected, bytes_read);
288
883727
  length_ -= bytes_read;
289
290
  // Free all empty buffers, but write_head's child
291
883727
  FreeEmpty();
292
293
883727
  return bytes_read;
294
}
295
296
297
883727
void NodeBIO::FreeEmpty() {
298
883727
  if (write_head_ == nullptr)
299
885384
    return;
300
882069
  Buffer* child = write_head_->next_;
301

882069
  if (child == write_head_ || child == read_head_)
302
874770
    return;
303
7299
  Buffer* cur = child->next_;
304

7299
  if (cur == write_head_ || cur == read_head_)
305
7298
    return;
306
307
1
  Buffer* prev = child;
308
3
  while (cur != read_head_) {
309
1
    CHECK_NE(cur, write_head_);
310
1
    CHECK_EQ(cur->write_pos_, cur->read_pos_);
311
312
1
    Buffer* next = cur->next_;
313
1
    delete cur;
314
1
    cur = next;
315
  }
316
1
  prev->next_ = cur;
317
}
318
319
320
838505
size_t NodeBIO::IndexOf(char delim, size_t limit) {
321
838505
  size_t bytes_read = 0;
322
838505
  size_t max = Length() > limit ? limit : Length();
323
838505
  size_t left = limit;
324
838505
  Buffer* current = read_head_;
325
326
906963
  while (bytes_read < max) {
327
838505
    CHECK_LE(current->read_pos_, current->write_pos_);
328
838505
    size_t avail = current->write_pos_ - current->read_pos_;
329
838505
    if (avail > left)
330
688272
      avail = left;
331
332
    // Walk through data
333
838505
    char* tmp = current->data_ + current->read_pos_;
334
838505
    size_t off = 0;
335

111661255
    while (off < avail && *tmp != delim) {
336
55411375
      off++;
337
55411375
      tmp++;
338
    }
339
340
    // Move pointers
341
838505
    bytes_read += off;
342
838505
    left -= off;
343
344
    // Found `delim`
345
838505
    if (off != avail) {
346
804276
      return bytes_read;
347
    }
348
349
    // Move to next buffer
350
34229
    if (current->read_pos_ + avail == current->len_) {
351
29514
      current = current->next_;
352
    }
353
  }
354
34229
  CHECK_EQ(max, bytes_read);
355
356
34229
  return max;
357
}
358
359
360
46591
void NodeBIO::Write(const char* data, size_t size) {
361
46591
  size_t offset = 0;
362
46591
  size_t left = size;
363
364
  // Allocate initial buffer if the ring is empty
365
46591
  TryAllocateForWrite(left);
366
367
140893
  while (left > 0) {
368
47151
    size_t to_write = left;
369
47151
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
370
47151
    size_t avail = write_head_->len_ - write_head_->write_pos_;
371
372
47151
    if (to_write > avail)
373
560
      to_write = avail;
374
375
    // Copy data
376
47151
    memcpy(write_head_->data_ + write_head_->write_pos_,
377
47151
           data + offset,
378
47151
           to_write);
379
380
    // Move pointers
381
47151
    left -= to_write;
382
47151
    offset += to_write;
383
47151
    length_ += to_write;
384
47151
    write_head_->write_pos_ += to_write;
385
47151
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
386
387
    // Go to next buffer if there still are some bytes to write
388
47151
    if (left != 0) {
389
560
      CHECK_EQ(write_head_->write_pos_, write_head_->len_);
390
560
      TryAllocateForWrite(left);
391
560
      write_head_ = write_head_->next_;
392
393
      // Additionally, since we're moved to the next buffer, read head
394
      // may be moved as well.
395
560
      TryMoveReadHead();
396
    }
397
  }
398
46591
  CHECK_EQ(left, 0);
399
46591
}
400
401
402
6594
char* NodeBIO::PeekWritable(size_t* size) {
403
6594
  TryAllocateForWrite(*size);
404
405
6594
  size_t available = write_head_->len_ - write_head_->write_pos_;
406

6594
  if (*size == 0 || available <= *size)
407
6493
    *size = available;
408
409
6594
  return write_head_->data_ + write_head_->write_pos_;
410
}
411
412
413
6469
void NodeBIO::Commit(size_t size) {
414
6469
  write_head_->write_pos_ += size;
415
6469
  length_ += size;
416
6469
  CHECK_LE(write_head_->write_pos_, write_head_->len_);
417
418
  // Allocate new buffer if write head is full,
419
  // and there're no other place to go
420
6469
  TryAllocateForWrite(0);
421
6469
  if (write_head_->write_pos_ == write_head_->len_) {
422
1155
    write_head_ = write_head_->next_;
423
424
    // Additionally, since we're moved to the next buffer, read head
425
    // may be moved as well.
426
1155
    TryMoveReadHead();
427
  }
428
6469
}
429
430
431
60214
void NodeBIO::TryAllocateForWrite(size_t hint) {
432
60214
  Buffer* w = write_head_;
433
60214
  Buffer* r = read_head_;
434
  // If write head is full, next buffer is either read head or not empty.
435

79617
  if (w == nullptr ||
436
21120
      (w->write_pos_ == w->len_ &&
437
2780
       (w->next_ == r || w->next_->write_pos_ != 0))) {
438
41465
    size_t len = w == nullptr ? initial_ :
439
41465
                             kThroughputBufferLength;
440
41465
    if (len < hint)
441
33546
      len = hint;
442
443
    // If there is a one time allocation size hint, use it.
444
41465
    if (allocate_hint_ > len) {
445
16
      len = allocate_hint_;
446
16
      allocate_hint_ = 0;
447
    }
448
449
41465
    Buffer* next = new Buffer(env_, len);
450
451
41465
    if (w == nullptr) {
452
40811
      next->next_ = next;
453
40811
      write_head_ = next;
454
40811
      read_head_ = next;
455
    } else {
456
654
      next->next_ = w->next_;
457
654
      w->next_ = next;
458
    }
459
  }
460
60214
}
461
462
463
void NodeBIO::Reset() {
464
  if (read_head_ == nullptr)
465
    return;
466
467
  while (read_head_->read_pos_ != read_head_->write_pos_) {
468
    CHECK(read_head_->write_pos_ > read_head_->read_pos_);
469
470
    length_ -= read_head_->write_pos_ - read_head_->read_pos_;
471
    read_head_->write_pos_ = 0;
472
    read_head_->read_pos_ = 0;
473
474
    read_head_ = read_head_->next_;
475
  }
476
  write_head_ = read_head_;
477
  CHECK_EQ(length_, 0);
478
}
479
480
481
182619
NodeBIO::~NodeBIO() {
482
60873
  if (read_head_ == nullptr)
483
20088
    return;
484
485
40785
  Buffer* current = read_head_;
486
650
  do {
487
41435
    Buffer* next = current->next_;
488
41435
    delete current;
489
41435
    current = next;
490
41435
  } while (current != read_head_);
491
492
40785
  read_head_ = nullptr;
493
40785
  write_head_ = nullptr;
494
121746
}
495
496
497
1119333
NodeBIO* NodeBIO::FromBIO(BIO* bio) {
498
1119333
  CHECK_NOT_NULL(BIO_get_data(bio));
499
1119333
  return static_cast<NodeBIO*>(BIO_get_data(bio));
500
}
501
502
503
}  // namespace crypto
504

14970
}  // namespace node