GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/node_crypto_bio.cc Lines: 232 255 91.0 %
Date: 2017-12-18 Branches: 118 162 72.8 %

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
51461
BIO* NodeBIO::New() {
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
51461
  return BIO_new(const_cast<BIO_METHOD*>(GetMethod()));
45
}
46
47
48
29835
BIO* NodeBIO::NewFixed(const char* data, size_t len) {
49
29835
  BIO* bio = New();
50
51

59670
  if (bio == nullptr ||
52
29835
      len > INT_MAX ||
53

89505
      BIO_write(bio, data, len) != static_cast<int>(len) ||
54
29835
      BIO_set_mem_eof_return(bio, 0) != 1) {
55
    BIO_free(bio);
56
    return nullptr;
57
  }
58
59
29835
  return bio;
60
}
61
62
63
2391
void NodeBIO::AssignEnvironment(Environment* env) {
64
2391
  env_ = env;
65
2391
}
66
67
68
51461
int NodeBIO::New(BIO* bio) {
69
51461
  BIO_set_data(bio, new NodeBIO());
70
71
51461
  BIO_set_init(bio, 1);
72
73
51461
  return 1;
74
}
75
76
77
51375
int NodeBIO::Free(BIO* bio) {
78
51375
  if (bio == nullptr)
79
    return 0;
80
81
51375
  if (BIO_get_shutdown(bio)) {
82

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


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



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

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

724554
  if (child == write_head_ || child == read_head_)
330
714677
    return;
331
9877
  Buffer* cur = child->next_;
332

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

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

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

76572
  if (w == nullptr ||
466
31331
      (w->write_pos_ == w->len_ &&
467
15125
       (w->next_ == r || w->next_->write_pos_ != 0))) {
468
    size_t len = w == nullptr ? initial_ :
469
33973
                             kThroughputBufferLength;
470
33973
    if (len < hint)
471
28654
      len = hint;
472
33973
    Buffer* next = new Buffer(env_, len);
473
474
33973
    if (w == nullptr) {
475
31504
      next->next_ = next;
476
31504
      write_head_ = next;
477
31504
      read_head_ = next;
478
    } else {
479
2469
      next->next_ = w->next_;
480
2469
      w->next_ = next;
481
    }
482
  }
483
54038
}
484
485
486
3
void NodeBIO::Reset() {
487
3
  if (read_head_ == nullptr)
488
3
    return;
489
490
9
  while (read_head_->read_pos_ != read_head_->write_pos_) {
491
3
    CHECK(read_head_->write_pos_ > read_head_->read_pos_);
492
493
3
    length_ -= read_head_->write_pos_ - read_head_->read_pos_;
494
3
    read_head_->write_pos_ = 0;
495
3
    read_head_->read_pos_ = 0;
496
497
3
    read_head_ = read_head_->next_;
498
  }
499
3
  write_head_ = read_head_;
500
3
  CHECK_EQ(length_, 0);
501
}
502
503
504
52135
NodeBIO::~NodeBIO() {
505
52135
  if (read_head_ == nullptr)
506
20706
    return;
507
508
31429
  Buffer* current = read_head_;
509
31468
  do {
510
31468
    Buffer* next = current->next_;
511
31468
    delete current;
512
31468
    current = next;
513
31468
  } while (current != read_head_);
514
515
31429
  read_head_ = nullptr;
516
31429
  write_head_ = nullptr;
517
52135
}
518
519
520
875886
NodeBIO* NodeBIO::FromBIO(BIO* bio) {
521
875886
  CHECK_NE(BIO_get_data(bio), nullptr);
522
875886
  return static_cast<NodeBIO*>(BIO_get_data(bio));
523
}
524
525
526
}  // namespace crypto
527
}  // namespace node