ArrayBuffer.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. // Implements ArrayBuffer according to Khronos spec.
  6. //----------------------------------------------------------------------------
  7. #pragma once
  8. namespace Js
  9. {
  10. class ArrayBufferParent;
  11. class ArrayBuffer;
  12. class SharedArrayBuffer;
  13. class ArrayBufferBase : public DynamicObject
  14. {
  15. protected:
  16. #if ENABLE_FAST_ARRAYBUFFER
  17. #define MAX_ASMJS_ARRAYBUFFER_LENGTH 0x100000000 // 4GB
  18. #define MAX_WASM__ARRAYBUFFER_LENGTH 0x200000000 // 8GB
  19. typedef void*(*AllocWrapperType)(size_t);
  20. #define AsmJsVirtualAllocator ((AllocWrapperType)Js::ArrayBuffer::AllocWrapper<MAX_ASMJS_ARRAYBUFFER_LENGTH>)
  21. #define WasmVirtualAllocator ((AllocWrapperType)Js::ArrayBuffer::AllocWrapper<MAX_WASM__ARRAYBUFFER_LENGTH>)
  22. template<size_t MaxVirtualSize = MAX_ASMJS_ARRAYBUFFER_LENGTH>
  23. static void* __cdecl AllocWrapper(DECLSPEC_GUARD_OVERFLOW size_t length)
  24. {
  25. LPVOID address = VirtualAlloc(nullptr, MaxVirtualSize, MEM_RESERVE, PAGE_NOACCESS);
  26. //throw out of memory
  27. if (!address)
  28. {
  29. return nullptr;
  30. }
  31. if (length == 0)
  32. {
  33. return address;
  34. }
  35. LPVOID arrayAddress = VirtualAlloc(address, length, MEM_COMMIT, PAGE_READWRITE);
  36. if (!arrayAddress)
  37. {
  38. VirtualFree(address, 0, MEM_RELEASE);
  39. return nullptr;
  40. }
  41. return arrayAddress;
  42. }
  43. static void FreeMemAlloc(Var ptr)
  44. {
  45. BOOL fSuccess = VirtualFree((LPVOID)ptr, 0, MEM_RELEASE);
  46. Assert(fSuccess);
  47. }
  48. #else
  49. static void* __cdecl AllocWrapper(DECLSPEC_GUARD_OVERFLOW size_t length)
  50. {
  51. // This allocator should never be used
  52. Js::Throw::FatalInternalError();
  53. }
  54. #define AsmJsVirtualAllocator Js::ArrayBuffer::AllocWrapper
  55. #define WasmVirtualAllocator Js::ArrayBuffer::AllocWrapper
  56. #endif
  57. public:
  58. DEFINE_VTABLE_CTOR_ABSTRACT(ArrayBufferBase, DynamicObject);
  59. virtual void MarshalToScriptContext(Js::ScriptContext * scriptContext) = 0;
  60. ArrayBufferBase(DynamicType *type) : DynamicObject(type), isDetached(false) { }
  61. bool IsDetached() { return isDetached; }
  62. #if ENABLE_TTD
  63. virtual void MarshalCrossSite_TTDInflate() = 0;
  64. #endif
  65. virtual bool IsArrayBuffer() = 0;
  66. virtual bool IsSharedArrayBuffer() = 0;
  67. virtual bool IsWebAssemblyArrayBuffer() { return false; }
  68. virtual ArrayBuffer * GetAsArrayBuffer() = 0;
  69. virtual SharedArrayBuffer * GetAsSharedArrayBuffer() { return nullptr; }
  70. virtual void AddParent(ArrayBufferParent* parent) { }
  71. virtual uint32 GetByteLength() const = 0;
  72. virtual BYTE* GetBuffer() const = 0;
  73. virtual bool IsValidVirtualBufferLength(uint length) const { return false; };
  74. static bool Is(Var value);
  75. static ArrayBufferBase* FromVar(Var value);
  76. static int GetIsDetachedOffset() { return offsetof(ArrayBufferBase, isDetached); }
  77. protected:
  78. Field(bool) isDetached;
  79. };
  80. class ArrayBuffer : public ArrayBufferBase
  81. {
  82. public:
  83. // we need to install cross-site thunk on the nested array buffer when marshaling
  84. // typed array.
  85. DEFINE_VTABLE_CTOR_ABSTRACT(ArrayBuffer, ArrayBufferBase);
  86. private:
  87. void DetachBufferFromParent(ArrayBufferParent* parent);
  88. public:
  89. template <typename FreeFN>
  90. class ArrayBufferDetachedState : public ArrayBufferDetachedStateBase
  91. {
  92. public:
  93. FreeFN* freeFunction;
  94. ArrayBufferDetachedState(BYTE* buffer, uint32 bufferLength, FreeFN* freeFunction, ArrayBufferAllocationType allocationType)
  95. : ArrayBufferDetachedStateBase(TypeIds_ArrayBuffer, buffer, bufferLength, allocationType),
  96. freeFunction(freeFunction)
  97. {}
  98. virtual void ClearSelfOnly() override
  99. {
  100. HeapDelete(this);
  101. }
  102. virtual void DiscardState() override
  103. {
  104. if (this->buffer != nullptr)
  105. {
  106. freeFunction(this->buffer);
  107. this->buffer = nullptr;
  108. }
  109. this->bufferLength = 0;
  110. }
  111. virtual void Discard() override
  112. {
  113. ClearSelfOnly();
  114. }
  115. };
  116. template <typename Allocator>
  117. ArrayBuffer(DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType * type, Allocator allocator);
  118. ArrayBuffer(byte* buffer, DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType * type);
  119. class EntryInfo
  120. {
  121. public:
  122. static FunctionInfo NewInstance;
  123. static FunctionInfo Slice;
  124. static FunctionInfo IsView;
  125. static FunctionInfo GetterByteLength;
  126. static FunctionInfo GetterSymbolSpecies;
  127. static FunctionInfo Transfer;
  128. };
  129. static Var NewInstance(RecyclableObject* function, CallInfo callInfo, ...);
  130. static Var EntrySlice(RecyclableObject* function, CallInfo callInfo, ...);
  131. static Var EntryIsView(RecyclableObject* function, CallInfo callInfo, ...);
  132. static Var EntryGetterByteLength(RecyclableObject* function, CallInfo callInfo, ...);
  133. static Var EntryGetterSymbolSpecies(RecyclableObject* function, CallInfo callInfo, ...);
  134. static Var EntryTransfer(RecyclableObject* function, CallInfo callInfo, ...);
  135. static bool Is(Var aValue);
  136. static ArrayBuffer* NewFromDetachedState(DetachedStateBase* state, JavascriptLibrary *library);
  137. static ArrayBuffer* FromVar(Var aValue);
  138. virtual BOOL GetDiagTypeString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext) override;
  139. virtual BOOL GetDiagValueString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext) override;
  140. ArrayBufferDetachedStateBase* DetachAndGetState();
  141. virtual uint32 GetByteLength() const override { return bufferLength; }
  142. virtual BYTE* GetBuffer() const override { return buffer; }
  143. static int GetByteLengthOffset() { return offsetof(ArrayBuffer, bufferLength); }
  144. static int GetBufferOffset() { return offsetof(ArrayBuffer, buffer); }
  145. virtual void AddParent(ArrayBufferParent* parent) override;
  146. #if _WIN64
  147. //maximum 2G -1 for amd64
  148. static const uint32 MaxArrayBufferLength = 0x7FFFFFFF;
  149. #else
  150. // maximum 1G to avoid arithmetic overflow.
  151. static const uint32 MaxArrayBufferLength = 1 << 30;
  152. #endif
  153. static const uint32 ParentsCleanupThreshold = 1000;
  154. virtual bool IsValidAsmJsBufferLength(uint length, bool forceCheck = false) { return false; }
  155. virtual bool IsArrayBuffer() override { return true; }
  156. virtual bool IsSharedArrayBuffer() override { return false; }
  157. virtual ArrayBuffer * GetAsArrayBuffer() override { return ArrayBuffer::FromVar(this); }
  158. static uint32 ToIndex(Var value, int32 errorCode, ScriptContext *scriptContext, uint32 MaxAllowedLength, bool checkSameValueZero = true);
  159. virtual ArrayBuffer * TransferInternal(DECLSPEC_GUARD_OVERFLOW uint32 newBufferLength) = 0;
  160. protected:
  161. void Detach();
  162. typedef void __cdecl FreeFn(void* ptr);
  163. virtual ArrayBufferDetachedStateBase* CreateDetachedState(BYTE* buffer, DECLSPEC_GUARD_OVERFLOW uint32 bufferLength) = 0;
  164. static uint32 GetIndexFromVar(Js::Var arg, uint32 length, ScriptContext* scriptContext);
  165. //In most cases, the ArrayBuffer will only have one parent
  166. Field(RecyclerWeakReference<ArrayBufferParent>*) primaryParent;
  167. struct OtherParents :public SList<RecyclerWeakReference<ArrayBufferParent>*, Recycler>
  168. {
  169. OtherParents(Recycler* recycler)
  170. :SList<RecyclerWeakReference<ArrayBufferParent>*, Recycler>(recycler), increasedCount(0)
  171. {
  172. }
  173. Field(uint) increasedCount;
  174. };
  175. Field(OtherParents*) otherParents;
  176. FieldNoBarrier(BYTE*) buffer; // Points to a heap allocated RGBA buffer, can be null
  177. Field(uint32) bufferLength; // Number of bytes allocated
  178. };
  179. class ArrayBufferParent : public ArrayObject
  180. {
  181. friend ArrayBuffer;
  182. friend ArrayBufferBase;
  183. private:
  184. Field(ArrayBufferBase*) arrayBuffer;
  185. protected:
  186. DEFINE_VTABLE_CTOR_ABSTRACT(ArrayBufferParent, ArrayObject);
  187. ArrayBufferParent(DynamicType * type, uint32 length, ArrayBufferBase* arrayBuffer)
  188. : ArrayObject(type, /*initSlots*/true, length),
  189. arrayBuffer(arrayBuffer)
  190. {
  191. arrayBuffer->AddParent(this);
  192. }
  193. public:
  194. ArrayBufferBase* GetArrayBuffer() const
  195. {
  196. return this->arrayBuffer;
  197. }
  198. #if ENABLE_TTD
  199. public:
  200. virtual void MarkVisitKindSpecificPtrs(TTD::SnapshotExtractor* extractor) override;
  201. virtual void ProcessCorePaths() override;
  202. #endif
  203. };
  204. // Normally we use malloc/free; for ArrayBuffer created from projection we need to use different allocator.
  205. class JavascriptArrayBuffer : public ArrayBuffer
  206. {
  207. protected:
  208. DEFINE_VTABLE_CTOR(JavascriptArrayBuffer, ArrayBuffer);
  209. DEFINE_MARSHAL_OBJECT_TO_SCRIPT_CONTEXT(JavascriptArrayBuffer);
  210. public:
  211. static JavascriptArrayBuffer* Create(DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType * type);
  212. static JavascriptArrayBuffer* Create(byte* buffer, DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType * type);
  213. virtual void Dispose(bool isShutdown) override;
  214. virtual void Finalize(bool isShutdown) override;
  215. static bool IsValidAsmJsBufferLengthAlgo(uint length, bool forceCheck);
  216. virtual bool IsValidAsmJsBufferLength(uint length, bool forceCheck = false) override;
  217. virtual bool IsValidVirtualBufferLength(uint length) const override;
  218. virtual ArrayBuffer * TransferInternal(DECLSPEC_GUARD_OVERFLOW uint32 newBufferLength) override;
  219. template<typename Func>
  220. void ReportDifferentialAllocation(uint32 newBufferLength, Func reportFailureFn);
  221. void ReportDifferentialAllocation(uint32 newBufferLength);
  222. protected:
  223. JavascriptArrayBuffer(DynamicType * type);
  224. virtual ArrayBufferDetachedStateBase* CreateDetachedState(BYTE* buffer, DECLSPEC_GUARD_OVERFLOW uint32 bufferLength) override;
  225. template<typename Allocator>
  226. JavascriptArrayBuffer(uint32 length, DynamicType * type, Allocator allocator): ArrayBuffer(length, type, allocator){}
  227. JavascriptArrayBuffer(uint32 length, DynamicType * type);
  228. JavascriptArrayBuffer(byte* buffer, uint32 length, DynamicType * type);
  229. #if ENABLE_TTD
  230. public:
  231. virtual TTD::NSSnapObjects::SnapObjectType GetSnapTag_TTD() const override;
  232. virtual void ExtractSnapObjectDataInto(TTD::NSSnapObjects::SnapObject* objData, TTD::SlabAllocator& alloc) override;
  233. #endif
  234. };
  235. class WebAssemblyArrayBuffer : public JavascriptArrayBuffer
  236. {
  237. template<typename Allocator>
  238. WebAssemblyArrayBuffer(uint32 length, DynamicType * type, Allocator allocator);
  239. WebAssemblyArrayBuffer(uint32 length, DynamicType * type);
  240. WebAssemblyArrayBuffer(byte* buffer, uint32 length, DynamicType * type);
  241. protected:
  242. DEFINE_VTABLE_CTOR(WebAssemblyArrayBuffer, JavascriptArrayBuffer);
  243. DEFINE_MARSHAL_OBJECT_TO_SCRIPT_CONTEXT(WebAssemblyArrayBuffer);
  244. public:
  245. static WebAssemblyArrayBuffer* Create(byte* buffer, DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType * type);
  246. WebAssemblyArrayBuffer* GrowMemory(DECLSPEC_GUARD_OVERFLOW uint32 newBufferLength);
  247. virtual bool IsValidVirtualBufferLength(uint length) const override;
  248. virtual ArrayBuffer * TransferInternal(DECLSPEC_GUARD_OVERFLOW uint32 newBufferLength) override;
  249. virtual bool IsWebAssemblyArrayBuffer() override { return true; }
  250. protected:
  251. virtual ArrayBufferDetachedStateBase* CreateDetachedState(BYTE* buffer, DECLSPEC_GUARD_OVERFLOW uint32 bufferLength) override;
  252. };
  253. // the memory must be allocated via CoTaskMemAlloc.
  254. class ProjectionArrayBuffer : public ArrayBuffer
  255. {
  256. protected:
  257. DEFINE_VTABLE_CTOR(ProjectionArrayBuffer, ArrayBuffer);
  258. DEFINE_MARSHAL_OBJECT_TO_SCRIPT_CONTEXT(ProjectionArrayBuffer);
  259. typedef void __stdcall FreeFn(LPVOID ptr);
  260. virtual ArrayBufferDetachedStateBase* CreateDetachedState(BYTE* buffer, DECLSPEC_GUARD_OVERFLOW uint32 bufferLength) override
  261. {
  262. return HeapNew(ArrayBufferDetachedState<FreeFn>, buffer, bufferLength, CoTaskMemFree, ArrayBufferAllocationType::CoTask);
  263. }
  264. public:
  265. // Create constructor. script engine creates a buffer allocated via CoTaskMemAlloc.
  266. static ProjectionArrayBuffer* Create(DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType * type);
  267. // take over ownership. a CoTaskMemAlloc'ed buffer passed in via projection.
  268. static ProjectionArrayBuffer* Create(byte* buffer, DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType * type);
  269. virtual void Dispose(bool isShutdown) override;
  270. virtual void Finalize(bool isShutdown) override {};
  271. virtual ArrayBuffer * TransferInternal(DECLSPEC_GUARD_OVERFLOW uint32 newBufferLength) override;
  272. private:
  273. ProjectionArrayBuffer(uint32 length, DynamicType * type);
  274. ProjectionArrayBuffer(byte* buffer, uint32 length, DynamicType * type);
  275. };
  276. // non-owning ArrayBuffer used for wrapping external data
  277. class ExternalArrayBuffer : public ArrayBuffer
  278. {
  279. protected:
  280. DEFINE_VTABLE_CTOR(ExternalArrayBuffer, ArrayBuffer);
  281. DEFINE_MARSHAL_OBJECT_TO_SCRIPT_CONTEXT(ExternalArrayBuffer);
  282. public:
  283. ExternalArrayBuffer(byte *buffer, DECLSPEC_GUARD_OVERFLOW uint32 length, DynamicType *type);
  284. virtual ArrayBuffer * TransferInternal(DECLSPEC_GUARD_OVERFLOW uint32 newBufferLength) override { Assert(UNREACHED); Throw::InternalError(); };
  285. protected:
  286. virtual ArrayBufferDetachedStateBase* CreateDetachedState(BYTE* buffer, DECLSPEC_GUARD_OVERFLOW uint32 bufferLength) override { Assert(UNREACHED); Throw::InternalError(); };
  287. #if ENABLE_TTD
  288. public:
  289. virtual TTD::NSSnapObjects::SnapObjectType GetSnapTag_TTD() const override;
  290. virtual void ExtractSnapObjectDataInto(TTD::NSSnapObjects::SnapObject* objData, TTD::SlabAllocator& alloc) override;
  291. #endif
  292. };
  293. }