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: 230 262 87.8 %
Date: 2019-10-06 22:30:00 Branches: 125 176 71.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 "node_crypto_bio.h"
25
#include "openssl/bio.h"
26
#include "util-inl.h"
27
#include <climits>
28
#include <cstring>
29
30
namespace node {
31
namespace crypto {
32
33
68292
BIOPointer NodeBIO::New(Environment* env) {
34
68292
  BIOPointer bio(BIO_new(GetMethod()));
35

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

131412
  if (!bio ||
45
43804
      len > INT_MAX ||
46

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

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


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



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

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

1057469
  if (child == write_head_ || child == read_head_)
301
1049797
    return;
302
7672
  Buffer* cur = child->next_;
303

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

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

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

124789
  if (w == nullptr ||
435
59542
      (w->write_pos_ == w->len_ &&
436
32663
       (w->next_ == r || w->next_->write_pos_ != 0))) {
437
    size_t len = w == nullptr ? initial_ :
438
58042
                             kThroughputBufferLength;
439
58042
    if (len < hint)
440
48541
      len = hint;
441
58042
    Buffer* next = new Buffer(env_, len);
442
443
58042
    if (w == nullptr) {
444
48205
      next->next_ = next;
445
48205
      write_head_ = next;
446
48205
      read_head_ = next;
447
    } else {
448
9837
      next->next_ = w->next_;
449
9837
      w->next_ = next;
450
    }
451
  }
452
86497
}
453
454
455
void NodeBIO::Reset() {
456
  if (read_head_ == nullptr)
457
    return;
458
459
  while (read_head_->read_pos_ != read_head_->write_pos_) {
460
    CHECK(read_head_->write_pos_ > read_head_->read_pos_);
461
462
    length_ -= read_head_->write_pos_ - read_head_->read_pos_;
463
    read_head_->write_pos_ = 0;
464
    read_head_->read_pos_ = 0;
465
466
    read_head_ = read_head_->next_;
467
  }
468
  write_head_ = read_head_;
469
  CHECK_EQ(length_, 0);
470
}
471
472
473
204762
NodeBIO::~NodeBIO() {
474
68254
  if (read_head_ == nullptr)
475
20087
    return;
476
477
48167
  Buffer* current = read_head_;
478
48867
  do {
479
48867
    Buffer* next = current->next_;
480
48867
    delete current;
481
48867
    current = next;
482
48867
  } while (current != read_head_);
483
484
48167
  read_head_ = nullptr;
485
48167
  write_head_ = nullptr;
486
116421
}
487
488
489
1319987
NodeBIO* NodeBIO::FromBIO(BIO* bio) {
490
1319987
  CHECK_NOT_NULL(BIO_get_data(bio));
491
1319987
  return static_cast<NodeBIO*>(BIO_get_data(bio));
492
}
493
494
495
}  // namespace crypto
496
}  // namespace node