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-07-28 22:34:34 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
67582
BIOPointer NodeBIO::New(Environment* env) {
34
67582
  BIOPointer bio(BIO_new(GetMethod()));
35

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

129240
  if (!bio ||
45
43080
      len > INT_MAX ||
46

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

67546
    if (BIO_get_init(bio) && BIO_get_data(bio) != nullptr) {
69
67546
      delete FromBIO(bio);
70
67546
      BIO_set_data(bio, nullptr);
71
    }
72
  }
73
74
67546
  return 1;
75
}
76
77
78
51727
int NodeBIO::Read(BIO* bio, char* out, int len) {
79
51727
  BIO_clear_retry_flags(bio);
80
81
51727
  NodeBIO* nbio = FromBIO(bio);
82
51727
  int bytes = nbio->Read(out, len);
83
84
51727
  if (bytes == 0) {
85
10964
    bytes = nbio->eof_return();
86
10964
    if (bytes != 0) {
87
10962
      BIO_set_retry_read(bio);
88
    }
89
  }
90
91
51727
  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
6451
size_t NodeBIO::PeekMultiple(char** out, size_t* size, size_t* count) {
102
6451
  Buffer* pos = read_head_;
103
6451
  size_t max = *count;
104
6451
  size_t total = 0;
105
106
  size_t i;
107
13377
  for (i = 0; i < max; i++) {
108
12753
    size[i] = pos->write_pos_ - pos->read_pos_;
109
12753
    total += size[i];
110
12753
    out[i] = pos->data_ + pos->read_pos_;
111
112
    /* Don't get past write head */
113
12753
    if (pos == write_head_)
114
5827
      break;
115
    else
116
6926
      pos = pos->next_;
117
  }
118
119
6451
  if (i == max)
120
624
    *count = i;
121
  else
122
5827
    *count = i + 1;
123
124
6451
  return total;
125
}
126
127
128
59173
int NodeBIO::Write(BIO* bio, const char* data, int len) {
129
59173
  BIO_clear_retry_flags(bio);
130
131
59173
  FromBIO(bio)->Write(data, len);
132
133
59173
  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
987326
int NodeBIO::Gets(BIO* bio, char* out, int size) {
143
987326
  NodeBIO* nbio = FromBIO(bio);
144
145
987326
  if (nbio->Length() == 0)
146
1375
    return 0;
147
148
985951
  int i = nbio->IndexOf('\n', size);
149
150
  // Include '\n', if it's there.  If not, don't read off the end.
151


985951
  if (i < size && i >= 0 && static_cast<size_t>(i) < nbio->Length())
152
945080
    i++;
153
154
  // Shift `i` a bit to nullptr-terminate string later
155
985951
  if (size == i)
156
    i--;
157
158
  // Flush read data
159
985951
  nbio->Read(out, i);
160
161
985951
  out[i] = 0;
162
163
985951
  return i;
164
}
165
166
167
71797
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
71797
  nbio = FromBIO(bio);
173
71797
  ret = 1;
174
175



71797
  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
43080
      nbio->set_eof_return(num);
184
43080
      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
17643
      ret = nbio->Length();
208
17643
      break;
209
    case BIO_CTRL_DUP:
210
    case BIO_CTRL_FLUSH:
211
5291
      ret = 1;
212
5291
      break;
213
    case BIO_CTRL_PUSH:
214
    case BIO_CTRL_POP:
215
    default:
216
5783
      ret = 0;
217
5783
      break;
218
  }
219
71797
  return ret;
220
}
221
222
223
68249
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
68249
  if (method == nullptr) {
229
668
    method = BIO_meth_new(BIO_TYPE_MEM, "node.js SSL buffer");
230
668
    BIO_meth_set_write(method, Write);
231
668
    BIO_meth_set_read(method, Read);
232
668
    BIO_meth_set_puts(method, Puts);
233
668
    BIO_meth_set_gets(method, Gets);
234
668
    BIO_meth_set_ctrl(method, Ctrl);
235
668
    BIO_meth_set_create(method, New);
236
668
    BIO_meth_set_destroy(method, Free);
237
  }
238
239
68249
  return method;
240
}
241
242
243
1051948
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

3207164
  while (read_head_->read_pos_ != 0 &&
249
1039328
         read_head_->read_pos_ == read_head_->write_pos_) {
250
    // Reset positions
251
63940
    read_head_->read_pos_ = 0;
252
63940
    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
63940
    if (read_head_ != write_head_)
257
9296
      read_head_ = read_head_->next_;
258
  }
259
1051948
}
260
261
262
1044021
size_t NodeBIO::Read(char* out, size_t size) {
263
1044021
  size_t bytes_read = 0;
264
1044021
  size_t expected = Length() > size ? size : Length();
265
1044021
  size_t offset = 0;
266
1044021
  size_t left = size;
267
268
3127370
  while (bytes_read < expected) {
269
1039328
    CHECK_LE(read_head_->read_pos_, read_head_->write_pos_);
270
1039328
    size_t avail = read_head_->write_pos_ - read_head_->read_pos_;
271
1039328
    if (avail > left)
272
975388
      avail = left;
273
274
    // Copy data
275
1039328
    if (out != nullptr)
276
1026714
      memcpy(out + offset, read_head_->data_ + read_head_->read_pos_, avail);
277
1039328
    read_head_->read_pos_ += avail;
278
279
    // Move pointers
280
1039328
    bytes_read += avail;
281
1039328
    offset += avail;
282
1039328
    left -= avail;
283
284
1039328
    TryMoveReadHead();
285
  }
286
1044021
  CHECK_EQ(expected, bytes_read);
287
1044021
  length_ -= bytes_read;
288
289
  // Free all empty buffers, but write_head's child
290
1044021
  FreeEmpty();
291
292
1044021
  return bytes_read;
293
}
294
295
296
1044021
void NodeBIO::FreeEmpty() {
297
1044021
  if (write_head_ == nullptr)
298
1045118
    return;
299
1042202
  Buffer* child = write_head_->next_;
300

1042202
  if (child == write_head_ || child == read_head_)
301
1034620
    return;
302
7582
  Buffer* cur = child->next_;
303

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

67186917
    while (off < avail && *tmp != delim) {
335
65215015
      off++;
336
65215015
      tmp++;
337
    }
338
339
    // Move pointers
340
985951
    bytes_read += off;
341
985951
    left -= off;
342
343
    // Found `delim`
344
985951
    if (off != avail) {
345
945080
      return bytes_read;
346
    }
347
348
    // Move to next buffer
349
40871
    if (current->read_pos_ + avail == current->len_) {
350
35282
      current = current->next_;
351
    }
352
  }
353
40871
  CHECK_EQ(max, bytes_read);
354
355
40871
  return max;
356
}
357
358
359
59173
void NodeBIO::Write(const char* data, size_t size) {
360
59173
  size_t offset = 0;
361
59173
  size_t left = size;
362
363
  // Allocate initial buffer if the ring is empty
364
59173
  TryAllocateForWrite(left);
365
366
187222
  while (left > 0) {
367
68876
    size_t to_write = left;
368
68876
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
369
68876
    size_t avail = write_head_->len_ - write_head_->write_pos_;
370
371
68876
    if (to_write > avail)
372
9703
      to_write = avail;
373
374
    // Copy data
375
68876
    memcpy(write_head_->data_ + write_head_->write_pos_,
376
68876
           data + offset,
377
137752
           to_write);
378
379
    // Move pointers
380
68876
    left -= to_write;
381
68876
    offset += to_write;
382
68876
    length_ += to_write;
383
68876
    write_head_->write_pos_ += to_write;
384
68876
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
385
386
    // Go to next buffer if there still are some bytes to write
387
68876
    if (left != 0) {
388
9703
      CHECK_EQ(write_head_->write_pos_, write_head_->len_);
389
9703
      TryAllocateForWrite(left);
390
9703
      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
9703
      TryMoveReadHead();
395
    }
396
  }
397
59173
  CHECK_EQ(left, 0);
398
59173
}
399
400
401
9484
char* NodeBIO::PeekWritable(size_t* size) {
402
9484
  TryAllocateForWrite(*size);
403
404
9484
  size_t available = write_head_->len_ - write_head_->write_pos_;
405

9484
  if (*size == 0 || available <= *size)
406
9432
    *size = available;
407
408
9484
  return write_head_->data_ + write_head_->write_pos_;
409
}
410
411
412
8926
void NodeBIO::Commit(size_t size) {
413
8926
  write_head_->write_pos_ += size;
414
8926
  length_ += size;
415
8926
  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
8926
  TryAllocateForWrite(0);
420
8926
  if (write_head_->write_pos_ == write_head_->len_) {
421
2917
    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
2917
    TryMoveReadHead();
426
  }
427
8926
}
428
429
430
87286
void NodeBIO::TryAllocateForWrite(size_t hint) {
431
87286
  Buffer* w = write_head_;
432
87286
  Buffer* r = read_head_;
433
  // If write head is full, next buffer is either read head or not empty.
434

127065
  if (w == nullptr ||
435
60996
      (w->write_pos_ == w->len_ &&
436
32602
       (w->next_ == r || w->next_->write_pos_ != 0))) {
437
    size_t len = w == nullptr ? initial_ :
438
57339
                             kThroughputBufferLength;
439
57339
    if (len < hint)
440
47954
      len = hint;
441
57339
    Buffer* next = new Buffer(env_, len);
442
443
57339
    if (w == nullptr) {
444
47507
      next->next_ = next;
445
47507
      write_head_ = next;
446
47507
      read_head_ = next;
447
    } else {
448
9832
      next->next_ = w->next_;
449
9832
      w->next_ = next;
450
    }
451
  }
452
87286
}
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
202638
NodeBIO::~NodeBIO() {
474
67546
  if (read_head_ == nullptr)
475
20075
    return;
476
477
47471
  Buffer* current = read_head_;
478
48169
  do {
479
48169
    Buffer* next = current->next_;
480
48169
    delete current;
481
48169
    current = next;
482
48169
  } while (current != read_head_);
483
484
47471
  read_head_ = nullptr;
485
47471
  write_head_ = nullptr;
486
115017
}
487
488
489
1304702
NodeBIO* NodeBIO::FromBIO(BIO* bio) {
490
1304702
  CHECK_NOT_NULL(BIO_get_data(bio));
491
1304702
  return static_cast<NodeBIO*>(BIO_get_data(bio));
492
}
493
494
495
}  // namespace crypto
496
}  // namespace node