Skip to content

Commit 2bb364a

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 5d545d0 commit 2bb364a

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"
@@ -84,14 +86,33 @@ WASI::WASI(Environment* env,
8486
Local<Object> object,
8587
uvwasi_options_t* options) : BaseObject(env, object) {
8688
MakeWeak();
89+
alloc_info_ = MakeAllocator();
90+
options->allocator = &alloc_info_;
8791
CHECK_EQ(uvwasi_init(&uvw_, options), UVWASI_ESUCCESS);
8892
}
8993

9094

9195
WASI::~WASI() {
9296
uvwasi_destroy(&uvw_);
97+
CHECK_EQ(current_uvwasi_memory_, 0);
9398
}
9499

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

96117
void WASI::New(const FunctionCallbackInfo<Value>& args) {
97118
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)