Skip to content

Commit a1bdacf

Browse files
committed
[MERGE #5429 @sethbrenith] Fix a case where ExternalArrayBuffer could report memory freed without ever reporting memory allocated
Merge pull request #5429 from sethbrenith:user/sethb/external-array-reporting Make ExternalArrayBuffer's behavior during Detach match its behavior during Create and Finalize: it should not tell the recycler any external memory was added or removed, because it has no direct control over the external allocation.
2 parents e782dc3 + 1bb0703 commit a1bdacf

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/Runtime/Library/ArrayBuffer.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,19 @@ namespace Js
217217
}
218218
}
219219

220+
void ArrayBuffer::ReportExternalMemoryFree()
221+
{
222+
Recycler* recycler = GetType()->GetLibrary()->GetRecycler();
223+
recycler->ReportExternalMemoryFree(bufferLength);
224+
}
225+
220226
void ArrayBuffer::Detach()
221227
{
222228
Assert(!this->isDetached);
223229

224230
// we are about to lose track of the buffer to another owner
225231
// report that we no longer own the memory
226-
Recycler* recycler = GetType()->GetLibrary()->GetRecycler();
227-
recycler->ReportExternalMemoryFree(bufferLength);
232+
ReportExternalMemoryFree();
228233

229234
this->buffer = nullptr;
230235
this->bufferLength = 0;
@@ -1090,6 +1095,11 @@ namespace Js
10901095
return HeapNew(ExternalArrayBufferDetachedState, buffer, bufferLength);
10911096
};
10921097

1098+
void ExternalArrayBuffer::ReportExternalMemoryFree()
1099+
{
1100+
// This type does not own the external memory, so don't ReportExternalMemoryFree like other ArrayBuffer types do
1101+
}
1102+
10931103
#if ENABLE_TTD
10941104
TTD::NSSnapObjects::SnapObjectType ExternalArrayBuffer::GetSnapTag_TTD() const
10951105
{

lib/Runtime/Library/ArrayBuffer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ namespace Js
196196
static uint32 ToIndex(Var value, int32 errorCode, ScriptContext *scriptContext, uint32 MaxAllowedLength, bool checkSameValueZero = true);
197197

198198
protected:
199+
virtual void ReportExternalMemoryFree();
199200
void Detach();
200201

201202
typedef void __cdecl FreeFn(void* ptr);
@@ -343,6 +344,7 @@ namespace Js
343344
static ExternalArrayBuffer* Create(byte* buffer, DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType * type);
344345
protected:
345346
virtual ArrayBufferDetachedStateBase* CreateDetachedState(BYTE* buffer, DECLSPEC_GUARD_OVERFLOW uint32 bufferLength) override;
347+
virtual void ReportExternalMemoryFree() override;
346348

347349
#if ENABLE_TTD
348350
public:

0 commit comments

Comments
 (0)