GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/src/node_crypto_bio.cc Lines: 205 226 90.7 %
Date: 2016-07-23 Branches: 115 173 66.5 %

Line 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
1198
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
20323
  return BIO_new(const_cast<BIO_METHOD*>(&method));
28
}
29
30
31
19125
BIO* NodeBIO::NewFixed(const char* data, size_t len) {
32
19125
  BIO* bio = New();
33
34
57375
  if (bio == nullptr ||
35
38250
      len > INT_MAX ||
36
38250
      BIO_write(bio, data, len) != static_cast<int>(len) ||
37
19125
      BIO_set_mem_eof_return(bio, 0) != 1) {
38
    BIO_free(bio);
39
    return nullptr;
40
  }
41
42
  return bio;
43
}
44
45
46
1779
void NodeBIO::AssignEnvironment(Environment* env) {
47
1779
  env_ = env;
48
1779
}
49
50
51
20323
int NodeBIO::New(BIO* bio) {
52
40646
  bio->ptr = new NodeBIO();
53
54
  // XXX Why am I doing it?!
55
20323
  bio->shutdown = 1;
56
20323
  bio->init = 1;
57
20323
  bio->num = -1;
58
59
20323
  return 1;
60
}
61
62
63
19545
int NodeBIO::Free(BIO* bio) {
64
19545
  if (bio == nullptr)
65
    return 0;
66
67
19545
  if (bio->shutdown) {
68
19545
    if (bio->init && bio->ptr != nullptr) {
69
39090
      delete FromBIO(bio);
70
19545
      bio->ptr = nullptr;
71
    }
72
  }
73
74
  return 1;
75
}
76
77
78
12464
int NodeBIO::Read(BIO* bio, char* out, int len) {
79
  int bytes;
80
12464
  BIO_clear_retry_flags(bio);
81
82
24928
  bytes = FromBIO(bio)->Read(out, len);
83
84
12464
  if (bytes == 0) {
85
2988
    bytes = bio->num;
86
2988
    if (bytes != 0) {
87
2986
      BIO_set_retry_read(bio);
88
    }
89
  }
90
91
12464
  return bytes;
92
}
93
94
95
378
char* NodeBIO::Peek(size_t* size) {
96
378
  *size = read_head_->write_pos_ - read_head_->read_pos_;
97
378
  return read_head_->data_ + read_head_->read_pos_;
98
}
99
100
101
2140
size_t NodeBIO::PeekMultiple(char** out, size_t* size, size_t* count) {
102
2140
  Buffer* pos = read_head_;
103
2140
  size_t max = *count;
104
2140
  size_t total = 0;
105
106
  size_t i;
107
2584
  for (i = 0; i < max; i++) {
108
2558
    size[i] = pos->write_pos_ - pos->read_pos_;
109
2558
    total += size[i];
110
2558
    out[i] = pos->data_ + pos->read_pos_;
111
112
    /* Don't get past write head */
113
2558
    if (pos == write_head_)
114
      break;
115
    else
116
444
      pos = pos->next_;
117
  }
118
119
2140
  if (i == max)
120
26
    *count = i;
121
  else
122
2114
    *count = i + 1;
123
124
2140
  return total;
125
}
126
127
128
24914
int NodeBIO::Write(BIO* bio, const char* data, int len) {
129
24914
  BIO_clear_retry_flags(bio);
130
131
49828
  FromBIO(bio)->Write(data, len);
132
133
24914
  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
449367
int NodeBIO::Gets(BIO* bio, char* out, int size) {
143
898734
  NodeBIO* nbio =  FromBIO(bio);
144
145
449367
  if (nbio->Length() == 0)
146
    return 0;
147
148
449074
  int i = nbio->IndexOf('\n', size);
149
150
  // Include '\n', if it's there.  If not, don't read off the end.
151
449074
  if (i < size && i >= 0 && static_cast<size_t>(i) < nbio->Length())
152
449064
    i++;
153
154
  // Shift `i` a bit to nullptr-terminate string later
155
449074
  if (size == i)
156
    i--;
157
158
  // Flush read data
159
449074
  nbio->Read(out, i);
160
161
449074
  out[i] = 0;
162
163
449074
  return i;
164
}
165
166
167
26193
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
52386
  nbio = FromBIO(bio);
173
26193
  ret = 1;
174
175
26193
  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
19125
      bio->num = num;
184
19125
      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
      ABORT();
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->shutdown;
200
      break;
201
    case BIO_CTRL_SET_CLOSE:
202
      bio->shutdown = num;
203
      break;
204
    case BIO_CTRL_WPENDING:
205
      ret = 0;
206
      break;
207
    case BIO_CTRL_PENDING:
208
5093
      ret = nbio->Length();
209
5093
      break;
210
    case BIO_CTRL_DUP:
211
    case BIO_CTRL_FLUSH:
212
839
      ret = 1;
213
839
      break;
214
    case BIO_CTRL_PUSH:
215
    case BIO_CTRL_POP:
216
    default:
217
      ret = 0;
218
      break;
219
  }
220
26193
  return ret;
221
}
222
223
224
void NodeBIO::TryMoveReadHead() {
225
  // `read_pos_` and `write_pos_` means the position of the reader and writer
226
  // inside the buffer, respectively. When they're equal - its safe to reset
227
  // them, because both reader and writer will continue doing their stuff
228
  // from new (zero) positions.
229
947413
  while (read_head_->read_pos_ != 0 &&
230
461180
         read_head_->read_pos_ == read_head_->write_pos_) {
231
    // Reset positions
232
23894
    read_head_->read_pos_ = 0;
233
23894
    read_head_->write_pos_ = 0;
234
235
    // Move read_head_ forward, just in case if there're still some data to
236
    // read in the next buffer.
237
23894
    if (read_head_ != write_head_)
238
1156
      read_head_ = read_head_->next_;
239
  }
240
}
241
242
243
463750
size_t NodeBIO::Read(char* out, size_t size) {
244
463750
  size_t bytes_read = 0;
245
463750
  size_t expected = Length() > size ? size : Length();
246
463750
  size_t offset = 0;
247
463750
  size_t left = size;
248
249
924930
  while (bytes_read < expected) {
250
461180
    CHECK_LE(read_head_->read_pos_, read_head_->write_pos_);
251
461180
    size_t avail = read_head_->write_pos_ - read_head_->read_pos_;
252
461180
    if (avail > left)
253
437286
      avail = left;
254
255
    // Copy data
256
461180
    if (out != nullptr)
257
458550
      memcpy(out + offset, read_head_->data_ + read_head_->read_pos_, avail);
258
461180
    read_head_->read_pos_ += avail;
259
260
    // Move pointers
261
461180
    bytes_read += avail;
262
461180
    offset += avail;
263
461180
    left -= avail;
264
265
    TryMoveReadHead();
266
  }
267
463750
  CHECK_EQ(expected, bytes_read);
268
463750
  length_ -= bytes_read;
269
270
  // Free all empty buffers, but write_head's child
271
463750
  FreeEmpty();
272
273
463750
  return bytes_read;
274
}
275
276
277
463750
void NodeBIO::FreeEmpty() {
278
463750
  if (write_head_ == nullptr)
279
    return;
280
463275
  Buffer* child = write_head_->next_;
281
463275
  if (child == write_head_ || child == read_head_)
282
    return;
283
2322
  Buffer* cur = child->next_;
284
2322
  if (cur == write_head_ || cur == read_head_)
285
    return;
286
287
  Buffer* prev = child;
288
334
  while (cur != read_head_) {
289
297
    CHECK_NE(cur, write_head_);
290
297
    CHECK_EQ(cur->write_pos_, cur->read_pos_);
291
292
297
    Buffer* next = cur->next_;
293
297
    delete cur;
294
    cur = next;
295
  }
296
37
  prev->next_ = cur;
297
}
298
299
300
449074
size_t NodeBIO::IndexOf(char delim, size_t limit) {
301
449074
  size_t bytes_read = 0;
302
449074
  size_t max = Length() > limit ? limit : Length();
303
449074
  size_t left = limit;
304
449074
  Buffer* current = read_head_;
305
306
449084
  while (bytes_read < max) {
307
449074
    CHECK_LE(current->read_pos_, current->write_pos_);
308
449074
    size_t avail = current->write_pos_ - current->read_pos_;
309
449074
    if (avail > left)
310
370653
      avail = left;
311
312
    // Walk through data
313
449074
    char* tmp = current->data_ + current->read_pos_;
314
449074
    size_t off = 0;
315
60280646
    while (off < avail && *tmp != delim) {
316
29915786
      off++;
317
29915786
      tmp++;
318
    }
319
320
    // Move pointers
321
449074
    bytes_read += off;
322
449074
    left -= off;
323
324
    // Found `delim`
325
449074
    if (off != avail) {
326
      return bytes_read;
327
    }
328
329
    // Move to next buffer
330
10
    if (current->read_pos_ + avail == current->len_) {
331
      current = current->next_;
332
    }
333
  }
334
10
  CHECK_EQ(max, bytes_read);
335
336
  return max;
337
}
338
339
340
25009
void NodeBIO::Write(const char* data, size_t size) {
341
25009
  size_t offset = 0;
342
25009
  size_t left = size;
343
344
  // Allocate initial buffer if the ring is empty
345
25009
  TryAllocateForWrite(left);
346
347
50462
  while (left > 0) {
348
25453
    size_t to_write = left;
349
25453
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
350
25453
    size_t avail = write_head_->len_ - write_head_->write_pos_;
351
352
25453
    if (to_write > avail)
353
444
      to_write = avail;
354
355
    // Copy data
356
25453
    memcpy(write_head_->data_ + write_head_->write_pos_,
357
25453
           data + offset,
358
25453
           to_write);
359
360
    // Move pointers
361
25453
    left -= to_write;
362
25453
    offset += to_write;
363
25453
    length_ += to_write;
364
25453
    write_head_->write_pos_ += to_write;
365
25453
    CHECK_LE(write_head_->write_pos_, write_head_->len_);
366
367
    // Go to next buffer if there still are some bytes to write
368
25453
    if (left != 0) {
369
444
      CHECK_EQ(write_head_->write_pos_, write_head_->len_);
370
444
      TryAllocateForWrite(left);
371
444
      write_head_ = write_head_->next_;
372
373
      // Additionally, since we're moved to the next buffer, read head
374
      // may be moved as well.
375
      TryMoveReadHead();
376
    }
377
  }
378
25009
  CHECK_EQ(left, 0);
379
25009
}
380
381
382
2321
char* NodeBIO::PeekWritable(size_t* size) {
383
2321
  TryAllocateForWrite(*size);
384
385
2321
  size_t available = write_head_->len_ - write_head_->write_pos_;
386
2321
  if (*size != 0 && available > *size)
387
    available = *size;
388
  else
389
2321
    *size = available;
390
391
2321
  return write_head_->data_ + write_head_->write_pos_;
392
}
393
394
395
2142
void NodeBIO::Commit(size_t size) {
396
2142
  write_head_->write_pos_ += size;
397
2142
  length_ += size;
398
2142
  CHECK_LE(write_head_->write_pos_, write_head_->len_);
399
400
  // Allocate new buffer if write head is full,
401
  // and there're no other place to go
402
2142
  TryAllocateForWrite(0);
403
2142
  if (write_head_->write_pos_ == write_head_->len_) {
404
715
    write_head_ = write_head_->next_;
405
406
    // Additionally, since we're moved to the next buffer, read head
407
    // may be moved as well.
408
    TryMoveReadHead();
409
  }
410
2142
}
411
412
413
29916
void NodeBIO::TryAllocateForWrite(size_t hint) {
414
29916
  Buffer* w = write_head_;
415
29916
  Buffer* r = read_head_;
416
  // If write head is full, next buffer is either read head or not empty.
417
39449
  if (w == nullptr ||
418
10852
      (w->write_pos_ == w->len_ &&
419
2308
       (w->next_ == r || w->next_->write_pos_ != 0))) {
420
    size_t len = w == nullptr ? initial_ :
421
20713
                             kThroughputBufferLength;
422
20713
    if (len < hint)
423
17816
      len = hint;
424
20713
    Buffer* next = new Buffer(env_, len);
425
426
20713
    if (w == nullptr) {
427
20383
      next->next_ = next;
428
20383
      write_head_ = next;
429
20383
      read_head_ = next;
430
    } else {
431
330
      next->next_ = w->next_;
432
330
      w->next_ = next;
433
    }
434
  }
435
29916
}
436
437
438
3
void NodeBIO::Reset() {
439
3
  if (read_head_ == nullptr)
440
    return;
441
442
9
  while (read_head_->read_pos_ != read_head_->write_pos_) {
443
3
    CHECK(read_head_->write_pos_ > read_head_->read_pos_);
444
445
3
    length_ -= read_head_->write_pos_ - read_head_->read_pos_;
446
3
    read_head_->write_pos_ = 0;
447
3
    read_head_->read_pos_ = 0;
448
449
3
    read_head_ = read_head_->next_;
450
  }
451
3
  write_head_ = read_head_;
452
3
  CHECK_EQ(length_, 0);
453
}
454
455
456
39281
NodeBIO::~NodeBIO() {
457
19753
  if (read_head_ == nullptr)
458
    return;
459
460
  Buffer* current = read_head_;
461
  do {
462
19538
    Buffer* next = current->next_;
463
19538
    delete current;
464
19538
    current = next;
465
19538
  } while (current != read_head_);
466
467
19528
  read_head_ = nullptr;
468
19528
  write_head_ = nullptr;
469
19753
}
470
471
}  // namespace node