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: 2020-11-20 19:51:53 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
61726
BIOPointer NodeBIO::New(Environment* env) {
37
61726
  BIOPointer bio(BIO_new(GetMethod()));
38

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

111960
  if (!bio ||
48
37320
      len > INT_MAX ||
49

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

61690
    if (BIO_get_init(bio) && BIO_get_data(bio) != nullptr) {
72
61690
      delete FromBIO(bio);
73
61690
      BIO_set_data(bio, nullptr);
74
    }
75
  }
76
77
61690
  return 1;
78
}
79
80
81
34465
int NodeBIO::Read(BIO* bio, char* out, int len) {
82
34465
  BIO_clear_retry_flags(bio);
83
84
34465
  NodeBIO* nbio = FromBIO(bio);
85
34465
  int bytes = nbio->Read(out, len);
86
87
34465
  if (bytes == 0) {
88
6985
    bytes = nbio->eof_return();
89
6985
    if (bytes != 0) {
90
6983
      BIO_set_retry_read(bio);
91
    }
92
  }
93
94
34465
  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
7284
size_t NodeBIO::PeekMultiple(char** out, size_t* size, size_t* count) {
105
7284
  Buffer* pos = read_head_;
106
7284
  size_t max = *count;
107
7284
  size_t total = 0;
108
109
  size_t i;
110
7407
  for (i = 0; i < max; i++) {
111
7407
    size[i] = pos->write_pos_ - pos->read_pos_;
112
7407
    total += size[i];
113
7407
    out[i] = pos->data_ + pos->read_pos_;
114
115
    /* Don't get past write head */
116
7407
    if (pos == write_head_)
117
7284
      break;
118
    else
119
123
      pos = pos->next_;
120
  }
121
122
7284
  if (i == max)
123
    *count = i;
124
  else
125
7284
    *count = i + 1;
126
127
7284
  return total;
128
}
129
130
131
47887
int NodeBIO::Write(BIO* bio, const char* data, int len) {
132
47887
  BIO_clear_retry_flags(bio);
133
134
47887
  FromBIO(bio)->Write(data, len);
135
136
47887
  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
853726
int NodeBIO::Gets(BIO* bio, char* out, int size) {
146
853726
  NodeBIO* nbio = FromBIO(bio);
147
148
853726
  if (nbio->Length() == 0)
149
1406
    return 0;
150
151
852320
  int i = nbio->IndexOf('\n', size);
152
153
  // Include '\n', if it's there.  If not, don't read off the end.
154


852320
  if (i < size && i >= 0 && static_cast<size_t>(i) < nbio->Length())
155
817263
    i++;
156
157
  // Shift `i` a bit to nullptr-terminate string later
158
852320
  if (size == i)
159
    i--;
160
161
  // Flush read data
162
852320
  nbio->Read(out, i);
163
164
852320
  out[i] = 0;
165
166
852320
  return i;
167
}
168
169
170
65418
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
65418
  nbio = FromBIO(bio);
176
65418
  ret = 1;
177
178



65418
  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
37320
      nbio->set_eof_return(num);
187
37320
      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
16495
      ret = nbio->Length();
211
16495
      break;
212
    case BIO_CTRL_DUP:
213
    case BIO_CTRL_FLUSH:
214
5649
      ret = 1;
215
5649
      break;
216
    case BIO_CTRL_PUSH:
217
    case BIO_CTRL_POP:
218
    default:
219
5954
      ret = 0;
220
5954
      break;
221
  }
222
65418
  return ret;
223
}
224
225
226
62363
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
62363
  if (method == nullptr) {
232
638
    method = BIO_meth_new(BIO_TYPE_MEM, "node.js SSL buffer");
233
638
    BIO_meth_set_write(method, Write);
234
638
    BIO_meth_set_read(method, Read);
235
638
    BIO_meth_set_puts(method, Puts);
236
638
    BIO_meth_set_gets(method, Gets);
237
638
    BIO_meth_set_ctrl(method, Ctrl);
238
638
    BIO_meth_set_create(method, New);
239
638
    BIO_meth_set_destroy(method, Free);
240
  }
241
242
62363
  return method;
243
}
244
245
246
937975
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

1875089
  while (read_head_->read_pos_ != 0 &&
252
887115
         read_head_->read_pos_ == read_head_->write_pos_) {
253
    // Reset positions
254
49999
    read_head_->read_pos_ = 0;
255
49999
    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
49999
    if (read_head_ != write_head_)
260
336
      read_head_ = read_head_->next_;
261
  }
262
887976
}
263
264
265
893985
size_t NodeBIO::Read(char* out, size_t size) {
266
893985
  size_t bytes_read = 0;
267
893985
  size_t expected = Length() > size ? size : Length();
268
893985
  size_t offset = 0;
269
893985
  size_t left = size;
270
271
2668215
  while (bytes_read < expected) {
272
887115
    CHECK_LE(read_head_->read_pos_, read_head_->write_pos_);
273
887115
    size_t avail = read_head_->write_pos_ - read_head_->read_pos_;
274
887115
    if (avail > left)
275
837116
      avail = left;
276
277
    // Copy data
278
887115
    if (out != nullptr)
279
879800
      memcpy(out + offset, read_head_->data_ + read_head_->read_pos_, avail);
280
887115
    read_head_->read_pos_ += avail;
281
282
    // Move pointers
283
887115
    bytes_read += avail;
284
887115
    offset += avail;
285
887115
    left -= avail;
286
287
887115
    TryMoveReadHead();
288
  }
289
893985
  CHECK_EQ(expected, bytes_read);
290
893985
  length_ -= bytes_read;
291
292
  // Free all empty buffers, but write_head's child
293
893985
  FreeEmpty();
294
295
893985
  return bytes_read;
296
}
297
298
299
893985
void NodeBIO::FreeEmpty() {
300
893985
  if (write_head_ == nullptr)
301
895636
    return;
302
892333
  Buffer* child = write_head_->next_;
303

892333
  if (child == write_head_ || child == read_head_)
304
886894
    return;
305
5439
  Buffer* cur = child->next_;
306

5439
  if (cur == write_head_ || cur == read_head_)
307
5438
    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
852320
size_t NodeBIO::IndexOf(char delim, size_t limit) {
323
852320
  size_t bytes_read = 0;
324
852320
  size_t max = Length() > limit ? limit : Length();
325
852320
  size_t left = limit;
326
852320
  Buffer* current = read_head_;
327
328
922434
  while (bytes_read < max) {
329
852320
    CHECK_LE(current->read_pos_, current->write_pos_);
330
852320
    size_t avail = current->write_pos_ - current->read_pos_;
331
852320
    if (avail > left)
332
698655
      avail = left;
333
334
    // Walk through data
335
852320
    char* tmp = current->data_ + current->read_pos_;
336
852320
    size_t off = 0;
337

113472286
    while (off < avail && *tmp != delim) {
338
56309983
      off++;
339
56309983
      tmp++;
340
    }
341
342
    // Move pointers
343
852320
    bytes_read += off;
344
852320
    left -= off;
345
346
    // Found `delim`
347
852320
    if (off != avail) {
348
817263
      return bytes_read;
349
    }
350
351
    // Move to next buffer
352
35057
    if (current->read_pos_ + avail == current->len_) {
353
29720
      current = current->next_;
354
    }
355
  }
356
35057
  CHECK_EQ(max, bytes_read);
357
358
35057
  return max;
359
}
360
361
362
47887
void NodeBIO::Write(const char* data, size_t size) {
363
47887
  size_t offset = 0;
364
47887
  size_t left = size;
365
366
  // Allocate initial buffer if the ring is empty
367
47887
  TryAllocateForWrite(left);
368
369
143911
  while (left > 0) {
370
48012
    size_t to_write = left;
371
48012
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
372
48012
    size_t avail = write_head_->len_ - write_head_->write_pos_;
373
374
48012
    if (to_write > avail)
375
125
      to_write = avail;
376
377
    // Copy data
378
48012
    memcpy(write_head_->data_ + write_head_->write_pos_,
379
48012
           data + offset,
380
48012
           to_write);
381
382
    // Move pointers
383
48012
    left -= to_write;
384
48012
    offset += to_write;
385
48012
    length_ += to_write;
386
48012
    write_head_->write_pos_ += to_write;
387
48012
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
388
389
    // Go to next buffer if there still are some bytes to write
390
48012
    if (left != 0) {
391
125
      CHECK_EQ(write_head_->write_pos_, write_head_->len_);
392
125
      TryAllocateForWrite(left);
393
125
      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
125
      TryMoveReadHead();
398
    }
399
  }
400
47887
  CHECK_EQ(left, 0);
401
47887
}
402
403
404
6143
char* NodeBIO::PeekWritable(size_t* size) {
405
6143
  TryAllocateForWrite(*size);
406
407
6143
  size_t available = write_head_->len_ - write_head_->write_pos_;
408

6143
  if (*size == 0 || available <= *size)
409
6100
    *size = available;
410
411
6143
  return write_head_->data_ + write_head_->write_pos_;
412
}
413
414
415
6016
void NodeBIO::Commit(size_t size) {
416
6016
  write_head_->write_pos_ += size;
417
6016
  length_ += size;
418
6016
  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
6016
  TryAllocateForWrite(0);
423
6016
  if (write_head_->write_pos_ == write_head_->len_) {
424
736
    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
736
    TryMoveReadHead();
429
  }
430
6016
}
431
432
433
60171
void NodeBIO::TryAllocateForWrite(size_t hint) {
434
60171
  Buffer* w = write_head_;
435
60171
  Buffer* r = read_head_;
436
  // If write head is full, next buffer is either read head or not empty.
437

78704
  if (w == nullptr ||
438
19396
      (w->write_pos_ == w->len_ &&
439
1071
       (w->next_ == r || w->next_->write_pos_ != 0))) {
440
42293
    size_t len = w == nullptr ? initial_ :
441
42293
                             kThroughputBufferLength;
442
42293
    if (len < hint)
443
33739
      len = hint;
444
445
    // If there is a one time allocation size hint, use it.
446
42293
    if (allocate_hint_ > len) {
447
19
      len = allocate_hint_;
448
19
      allocate_hint_ = 0;
449
    }
450
451
42293
    Buffer* next = new Buffer(env_, len);
452
453
42293
    if (w == nullptr) {
454
41638
      next->next_ = next;
455
41638
      write_head_ = next;
456
41638
      read_head_ = next;
457
    } else {
458
655
      next->next_ = w->next_;
459
655
      w->next_ = next;
460
    }
461
  }
462
60171
}
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
185070
NodeBIO::~NodeBIO() {
484
61690
  if (read_head_ == nullptr)
485
20088
    return;
486
487
41602
  Buffer* current = read_head_;
488
651
  do {
489
42253
    Buffer* next = current->next_;
490
42253
    delete current;
491
42253
    current = next;
492
42253
  } while (current != read_head_);
493
494
41602
  read_head_ = nullptr;
495
41602
  write_head_ = nullptr;
496
123380
}
497
498
499
1128559
NodeBIO* NodeBIO::FromBIO(BIO* bio) {
500
1128559
  CHECK_NOT_NULL(BIO_get_data(bio));
501
1128559
  return static_cast<NodeBIO*>(BIO_get_data(bio));
502
}
503
504
505
}  // namespace crypto
506

14034
}  // namespace node