Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2bb364a

Browse filesBrowse 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 <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 5d545d0 commit 2bb364a
Copy full SHA for 2bb364a

File tree

Expand file treeCollapse file tree

2 files changed

+32
-6
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+32
-6
lines changed
Open diff view settings
Collapse file

‎src/node_wasi.cc‎

Copy file name to clipboardExpand all lines: src/node_wasi.cc
+21Lines changed: 21 additions & 0 deletions
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());
Collapse file

‎src/node_wasi.h‎

Copy file name to clipboardExpand all lines: src/node_wasi.h
+11-6Lines changed: 11 additions & 6 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.