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: 231 263 87.8 %
Date: 2019-02-13 22:28:58 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 "node_crypto_bio.h"
23
#include "openssl/bio.h"
24
#include "util-inl.h"
25
#include <limits.h>
26
#include <string.h>
27
28
namespace node {
29
namespace crypto {
30
31
#if OPENSSL_VERSION_NUMBER < 0x10100000L
32
#define BIO_set_data(bio, data) bio->ptr = data
33
#define BIO_get_data(bio) bio->ptr
34
#define BIO_set_shutdown(bio, shutdown_) bio->shutdown = shutdown_
35
#define BIO_get_shutdown(bio) bio->shutdown
36
#define BIO_set_init(bio, init_) bio->init = init_
37
#define BIO_get_init(bio) bio->init
38
#endif
39
40
41
54636
BIOPointer NodeBIO::New(Environment* env) {
42
  // The const_cast doesn't violate const correctness.  OpenSSL's usage of
43
  // BIO_METHOD is effectively const but BIO_new() takes a non-const argument.
44
54636
  BIOPointer bio(BIO_new(const_cast<BIO_METHOD*>(GetMethod())));
45

54636
  if (bio && env != nullptr)
46
23416
    NodeBIO::FromBIO(bio.get())->env_ = env;
47
54636
  return bio;
48
}
49
50
51
31220
BIOPointer NodeBIO::NewFixed(const char* data, size_t len, Environment* env) {
52
31220
  BIOPointer bio = New(env);
53
54

93660
  if (!bio ||
55
31220
      len > INT_MAX ||
56

93660
      BIO_write(bio.get(), data, len) != static_cast<int>(len) ||
57
31220
      BIO_set_mem_eof_return(bio.get(), 0) != 1) {
58
    return BIOPointer();
59
  }
60
61
31220
  return bio;
62
}
63
64
65
54636
int NodeBIO::New(BIO* bio) {
66
54636
  BIO_set_data(bio, new NodeBIO());
67
54636
  BIO_set_init(bio, 1);
68
69
54636
  return 1;
70
}
71
72
73
54608
int NodeBIO::Free(BIO* bio) {
74
54608
  if (bio == nullptr)
75
    return 0;
76
77
54608
  if (BIO_get_shutdown(bio)) {
78

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


715624
  if (i < size && i >= 0 && static_cast<size_t>(i) < nbio->Length())
162
715619
    i++;
163
164
  // Shift `i` a bit to nullptr-terminate string later
165
715624
  if (size == i)
166
    i--;
167
168
  // Flush read data
169
715624
  nbio->Read(out, i);
170
171
715624
  out[i] = 0;
172
173
715624
  return i;
174
}
175
176
177
52628
long NodeBIO::Ctrl(BIO* bio, int cmd, long num,  // NOLINT(runtime/int)
178
                   void* ptr) {
179
  NodeBIO* nbio;
180
  long ret;  // NOLINT(runtime/int)
181
182
52628
  nbio = FromBIO(bio);
183
52628
  ret = 1;
184
185



52628
  switch (cmd) {
186
    case BIO_CTRL_RESET:
187
      nbio->Reset();
188
      break;
189
    case BIO_CTRL_EOF:
190
      ret = nbio->Length() == 0;
191
      break;
192
    case BIO_C_SET_BUF_MEM_EOF_RETURN:
193
31220
      nbio->set_eof_return(num);
194
31220
      break;
195
    case BIO_CTRL_INFO:
196
      ret = nbio->Length();
197
      if (ptr != nullptr)
198
        *reinterpret_cast<void**>(ptr) = nullptr;
199
      break;
200
    case BIO_C_SET_BUF_MEM:
201
      CHECK(0 && "Can't use SET_BUF_MEM_PTR with NodeBIO");
202
      break;
203
    case BIO_C_GET_BUF_MEM_PTR:
204
      CHECK(0 && "Can't use GET_BUF_MEM_PTR with NodeBIO");
205
      ret = 0;
206
      break;
207
    case BIO_CTRL_GET_CLOSE:
208
      ret = BIO_get_shutdown(bio);
209
      break;
210
    case BIO_CTRL_SET_CLOSE:
211
      BIO_set_shutdown(bio, num);
212
      break;
213
    case BIO_CTRL_WPENDING:
214
      ret = 0;
215
      break;
216
    case BIO_CTRL_PENDING:
217
14003
      ret = nbio->Length();
218
14003
      break;
219
    case BIO_CTRL_DUP:
220
    case BIO_CTRL_FLUSH:
221
3682
      ret = 1;
222
3682
      break;
223
    case BIO_CTRL_PUSH:
224
    case BIO_CTRL_POP:
225
    default:
226
3723
      ret = 0;
227
3723
      break;
228
  }
229
52628
  return ret;
230
}
231
232
233
55187
const BIO_METHOD* NodeBIO::GetMethod() {
234
#if OPENSSL_VERSION_NUMBER < 0x10100000L
235
  static const BIO_METHOD method = {
236
    BIO_TYPE_MEM,
237
    "node.js SSL buffer",
238
    Write,
239
    Read,
240
    Puts,
241
    Gets,
242
    Ctrl,
243
    New,
244
    Free,
245
    nullptr
246
  };
247
248
  return &method;
249
#else
250
  // This is called from InitCryptoOnce() to avoid race conditions during
251
  // initialization.
252
  static BIO_METHOD* method = nullptr;
253
254
55187
  if (method == nullptr) {
255
552
    method = BIO_meth_new(BIO_TYPE_MEM, "node.js SSL buffer");
256
552
    BIO_meth_set_write(method, Write);
257
552
    BIO_meth_set_read(method, Read);
258
552
    BIO_meth_set_puts(method, Puts);
259
552
    BIO_meth_set_gets(method, Gets);
260
552
    BIO_meth_set_ctrl(method, Ctrl);
261
552
    BIO_meth_set_create(method, New);
262
552
    BIO_meth_set_destroy(method, Free);
263
  }
264
265
55187
  return method;
266
#endif
267
}
268
269
270
806243
void NodeBIO::TryMoveReadHead() {
271
  // `read_pos_` and `write_pos_` means the position of the reader and writer
272
  // inside the buffer, respectively. When they're equal - its safe to reset
273
  // them, because both reader and writer will continue doing their stuff
274
  // from new (zero) positions.
275

2454918
  while (read_head_->read_pos_ != 0 &&
276
793646
         read_head_->read_pos_ == read_head_->write_pos_) {
277
    // Reset positions
278
48786
    read_head_->read_pos_ = 0;
279
48786
    read_head_->write_pos_ = 0;
280
281
    // Move read_head_ forward, just in case if there're still some data to
282
    // read in the next buffer.
283
48786
    if (read_head_ != write_head_)
284
8999
      read_head_ = read_head_->next_;
285
  }
286
806243
}
287
288
289
796278
size_t NodeBIO::Read(char* out, size_t size) {
290
796278
  size_t bytes_read = 0;
291
796278
  size_t expected = Length() > size ? size : Length();
292
796278
  size_t offset = 0;
293
796278
  size_t left = size;
294
295
2386202
  while (bytes_read < expected) {
296
793646
    CHECK_LE(read_head_->read_pos_, read_head_->write_pos_);
297
793646
    size_t avail = read_head_->write_pos_ - read_head_->read_pos_;
298
793646
    if (avail > left)
299
744860
      avail = left;
300
301
    // Copy data
302
793646
    if (out != nullptr)
303
782460
      memcpy(out + offset, read_head_->data_ + read_head_->read_pos_, avail);
304
793646
    read_head_->read_pos_ += avail;
305
306
    // Move pointers
307
793646
    bytes_read += avail;
308
793646
    offset += avail;
309
793646
    left -= avail;
310
311
793646
    TryMoveReadHead();
312
  }
313
796278
  CHECK_EQ(expected, bytes_read);
314
796278
  length_ -= bytes_read;
315
316
  // Free all empty buffers, but write_head's child
317
796278
  FreeEmpty();
318
319
796278
  return bytes_read;
320
}
321
322
323
796278
void NodeBIO::FreeEmpty() {
324
796278
  if (write_head_ == nullptr)
325
796950
    return;
326
794907
  Buffer* child = write_head_->next_;
327

794907
  if (child == write_head_ || child == read_head_)
328
783850
    return;
329
11057
  Buffer* cur = child->next_;
330

11057
  if (cur == write_head_ || cur == read_head_)
331
10358
    return;
332
333
699
  Buffer* prev = child;
334
7517
  while (cur != read_head_) {
335
6119
    CHECK_NE(cur, write_head_);
336
6119
    CHECK_EQ(cur->write_pos_, cur->read_pos_);
337
338
6119
    Buffer* next = cur->next_;
339
6119
    delete cur;
340
6119
    cur = next;
341
  }
342
699
  prev->next_ = cur;
343
}
344
345
346
715624
size_t NodeBIO::IndexOf(char delim, size_t limit) {
347
715624
  size_t bytes_read = 0;
348
715624
  size_t max = Length() > limit ? limit : Length();
349
715624
  size_t left = limit;
350
715624
  Buffer* current = read_head_;
351
352
1431253
  while (bytes_read < max) {
353
715624
    CHECK_LE(current->read_pos_, current->write_pos_);
354
715624
    size_t avail = current->write_pos_ - current->read_pos_;
355
715624
    if (avail > left)
356
587611
      avail = left;
357
358
    // Walk through data
359
715624
    char* tmp = current->data_ + current->read_pos_;
360
715624
    size_t off = 0;
361

48887129
    while (off < avail && *tmp != delim) {
362
47455881
      off++;
363
47455881
      tmp++;
364
    }
365
366
    // Move pointers
367
715624
    bytes_read += off;
368
715624
    left -= off;
369
370
    // Found `delim`
371
715624
    if (off != avail) {
372
715619
      return bytes_read;
373
    }
374
375
    // Move to next buffer
376
5
    if (current->read_pos_ + avail == current->len_) {
377
2
      current = current->next_;
378
    }
379
  }
380
5
  CHECK_EQ(max, bytes_read);
381
382
5
  return max;
383
}
384
385
386
62920
void NodeBIO::Write(const char* data, size_t size) {
387
62920
  size_t offset = 0;
388
62920
  size_t left = size;
389
390
  // Allocate initial buffer if the ring is empty
391
62920
  TryAllocateForWrite(left);
392
393
198416
  while (left > 0) {
394
72576
    size_t to_write = left;
395
72576
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
396
72576
    size_t avail = write_head_->len_ - write_head_->write_pos_;
397
398
72576
    if (to_write > avail)
399
9656
      to_write = avail;
400
401
    // Copy data
402
72576
    memcpy(write_head_->data_ + write_head_->write_pos_,
403
72576
           data + offset,
404
145152
           to_write);
405
406
    // Move pointers
407
72576
    left -= to_write;
408
72576
    offset += to_write;
409
72576
    length_ += to_write;
410
72576
    write_head_->write_pos_ += to_write;
411
72576
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
412
413
    // Go to next buffer if there still are some bytes to write
414
72576
    if (left != 0) {
415
9656
      CHECK_EQ(write_head_->write_pos_, write_head_->len_);
416
9656
      TryAllocateForWrite(left);
417
9656
      write_head_ = write_head_->next_;
418
419
      // Additionally, since we're moved to the next buffer, read head
420
      // may be moved as well.
421
9656
      TryMoveReadHead();
422
    }
423
  }
424
62920
  CHECK_EQ(left, 0);
425
62920
}
426
427
428
7335
char* NodeBIO::PeekWritable(size_t* size) {
429
7335
  TryAllocateForWrite(*size);
430
431
7335
  size_t available = write_head_->len_ - write_head_->write_pos_;
432

7335
  if (*size != 0 && available > *size)
433
44
    available = *size;
434
  else
435
7291
    *size = available;
436
437
7335
  return write_head_->data_ + write_head_->write_pos_;
438
}
439
440
441
6957
void NodeBIO::Commit(size_t size) {
442
6957
  write_head_->write_pos_ += size;
443
6957
  length_ += size;
444
6957
  CHECK_LE(write_head_->write_pos_, write_head_->len_);
445
446
  // Allocate new buffer if write head is full,
447
  // and there're no other place to go
448
6957
  TryAllocateForWrite(0);
449
6957
  if (write_head_->write_pos_ == write_head_->len_) {
450
2941
    write_head_ = write_head_->next_;
451
452
    // Additionally, since we're moved to the next buffer, read head
453
    // may be moved as well.
454
2941
    TryMoveReadHead();
455
  }
456
6957
}
457
458
459
86868
void NodeBIO::TryAllocateForWrite(size_t hint) {
460
86868
  Buffer* w = write_head_;
461
86868
  Buffer* r = read_head_;
462
  // If write head is full, next buffer is either read head or not empty.
463

139169
  if (w == nullptr ||
464
70685
      (w->write_pos_ == w->len_ &&
465
26978
       (w->next_ == r || w->next_->write_pos_ != 0))) {
466
    size_t len = w == nullptr ? initial_ :
467
44357
                             kThroughputBufferLength;
468
44357
    if (len < hint)
469
34321
      len = hint;
470
44357
    Buffer* next = new Buffer(env_, len);
471
472
44357
    if (w == nullptr) {
473
34567
      next->next_ = next;
474
34567
      write_head_ = next;
475
34567
      read_head_ = next;
476
    } else {
477
9790
      next->next_ = w->next_;
478
9790
      w->next_ = next;
479
    }
480
  }
481
86868
}
482
483
484
void NodeBIO::Reset() {
485
  if (read_head_ == nullptr)
486
    return;
487
488
  while (read_head_->read_pos_ != read_head_->write_pos_) {
489
    CHECK(read_head_->write_pos_ > read_head_->read_pos_);
490
491
    length_ -= read_head_->write_pos_ - read_head_->read_pos_;
492
    read_head_->write_pos_ = 0;
493
    read_head_->read_pos_ = 0;
494
495
    read_head_ = read_head_->next_;
496
  }
497
  write_head_ = read_head_;
498
  CHECK_EQ(length_, 0);
499
}
500
501
502
163824
NodeBIO::~NodeBIO() {
503
54608
  if (read_head_ == nullptr)
504
20069
    return;
505
506
34539
  Buffer* current = read_head_;
507
35171
  do {
508
35171
    Buffer* next = current->next_;
509
35171
    delete current;
510
35171
    current = next;
511
35171
  } while (current != read_head_);
512
513
34539
  read_head_ = nullptr;
514
34539
  write_head_ = nullptr;
515
89147
}
516
517
518
1021363
NodeBIO* NodeBIO::FromBIO(BIO* bio) {
519
1021363
  CHECK_NOT_NULL(BIO_get_data(bio));
520
1021363
  return static_cast<NodeBIO*>(BIO_get_data(bio));
521
}
522
523
524
}  // namespace crypto
525
}  // namespace node