| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 55f8689 commit fb03e57
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -145,6 +145,23 @@ for (var b of buf) | |||
| 145 | 145 | Additionally, the [`buf.values()`][], [`buf.keys()`][], and | |
| 146 | 146 | [`buf.entries()`][] methods can be used to create iterators. | |
| 147 | 147 | ||
| 148 | + ## The `--zero-fill-buffers` command line option | ||
| 149 | + | ||
| 150 | + Node.js can be started using the `--zero-fill-buffers` command line option to | ||
| 151 | + force all newly allocated `Buffer` and `SlowBuffer` instances created using | ||
| 152 | + either `new Buffer(size)` and `new SlowBuffer(size)` to be *automatically | ||
| 153 | + zero-filled* upon creation. Use of this flag *changes the default behavior* of | ||
| 154 | + these methods and *can have a significant impact* on performance. Use of the | ||
| 155 | + `--zero-fill-buffers` option is recommended only when absolutely necessary to | ||
| 156 | + enforce that newly allocated `Buffer` instances cannot contain potentially | ||
| 157 | + sensitive data. | ||
| 158 | + | ||
| 159 | + ``` | ||
| 160 | + $ node --zero-fill-buffers | ||
| 161 | + > Buffer(5); | ||
| 162 | + <Buffer 00 00 00 00 00> | ||
| 163 | + ``` | ||
| 164 | + | ||
| 148 | 165 | ## Class: Buffer | |
| 149 | 166 | ||
| 150 | 167 | The Buffer class is a global type for dealing with binary data directly. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -95,6 +95,10 @@ Process v8 profiler output generated using the v8 option \fB\-\-prof\fR | |||
| 95 | 95 | .BR \-\-v8\-options | |
| 96 | 96 | Print v8 command line options. | |
| 97 | 97 | ||
| 98 | + .TP | ||
| 99 | + .BR \-\-zero\-fill\-buffers | ||
| 100 | + Automatically zero-fills all newly allocated Buffer and SlowBuffer instances. | ||
| 101 | + | ||
| 98 | 102 | .TP | |
| 99 | 103 | .BR \-\-tls\-cipher\-list =\fIlist\fR | |
| 100 | 104 | Specify an alternative default TLS cipher list. (Requires Node.js to be built with crypto support. (Default)) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -947,7 +947,9 @@ Local<Value> WinapiErrnoException(Isolate* isolate, | |||
| 947 | 947 | ||
| 948 | 948 | ||
| 949 | 949 | void* ArrayBufferAllocator::Allocate(size_t size) { | |
| 950 | - if (env_ == nullptr || !env_->array_buffer_allocator_info()->no_zero_fill()) | ||
| 950 | + if (env_ == nullptr || | ||
| 951 | + !env_->array_buffer_allocator_info()->no_zero_fill() || | ||
| 952 | + zero_fill_all_buffers) | ||
| 951 | 953 | return calloc(size, 1); | |
| 952 | 954 | env_->array_buffer_allocator_info()->reset_fill_flag(); | |
| 953 | 955 | return malloc(size); | |
@@ -3283,6 +3285,8 @@ static void PrintHelp() { | |||
| 3283 | 3285 | "snapshots\n" | |
| 3284 | 3286 | " --prof-process process v8 profiler output generated\n" | |
| 3285 | 3287 | " using --prof\n" | |
| 3288 | + " --zero-fill-buffers automatically zero-fill all newly allocated\n" | ||
| 3289 | + " Buffer and SlowBuffer instances\n" | ||
| 3286 | 3290 | " --v8-options print v8 command line options\n" | |
| 3287 | 3291 | #if HAVE_OPENSSL | |
| 3288 | 3292 | " --tls-cipher-list=val use an alternative default TLS cipher list\n" | |
@@ -3422,6 +3426,8 @@ static void ParseArgs(int* argc, | |||
| 3422 | 3426 | } else if (strcmp(arg, "--prof-process") == 0) { | |
| 3423 | 3427 | prof_process = true; | |
| 3424 | 3428 | short_circuit = true; | |
| 3429 | + } else if (strcmp(arg, "--zero-fill-buffers") == 0) { | ||
| 3430 | + zero_fill_all_buffers = true; | ||
| 3425 | 3431 | } else if (strcmp(arg, "--v8-options") == 0) { | |
| 3426 | 3432 | new_v8_argv[new_v8_argc] = "--help"; | |
| 3427 | 3433 | new_v8_argc += 1; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -48,7 +48,14 @@ | |||
| 48 | 48 | CHECK_NOT_OOB(end <= end_max); \ | |
| 49 | 49 | size_t length = end - start; | |
| 50 | 50 | ||
| 51 | + #define BUFFER_MALLOC(length) \ | ||
| 52 | + zero_fill_all_buffers ? calloc(length, 1) : malloc(length) | ||
| 53 | + | ||
| 51 | 54 | namespace node { | |
| 55 | + | ||
| 56 | + // if true, all Buffer and SlowBuffer instances will automatically zero-fill | ||
| 57 | + bool zero_fill_all_buffers = false; | ||
| 58 | + | ||
| 52 | 59 | namespace Buffer { | |
| 53 | 60 | ||
| 54 | 61 | using v8::ArrayBuffer; | |
@@ -220,7 +227,7 @@ MaybeLocal<Object> New(Isolate* isolate, | |||
| 220 | 227 | // nullptr for zero-sized allocation requests. Normalize by always using | |
| 221 | 228 | // a nullptr. | |
| 222 | 229 | if (length > 0) { | |
| 223 | - data = static_cast<char*>(malloc(length)); | ||
| 230 | + data = static_cast<char*>(BUFFER_MALLOC(length)); | ||
| 224 | 231 | ||
| 225 | 232 | if (data == nullptr) | |
| 226 | 233 | return Local<Object>(); | |
@@ -266,7 +273,7 @@ MaybeLocal<Object> New(Environment* env, size_t length) { | |||
| 266 | 273 | ||
| 267 | 274 | void* data; | |
| 268 | 275 | if (length > 0) { | |
| 269 | - data = malloc(length); | ||
| 276 | + data = BUFFER_MALLOC(length); | ||
| 270 | 277 | if (data == nullptr) | |
| 271 | 278 | return Local<Object>(); | |
| 272 | 279 | } else { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,9 @@ | |||
| 5 | 5 | #include "v8.h" | |
| 6 | 6 | ||
| 7 | 7 | namespace node { | |
| 8 | + | ||
| 9 | + extern bool zero_fill_all_buffers; | ||
| 10 | + | ||
| 8 | 11 | namespace Buffer { | |
| 9 | 12 | ||
| 10 | 13 | static const unsigned int kMaxLength = | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,30 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + // Flags: --zero-fill-buffers | ||
| 3 | + | ||
| 4 | + // when using --zero-fill-buffers, every Buffer and SlowBuffer | ||
| 5 | + // instance must be zero filled upon creation | ||
| 6 | + | ||
| 7 | + require('../common'); | ||
| 8 | + const SlowBuffer = require('buffer').SlowBuffer; | ||
| 9 | + const assert = require('assert'); | ||
| 10 | + | ||
| 11 | + function isZeroFilled(buf) { | ||
| 12 | + for (let n = 0; n < buf.length; n++) | ||
| 13 | + if (buf[n] > 0) return false; | ||
| 14 | + return true; | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + // This can be somewhat unreliable because the | ||
| 18 | + // allocated memory might just already happen to | ||
| 19 | + // contain all zeroes. The test is run multiple | ||
| 20 | + // times to improve the reliability. | ||
| 21 | + for (let i = 0; i < 50; i++) { | ||
| 22 | + const bufs = [ | ||
| 23 | + SlowBuffer(20), | ||
| 24 | + Buffer(20), | ||
| 25 | + new SlowBuffer(20) | ||
| 26 | + ]; | ||
| 27 | + for (const buf of bufs) { | ||
| 28 | + assert(isZeroFilled(buf)); | ||
| 29 | + } | ||
| 30 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments