Skip to content

Commit 14269d1

Browse files
addaleaxtargos
authored andcommitted
wasi: use memory-tracking allocator
This: - Protects against memory leaks in uvwasi. - Allows tracking the allocated memory in heap dumps. PR-URL: #30745 Refs: https://github.com/nodejs/quic/blob/34ee0bc96f804c73cb22b2945a1a78f780938492/src/node_mem.h Refs: nodejs/quic#126 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 13fe9f7 commit 14269d1

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/node_wasi.cc

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "env-inl.h"
22
#include "base_object-inl.h"
33
#include "debug_utils.h"
4+
#include "memory_tracker-inl.h"
5+
#include "node_mem-inl.h"
46
#include "util-inl.h"
57
#include "node.h"
68
#include "uv.h"
@@ -85,14 +87,33 @@ WASI::WASI(Environment* env,
8587
Local<Object> object,
8688
uvwasi_options_t* options) : BaseObject(env, object) {
8789
MakeWeak();
90+
alloc_info_ = MakeAllocator();
91+
options->allocator = &alloc_info_;
8892
CHECK_EQ(uvwasi_init(&uvw_, options), UVWASI_ESUCCESS);
8993
}
9094

9195

9296
WASI::~WASI() {
9397
uvwasi_destroy(&uvw_);
98+
CHECK_EQ(current_uvwasi_memory_, 0);
9499
}
95100

101+
void WASI::MemoryInfo(MemoryTracker* tracker) const {
102+
tracker->TrackField("memory", memory_);
103+
tracker->TrackFieldWithSize("uvwasi_memory", current_uvwasi_memory_);
104+
}
105+
106+
void WASI::CheckAllocatedSize(size_t previous_size) const {
107+
CHECK_GE(current_uvwasi_memory_, previous_size);
108+
}
109+
110+
void WASI::IncreaseAllocatedSize(size_t size) {
111+
current_uvwasi_memory_ += size;
112+
}
113+
114+
void WASI::DecreaseAllocatedSize(size_t size) {
115+
current_uvwasi_memory_ -= size;
116+
}
96117

97118
void WASI::New(const FunctionCallbackInfo<Value>& args) {
98119
CHECK(args.IsConstructCall());

src/node_wasi.h

+11-6
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,22 @@
44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

66
#include "base_object.h"
7-
#include "memory_tracker-inl.h"
7+
#include "node_mem.h"
88
#include "uvwasi.h"
99

1010
namespace node {
1111
namespace wasi {
1212

1313

14-
class WASI : public BaseObject {
14+
class WASI : public BaseObject,
15+
public mem::NgLibMemoryManager<WASI, uvwasi_mem_t> {
1516
public:
1617
WASI(Environment* env,
1718
v8::Local<v8::Object> object,
1819
uvwasi_options_t* options);
1920
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
20-
void MemoryInfo(MemoryTracker* tracker) const override {
21-
/* TODO(cjihrig): Get memory consumption from uvwasi. */
22-
tracker->TrackField("memory", memory_);
23-
}
2421

22+
void MemoryInfo(MemoryTracker* tracker) const override;
2523
SET_MEMORY_INFO_NAME(WASI)
2624
SET_SELF_SIZE(WASI)
2725

@@ -79,6 +77,11 @@ class WASI : public BaseObject {
7977

8078
static void _SetMemory(const v8::FunctionCallbackInfo<v8::Value>& args);
8179

80+
// Implementation for mem::NgLibMemoryManager
81+
void CheckAllocatedSize(size_t previous_size) const;
82+
void IncreaseAllocatedSize(size_t size);
83+
void DecreaseAllocatedSize(size_t size);
84+
8285
private:
8386
~WASI() override;
8487
inline void readUInt8(char* memory, uint8_t* value, uint32_t offset);
@@ -92,6 +95,8 @@ class WASI : public BaseObject {
9295
uvwasi_errno_t backingStore(char** store, size_t* byte_length);
9396
uvwasi_t uvw_;
9497
v8::Global<v8::Object> memory_;
98+
uvwasi_mem_t alloc_info_;
99+
size_t current_uvwasi_memory_ = 0;
95100
};
96101

97102

0 commit comments

Comments
 (0)