Skip to content

Commit 0157e3e

Browse files
ofrobotsMylesBorins
authored andcommitted
src,deps: add ABI safe use of CheckMemoryPressure
CheckMemoryPressure cannot be used ABI-safely from v8.h. Add a alternate implementation of AdjustAmountOfExternalAllocatedMemory and then use that from Node. PR-URL: #24499 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Yang Guo <[email protected]> Reviewed-By: Myles Borins <[email protected]>
1 parent 5245d6a commit 0157e3e

11 files changed

+60
-17
lines changed

deps/v8/include/v8-version.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define V8_MAJOR_VERSION 6
1212
#define V8_MINOR_VERSION 2
1313
#define V8_BUILD_NUMBER 414
14-
#define V8_PATCH_LEVEL 71
14+
#define V8_PATCH_LEVEL 72
1515

1616
// Use 1 for candidates and 0 otherwise.
1717
// (Boolean macro values are not supported by all preprocessors.)

deps/v8/include/v8.h

+6
Original file line numberDiff line numberDiff line change
@@ -7207,6 +7207,12 @@ class V8_EXPORT Isolate {
72077207
V8_INLINE int64_t
72087208
AdjustAmountOfExternalAllocatedMemory(int64_t change_in_bytes);
72097209

7210+
/**
7211+
* This is a Node.js 8.x specific version of the function that uses
7212+
* CheckMemoryPressure.
7213+
*/
7214+
int64_t AdjustAmountOfExternalAllocatedMemoryCustom(int64_t change_in_bytes);
7215+
72107216
/**
72117217
* Returns the number of phantom handles without callbacks that were reset
72127218
* by the garbage collector since the last call to this function.

deps/v8/src/api.cc

+34
Original file line numberDiff line numberDiff line change
@@ -8879,6 +8879,40 @@ void Isolate::GetStackSample(const RegisterState& state, void** frames,
88798879
sample_info->external_callback_entry = nullptr;
88808880
}
88818881

8882+
int64_t Isolate::AdjustAmountOfExternalAllocatedMemoryCustom(
8883+
int64_t change_in_bytes) {
8884+
const int64_t kMemoryReducerActivationLimit = 1024 * 1024;
8885+
typedef internal::Internals I;
8886+
int64_t* external_memory = reinterpret_cast<int64_t*>(
8887+
reinterpret_cast<uint8_t*>(this) + I::kExternalMemoryOffset);
8888+
int64_t* external_memory_limit = reinterpret_cast<int64_t*>(
8889+
reinterpret_cast<uint8_t*>(this) + I::kExternalMemoryLimitOffset);
8890+
int64_t* external_memory_at_last_mc =
8891+
reinterpret_cast<int64_t*>(reinterpret_cast<uint8_t*>(this) +
8892+
I::kExternalMemoryAtLastMarkCompactOffset);
8893+
const int64_t amount = *external_memory + change_in_bytes;
8894+
8895+
*external_memory = amount;
8896+
8897+
int64_t allocation_diff_since_last_mc =
8898+
*external_memory_at_last_mc - *external_memory;
8899+
allocation_diff_since_last_mc = allocation_diff_since_last_mc < 0
8900+
? -allocation_diff_since_last_mc
8901+
: allocation_diff_since_last_mc;
8902+
if (allocation_diff_since_last_mc > kMemoryReducerActivationLimit) {
8903+
CheckMemoryPressure();
8904+
}
8905+
8906+
if (change_in_bytes < 0) {
8907+
*external_memory_limit += change_in_bytes;
8908+
}
8909+
8910+
if (change_in_bytes > 0 && amount > *external_memory_limit) {
8911+
ReportExternalAllocationLimitReached();
8912+
}
8913+
return *external_memory;
8914+
}
8915+
88828916
size_t Isolate::NumberOfPhantomHandleResetsSinceLastCall() {
88838917
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
88848918
size_t result = isolate->global_handles()->NumberOfPhantomHandleResets();

src/node_api.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -3326,7 +3326,7 @@ napi_status napi_adjust_external_memory(napi_env env,
33263326
CHECK_ENV(env);
33273327
CHECK_ARG(env, adjusted_value);
33283328

3329-
*adjusted_value = env->isolate->AdjustAmountOfExternalAllocatedMemory(
3329+
*adjusted_value = env->isolate->AdjustAmountOfExternalAllocatedMemoryCustom(
33303330
change_in_bytes);
33313331

33323332
return napi_clear_last_error(env);

src/node_buffer.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ CallbackInfo::CallbackInfo(Isolate* isolate,
143143
persistent_.SetWeak(this, WeakCallback, v8::WeakCallbackType::kParameter);
144144
persistent_.SetWrapperClassId(BUFFER_ID);
145145
persistent_.MarkIndependent();
146-
isolate->AdjustAmountOfExternalAllocatedMemory(sizeof(*this));
146+
isolate->AdjustAmountOfExternalAllocatedMemoryCustom(sizeof(*this));
147147
}
148148

149149

@@ -163,7 +163,7 @@ void CallbackInfo::WeakCallback(
163163
void CallbackInfo::WeakCallback(Isolate* isolate) {
164164
callback_(data_, hint_);
165165
int64_t change_in_bytes = -static_cast<int64_t>(sizeof(*this));
166-
isolate->AdjustAmountOfExternalAllocatedMemory(change_in_bytes);
166+
isolate->AdjustAmountOfExternalAllocatedMemoryCustom(change_in_bytes);
167167
}
168168

169169

src/node_crypto.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2789,7 +2789,7 @@ void SSLWrap<Base>::DestroySSL() {
27892789
return;
27902790

27912791
SSL_free(ssl_);
2792-
env_->isolate()->AdjustAmountOfExternalAllocatedMemory(-kExternalSize);
2792+
env_->isolate()->AdjustAmountOfExternalAllocatedMemoryCustom(-kExternalSize);
27932793
ssl_ = nullptr;
27942794
}
27952795

src/node_crypto.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,16 @@ class SecureContext : public BaseObject {
168168
cert_(nullptr),
169169
issuer_(nullptr) {
170170
MakeWeak<SecureContext>(this);
171-
env->isolate()->AdjustAmountOfExternalAllocatedMemory(kExternalSize);
171+
env->isolate()->AdjustAmountOfExternalAllocatedMemoryCustom(kExternalSize);
172172
}
173173

174174
void FreeCTXMem() {
175175
if (!ctx_) {
176176
return;
177177
}
178178

179-
env()->isolate()->AdjustAmountOfExternalAllocatedMemory(-kExternalSize);
179+
env()->isolate()->AdjustAmountOfExternalAllocatedMemoryCustom(
180+
-kExternalSize);
180181
SSL_CTX_free(ctx_);
181182
if (cert_ != nullptr)
182183
X509_free(cert_);
@@ -208,7 +209,7 @@ class SSLWrap {
208209
cert_cb_arg_(nullptr),
209210
cert_cb_running_(false) {
210211
ssl_ = SSL_new(sc->ctx_);
211-
env_->isolate()->AdjustAmountOfExternalAllocatedMemory(kExternalSize);
212+
env_->isolate()->AdjustAmountOfExternalAllocatedMemoryCustom(kExternalSize);
212213
CHECK_NE(ssl_, nullptr);
213214
}
214215

src/node_crypto_bio.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ class NodeBIO {
135135
next_(nullptr) {
136136
data_ = new char[len];
137137
if (env_ != nullptr)
138-
env_->isolate()->AdjustAmountOfExternalAllocatedMemory(len);
138+
env_->isolate()->AdjustAmountOfExternalAllocatedMemoryCustom(len);
139139
}
140140

141141
~Buffer() {
142142
delete[] data_;
143143
if (env_ != nullptr) {
144144
const int64_t len = static_cast<int64_t>(len_);
145-
env_->isolate()->AdjustAmountOfExternalAllocatedMemory(-len);
145+
env_->isolate()->AdjustAmountOfExternalAllocatedMemoryCustom(-len);
146146
}
147147
}
148148

src/node_process.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
168168
fields[0] = rss;
169169
fields[1] = v8_heap_stats.total_heap_size();
170170
fields[2] = v8_heap_stats.used_heap_size();
171-
fields[3] = isolate->AdjustAmountOfExternalAllocatedMemory(0);
171+
fields[3] = isolate->AdjustAmountOfExternalAllocatedMemoryCustom(0);
172172
}
173173

174174
// Most of the time, it's best to use `console.error` to write

src/node_zlib.cc

+6-4
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,14 @@ class ZCtx : public AsyncWrap {
109109
if (mode_ == DEFLATE || mode_ == GZIP || mode_ == DEFLATERAW) {
110110
(void)deflateEnd(&strm_);
111111
int64_t change_in_bytes = -static_cast<int64_t>(kDeflateContextSize);
112-
env()->isolate()->AdjustAmountOfExternalAllocatedMemory(change_in_bytes);
112+
env()->isolate()->AdjustAmountOfExternalAllocatedMemoryCustom(
113+
change_in_bytes);
113114
} else if (mode_ == INFLATE || mode_ == GUNZIP || mode_ == INFLATERAW ||
114115
mode_ == UNZIP) {
115116
(void)inflateEnd(&strm_);
116117
int64_t change_in_bytes = -static_cast<int64_t>(kInflateContextSize);
117-
env()->isolate()->AdjustAmountOfExternalAllocatedMemory(change_in_bytes);
118+
env()->isolate()->AdjustAmountOfExternalAllocatedMemoryCustom(
119+
change_in_bytes);
118120
}
119121
mode_ = NONE;
120122

@@ -536,15 +538,15 @@ class ZCtx : public AsyncWrap {
536538
ctx->memLevel_,
537539
ctx->strategy_);
538540
ctx->env()->isolate()
539-
->AdjustAmountOfExternalAllocatedMemory(kDeflateContextSize);
541+
->AdjustAmountOfExternalAllocatedMemoryCustom(kDeflateContextSize);
540542
break;
541543
case INFLATE:
542544
case GUNZIP:
543545
case INFLATERAW:
544546
case UNZIP:
545547
ctx->err_ = inflateInit2(&ctx->strm_, ctx->windowBits_);
546548
ctx->env()->isolate()
547-
->AdjustAmountOfExternalAllocatedMemory(kInflateContextSize);
549+
->AdjustAmountOfExternalAllocatedMemoryCustom(kInflateContextSize);
548550
break;
549551
default:
550552
UNREACHABLE();

src/string_bytes.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class ExternString: public ResourceType {
6666
public:
6767
~ExternString() override {
6868
free(const_cast<TypeName*>(data_));
69-
isolate()->AdjustAmountOfExternalAllocatedMemory(-byte_length());
69+
isolate()->AdjustAmountOfExternalAllocatedMemoryCustom(-byte_length());
7070
}
7171

7272
const TypeName* data() const override {
@@ -122,7 +122,7 @@ class ExternString: public ResourceType {
122122
data,
123123
length);
124124
MaybeLocal<Value> str = NewExternal(isolate, h_str);
125-
isolate->AdjustAmountOfExternalAllocatedMemory(h_str->byte_length());
125+
isolate->AdjustAmountOfExternalAllocatedMemoryCustom(h_str->byte_length());
126126

127127
if (str.IsEmpty()) {
128128
delete h_str;

0 commit comments

Comments
 (0)