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: 234 267 87.6 %
Date: 2020-08-17 22:13:26 Branches: 126 180 70.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 "allocated_buffer-inl.h"  // Inlined functions needed by node_crypto.h.
25
#include "node_crypto_bio.h"
26
#include "openssl/bio.h"
27
#include "util-inl.h"
28
#include <climits>
29
#include <cstring>
30
31
namespace node {
32
namespace crypto {
33
34
60567
BIOPointer NodeBIO::New(Environment* env) {
35
60567
  BIOPointer bio(BIO_new(GetMethod()));
36

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

108579
  if (!bio ||
46
36193
      len > INT_MAX ||
47

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

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


831845
  if (i < size && i >= 0 && static_cast<size_t>(i) < nbio->Length())
153
797892
    i++;
154
155
  // Shift `i` a bit to nullptr-terminate string later
156
831845
  if (size == i)
157
    i--;
158
159
  // Flush read data
160
831845
  nbio->Read(out, i);
161
162
831845
  out[i] = 0;
163
164
831845
  return i;
165
}
166
167
168
63504
long NodeBIO::Ctrl(BIO* bio, int cmd, long num,  // NOLINT(runtime/int)
169
                   void* ptr) {
170
  NodeBIO* nbio;
171
  long ret;  // NOLINT(runtime/int)
172
173
63504
  nbio = FromBIO(bio);
174
63504
  ret = 1;
175
176



63504
  switch (cmd) {
177
    case BIO_CTRL_RESET:
178
      nbio->Reset();
179
      break;
180
    case BIO_CTRL_EOF:
181
      ret = nbio->Length() == 0;
182
      break;
183
    case BIO_C_SET_BUF_MEM_EOF_RETURN:
184
36193
      nbio->set_eof_return(num);
185
36193
      break;
186
    case BIO_CTRL_INFO:
187
      ret = nbio->Length();
188
      if (ptr != nullptr)
189
        *reinterpret_cast<void**>(ptr) = nullptr;
190
      break;
191
    case BIO_C_SET_BUF_MEM:
192
      CHECK(0 && "Can't use SET_BUF_MEM_PTR with NodeBIO");
193
      break;
194
    case BIO_C_GET_BUF_MEM_PTR:
195
      CHECK(0 && "Can't use GET_BUF_MEM_PTR with NodeBIO");
196
      ret = 0;
197
      break;
198
    case BIO_CTRL_GET_CLOSE:
199
      ret = BIO_get_shutdown(bio);
200
      break;
201
    case BIO_CTRL_SET_CLOSE:
202
      BIO_set_shutdown(bio, num);
203
      break;
204
    case BIO_CTRL_WPENDING:
205
      ret = 0;
206
      break;
207
    case BIO_CTRL_PENDING:
208
15825
      ret = nbio->Length();
209
15825
      break;
210
    case BIO_CTRL_DUP:
211
    case BIO_CTRL_FLUSH:
212
5594
      ret = 1;
213
5594
      break;
214
    case BIO_CTRL_PUSH:
215
    case BIO_CTRL_POP:
216
    default:
217
5892
      ret = 0;
218
5892
      break;
219
  }
220
63504
  return ret;
221
}
222
223
224
61156
const BIO_METHOD* NodeBIO::GetMethod() {
225
  // This is called from InitCryptoOnce() to avoid race conditions during
226
  // initialization.
227
  static BIO_METHOD* method = nullptr;
228
229
61156
  if (method == nullptr) {
230
590
    method = BIO_meth_new(BIO_TYPE_MEM, "node.js SSL buffer");
231
590
    BIO_meth_set_write(method, Write);
232
590
    BIO_meth_set_read(method, Read);
233
590
    BIO_meth_set_puts(method, Puts);
234
590
    BIO_meth_set_gets(method, Gets);
235
590
    BIO_meth_set_ctrl(method, Ctrl);
236
590
    BIO_meth_set_create(method, New);
237
590
    BIO_meth_set_destroy(method, Free);
238
  }
239
240
61156
  return method;
241
}
242
243
244
915389
void NodeBIO::TryMoveReadHead() {
245
  // `read_pos_` and `write_pos_` means the position of the reader and writer
246
  // inside the buffer, respectively. When they're equal - its safe to reset
247
  // them, because both reader and writer will continue doing their stuff
248
  // from new (zero) positions.
249

1829922
  while (read_head_->read_pos_ != 0 &&
250
866034
         read_head_->read_pos_ == read_head_->write_pos_) {
251
    // Reset positions
252
48499
    read_head_->read_pos_ = 0;
253
48499
    read_head_->write_pos_ = 0;
254
255
    // Move read_head_ forward, just in case if there're still some data to
256
    // read in the next buffer.
257
48499
    if (read_head_ != write_head_)
258
331
      read_head_ = read_head_->next_;
259
  }
260
866890
}
261
262
263
872866
size_t NodeBIO::Read(char* out, size_t size) {
264
872866
  size_t bytes_read = 0;
265
872866
  size_t expected = Length() > size ? size : Length();
266
872866
  size_t offset = 0;
267
872866
  size_t left = size;
268
269
2604934
  while (bytes_read < expected) {
270
866034
    CHECK_LE(read_head_->read_pos_, read_head_->write_pos_);
271
866034
    size_t avail = read_head_->write_pos_ - read_head_->read_pos_;
272
866034
    if (avail > left)
273
817535
      avail = left;
274
275
    // Copy data
276
866034
    if (out != nullptr)
277
859061
      memcpy(out + offset, read_head_->data_ + read_head_->read_pos_, avail);
278
866034
    read_head_->read_pos_ += avail;
279
280
    // Move pointers
281
866034
    bytes_read += avail;
282
866034
    offset += avail;
283
866034
    left -= avail;
284
285
866034
    TryMoveReadHead();
286
  }
287
872866
  CHECK_EQ(expected, bytes_read);
288
872866
  length_ -= bytes_read;
289
290
  // Free all empty buffers, but write_head's child
291
872866
  FreeEmpty();
292
293
872866
  return bytes_read;
294
}
295
296
297
872866
void NodeBIO::FreeEmpty() {
298
872866
  if (write_head_ == nullptr)
299
874522
    return;
300
871209
  Buffer* child = write_head_->next_;
301

871209
  if (child == write_head_ || child == read_head_)
302
866069
    return;
303
5140
  Buffer* cur = child->next_;
304

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

110770915
    while (off < avail && *tmp != delim) {
336
54969535
      off++;
337
54969535
      tmp++;
338
    }
339
340
    // Move pointers
341
831845
    bytes_read += off;
342
831845
    left -= off;
343
344
    // Found `delim`
345
831845
    if (off != avail) {
346
797892
      return bytes_read;
347
    }
348
349
    // Move to next buffer
350
33953
    if (current->read_pos_ + avail == current->len_) {
351
29276
      current = current->next_;
352
    }
353
  }
354
33953
  CHECK_EQ(max, bytes_read);
355
356
33953
  return max;
357
}
358
359
360
45302
void NodeBIO::Write(const char* data, size_t size) {
361
45302
  size_t offset = 0;
362
45302
  size_t left = size;
363
364
  // Allocate initial buffer if the ring is empty
365
45302
  TryAllocateForWrite(left);
366
367
136150
  while (left > 0) {
368
45424
    size_t to_write = left;
369
45424
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
370
45424
    size_t avail = write_head_->len_ - write_head_->write_pos_;
371
372
45424
    if (to_write > avail)
373
122
      to_write = avail;
374
375
    // Copy data
376
45424
    memcpy(write_head_->data_ + write_head_->write_pos_,
377
45424
           data + offset,
378
45424
           to_write);
379
380
    // Move pointers
381
45424
    left -= to_write;
382
45424
    offset += to_write;
383
45424
    length_ += to_write;
384
45424
    write_head_->write_pos_ += to_write;
385
45424
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
386
387
    // Go to next buffer if there still are some bytes to write
388
45424
    if (left != 0) {
389
122
      CHECK_EQ(write_head_->write_pos_, write_head_->len_);
390
122
      TryAllocateForWrite(left);
391
122
      write_head_ = write_head_->next_;
392
393
      // Additionally, since we're moved to the next buffer, read head
394
      // may be moved as well.
395
122
      TryMoveReadHead();
396
    }
397
  }
398
45302
  CHECK_EQ(left, 0);
399
45302
}
400
401
402
6096
char* NodeBIO::PeekWritable(size_t* size) {
403
6096
  TryAllocateForWrite(*size);
404
405
6096
  size_t available = write_head_->len_ - write_head_->write_pos_;
406

6096
  if (*size == 0 || available <= *size)
407
6053
    *size = available;
408
409
6096
  return write_head_->data_ + write_head_->write_pos_;
410
}
411
412
413
5972
void NodeBIO::Commit(size_t size) {
414
5972
  write_head_->write_pos_ += size;
415
5972
  length_ += size;
416
5972
  CHECK_LE(write_head_->write_pos_, write_head_->len_);
417
418
  // Allocate new buffer if write head is full,
419
  // and there're no other place to go
420
5972
  TryAllocateForWrite(0);
421
5972
  if (write_head_->write_pos_ == write_head_->len_) {
422
734
    write_head_ = write_head_->next_;
423
424
    // Additionally, since we're moved to the next buffer, read head
425
    // may be moved as well.
426
734
    TryMoveReadHead();
427
  }
428
5972
}
429
430
431
57492
void NodeBIO::TryAllocateForWrite(size_t hint) {
432
57492
  Buffer* w = write_head_;
433
57492
  Buffer* r = read_head_;
434
  // If write head is full, next buffer is either read head or not empty.
435

74505
  if (w == nullptr ||
436
17871
      (w->write_pos_ == w->len_ &&
437
1066
       (w->next_ == r || w->next_->write_pos_ != 0))) {
438
41129
    size_t len = w == nullptr ? initial_ :
439
41129
                             kThroughputBufferLength;
440
41129
    if (len < hint)
441
33262
      len = hint;
442
443
    // If there is a one time allocation size hint, use it.
444
41129
    if (allocate_hint_ > len) {
445
16
      len = allocate_hint_;
446
16
      allocate_hint_ = 0;
447
    }
448
449
41129
    Buffer* next = new Buffer(env_, len);
450
451
41129
    if (w == nullptr) {
452
40479
      next->next_ = next;
453
40479
      write_head_ = next;
454
40479
      read_head_ = next;
455
    } else {
456
650
      next->next_ = w->next_;
457
650
      w->next_ = next;
458
    }
459
  }
460
57492
}
461
462
463
void NodeBIO::Reset() {
464
  if (read_head_ == nullptr)
465
    return;
466
467
  while (read_head_->read_pos_ != read_head_->write_pos_) {
468
    CHECK(read_head_->write_pos_ > read_head_->read_pos_);
469
470
    length_ -= read_head_->write_pos_ - read_head_->read_pos_;
471
    read_head_->write_pos_ = 0;
472
    read_head_->read_pos_ = 0;
473
474
    read_head_ = read_head_->next_;
475
  }
476
  write_head_ = read_head_;
477
  CHECK_EQ(length_, 0);
478
}
479
480
481
181635
NodeBIO::~NodeBIO() {
482
60545
  if (read_head_ == nullptr)
483
20088
    return;
484
485
40457
  Buffer* current = read_head_;
486
646
  do {
487
41103
    Buffer* next = current->next_;
488
41103
    delete current;
489
41103
    current = next;
490
41103
  } while (current != read_head_);
491
492
40457
  read_head_ = nullptr;
493
40457
  write_head_ = nullptr;
494
121090
}
495
496
497
1101025
NodeBIO* NodeBIO::FromBIO(BIO* bio) {
498
1101025
  CHECK_NOT_NULL(BIO_get_data(bio));
499
1101025
  return static_cast<NodeBIO*>(BIO_get_data(bio));
500
}
501
502
503
}  // namespace crypto
504

13380
}  // namespace node