forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayBuffer.h
More file actions
422 lines (352 loc) · 17.2 KB
/
Copy pathArrayBuffer.h
File metadata and controls
422 lines (352 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
// Implements ArrayBuffer according to Khronos spec.
//----------------------------------------------------------------------------
#pragma once
namespace Js
{
class ArrayBufferParent;
class ArrayBuffer;
class SharedArrayBuffer;
class ArrayBufferContentForDelayedFreeBase;
class ArrayBufferBase : public DynamicObject
{
protected:
#if ENABLE_FAST_ARRAYBUFFER
#define MAX_ASMJS_ARRAYBUFFER_LENGTH 0x100000000 // 4GB
#define MAX_WASM__ARRAYBUFFER_LENGTH 0x200000000 // 8GB
typedef void*(*AllocWrapperType)(size_t);
#define AsmJsVirtualAllocator ((AllocWrapperType)Js::ArrayBuffer::AllocWrapper<MAX_ASMJS_ARRAYBUFFER_LENGTH>)
#define WasmVirtualAllocator ((AllocWrapperType)Js::ArrayBuffer::AllocWrapper<MAX_WASM__ARRAYBUFFER_LENGTH>)
#else
#define AsmJsVirtualAllocator Js::ArrayBuffer::BadAllocCall
#define WasmVirtualAllocator Js::ArrayBuffer::BadAllocCall
static void* __cdecl BadAllocCall(DECLSPEC_GUARD_OVERFLOW size_t length)
{
// This allocator should never be used
Js::Throw::FatalInternalError();
}
#endif
#ifdef _WIN32
static void* __cdecl AllocWrapper(DECLSPEC_GUARD_OVERFLOW size_t length, size_t MaxVirtualSize)
{
LPVOID address = VirtualAlloc(nullptr, MaxVirtualSize, MEM_RESERVE, PAGE_NOACCESS);
//throw out of memory
if (!address)
{
return nullptr;
}
if (length == 0)
{
return address;
}
LPVOID arrayAddress = VirtualAlloc(address, length, MEM_COMMIT, PAGE_READWRITE);
if (!arrayAddress)
{
VirtualFree(address, 0, MEM_RELEASE);
return nullptr;
}
return arrayAddress;
}
template<size_t MaxVirtualSize>
static void* __cdecl AllocWrapper(DECLSPEC_GUARD_OVERFLOW size_t length)
{
return AllocWrapper(length, MaxVirtualSize);
}
static void __cdecl FreeMemAlloc(Var ptr)
{
BOOL fSuccess = VirtualFree((LPVOID)ptr, 0, MEM_RELEASE);
Assert(fSuccess);
}
#else
static void __cdecl FreeMemAlloc(Var ptr)
{
// This free function should never be used
Js::Throw::FatalInternalError();
}
#endif
public:
DEFINE_VTABLE_CTOR_ABSTRACT(ArrayBufferBase, DynamicObject);
virtual void MarshalToScriptContext(Js::ScriptContext * scriptContext) = 0;
ArrayBufferBase(DynamicType *type) :
DynamicObject(type),
isDetached(false),
infoBits(0),
externalized(false) { }
bool IsDetached() { return isDetached; }
#if ENABLE_TTD
virtual void MarshalCrossSite_TTDInflate() = 0;
#endif
virtual bool IsArrayBuffer() = 0;
virtual bool IsSharedArrayBuffer() = 0;
virtual bool IsWebAssemblyArrayBuffer() { return false; }
virtual ArrayBuffer * GetAsArrayBuffer() = 0;
virtual SharedArrayBuffer * GetAsSharedArrayBuffer() { return nullptr; }
virtual void AddParent(ArrayBufferParent* parent) { }
virtual uint32 GetByteLength() const = 0;
virtual BYTE* GetBuffer() const = 0;
virtual bool IsValidVirtualBufferLength(uint length) const { return false; };
char GetExtraInfoBits() { return infoBits; }
void SetExtraInfoBits(char info) { infoBits = info; }
static int GetIsDetachedOffset() { return offsetof(ArrayBufferBase, isDetached); }
void Externalize() { this->externalized = true; }
protected:
Field(bool) isDetached;
Field(bool) externalized;
Field(char) infoBits;
};
template <> bool VarIsImpl<ArrayBufferBase>(RecyclableObject* obj);
// This encapsulate buffer blob and the refCount.
class RefCountedBuffer
{
private:
FieldNoBarrier(BYTE*) buffer; // Points to a heap allocated RGBA buffer, can be null
// Addref/release counter for current buffer, this is needed hold the current buffer alive
Field(long) refCount;
public:
long AddRef();
long Release();
BYTE* GetBuffer() { return buffer; };
long GetRefCount() { return refCount; }
static int GetBufferOffset() { return offsetof(RefCountedBuffer, buffer); }
RefCountedBuffer(BYTE* b)
: buffer(b), refCount(1)
{ }
};
class ArrayBuffer : public ArrayBufferBase
{
public:
// we need to install cross-site thunk on the nested array buffer when marshaling
// typed array.
DEFINE_VTABLE_CTOR_ABSTRACT(ArrayBuffer, ArrayBufferBase);
private:
void DetachBufferFromParent(ArrayBufferParent* parent);
public:
template <typename FreeFN>
class ArrayBufferDetachedState : public ArrayBufferDetachedStateBase
{
public:
FreeFN freeFunction;
Recycler* recycler;
ArrayBufferDetachedState(RefCountedBuffer* buffer, uint32 bufferLength, FreeFN freeFunction, Recycler* r, ArrayBufferAllocationType allocationType)
: ArrayBufferDetachedStateBase(TypeIds_ArrayBuffer, buffer, bufferLength, allocationType),
recycler(r),
freeFunction(freeFunction)
{}
virtual void ClearSelfOnly() override
{
HeapDelete(this);
}
virtual void DiscardState() override
{
DiscardStateBase(freeFunction);
}
virtual void Discard() override
{
ClearSelfOnly();
}
};
template <typename Allocator>
ArrayBuffer(DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType * type, Allocator allocator);
ArrayBuffer(byte* buffer, DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType * type, bool isExternal = false);
ArrayBuffer(RefCountedBuffer* buffContent, DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType * type);
class EntryInfo
{
public:
static FunctionInfo NewInstance;
static FunctionInfo Slice;
static FunctionInfo IsView;
static FunctionInfo GetterByteLength;
static FunctionInfo GetterSymbolSpecies;
#if ENABLE_DEBUG_CONFIG_OPTIONS
static FunctionInfo Detach;
#endif
};
static Var NewInstance(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntrySlice(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryIsView(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryGetterByteLength(RecyclableObject* function, CallInfo callInfo, ...);
static Var EntryGetterSymbolSpecies(RecyclableObject* function, CallInfo callInfo, ...);
#if ENABLE_DEBUG_CONFIG_OPTIONS
static Var EntryDetach(RecyclableObject* function, CallInfo callInfo, ...);
#endif
static ArrayBuffer* NewFromDetachedState(DetachedStateBase* state, JavascriptLibrary *library);
virtual BOOL GetDiagTypeString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext) override;
virtual BOOL GetDiagValueString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext) override;
ArrayBufferDetachedStateBase* DetachAndGetState(bool queueForDelayFree = true);
virtual uint32 GetByteLength() const override;
virtual BYTE* GetBuffer() const override;
RefCountedBuffer *GetBufferContent() { return bufferContent; }
static int GetBufferContentsOffset() { return offsetof(ArrayBuffer, bufferContent); }
typedef void(__cdecl *FreeFn)(void*);
virtual FreeFn GetArrayBufferFreeFn() { return nullptr; }
static int GetByteLengthOffset() { return offsetof(ArrayBuffer, bufferLength); }
virtual void AddParent(ArrayBufferParent* parent) override;
void Detach();
#if defined(TARGET_64)
//maximum 2G -1 for amd64
static const uint32 MaxArrayBufferLength = 0x7FFFFFFF;
#else
// maximum 1G to avoid arithmetic overflow.
static const uint32 MaxArrayBufferLength = 1 << 30;
#endif
static const uint32 ParentsCleanupThreshold = 1000;
virtual bool IsValidAsmJsBufferLength(uint length, bool forceCheck = false) { return false; }
virtual bool IsArrayBuffer() override { return true; }
virtual bool IsSharedArrayBuffer() override { return false; }
virtual ArrayBuffer * GetAsArrayBuffer() override;
virtual ArrayBufferContentForDelayedFreeBase* CopyBufferContentForDelayedFree(RefCountedBuffer * content, DECLSPEC_GUARD_OVERFLOW uint32 bufferLength);
static uint32 ToIndex(Var value, int32 errorCode, ScriptContext *scriptContext, uint32 MaxAllowedLength, bool checkSameValueZero = true);
protected:
virtual void ReportExternalMemoryFree();
virtual ArrayBufferDetachedStateBase* CreateDetachedState(RefCountedBuffer * content, DECLSPEC_GUARD_OVERFLOW uint32 bufferLength) = 0;
// This function will be called from External buffer and projection buffer as they pass the buffer
virtual void ReleaseBufferContent();
//In most cases, the ArrayBuffer will only have one parent
Field(RecyclerWeakReference<ArrayBufferParent>*) primaryParent;
struct OtherParents :public SList<RecyclerWeakReference<ArrayBufferParent>*, Recycler>
{
OtherParents(Recycler* recycler)
:SList<RecyclerWeakReference<ArrayBufferParent>*, Recycler>(recycler), increasedCount(0)
{
}
Field(uint) increasedCount;
};
Field(OtherParents*) otherParents;
FieldNoBarrier(RefCountedBuffer *) bufferContent;
Field(uint32) bufferLength; // Number of bytes allocated
};
template <> inline bool VarIsImpl<ArrayBuffer>(RecyclableObject* obj)
{
return JavascriptOperators::GetTypeId(obj) == TypeIds_ArrayBuffer;
}
class ArrayBufferParent : public ArrayObject
{
friend ArrayBuffer;
friend ArrayBufferBase;
private:
Field(ArrayBufferBase*) arrayBuffer;
protected:
DEFINE_VTABLE_CTOR_ABSTRACT(ArrayBufferParent, ArrayObject);
ArrayBufferParent(DynamicType * type, uint32 length, ArrayBufferBase* arrayBuffer)
: ArrayObject(type, /*initSlots*/true, length),
arrayBuffer(arrayBuffer)
{
arrayBuffer->AddParent(this);
}
public:
ArrayBufferBase* GetArrayBuffer() const
{
return this->arrayBuffer;
}
#if ENABLE_TTD
public:
virtual void MarkVisitKindSpecificPtrs(TTD::SnapshotExtractor* extractor) override;
virtual void ProcessCorePaths() override;
#endif
};
// Normally we use malloc/free; for ArrayBuffer created from projection we need to use different allocator.
class JavascriptArrayBuffer : public ArrayBuffer
{
protected:
DEFINE_VTABLE_CTOR(JavascriptArrayBuffer, ArrayBuffer);
DEFINE_MARSHAL_OBJECT_TO_SCRIPT_CONTEXT(JavascriptArrayBuffer);
public:
static JavascriptArrayBuffer* Create(DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType * type);
static JavascriptArrayBuffer* Create(byte* buffer, DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType * type);
static JavascriptArrayBuffer* Create(RefCountedBuffer* buffer, DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType * type);
virtual void Dispose(bool isShutdown) override;
virtual void Finalize(bool isShutdown) override;
static bool IsValidAsmJsBufferLengthAlgo(uint length, bool forceCheck);
virtual bool IsValidAsmJsBufferLength(uint length, bool forceCheck = false) override;
virtual bool IsValidVirtualBufferLength(uint length) const override;
virtual FreeFn GetArrayBufferFreeFn() override;
protected:
JavascriptArrayBuffer(DynamicType * type);
virtual ArrayBufferDetachedStateBase* CreateDetachedState(RefCountedBuffer * content, DECLSPEC_GUARD_OVERFLOW uint32 bufferLength) override;
template<typename Allocator>
JavascriptArrayBuffer(uint32 length, DynamicType * type, Allocator allocator): ArrayBuffer(length, type, allocator){}
JavascriptArrayBuffer(uint32 length, DynamicType * type);
JavascriptArrayBuffer(byte* buffer, uint32 length, DynamicType * type);
JavascriptArrayBuffer(RefCountedBuffer* buffer, uint32 length, DynamicType * type);
#if ENABLE_TTD
public:
virtual TTD::NSSnapObjects::SnapObjectType GetSnapTag_TTD() const override;
virtual void ExtractSnapObjectDataInto(TTD::NSSnapObjects::SnapObject* objData, TTD::SlabAllocator& alloc) override;
#endif
};
#ifdef ENABLE_WASM
class WebAssemblyArrayBuffer : public JavascriptArrayBuffer
{
template<typename Allocator>
WebAssemblyArrayBuffer(uint32 length, DynamicType * type, Allocator allocator);
WebAssemblyArrayBuffer(uint32 length, DynamicType * type);
WebAssemblyArrayBuffer(byte* buffer, uint32 length, DynamicType * type);
protected:
DEFINE_VTABLE_CTOR(WebAssemblyArrayBuffer, JavascriptArrayBuffer);
DEFINE_MARSHAL_OBJECT_TO_SCRIPT_CONTEXT(WebAssemblyArrayBuffer);
public:
static WebAssemblyArrayBuffer* Create(byte* buffer, DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType * type);
WebAssemblyArrayBuffer* GrowMemory(uint32 newBufferLength);
virtual bool IsValidVirtualBufferLength(uint length) const override;
virtual bool IsWebAssemblyArrayBuffer() override { return true; }
protected:
virtual ArrayBufferDetachedStateBase* CreateDetachedState(RefCountedBuffer * content, DECLSPEC_GUARD_OVERFLOW uint32 bufferLength) override;
};
#endif
// the memory must be allocated via CoTaskMemAlloc.
class ProjectionArrayBuffer : public ArrayBuffer
{
protected:
DEFINE_VTABLE_CTOR(ProjectionArrayBuffer, ArrayBuffer);
DEFINE_MARSHAL_OBJECT_TO_SCRIPT_CONTEXT(ProjectionArrayBuffer);
typedef void (__stdcall *FreeFn)(LPVOID ptr);
virtual ArrayBufferDetachedStateBase* CreateDetachedState(RefCountedBuffer * content, DECLSPEC_GUARD_OVERFLOW uint32 bufferLength) override
{
return HeapNew(ArrayBufferDetachedState<FreeFn>, content, bufferLength, CoTaskMemFree, GetScriptContext()->GetRecycler(), ArrayBufferAllocationType::CoTask);
}
public:
// Create constructor. script engine creates a buffer allocated via CoTaskMemAlloc.
static ProjectionArrayBuffer* Create(DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType * type);
// take over ownership. a CoTaskMemAlloc'ed buffer passed in via projection.
static ProjectionArrayBuffer* Create(byte* buffer, DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType * type);
static ProjectionArrayBuffer* Create(RefCountedBuffer* buffer, DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType * type);
virtual ArrayBufferContentForDelayedFreeBase* CopyBufferContentForDelayedFree(RefCountedBuffer * content, DECLSPEC_GUARD_OVERFLOW uint32 bufferLength) override;
virtual void Dispose(bool isShutdown) override;
virtual void Finalize(bool isShutdown) override;
private:
ProjectionArrayBuffer(uint32 length, DynamicType * type);
ProjectionArrayBuffer(byte* buffer, uint32 length, DynamicType * type);
ProjectionArrayBuffer(RefCountedBuffer* buffer, uint32 length, DynamicType * type);
};
// non-owning ArrayBuffer used for wrapping external data
class ExternalArrayBuffer : public ArrayBuffer
{
protected:
DEFINE_VTABLE_CTOR(ExternalArrayBuffer, ArrayBuffer);
DEFINE_MARSHAL_OBJECT_TO_SCRIPT_CONTEXT(ExternalArrayBuffer);
public:
ExternalArrayBuffer(byte *buffer, DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType *type);
ExternalArrayBuffer(RefCountedBuffer *buffer, DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType *type);
static ExternalArrayBuffer* Create(RefCountedBuffer* buffer, DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType * type);
protected:
virtual ArrayBufferDetachedStateBase* CreateDetachedState(RefCountedBuffer* buffer, DECLSPEC_GUARD_OVERFLOW uint32 bufferLength) override;
virtual void ReportExternalMemoryFree() override;
#if ENABLE_TTD
public:
virtual TTD::NSSnapObjects::SnapObjectType GetSnapTag_TTD() const override;
virtual void ExtractSnapObjectDataInto(TTD::NSSnapObjects::SnapObject* objData, TTD::SlabAllocator& alloc) override;
#endif
};
class ExternalArrayBufferDetachedState : public ArrayBufferDetachedStateBase
{
public:
ExternalArrayBufferDetachedState(RefCountedBuffer* buffer, uint32 bufferLength);
virtual void ClearSelfOnly() override;
virtual void DiscardState() override;
virtual void Discard() override;
virtual ArrayBuffer* Create(JavascriptLibrary* library);
};
}