GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/node_crypto_bio.cc Lines: 205 225 91.1 %
Date: 2016-11-30 Branches: 115 173 66.5 %

Line Branch Exec Source
1
#include "node_crypto_bio.h"
2
#include "openssl/bio.h"
3
#include "util.h"
4
#include "util-inl.h"
5
#include <limits.h>
6
#include <string.h>
7
8
namespace node {
9
10
const BIO_METHOD NodeBIO::method = {
11
  BIO_TYPE_MEM,
12
  "node.js SSL buffer",
13
  NodeBIO::Write,
14
  NodeBIO::Read,
15
  NodeBIO::Puts,
16
  NodeBIO::Gets,
17
  NodeBIO::Ctrl,
18
  NodeBIO::New,
19
  NodeBIO::Free,
20
  nullptr
21
};
22
23
24
1220
BIO* NodeBIO::New() {
25
  // The const_cast doesn't violate const correctness.  OpenSSL's usage of
26
  // BIO_METHOD is effectively const but BIO_new() takes a non-const argument.
27
21405
  return BIO_new(const_cast<BIO_METHOD*>(&method));
28
}
29
30
31
20185
BIO* NodeBIO::NewFixed(const char* data, size_t len) {
32
20185
  BIO* bio = New();
33
34
60555
  if (bio == nullptr ||
35
40370
      len > INT_MAX ||
36

40370
      BIO_write(bio, data, len) != static_cast<int>(len) ||
37
20185
      BIO_set_mem_eof_return(bio, 0) != 1) {
38
    BIO_free(bio);
39
    return nullptr;
40
  }
41
42
  return bio;
43
}
44
45
46
1812
void NodeBIO::AssignEnvironment(Environment* env) {
47
1812
  env_ = env;
48
1812
}
49
50
51
21405
int NodeBIO::New(BIO* bio) {
52
42810
  bio->ptr = new NodeBIO();
53
54
  // XXX Why am I doing it?!
55
21405
  bio->shutdown = 1;
56
21405
  bio->init = 1;
57
21405
  bio->num = -1;
58
59
21405
  return 1;
60
}
61
62
63
21377
int NodeBIO::Free(BIO* bio) {
64
21377
  if (bio == nullptr)
65
    return 0;
66
67
21377
  if (bio->shutdown) {
68

21377
    if (bio->init && bio->ptr != nullptr) {
69
42754
      delete FromBIO(bio);
70
21377
      bio->ptr = nullptr;
71
    }
72
  }
73
74
  return 1;
75
}
76
77
78
12401
int NodeBIO::Read(BIO* bio, char* out, int len) {
79
  int bytes;
80
12401
  BIO_clear_retry_flags(bio);
81
82
24802
  bytes = FromBIO(bio)->Read(out, len);
83
84
12401
  if (bytes == 0) {
85
2958
    bytes = bio->num;
86
2958
    if (bytes != 0) {
87
2956
      BIO_set_retry_read(bio);
88
    }
89
  }
90
91
12401
  return bytes;
92
}
93
94
95
384
char* NodeBIO::Peek(size_t* size) {
96
384
  *size = read_head_->write_pos_ - read_head_->read_pos_;
97
384
  return read_head_->data_ + read_head_->read_pos_;
98
}
99
100
101
2221
size_t NodeBIO::PeekMultiple(char** out, size_t* size, size_t* count) {
102
2221
  Buffer* pos = read_head_;
103
2221
  size_t max = *count;
104
2221
  size_t total = 0;
105
106
  size_t i;
107
2622
  for (i = 0; i < max; i++) {
108
2598
    size[i] = pos->write_pos_ - pos->read_pos_;
109
2598
    total += size[i];
110
2598
    out[i] = pos->data_ + pos->read_pos_;
111
112
    /* Don't get past write head */
113
2598
    if (pos == write_head_)
114
      break;
115
    else
116
401
      pos = pos->next_;
117
  }
118
119
2221
  if (i == max)
120
24
    *count = i;
121
  else
122
2197
    *count = i + 1;
123
124
2221
  return total;
125
}
126
127
128
26469
int NodeBIO::Write(BIO* bio, const char* data, int len) {
129
26469
  BIO_clear_retry_flags(bio);
130
131
52938
  FromBIO(bio)->Write(data, len);
132
133
26469
  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
474407
int NodeBIO::Gets(BIO* bio, char* out, int size) {
143
948814
  NodeBIO* nbio =  FromBIO(bio);
144
145
474407
  if (nbio->Length() == 0)
146
    return 0;
147
148
474107
  int i = nbio->IndexOf('\n', size);
149
150
  // Include '\n', if it's there.  If not, don't read off the end.
151

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


27399
  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
20185
      bio->num = num;
184
20185
      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->shutdown;
199
      break;
200
    case BIO_CTRL_SET_CLOSE:
201
      bio->shutdown = num;
202
      break;
203
    case BIO_CTRL_WPENDING:
204
      ret = 0;
205
      break;
206
    case BIO_CTRL_PENDING:
207
5200
      ret = nbio->Length();
208
5200
      break;
209
    case BIO_CTRL_DUP:
210
    case BIO_CTRL_FLUSH:
211
851
      ret = 1;
212
851
      break;
213
    case BIO_CTRL_PUSH:
214
    case BIO_CTRL_POP:
215
    default:
216
      ret = 0;
217
      break;
218
  }
219
27399
  return ret;
220
}
221
222
223
void NodeBIO::TryMoveReadHead() {
224
  // `read_pos_` and `write_pos_` means the position of the reader and writer
225
  // inside the buffer, respectively. When they're equal - its safe to reset
226
  // them, because both reader and writer will continue doing their stuff
227
  // from new (zero) positions.
228




998458
  while (read_head_->read_pos_ != 0 &&
229
486222
         read_head_->read_pos_ == read_head_->write_pos_) {
230
    // Reset positions
231
24952
    read_head_->read_pos_ = 0;
232
24952
    read_head_->write_pos_ = 0;
233
234
    // Move read_head_ forward, just in case if there're still some data to
235
    // read in the next buffer.
236


24952
    if (read_head_ != write_head_)
237
1057
      read_head_ = read_head_->next_;
238
  }
239
}
240
241
242
488803
size_t NodeBIO::Read(char* out, size_t size) {
243
488803
  size_t bytes_read = 0;
244
488803
  size_t expected = Length() > size ? size : Length();
245
488803
  size_t offset = 0;
246
488803
  size_t left = size;
247
248
975025
  while (bytes_read < expected) {
249
486222
    CHECK_LE(read_head_->read_pos_, read_head_->write_pos_);
250
486222
    size_t avail = read_head_->write_pos_ - read_head_->read_pos_;
251
486222
    if (avail > left)
252
461270
      avail = left;
253
254
    // Copy data
255
486222
    if (out != nullptr)
256
483550
      memcpy(out + offset, read_head_->data_ + read_head_->read_pos_, avail);
257
486222
    read_head_->read_pos_ += avail;
258
259
    // Move pointers
260
486222
    bytes_read += avail;
261
486222
    offset += avail;
262
486222
    left -= avail;
263
264
    TryMoveReadHead();
265
  }
266
488803
  CHECK_EQ(expected, bytes_read);
267
488803
  length_ -= bytes_read;
268
269
  // Free all empty buffers, but write_head's child
270
488803
  FreeEmpty();
271
272
488803
  return bytes_read;
273
}
274
275
276
488803
void NodeBIO::FreeEmpty() {
277
488803
  if (write_head_ == nullptr)
278
    return;
279
488319
  Buffer* child = write_head_->next_;
280

488319
  if (child == write_head_ || child == read_head_)
281
    return;
282
2241
  Buffer* cur = child->next_;
283

2241
  if (cur == write_head_ || cur == read_head_)
284
    return;
285
286
  Buffer* prev = child;
287
289
  while (cur != read_head_) {
288
259
    CHECK_NE(cur, write_head_);
289
259
    CHECK_EQ(cur->write_pos_, cur->read_pos_);
290
291
259
    Buffer* next = cur->next_;
292
259
    delete cur;
293
    cur = next;
294
  }
295
30
  prev->next_ = cur;
296
}
297
298
299
474107
size_t NodeBIO::IndexOf(char delim, size_t limit) {
300
474107
  size_t bytes_read = 0;
301
474107
  size_t max = Length() > limit ? limit : Length();
302
474107
  size_t left = limit;
303
474107
  Buffer* current = read_head_;
304
305
474118
  while (bytes_read < max) {
306
474107
    CHECK_LE(current->read_pos_, current->write_pos_);
307
474107
    size_t avail = current->write_pos_ - current->read_pos_;
308
474107
    if (avail > left)
309
391345
      avail = left;
310
311
    // Walk through data
312
474107
    char* tmp = current->data_ + current->read_pos_;
313
474107
    size_t off = 0;
314

63646609
    while (off < avail && *tmp != delim) {
315
31586251
      off++;
316
31586251
      tmp++;
317
    }
318
319
    // Move pointers
320
474107
    bytes_read += off;
321
474107
    left -= off;
322
323
    // Found `delim`
324
474107
    if (off != avail) {
325
      return bytes_read;
326
    }
327
328
    // Move to next buffer
329
11
    if (current->read_pos_ + avail == current->len_) {
330
      current = current->next_;
331
    }
332
  }
333
11
  CHECK_EQ(max, bytes_read);
334
335
  return max;
336
}
337
338
339
26566
void NodeBIO::Write(const char* data, size_t size) {
340
26566
  size_t offset = 0;
341
26566
  size_t left = size;
342
343
  // Allocate initial buffer if the ring is empty
344
26566
  TryAllocateForWrite(left);
345
346
53533
  while (left > 0) {
347
26967
    size_t to_write = left;
348
26967
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
349
26967
    size_t avail = write_head_->len_ - write_head_->write_pos_;
350
351
26967
    if (to_write > avail)
352
401
      to_write = avail;
353
354
    // Copy data
355
26967
    memcpy(write_head_->data_ + write_head_->write_pos_,
356
26967
           data + offset,
357
26967
           to_write);
358
359
    // Move pointers
360
26967
    left -= to_write;
361
26967
    offset += to_write;
362
26967
    length_ += to_write;
363
26967
    write_head_->write_pos_ += to_write;
364
26967
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
365
366
    // Go to next buffer if there still are some bytes to write
367
26967
    if (left != 0) {
368
401
      CHECK_EQ(write_head_->write_pos_, write_head_->len_);
369
401
      TryAllocateForWrite(left);
370
401
      write_head_ = write_head_->next_;
371
372
      // Additionally, since we're moved to the next buffer, read head
373
      // may be moved as well.
374
      TryMoveReadHead();
375
    }
376
  }
377
26566
  CHECK_EQ(left, 0);
378
26566
}
379
380
381
2283
char* NodeBIO::PeekWritable(size_t* size) {
382
2283
  TryAllocateForWrite(*size);
383
384
2283
  size_t available = write_head_->len_ - write_head_->write_pos_;
385

2283
  if (*size != 0 && available > *size)
386
    available = *size;
387
  else
388
2283
    *size = available;
389
390
2283
  return write_head_->data_ + write_head_->write_pos_;
391
}
392
393
394
2101
void NodeBIO::Commit(size_t size) {
395
2101
  write_head_->write_pos_ += size;
396
2101
  length_ += size;
397
2101
  CHECK_LE(write_head_->write_pos_, write_head_->len_);
398
399
  // Allocate new buffer if write head is full,
400
  // and there're no other place to go
401
2101
  TryAllocateForWrite(0);
402
2101
  if (write_head_->write_pos_ == write_head_->len_) {
403
661
    write_head_ = write_head_->next_;
404
405
    // Additionally, since we're moved to the next buffer, read head
406
    // may be moved as well.
407
    TryMoveReadHead();
408
  }
409
2101
}
410
411
412
31351
void NodeBIO::TryAllocateForWrite(size_t hint) {
413
31351
  Buffer* w = write_head_;
414
31351
  Buffer* r = read_head_;
415
  // If write head is full, next buffer is either read head or not empty.
416

41237
  if (w == nullptr ||
417
11108
      (w->write_pos_ == w->len_ &&
418
2150
       (w->next_ == r || w->next_->write_pos_ != 0))) {
419
    size_t len = w == nullptr ? initial_ :
420
21759
                             kThroughputBufferLength;
421
21759
    if (len < hint)
422
18803
      len = hint;
423
21759
    Buffer* next = new Buffer(env_, len);
424
425
21759
    if (w == nullptr) {
426
21465
      next->next_ = next;
427
21465
      write_head_ = next;
428
21465
      read_head_ = next;
429
    } else {
430
294
      next->next_ = w->next_;
431
294
      w->next_ = next;
432
    }
433
  }
434
31351
}
435
436
437
3
void NodeBIO::Reset() {
438
3
  if (read_head_ == nullptr)
439
    return;
440
441
9
  while (read_head_->read_pos_ != read_head_->write_pos_) {
442
3
    CHECK(read_head_->write_pos_ > read_head_->read_pos_);
443
444
3
    length_ -= read_head_->write_pos_ - read_head_->read_pos_;
445
3
    read_head_->write_pos_ = 0;
446
3
    read_head_->read_pos_ = 0;
447
448
3
    read_head_ = read_head_->next_;
449
  }
450
3
  write_head_ = read_head_;
451
3
  CHECK_EQ(length_, 0);
452
}
453
454
455
43410
NodeBIO::~NodeBIO() {
456
21971
  if (read_head_ == nullptr)
457
    return;
458
459
  Buffer* current = read_head_;
460
  do {
461
21471
    Buffer* next = current->next_;
462
21471
    delete current;
463
21471
    current = next;
464
21471
  } while (current != read_head_);
465
466
21439
  read_head_ = nullptr;
467
21439
  write_head_ = nullptr;
468
21971
}
469
470
}  // namespace node