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-12-18 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
1272
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
21483
  return BIO_new(const_cast<BIO_METHOD*>(&method));
28
}
29
30
31
20211
BIO* NodeBIO::NewFixed(const char* data, size_t len) {
32
20211
  BIO* bio = New();
33
34
60633
  if (bio == nullptr ||
35
40422
      len > INT_MAX ||
36

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

21455
    if (bio->init && bio->ptr != nullptr) {
69
42910
      delete FromBIO(bio);
70
21455
      bio->ptr = nullptr;
71
    }
72
  }
73
74
  return 1;
75
}
76
77
78
12855
int NodeBIO::Read(BIO* bio, char* out, int len) {
79
  int bytes;
80
12855
  BIO_clear_retry_flags(bio);
81
82
25710
  bytes = FromBIO(bio)->Read(out, len);
83
84
12855
  if (bytes == 0) {
85
3028
    bytes = bio->num;
86
3028
    if (bytes != 0) {
87
3026
      BIO_set_retry_read(bio);
88
    }
89
  }
90
91
12855
  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
2272
size_t NodeBIO::PeekMultiple(char** out, size_t* size, size_t* count) {
102
2272
  Buffer* pos = read_head_;
103
2272
  size_t max = *count;
104
2272
  size_t total = 0;
105
106
  size_t i;
107
2673
  for (i = 0; i < max; i++) {
108
2649
    size[i] = pos->write_pos_ - pos->read_pos_;
109
2649
    total += size[i];
110
2649
    out[i] = pos->data_ + pos->read_pos_;
111
112
    /* Don't get past write head */
113
2649
    if (pos == write_head_)
114
      break;
115
    else
116
401
      pos = pos->next_;
117
  }
118
119
2272
  if (i == max)
120
24
    *count = i;
121
  else
122
2248
    *count = i + 1;
123
124
2272
  return total;
125
}
126
127
128
26559
int NodeBIO::Write(BIO* bio, const char* data, int len) {
129
26559
  BIO_clear_retry_flags(bio);
130
131
53118
  FromBIO(bio)->Write(data, len);
132
133
26559
  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
474777
int NodeBIO::Gets(BIO* bio, char* out, int size) {
143
949554
  NodeBIO* nbio =  FromBIO(bio);
144
145
474777
  if (nbio->Length() == 0)
146
    return 0;
147
148
474464
  int i = nbio->IndexOf('\n', size);
149
150
  // Include '\n', if it's there.  If not, don't read off the end.
151

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


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




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


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

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

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

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

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