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-03-02 22:23:06 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 <climits>
26
#include <cstring>
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
54634
BIOPointer NodeBIO::New(Environment* env) {
42
54634
  BIOPointer bio(BIO_new(GetMethod()));
43

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

93660
  if (!bio ||
53
31220
      len > INT_MAX ||
54

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

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


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



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

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

794941
  if (child == write_head_ || child == read_head_)
326
783878
    return;
327
11063
  Buffer* cur = child->next_;
328

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

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

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

139225
  if (w == nullptr ||
462
70721
      (w->write_pos_ == w->len_ &&
463
26987
       (w->next_ == r || w->next_->write_pos_ != 0))) {
464
    size_t len = w == nullptr ? initial_ :
465
44356
                             kThroughputBufferLength;
466
44356
    if (len < hint)
467
34319
      len = hint;
468
44356
    Buffer* next = new Buffer(env_, len);
469
470
44356
    if (w == nullptr) {
471
34563
      next->next_ = next;
472
34563
      write_head_ = next;
473
34563
      read_head_ = next;
474
    } else {
475
9793
      next->next_ = w->next_;
476
9793
      w->next_ = next;
477
    }
478
  }
479
86894
}
480
481
482
void NodeBIO::Reset() {
483
  if (read_head_ == nullptr)
484
    return;
485
486
  while (read_head_->read_pos_ != read_head_->write_pos_) {
487
    CHECK(read_head_->write_pos_ > read_head_->read_pos_);
488
489
    length_ -= read_head_->write_pos_ - read_head_->read_pos_;
490
    read_head_->write_pos_ = 0;
491
    read_head_->read_pos_ = 0;
492
493
    read_head_ = read_head_->next_;
494
  }
495
  write_head_ = read_head_;
496
  CHECK_EQ(length_, 0);
497
}
498
499
500
163812
NodeBIO::~NodeBIO() {
501
54604
  if (read_head_ == nullptr)
502
20069
    return;
503
504
34535
  Buffer* current = read_head_;
505
35170
  do {
506
35170
    Buffer* next = current->next_;
507
35170
    delete current;
508
35170
    current = next;
509
35170
  } while (current != read_head_);
510
511
34535
  read_head_ = nullptr;
512
34535
  write_head_ = nullptr;
513
89139
}
514
515
516
1021444
NodeBIO* NodeBIO::FromBIO(BIO* bio) {
517
1021444
  CHECK_NOT_NULL(BIO_get_data(bio));
518
1021444
  return static_cast<NodeBIO*>(BIO_get_data(bio));
519
}
520
521
522
}  // namespace crypto
523
}  // namespace node