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-08-17 22:35:23 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
67880
BIOPointer NodeBIO::New(Environment* env) {
34
67880
  BIOPointer bio(BIO_new(GetMethod()));
35

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

130080
  if (!bio ||
45
43360
      len > INT_MAX ||
46

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

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


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



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

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

1047498
  if (child == write_head_ || child == read_head_)
301
1039806
    return;
302
7692
  Buffer* cur = child->next_;
303

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

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

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

124769
  if (w == nullptr ||
435
59731
      (w->write_pos_ == w->len_ &&
436
32654
       (w->next_ == r || w->next_->write_pos_ != 0))) {
437
    size_t len = w == nullptr ? initial_ :
438
57621
                             kThroughputBufferLength;
439
57621
    if (len < hint)
440
48198
      len = hint;
441
57621
    Buffer* next = new Buffer(env_, len);
442
443
57621
    if (w == nullptr) {
444
47791
      next->next_ = next;
445
47791
      write_head_ = next;
446
47791
      read_head_ = next;
447
    } else {
448
9830
      next->next_ = w->next_;
449
9830
      w->next_ = next;
450
    }
451
  }
452
86280
}
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
203532
NodeBIO::~NodeBIO() {
474
67844
  if (read_head_ == nullptr)
475
20087
    return;
476
477
47757
  Buffer* current = read_head_;
478
48455
  do {
479
48455
    Buffer* next = current->next_;
480
48455
    delete current;
481
48455
    current = next;
482
48455
  } while (current != read_head_);
483
484
47757
  read_head_ = nullptr;
485
47757
  write_head_ = nullptr;
486
115601
}
487
488
489
1309332
NodeBIO* NodeBIO::FromBIO(BIO* bio) {
490
1309332
  CHECK_NOT_NULL(BIO_get_data(bio));
491
1309332
  return static_cast<NodeBIO*>(BIO_get_data(bio));
492
}
493
494
495
}  // namespace crypto
496
}  // namespace node