diff --git a/src/node.cc b/src/node.cc index 47a13077db0..21a9c9beefa 100644 --- a/src/node.cc +++ b/src/node.cc @@ -629,6 +629,77 @@ void* ArrayBufferAllocator::Allocate(size_t size) { return UncheckedMalloc(size); } +DebuggingArrayBufferAllocator::~DebuggingArrayBufferAllocator() { + CHECK(allocations_.empty()); +} + +void* DebuggingArrayBufferAllocator::Allocate(size_t size) { + Mutex::ScopedLock lock(mutex_); + void* data = ArrayBufferAllocator::Allocate(size); + RegisterPointerInternal(data, size); + return data; +} + +void* DebuggingArrayBufferAllocator::AllocateUninitialized(size_t size) { + Mutex::ScopedLock lock(mutex_); + void* data = ArrayBufferAllocator::AllocateUninitialized(size); + RegisterPointerInternal(data, size); + return data; +} + +void DebuggingArrayBufferAllocator::Free(void* data, size_t size) { + Mutex::ScopedLock lock(mutex_); + UnregisterPointerInternal(data, size); + ArrayBufferAllocator::Free(data, size); +} + +void* DebuggingArrayBufferAllocator::Reallocate(void* data, + size_t old_size, + size_t size) { + Mutex::ScopedLock lock(mutex_); + void* ret = ArrayBufferAllocator::Reallocate(data, old_size, size); + if (ret == nullptr) { + if (size == 0) // i.e. equivalent to free(). + UnregisterPointerInternal(data, old_size); + return nullptr; + } + + if (data != nullptr) { + auto it = allocations_.find(data); + CHECK_NE(it, allocations_.end()); + allocations_.erase(it); + } + + RegisterPointerInternal(ret, size); + return ret; +} + +void DebuggingArrayBufferAllocator::RegisterPointer(void* data, size_t size) { + Mutex::ScopedLock lock(mutex_); + RegisterPointerInternal(data, size); +} + +void DebuggingArrayBufferAllocator::UnregisterPointer(void* data, size_t size) { + Mutex::ScopedLock lock(mutex_); + UnregisterPointerInternal(data, size); +} + +void DebuggingArrayBufferAllocator::UnregisterPointerInternal(void* data, + size_t size) { + if (data == nullptr) return; + auto it = allocations_.find(data); + CHECK_NE(it, allocations_.end()); + CHECK_EQ(it->second, size); + allocations_.erase(it); +} + +void DebuggingArrayBufferAllocator::RegisterPointerInternal(void* data, + size_t size) { + if (data == nullptr) return; + CHECK_EQ(allocations_.count(data), 0); + allocations_[data] = size; +} + namespace { bool ShouldAbortOnUncaughtException(Isolate* isolate) { @@ -2805,7 +2876,10 @@ int EmitExit(Environment* env) { ArrayBufferAllocator* CreateArrayBufferAllocator() { - return new ArrayBufferAllocator(); + if (per_process_opts->debug_arraybuffer_allocations) + return new DebuggingArrayBufferAllocator(); + else + return new ArrayBufferAllocator(); } diff --git a/src/node_internals.h b/src/node_internals.h index 3f66787fee8..9f4fdf2e162 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -398,15 +398,38 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { public: inline uint32_t* zero_fill_field() { return &zero_fill_field_; } - virtual void* Allocate(size_t size); // Defined in src/node.cc - virtual void* AllocateUninitialized(size_t size) + void* Allocate(size_t size) override; // Defined in src/node.cc + void* AllocateUninitialized(size_t size) override { return node::UncheckedMalloc(size); } - virtual void Free(void* data, size_t) { free(data); } + void Free(void* data, size_t) override { free(data); } + virtual void* Reallocate(void* data, size_t old_size, size_t size) { + return static_cast( + UncheckedRealloc(static_cast(data), size)); + } + virtual void RegisterPointer(void* data, size_t size) {} + virtual void UnregisterPointer(void* data, size_t size) {} private: uint32_t zero_fill_field_ = 1; // Boolean but exposed as uint32 to JS land. }; +class DebuggingArrayBufferAllocator final : public ArrayBufferAllocator { + public: + ~DebuggingArrayBufferAllocator() override; + void* Allocate(size_t size) override; + void* AllocateUninitialized(size_t size) override; + void Free(void* data, size_t size) override; + void* Reallocate(void* data, size_t old_size, size_t size) override; + void RegisterPointer(void* data, size_t size) override; + void UnregisterPointer(void* data, size_t size) override; + + private: + void RegisterPointerInternal(void* data, size_t size); + void UnregisterPointerInternal(void* data, size_t size); + Mutex mutex_; + std::unordered_map allocations_; +}; + namespace Buffer { v8::MaybeLocal Copy(Environment* env, const char* data, size_t len); v8::MaybeLocal New(Environment* env, size_t size); diff --git a/src/node_options.cc b/src/node_options.cc index 5cce1179b79..c34e29fdda3 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -223,6 +223,10 @@ PerProcessOptionsParser::PerProcessOptionsParser() { "SlowBuffer instances", &PerProcessOptions::zero_fill_all_buffers, kAllowedInEnvironment); + AddOption("--debug-arraybuffer-allocations", + "", /* undocumented, only for debugging */ + &PerProcessOptions::debug_arraybuffer_allocations, + kAllowedInEnvironment); AddOption("--security-reverts", "", &PerProcessOptions::security_reverts); AddOption("--help", diff --git a/src/node_options.h b/src/node_options.h index ec0179d890f..c19099dfa2d 100644 --- a/src/node_options.h +++ b/src/node_options.h @@ -111,6 +111,7 @@ class PerProcessOptions { std::string trace_event_file_pattern = "node_trace.${rotation}.log"; int64_t v8_thread_pool_size = 4; bool zero_fill_all_buffers = false; + bool debug_arraybuffer_allocations = false; std::vector security_reverts; bool print_help = false;