GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/crypto/crypto_bio.cc Lines: 234 267 87.6 %
Date: 2021-06-01 04:11:54 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 "crypto/crypto_bio.h"
23
#include "base_object-inl.h"
24
#include "memory_tracker-inl.h"
25
#include "allocated_buffer-inl.h"
26
#include "util-inl.h"
27
28
#include <openssl/bio.h>
29
30
#include <climits>
31
#include <cstring>
32
33
namespace node {
34
namespace crypto {
35
36
63381
BIOPointer NodeBIO::New(Environment* env) {
37
63381
  BIOPointer bio(BIO_new(GetMethod()));
38

63381
  if (bio && env != nullptr)
39
24494
    NodeBIO::FromBIO(bio.get())->env_ = env;
40
63381
  return bio;
41
}
42
43
44
38887
BIOPointer NodeBIO::NewFixed(const char* data, size_t len, Environment* env) {
45
77774
  BIOPointer bio = New(env);
46
47

116661
  if (!bio ||
48
38887
      len > INT_MAX ||
49

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

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


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



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

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

928561
  if (child == write_head_ || child == read_head_)
304
923047
    return;
305
5514
  Buffer* cur = child->next_;
306

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

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

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

80838
  if (w == nullptr ||
438
19660
      (w->write_pos_ == w->len_ &&
439
1079
       (w->next_ == r || w->next_->write_pos_ != 0))) {
440
43923
    size_t len = w == nullptr ? initial_ :
441
43923
                             kThroughputBufferLength;
442
43923
    if (len < hint)
443
35087
      len = hint;
444
445
    // If there is a one time allocation size hint, use it.
446
43923
    if (allocate_hint_ > len) {
447
20
      len = allocate_hint_;
448
20
      allocate_hint_ = 0;
449
    }
450
451
43923
    Buffer* next = new Buffer(env_, len);
452
453
43923
    if (w == nullptr) {
454
43260
      next->next_ = next;
455
43260
      write_head_ = next;
456
43260
      read_head_ = next;
457
    } else {
458
663
      next->next_ = w->next_;
459
663
      w->next_ = next;
460
    }
461
  }
462
62049
}
463
464
465
void NodeBIO::Reset() {
466
  if (read_head_ == nullptr)
467
    return;
468
469
  while (read_head_->read_pos_ != read_head_->write_pos_) {
470
    CHECK(read_head_->write_pos_ > read_head_->read_pos_);
471
472
    length_ -= read_head_->write_pos_ - read_head_->read_pos_;
473
    read_head_->write_pos_ = 0;
474
    read_head_->read_pos_ = 0;
475
476
    read_head_ = read_head_->next_;
477
  }
478
  write_head_ = read_head_;
479
  CHECK_EQ(length_, 0);
480
}
481
482
483
190023
NodeBIO::~NodeBIO() {
484
63341
  if (read_head_ == nullptr)
485
20121
    return;
486
487
43220
  Buffer* current = read_head_;
488
659
  do {
489
43879
    Buffer* next = current->next_;
490
43879
    delete current;
491
43879
    current = next;
492
43879
  } while (current != read_head_);
493
494
43220
  read_head_ = nullptr;
495
43220
  write_head_ = nullptr;
496
126682
}
497
498
499
1170729
NodeBIO* NodeBIO::FromBIO(BIO* bio) {
500
1170729
  CHECK_NOT_NULL(BIO_get_data(bio));
501
1170729
  return static_cast<NodeBIO*>(BIO_get_data(bio));
502
}
503
504
505
}  // namespace crypto
506

14520
}  // namespace node