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: 231 263 87.8 %
Date: 2019-01-07 12:15:22 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
52496
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
52496
  BIOPointer bio(BIO_new(const_cast<BIO_METHOD*>(GetMethod())));
45

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

88218
  if (!bio ||
55
29406
      len > INT_MAX ||
56

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

52476
    if (BIO_get_init(bio) && BIO_get_data(bio) != nullptr) {
79
52476
      delete FromBIO(bio);
80
52476
      BIO_set_data(bio, nullptr);
81
    }
82
  }
83
84
52476
  return 1;
85
}
86
87
88
27567
int NodeBIO::Read(BIO* bio, char* out, int len) {
89
27567
  BIO_clear_retry_flags(bio);
90
91
27567
  NodeBIO* nbio = FromBIO(bio);
92
27567
  int bytes = nbio->Read(out, len);
93
94
27567
  if (bytes == 0) {
95
5844
    bytes = nbio->eof_return();
96
5844
    if (bytes != 0) {
97
5842
      BIO_set_retry_read(bio);
98
    }
99
  }
100
101
27567
  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
4123
size_t NodeBIO::PeekMultiple(char** out, size_t* size, size_t* count) {
112
4123
  Buffer* pos = read_head_;
113
4123
  size_t max = *count;
114
4123
  size_t total = 0;
115
116
  size_t i;
117
6841
  for (i = 0; i < max; i++) {
118
6625
    size[i] = pos->write_pos_ - pos->read_pos_;
119
6625
    total += size[i];
120
6625
    out[i] = pos->data_ + pos->read_pos_;
121
122
    /* Don't get past write head */
123
6625
    if (pos == write_head_)
124
3907
      break;
125
    else
126
2718
      pos = pos->next_;
127
  }
128
129
4123
  if (i == max)
130
216
    *count = i;
131
  else
132
3907
    *count = i + 1;
133
134
4123
  return total;
135
}
136
137
138
36950
int NodeBIO::Write(BIO* bio, const char* data, int len) {
139
36950
  BIO_clear_retry_flags(bio);
140
141
36950
  FromBIO(bio)->Write(data, len);
142
143
36950
  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
674803
int NodeBIO::Gets(BIO* bio, char* out, int size) {
153
674803
  NodeBIO* nbio = FromBIO(bio);
154
155
674803
  if (nbio->Length() == 0)
156
808
    return 0;
157
158
673995
  int i = nbio->IndexOf('\n', size);
159
160
  // Include '\n', if it's there.  If not, don't read off the end.
161


673995
  if (i < size && i >= 0 && static_cast<size_t>(i) < nbio->Length())
162
673990
    i++;
163
164
  // Shift `i` a bit to nullptr-terminate string later
165
673995
  if (size == i)
166
    i--;
167
168
  // Flush read data
169
673995
  nbio->Read(out, i);
170
171
673995
  out[i] = 0;
172
173
673995
  return i;
174
}
175
176
177
45053
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
45053
  nbio = FromBIO(bio);
183
45053
  ret = 1;
184
185



45053
  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
29406
      nbio->set_eof_return(num);
194
29406
      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
10159
      ret = nbio->Length();
218
10159
      break;
219
    case BIO_CTRL_DUP:
220
    case BIO_CTRL_FLUSH:
221
2439
      ret = 1;
222
2439
      break;
223
    case BIO_CTRL_PUSH:
224
    case BIO_CTRL_POP:
225
    default:
226
3049
      ret = 0;
227
3049
      break;
228
  }
229
45053
  return ret;
230
}
231
232
233
52986
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
52986
  if (method == nullptr) {
255
491
    method = BIO_meth_new(BIO_TYPE_MEM, "node.js SSL buffer");
256
491
    BIO_meth_set_write(method, Write);
257
491
    BIO_meth_set_read(method, Read);
258
491
    BIO_meth_set_puts(method, Puts);
259
491
    BIO_meth_set_gets(method, Gets);
260
491
    BIO_meth_set_ctrl(method, Ctrl);
261
491
    BIO_meth_set_create(method, New);
262
491
    BIO_meth_set_destroy(method, Free);
263
  }
264
265
52986
  return method;
266
#endif
267
}
268
269
270
706503
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

2155292
  while (read_head_->read_pos_ != 0 &&
276
702297
         read_head_->read_pos_ == read_head_->write_pos_) {
277
    // Reset positions
278
39989
    read_head_->read_pos_ = 0;
279
39989
    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
39989
    if (read_head_ != write_head_)
284
3646
      read_head_ = read_head_->next_;
285
  }
286
706503
}
287
288
289
705648
size_t NodeBIO::Read(char* out, size_t size) {
290
705648
  size_t bytes_read = 0;
291
705648
  size_t expected = Length() > size ? size : Length();
292
705648
  size_t offset = 0;
293
705648
  size_t left = size;
294
295
2113593
  while (bytes_read < expected) {
296
702297
    CHECK_LE(read_head_->read_pos_, read_head_->write_pos_);
297
702297
    size_t avail = read_head_->write_pos_ - read_head_->read_pos_;
298
702297
    if (avail > left)
299
662308
      avail = left;
300
301
    // Copy data
302
702297
    if (out != nullptr)
303
695718
      memcpy(out + offset, read_head_->data_ + read_head_->read_pos_, avail);
304
702297
    read_head_->read_pos_ += avail;
305
306
    // Move pointers
307
702297
    bytes_read += avail;
308
702297
    offset += avail;
309
702297
    left -= avail;
310
311
702297
    TryMoveReadHead();
312
  }
313
705648
  CHECK_EQ(expected, bytes_read);
314
705648
  length_ -= bytes_read;
315
316
  // Free all empty buffers, but write_head's child
317
705648
  FreeEmpty();
318
319
705648
  return bytes_read;
320
}
321
322
323
705648
void NodeBIO::FreeEmpty() {
324
705648
  if (write_head_ == nullptr)
325
706604
    return;
326
704380
  Buffer* child = write_head_->next_;
327

704380
  if (child == write_head_ || child == read_head_)
328
700639
    return;
329
3741
  Buffer* cur = child->next_;
330

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

46037155
    while (off < avail && *tmp != delim) {
362
44689165
      off++;
363
44689165
      tmp++;
364
    }
365
366
    // Move pointers
367
673995
    bytes_read += off;
368
673995
    left -= off;
369
370
    // Found `delim`
371
673995
    if (off != avail) {
372
673990
      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
36950
void NodeBIO::Write(const char* data, size_t size) {
387
36950
  size_t offset = 0;
388
36950
  size_t left = size;
389
390
  // Allocate initial buffer if the ring is empty
391
36950
  TryAllocateForWrite(left);
392
393
113604
  while (left > 0) {
394
39704
    size_t to_write = left;
395
39704
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
396
39704
    size_t avail = write_head_->len_ - write_head_->write_pos_;
397
398
39704
    if (to_write > avail)
399
2754
      to_write = avail;
400
401
    // Copy data
402
39704
    memcpy(write_head_->data_ + write_head_->write_pos_,
403
39704
           data + offset,
404
79408
           to_write);
405
406
    // Move pointers
407
39704
    left -= to_write;
408
39704
    offset += to_write;
409
39704
    length_ += to_write;
410
39704
    write_head_->write_pos_ += to_write;
411
39704
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
412
413
    // Go to next buffer if there still are some bytes to write
414
39704
    if (left != 0) {
415
2754
      CHECK_EQ(write_head_->write_pos_, write_head_->len_);
416
2754
      TryAllocateForWrite(left);
417
2754
      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
2754
      TryMoveReadHead();
422
    }
423
  }
424
36950
  CHECK_EQ(left, 0);
425
36950
}
426
427
428
4883
char* NodeBIO::PeekWritable(size_t* size) {
429
4883
  TryAllocateForWrite(*size);
430
431
4883
  size_t available = write_head_->len_ - write_head_->write_pos_;
432

4883
  if (*size != 0 && available > *size)
433
33
    available = *size;
434
  else
435
4850
    *size = available;
436
437
4883
  return write_head_->data_ + write_head_->write_pos_;
438
}
439
440
441
4576
void NodeBIO::Commit(size_t size) {
442
4576
  write_head_->write_pos_ += size;
443
4576
  length_ += size;
444
4576
  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
4576
  TryAllocateForWrite(0);
449
4576
  if (write_head_->write_pos_ == write_head_->len_) {
450
1452
    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
1452
    TryMoveReadHead();
455
  }
456
4576
}
457
458
459
49163
void NodeBIO::TryAllocateForWrite(size_t hint) {
460
49163
  Buffer* w = write_head_;
461
49163
  Buffer* r = read_head_;
462
  // If write head is full, next buffer is either read head or not empty.
463

65897
  if (w == nullptr ||
464
22961
      (w->write_pos_ == w->len_ &&
465
9572
       (w->next_ == r || w->next_->write_pos_ != 0))) {
466
    size_t len = w == nullptr ? initial_ :
467
35311
                             kThroughputBufferLength;
468
35311
    if (len < hint)
469
28808
      len = hint;
470
35311
    Buffer* next = new Buffer(env_, len);
471
472
35311
    if (w == nullptr) {
473
32429
      next->next_ = next;
474
32429
      write_head_ = next;
475
32429
      read_head_ = next;
476
    } else {
477
2882
      next->next_ = w->next_;
478
2882
      w->next_ = next;
479
    }
480
  }
481
49163
}
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
157428
NodeBIO::~NodeBIO() {
503
52476
  if (read_head_ == nullptr)
504
20067
    return;
505
506
32409
  Buffer* current = read_head_;
507
33015
  do {
508
33015
    Buffer* next = current->next_;
509
33015
    delete current;
510
33015
    current = next;
511
33015
  } while (current != read_head_);
512
513
32409
  read_head_ = nullptr;
514
32409
  write_head_ = nullptr;
515
84885
}
516
517
518
888647
NodeBIO* NodeBIO::FromBIO(BIO* bio) {
519
888647
  CHECK_NOT_NULL(BIO_get_data(bio));
520
888647
  return static_cast<NodeBIO*>(BIO_get_data(bio));
521
}
522
523
524
}  // namespace crypto
525
}  // namespace node