diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index 3aab61e1b360fc..492e151832ea32 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -174,8 +174,7 @@ static void ares_poll_close_cb(uv_handle_t* watcher) { /* Allocates and returns a new node_ares_task */ static node_ares_task* ares_task_create(Environment* env, ares_socket_t sock) { - node_ares_task* task = - static_cast(node::Malloc(sizeof(*task))); + auto task = node::UncheckedMalloc(1); if (task == nullptr) { /* Out of memory. */ diff --git a/src/node.cc b/src/node.cc index be0c42b19f7b39..5ced5a3db62461 100644 --- a/src/node.cc +++ b/src/node.cc @@ -183,6 +183,8 @@ bool trace_warnings = false; // that is used by lib/module.js bool config_preserve_symlinks = false; +bool v8_initialized = false; + // process-relative uptime base, initialized at start-up static double prog_start_time; static bool debugger_running; @@ -979,9 +981,9 @@ Local WinapiErrnoException(Isolate* isolate, void* ArrayBufferAllocator::Allocate(size_t size) { if (zero_fill_field_ || zero_fill_all_buffers) - return node::Calloc(size, 1); + return node::UncheckedCalloc(size); else - return node::Malloc(size); + return node::UncheckedMalloc(size); } static bool DomainHasErrorHandler(const Environment* env, @@ -4490,6 +4492,7 @@ int Start(int argc, char** argv) { v8_platform.Initialize(v8_thread_pool_size); V8::Initialize(); + v8_initialized = true; int exit_code = 1; { @@ -4503,6 +4506,7 @@ int Start(int argc, char** argv) { StartNodeInstance(&instance_data); exit_code = instance_data.exit_code(); } + v8_initialized = false; V8::Dispose(); v8_platform.Dispose(); diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 55004a36b7bfc8..467a6e88474b39 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -49,9 +49,6 @@ THROW_AND_RETURN_IF_OOB(end <= end_max); \ size_t length = end - start; -#define BUFFER_MALLOC(length) \ - zero_fill_all_buffers ? node::Calloc(length, 1) : node::Malloc(length) - #if defined(__GNUC__) || defined(__clang__) #define BSWAP_INTRINSIC_2(x) __builtin_bswap16(x) #define BSWAP_INTRINSIC_4(x) __builtin_bswap32(x) @@ -89,6 +86,15 @@ namespace node { // if true, all Buffer and SlowBuffer instances will automatically zero-fill bool zero_fill_all_buffers = false; +namespace { + +inline void* BufferMalloc(size_t length) { + return zero_fill_all_buffers ? node::UncheckedCalloc(length) : + node::UncheckedMalloc(length); +} + +} // namespace + namespace Buffer { using v8::ArrayBuffer; @@ -266,7 +272,7 @@ MaybeLocal New(Isolate* isolate, char* data = nullptr; if (length > 0) { - data = static_cast(BUFFER_MALLOC(length)); + data = static_cast(BufferMalloc(length)); if (data == nullptr) return Local(); @@ -278,8 +284,7 @@ MaybeLocal New(Isolate* isolate, free(data); data = nullptr; } else if (actual < length) { - data = static_cast(node::Realloc(data, actual)); - CHECK_NE(data, nullptr); + data = node::Realloc(data, actual); } } @@ -312,7 +317,7 @@ MaybeLocal New(Environment* env, size_t length) { void* data; if (length > 0) { - data = BUFFER_MALLOC(length); + data = BufferMalloc(length); if (data == nullptr) return Local(); } else { @@ -357,7 +362,7 @@ MaybeLocal Copy(Environment* env, const char* data, size_t length) { void* new_data; if (length > 0) { CHECK_NE(data, nullptr); - new_data = node::Malloc(length); + new_data = node::UncheckedMalloc(length); if (new_data == nullptr) return Local(); memcpy(new_data, data, length); @@ -1080,7 +1085,7 @@ void IndexOfString(const FunctionCallbackInfo& args) { offset, is_forward); } else if (enc == LATIN1) { - uint8_t* needle_data = static_cast(node::Malloc(needle_length)); + uint8_t* needle_data = node::UncheckedMalloc(needle_length); if (needle_data == nullptr) { return args.GetReturnValue().Set(-1); } diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 7997d2113b4493..7ad6eceeecc0f7 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -2279,8 +2279,7 @@ int SSLWrap::TLSExtStatusCallback(SSL* s, void* arg) { size_t len = Buffer::Length(obj); // OpenSSL takes control of the pointer after accepting it - char* data = reinterpret_cast(node::Malloc(len)); - CHECK_NE(data, nullptr); + char* data = node::Malloc(len); memcpy(data, resp, len); if (!SSL_set_tlsext_status_ocsp_resp(s, data, len)) @@ -3330,8 +3329,7 @@ bool CipherBase::GetAuthTag(char** out, unsigned int* out_len) const { if (initialised_ || kind_ != kCipher || !auth_tag_) return false; *out_len = auth_tag_len_; - *out = static_cast(node::Malloc(auth_tag_len_)); - CHECK_NE(*out, nullptr); + *out = node::Malloc(auth_tag_len_); memcpy(*out, auth_tag_, auth_tag_len_); return true; } @@ -4906,8 +4904,7 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo& args) { // NOTE: field_size is in bits int field_size = EC_GROUP_get_degree(ecdh->group_); size_t out_len = (field_size + 7) / 8; - char* out = static_cast(node::Malloc(out_len)); - CHECK_NE(out, nullptr); + char* out = node::Malloc(out_len); int r = ECDH_compute_key(out, out_len, pub, ecdh->key_, nullptr); EC_POINT_free(pub); @@ -4942,8 +4939,7 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo& args) { if (size == 0) return env->ThrowError("Failed to get public key length"); - unsigned char* out = static_cast(node::Malloc(size)); - CHECK_NE(out, nullptr); + unsigned char* out = node::Malloc(size); int r = EC_POINT_point2oct(ecdh->group_, pub, form, out, size, nullptr); if (r != size) { @@ -4968,8 +4964,7 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo& args) { return env->ThrowError("Failed to get ECDH private key"); int size = BN_num_bytes(b); - unsigned char* out = static_cast(node::Malloc(size)); - CHECK_NE(out, nullptr); + unsigned char* out = node::Malloc(size); if (size != BN_bn2bin(b, out)) { free(out); @@ -5099,10 +5094,8 @@ class PBKDF2Request : public AsyncWrap { saltlen_(saltlen), salt_(salt), keylen_(keylen), - key_(static_cast(node::Malloc(keylen))), + key_(node::Malloc(keylen)), iter_(iter) { - if (key() == nullptr) - FatalError("node::PBKDF2Request()", "Out of Memory"); Wrap(object, this); } @@ -5262,10 +5255,7 @@ void PBKDF2(const FunctionCallbackInfo& args) { THROW_AND_RETURN_IF_NOT_BUFFER(args[1], "Salt"); - pass = static_cast(node::Malloc(passlen)); - if (pass == nullptr) { - FatalError("node::PBKDF2()", "Out of Memory"); - } + pass = node::Malloc(passlen); memcpy(pass, Buffer::Data(args[0]), passlen); saltlen = Buffer::Length(args[1]); @@ -5274,10 +5264,7 @@ void PBKDF2(const FunctionCallbackInfo& args) { goto err; } - salt = static_cast(node::Malloc(saltlen)); - if (salt == nullptr) { - FatalError("node::PBKDF2()", "Out of Memory"); - } + salt = node::Malloc(saltlen); memcpy(salt, Buffer::Data(args[1]), saltlen); if (!args[2]->IsNumber()) { @@ -5367,9 +5354,7 @@ class RandomBytesRequest : public AsyncWrap { : AsyncWrap(env, object, AsyncWrap::PROVIDER_CRYPTO), error_(0), size_(size), - data_(static_cast(node::Malloc(size))) { - if (data() == nullptr) - FatalError("node::RandomBytesRequest()", "Out of Memory"); + data_(node::Malloc(size)) { Wrap(object, this); } @@ -5592,13 +5577,9 @@ void GetCurves(const FunctionCallbackInfo& args) { const size_t num_curves = EC_get_builtin_curves(nullptr, 0); Local arr = Array::New(env->isolate(), num_curves); EC_builtin_curve* curves; - size_t alloc_size; if (num_curves) { - alloc_size = sizeof(*curves) * num_curves; - curves = static_cast(node::Malloc(alloc_size)); - - CHECK_NE(curves, nullptr); + curves = node::Malloc(num_curves); if (EC_get_builtin_curves(curves, num_curves)) { for (size_t i = 0; i < num_curves; i++) { diff --git a/src/node_internals.h b/src/node_internals.h index 8af4adb053a4f7..72888ef36d519e 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -37,6 +37,9 @@ namespace node { // that is used by lib/module.js extern bool config_preserve_symlinks; +// Tells whether it is safe to call v8::Isolate::GetCurrent(). +extern bool v8_initialized; + // Forward declaration class Environment; @@ -160,7 +163,7 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { virtual void* Allocate(size_t size); // Defined in src/node.cc virtual void* AllocateUninitialized(size_t size) - { return node::Malloc(size); } + { return node::UncheckedMalloc(size); } virtual void Free(void* data, size_t) { free(data); } private: diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index d294b641ffa444..ac656505503b22 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -148,14 +148,8 @@ void StreamWrap::OnAlloc(uv_handle_t* handle, void StreamWrap::OnAllocImpl(size_t size, uv_buf_t* buf, void* ctx) { - buf->base = static_cast(node::Malloc(size)); + buf->base = node::Malloc(size); buf->len = size; - - if (buf->base == nullptr && size > 0) { - FatalError( - "node::StreamWrap::DoAlloc(size_t, uv_buf_t*, void*)", - "Out Of Memory"); - } } @@ -204,8 +198,8 @@ void StreamWrap::OnReadImpl(ssize_t nread, return; } - char* base = static_cast(node::Realloc(buf->base, nread)); CHECK_LE(static_cast(nread), buf->len); + char* base = node::Realloc(buf->base, nread); if (pending == UV_TCP) { pending_obj = AcceptHandle(env, wrap); diff --git a/src/string_bytes.cc b/src/string_bytes.cc index fa641af7469d07..d9e8b97114e2cd 100644 --- a/src/string_bytes.cc +++ b/src/string_bytes.cc @@ -53,8 +53,7 @@ class ExternString: public ResourceType { if (length == 0) return scope.Escape(String::Empty(isolate)); - TypeName* new_data = - static_cast(node::Malloc(length * sizeof(*new_data))); + TypeName* new_data = node::UncheckedMalloc(length); if (new_data == nullptr) { return Local(); } @@ -624,7 +623,7 @@ Local StringBytes::Encode(Isolate* isolate, case ASCII: if (contains_non_ascii(buf, buflen)) { - char* out = static_cast(node::Malloc(buflen)); + char* out = node::UncheckedMalloc(buflen); if (out == nullptr) { return Local(); } @@ -659,7 +658,7 @@ Local StringBytes::Encode(Isolate* isolate, case BASE64: { size_t dlen = base64_encoded_size(buflen); - char* dst = static_cast(node::Malloc(dlen)); + char* dst = node::UncheckedMalloc(dlen); if (dst == nullptr) { return Local(); } @@ -678,7 +677,7 @@ Local StringBytes::Encode(Isolate* isolate, case HEX: { size_t dlen = buflen * 2; - char* dst = static_cast(node::Malloc(dlen)); + char* dst = node::UncheckedMalloc(dlen); if (dst == nullptr) { return Local(); } diff --git a/src/tls_wrap.cc b/src/tls_wrap.cc index d1b1aeccdd95b0..d56128fec6c5ce 100644 --- a/src/tls_wrap.cc +++ b/src/tls_wrap.cc @@ -661,8 +661,7 @@ void TLSWrap::OnReadImpl(ssize_t nread, void TLSWrap::OnAllocSelf(size_t suggested_size, uv_buf_t* buf, void* ctx) { - buf->base = static_cast(node::Malloc(suggested_size)); - CHECK_NE(buf->base, nullptr); + buf->base = node::Malloc(suggested_size); buf->len = suggested_size; } diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index c8009a5276e228..30113b7a8e3201 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -374,13 +374,8 @@ void UDPWrap::OnSend(uv_udp_send_t* req, int status) { void UDPWrap::OnAlloc(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) { - buf->base = static_cast(node::Malloc(suggested_size)); + buf->base = node::Malloc(suggested_size); buf->len = suggested_size; - - if (buf->base == nullptr && suggested_size > 0) { - FatalError("node::UDPWrap::OnAlloc(uv_handle_t*, size_t, uv_buf_t*)", - "Out Of Memory"); - } } @@ -416,7 +411,7 @@ void UDPWrap::OnRecv(uv_udp_t* handle, return; } - char* base = static_cast(node::Realloc(buf->base, nread)); + char* base = node::UncheckedRealloc(buf->base, nread); argv[2] = Buffer::New(env, base, nread).ToLocalChecked(); argv[3] = AddressToJS(env, addr); wrap->MakeCallback(env->onmessage_string(), arraysize(argv), argv); diff --git a/src/util-inl.h b/src/util-inl.h index 9357f675021367..51adb816926e52 100644 --- a/src/util-inl.h +++ b/src/util-inl.h @@ -229,6 +229,14 @@ bool StringEqualNoCaseN(const char* a, const char* b, size_t length) { return true; } +inline size_t MultiplyWithOverflowCheck(size_t a, size_t b) { + size_t ret = a * b; + if (a != 0) + CHECK_EQ(b, ret / a); + + return ret; +} + // These should be used in our code as opposed to the native // versions as they abstract out some platform and or // compiler version specific functionality. @@ -236,25 +244,59 @@ bool StringEqualNoCaseN(const char* a, const char* b, size_t length) { // that the standard allows them to either return a unique pointer or a // nullptr for zero-sized allocation requests. Normalize by always using // a nullptr. -void* Realloc(void* pointer, size_t size) { - if (size == 0) { +template +T* UncheckedRealloc(T* pointer, size_t n) { + size_t full_size = MultiplyWithOverflowCheck(sizeof(T), n); + + if (full_size == 0) { free(pointer); return nullptr; } - return realloc(pointer, size); + + void* allocated = realloc(pointer, full_size); + + if (UNLIKELY(allocated == nullptr)) { + // Tell V8 that memory is low and retry. + LowMemoryNotification(); + allocated = realloc(pointer, full_size); + } + + return static_cast(allocated); } // As per spec realloc behaves like malloc if passed nullptr. -void* Malloc(size_t size) { - if (size == 0) size = 1; - return Realloc(nullptr, size); +template +T* UncheckedMalloc(size_t n) { + if (n == 0) n = 1; + return UncheckedRealloc(nullptr, n); } -void* Calloc(size_t n, size_t size) { +template +T* UncheckedCalloc(size_t n) { if (n == 0) n = 1; - if (size == 0) size = 1; - CHECK_GE(n * size, n); // Overflow guard. - return calloc(n, size); + MultiplyWithOverflowCheck(sizeof(T), n); + return static_cast(calloc(n, sizeof(T))); +} + +template +T* Realloc(T* pointer, size_t n) { + T* ret = UncheckedRealloc(pointer, n); + if (n > 0) CHECK_NE(ret, nullptr); + return ret; +} + +template +T* Malloc(size_t n) { + T* ret = UncheckedMalloc(n); + if (n > 0) CHECK_NE(ret, nullptr); + return ret; +} + +template +T* Calloc(size_t n) { + T* ret = UncheckedCalloc(n); + if (n > 0) CHECK_NE(ret, nullptr); + return ret; } } // namespace node diff --git a/src/util.cc b/src/util.cc index 7ce99d5c76aa93..9fb5c3fd2855d3 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1,6 +1,7 @@ #include "util.h" #include "string_bytes.h" #include "node_buffer.h" +#include "node_internals.h" #include namespace node { @@ -76,4 +77,13 @@ BufferValue::BufferValue(Isolate* isolate, Local value) { } } +void LowMemoryNotification() { + if (v8_initialized) { + auto isolate = v8::Isolate::GetCurrent(); + if (isolate != nullptr) { + isolate->LowMemoryNotification(); + } + } +} + } // namespace node diff --git a/src/util.h b/src/util.h index eb99bb74e5d0de..25f2eb01783144 100644 --- a/src/util.h +++ b/src/util.h @@ -22,9 +22,32 @@ namespace node { // that the standard allows them to either return a unique pointer or a // nullptr for zero-sized allocation requests. Normalize by always using // a nullptr. -inline void* Realloc(void* pointer, size_t size); -inline void* Malloc(size_t size); -inline void* Calloc(size_t n, size_t size); +template +inline T* UncheckedRealloc(T* pointer, size_t n); +template +inline T* UncheckedMalloc(size_t n); +template +inline T* UncheckedCalloc(size_t n); + +// Same things, but aborts immediately instead of returning nullptr when +// no memory is available. +template +inline T* Realloc(T* pointer, size_t n); +template +inline T* Malloc(size_t n); +template +inline T* Calloc(size_t n); + +// Shortcuts for char*. +inline char* Malloc(size_t n) { return Malloc(n); } +inline char* Calloc(size_t n) { return Calloc(n); } +inline char* UncheckedMalloc(size_t n) { return UncheckedMalloc(n); } +inline char* UncheckedCalloc(size_t n) { return UncheckedCalloc(n); } + +// Used by the allocation functions when allocation fails. +// Thin wrapper around v8::Isolate::LowMemoryNotification() that checks +// whether V8 is initialized. +void LowMemoryNotification(); #ifdef __GNUC__ #define NO_RETURN __attribute__((noreturn)) @@ -285,11 +308,7 @@ class MaybeStackBuffer { if (storage <= kStackStorageSize) { buf_ = buf_st_; } else { - // Guard against overflow. - CHECK_LE(storage, sizeof(T) * storage); - - buf_ = static_cast(Malloc(sizeof(T) * storage)); - CHECK_NE(buf_, nullptr); + buf_ = Malloc(storage); } // Remember how much was allocated to check against that in SetLength(). diff --git a/test/cctest/util.cc b/test/cctest/util.cc index 79f1524660b213..434eeba96f8445 100644 --- a/test/cctest/util.cc +++ b/test/cctest/util.cc @@ -90,16 +90,38 @@ TEST(UtilTest, ToLower) { EXPECT_EQ('a', ToLower('A')); } +namespace node { + void LowMemoryNotification() {} +} + TEST(UtilTest, Malloc) { using node::Malloc; + EXPECT_NE(nullptr, Malloc(0)); + EXPECT_NE(nullptr, Malloc(1)); EXPECT_NE(nullptr, Malloc(0)); EXPECT_NE(nullptr, Malloc(1)); } TEST(UtilTest, Calloc) { using node::Calloc; - EXPECT_NE(nullptr, Calloc(0, 0)); - EXPECT_NE(nullptr, Calloc(1, 0)); - EXPECT_NE(nullptr, Calloc(0, 1)); - EXPECT_NE(nullptr, Calloc(1, 1)); -} \ No newline at end of file + EXPECT_NE(nullptr, Calloc(0)); + EXPECT_NE(nullptr, Calloc(1)); + EXPECT_NE(nullptr, Calloc(0)); + EXPECT_NE(nullptr, Calloc(1)); +} + +TEST(UtilTest, UncheckedMalloc) { + using node::UncheckedMalloc; + EXPECT_NE(nullptr, UncheckedMalloc(0)); + EXPECT_NE(nullptr, UncheckedMalloc(1)); + EXPECT_NE(nullptr, UncheckedMalloc(0)); + EXPECT_NE(nullptr, UncheckedMalloc(1)); +} + +TEST(UtilTest, UncheckedCalloc) { + using node::UncheckedCalloc; + EXPECT_NE(nullptr, UncheckedCalloc(0)); + EXPECT_NE(nullptr, UncheckedCalloc(1)); + EXPECT_NE(nullptr, UncheckedCalloc(0)); + EXPECT_NE(nullptr, UncheckedCalloc(1)); +}