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-20 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
1216
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
20949
  return BIO_new(const_cast<BIO_METHOD*>(&method));
28
}
29
30
31
19733
BIO* NodeBIO::NewFixed(const char* data, size_t len) {
32
19733
  BIO* bio = New();
33
34
59199
  if (bio == nullptr ||
35
39466
      len > INT_MAX ||
36

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

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

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


26915
  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
19733
      bio->num = num;
184
19733
      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
5176
      ret = nbio->Length();
208
5176
      break;
209
    case BIO_CTRL_DUP:
210
    case BIO_CTRL_FLUSH:
211
848
      ret = 1;
212
848
      break;
213
    case BIO_CTRL_PUSH:
214
    case BIO_CTRL_POP:
215
    default:
216
      ret = 0;
217
      break;
218
  }
219
26915
  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




976494
  while (read_head_->read_pos_ != 0 &&
229
475477
         read_head_->read_pos_ == read_head_->write_pos_) {
230
    // Reset positions
231
24478
    read_head_->read_pos_ = 0;
232
24478
    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


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

477561
  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
463409
size_t NodeBIO::IndexOf(char delim, size_t limit) {
300
463409
  size_t bytes_read = 0;
301
463409
  size_t max = Length() > limit ? limit : Length();
302
463409
  size_t left = limit;
303
463409
  Buffer* current = read_head_;
304
305
463420
  while (bytes_read < max) {
306
463409
    CHECK_LE(current->read_pos_, current->write_pos_);
307
463409
    size_t avail = current->write_pos_ - current->read_pos_;
308
463409
    if (avail > left)
309
382498
      avail = left;
310
311
    // Walk through data
312
463409
    char* tmp = current->data_ + current->read_pos_;
313
463409
    size_t off = 0;
314

62207535
    while (off < avail && *tmp != delim) {
315
30872063
      off++;
316
30872063
      tmp++;
317
    }
318
319
    // Move pointers
320
463409
    bytes_read += off;
321
463409
    left -= off;
322
323
    // Found `delim`
324
463409
    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
26105
void NodeBIO::Write(const char* data, size_t size) {
340
26105
  size_t offset = 0;
341
26105
  size_t left = size;
342
343
  // Allocate initial buffer if the ring is empty
344
26105
  TryAllocateForWrite(left);
345
346
52611
  while (left > 0) {
347
26506
    size_t to_write = left;
348
26506
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
349
26506
    size_t avail = write_head_->len_ - write_head_->write_pos_;
350
351
26506
    if (to_write > avail)
352
401
      to_write = avail;
353
354
    // Copy data
355
26506
    memcpy(write_head_->data_ + write_head_->write_pos_,
356
26506
           data + offset,
357
26506
           to_write);
358
359
    // Move pointers
360
26506
    left -= to_write;
361
26506
    offset += to_write;
362
26506
    length_ += to_write;
363
26506
    write_head_->write_pos_ += to_write;
364
26506
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
365
366
    // Go to next buffer if there still are some bytes to write
367
26506
    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
26105
  CHECK_EQ(left, 0);
378
26105
}
379
380
381
2270
char* NodeBIO::PeekWritable(size_t* size) {
382
2270
  TryAllocateForWrite(*size);
383
384
2270
  size_t available = write_head_->len_ - write_head_->write_pos_;
385

2270
  if (*size != 0 && available > *size)
386
    available = *size;
387
  else
388
2270
    *size = available;
389
390
2270
  return write_head_->data_ + write_head_->write_pos_;
391
}
392
393
394
2088
void NodeBIO::Commit(size_t size) {
395
2088
  write_head_->write_pos_ += size;
396
2088
  length_ += size;
397
2088
  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
2088
  TryAllocateForWrite(0);
402
2088
  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
2088
}
410
411
412
30864
void NodeBIO::TryAllocateForWrite(size_t hint) {
413
30864
  Buffer* w = write_head_;
414
30864
  Buffer* r = read_head_;
415
  // If write head is full, next buffer is either read head or not empty.
416

40719
  if (w == nullptr ||
417
11077
      (w->write_pos_ == w->len_ &&
418
2150
       (w->next_ == r || w->next_->write_pos_ != 0))) {
419
    size_t len = w == nullptr ? initial_ :
420
21303
                             kThroughputBufferLength;
421
21303
    if (len < hint)
422
18381
      len = hint;
423
21303
    Buffer* next = new Buffer(env_, len);
424
425
21303
    if (w == nullptr) {
426
21009
      next->next_ = next;
427
21009
      write_head_ = next;
428
21009
      read_head_ = next;
429
    } else {
430
294
      next->next_ = w->next_;
431
294
      w->next_ = next;
432
    }
433
  }
434
30864
}
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
42496
NodeBIO::~NodeBIO() {
456
21513
  if (read_head_ == nullptr)
457
    return;
458
459
  Buffer* current = read_head_;
460
  do {
461
21015
    Buffer* next = current->next_;
462
21015
    delete current;
463
21015
    current = next;
464
21015
  } while (current != read_head_);
465
466
20983
  read_head_ = nullptr;
467
20983
  write_head_ = nullptr;
468
21513
}
469
470
}  // namespace node