AtomicsObject.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. #include "RuntimeLibraryPch.h"
  6. #define ATOMICS_FUNCTION_ENTRY_CHECKS(length, methodName) \
  7. PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault); \
  8. ARGUMENTS(args, callInfo); \
  9. ScriptContext* scriptContext = function->GetScriptContext(); \
  10. Assert(!(callInfo.Flags & CallFlags_New)); \
  11. if (args.Info.Count <= length) \
  12. { \
  13. JavascriptError::ThrowRangeError(scriptContext, JSERR_WinRTFunction_TooFewArguments, _u(methodName)); \
  14. } \
  15. namespace Js
  16. {
  17. Var AtomicsObject::ValidateSharedIntegerTypedArray(Var typedArray, ScriptContext *scriptContext, bool onlyInt32)
  18. {
  19. if (!TypedArrayBase::Is(typedArray))
  20. {
  21. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedTypedArrayObject);
  22. }
  23. TypeId typeId = JavascriptOperators::GetTypeId(typedArray);
  24. if (onlyInt32)
  25. {
  26. if (typeId != TypeIds_Int32Array)
  27. {
  28. JavascriptError::ThrowTypeError(scriptContext, JSERR_InvalidOperationOnTypedArray);
  29. }
  30. }
  31. else
  32. {
  33. if (!(typeId == TypeIds_Int8Array || typeId == TypeIds_Uint8Array || typeId == TypeIds_Int16Array ||
  34. typeId == TypeIds_Uint16Array || typeId == TypeIds_Int32Array || typeId == TypeIds_Uint32Array))
  35. {
  36. JavascriptError::ThrowTypeError(scriptContext, JSERR_InvalidOperationOnTypedArray);
  37. }
  38. }
  39. TypedArrayBase *typedArrayBase = TypedArrayBase::UnsafeFromVar(typedArray);
  40. ArrayBufferBase* arrayBuffer = typedArrayBase->GetArrayBuffer();
  41. if (arrayBuffer == nullptr || !ArrayBufferBase::Is(arrayBuffer) || !arrayBuffer->IsSharedArrayBuffer())
  42. {
  43. JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedSharedArrayBufferObject);
  44. }
  45. return arrayBuffer;
  46. }
  47. uint32 AtomicsObject::ValidateAtomicAccess(Var typedArray, Var requestIndex, ScriptContext *scriptContext)
  48. {
  49. Assert(TypedArrayBase::Is(typedArray));
  50. int32 accessIndex = -1;
  51. if (TaggedInt::Is(requestIndex))
  52. {
  53. accessIndex = TaggedInt::ToInt32(requestIndex);
  54. }
  55. else if(Js::JavascriptOperators::IsUndefined(requestIndex))
  56. {
  57. accessIndex = 0;
  58. }
  59. else
  60. {
  61. accessIndex = JavascriptConversion::ToInt32_Full(requestIndex, scriptContext);
  62. double dblValue = JavascriptConversion::ToInteger(requestIndex, scriptContext);
  63. if (dblValue != accessIndex)
  64. {
  65. JavascriptError::ThrowRangeError(scriptContext, JSERR_InvalidTypedArrayIndex);
  66. }
  67. }
  68. if (accessIndex < 0 || accessIndex >= (int32)TypedArrayBase::FromVar(typedArray)->GetLength())
  69. {
  70. JavascriptError::ThrowRangeError(scriptContext, JSERR_InvalidTypedArrayIndex);
  71. }
  72. return (uint32)accessIndex;
  73. }
  74. TypedArrayBase * AtomicsObject::ValidateAndGetTypedArray(Var typedArray, Var index, __out uint32 *accessIndex, ScriptContext *scriptContext, bool onlyInt32)
  75. {
  76. ValidateSharedIntegerTypedArray(typedArray, scriptContext, onlyInt32);
  77. uint32 i = ValidateAtomicAccess(typedArray, index, scriptContext);
  78. if (accessIndex != nullptr)
  79. {
  80. *accessIndex = i;
  81. }
  82. return TypedArrayBase::FromVar(typedArray);
  83. }
  84. Var AtomicsObject::EntryAdd(RecyclableObject* function, CallInfo callInfo, ...)
  85. {
  86. ATOMICS_FUNCTION_ENTRY_CHECKS(3, "Atomics.add");
  87. uint32 accessIndex = 0;
  88. TypedArrayBase *typedArrayBase = ValidateAndGetTypedArray(args[1], args[2], &accessIndex, scriptContext);
  89. return typedArrayBase->TypedAdd(accessIndex, args[3]);
  90. }
  91. Var AtomicsObject::EntryAnd(RecyclableObject* function, CallInfo callInfo, ...)
  92. {
  93. ATOMICS_FUNCTION_ENTRY_CHECKS(3, "Atomics.and");
  94. uint32 accessIndex = 0;
  95. TypedArrayBase *typedArrayBase = ValidateAndGetTypedArray(args[1], args[2], &accessIndex, scriptContext);
  96. return typedArrayBase->TypedAnd(accessIndex, args[3]);
  97. }
  98. Var AtomicsObject::EntryCompareExchange(RecyclableObject* function, CallInfo callInfo, ...)
  99. {
  100. ATOMICS_FUNCTION_ENTRY_CHECKS(4, "Atomics.compareExchange");
  101. uint32 accessIndex = 0;
  102. TypedArrayBase *typedArrayBase = ValidateAndGetTypedArray(args[1], args[2], &accessIndex, scriptContext);
  103. return typedArrayBase->TypedCompareExchange(accessIndex, args[3], args[4]);
  104. }
  105. Var AtomicsObject::EntryExchange(RecyclableObject* function, CallInfo callInfo, ...)
  106. {
  107. ATOMICS_FUNCTION_ENTRY_CHECKS(3, "Atomics.exchange");
  108. uint32 accessIndex = 0;
  109. TypedArrayBase *typedArrayBase = ValidateAndGetTypedArray(args[1], args[2], &accessIndex, scriptContext);
  110. return typedArrayBase->TypedExchange(accessIndex, args[3]);
  111. }
  112. Var AtomicsObject::EntryIsLockFree(RecyclableObject* function, CallInfo callInfo, ...)
  113. {
  114. ATOMICS_FUNCTION_ENTRY_CHECKS(1, "Atomics.isLockFree");
  115. uint32 size = JavascriptConversion::ToUInt32(args[1], scriptContext);
  116. // TODO : Currently for the size 1, 2 and 4 we are treating them lock free, we will take a look at this later.
  117. return (size == 1 || size == 2 || size == 4) ? scriptContext->GetLibrary()->GetTrue() : scriptContext->GetLibrary()->GetFalse();
  118. }
  119. Var AtomicsObject::EntryLoad(RecyclableObject* function, CallInfo callInfo, ...)
  120. {
  121. ATOMICS_FUNCTION_ENTRY_CHECKS(2, "Atomics.load");
  122. uint32 accessIndex = 0;
  123. TypedArrayBase *typedArrayBase = ValidateAndGetTypedArray(args[1], args[2], &accessIndex, scriptContext);
  124. return typedArrayBase->TypedLoad(accessIndex);
  125. }
  126. Var AtomicsObject::EntryOr(RecyclableObject* function, CallInfo callInfo, ...)
  127. {
  128. ATOMICS_FUNCTION_ENTRY_CHECKS(3, "Atomics.or");
  129. uint32 accessIndex = 0;
  130. TypedArrayBase *typedArrayBase = ValidateAndGetTypedArray(args[1], args[2], &accessIndex, scriptContext);
  131. return typedArrayBase->TypedOr(accessIndex, args[3]);
  132. }
  133. Var AtomicsObject::EntryStore(RecyclableObject* function, CallInfo callInfo, ...)
  134. {
  135. ATOMICS_FUNCTION_ENTRY_CHECKS(3, "Atomics.store");
  136. uint32 accessIndex = 0;
  137. TypedArrayBase *typedArrayBase = ValidateAndGetTypedArray(args[1], args[2], &accessIndex, scriptContext);
  138. return typedArrayBase->TypedStore(accessIndex, args[3]);
  139. }
  140. Var AtomicsObject::EntrySub(RecyclableObject* function, CallInfo callInfo, ...)
  141. {
  142. ATOMICS_FUNCTION_ENTRY_CHECKS(3, "Atomics.sub");
  143. uint32 accessIndex = 0;
  144. TypedArrayBase *typedArrayBase = ValidateAndGetTypedArray(args[1], args[2], &accessIndex, scriptContext);
  145. return typedArrayBase->TypedSub(accessIndex, args[3]);
  146. }
  147. Var AtomicsObject::EntryWait(RecyclableObject* function, CallInfo callInfo, ...)
  148. {
  149. ATOMICS_FUNCTION_ENTRY_CHECKS(3, "Atomics.wait");
  150. uint32 accessIndex = 0;
  151. TypedArrayBase *typedArrayBase = ValidateAndGetTypedArray(args[1], args[2], &accessIndex, scriptContext, true /*onlyInt32*/);
  152. int32 value = JavascriptConversion::ToInt32(args[3], scriptContext);
  153. uint32 timeout = INFINITE;
  154. if (args.Info.Count > 4 && !JavascriptOperators::IsUndefinedObject(args[4]))
  155. {
  156. double t =JavascriptConversion::ToNumber(args[4], scriptContext);
  157. if (!(NumberUtilities::IsNan(t) || JavascriptNumber::IsPosInf(t)))
  158. {
  159. int32 t1 = JavascriptConversion::ToInt32(t);
  160. timeout = (uint32)max(0, t1);
  161. }
  162. }
  163. if (!AgentOfBuffer::AgentCanSuspend(scriptContext))
  164. {
  165. JavascriptError::ThrowTypeError(scriptContext, JSERR_CannotSuspendBuffer);
  166. }
  167. Assert(typedArrayBase->GetBytesPerElement() == 4);
  168. uint32 bufferIndex = (accessIndex * 4) + typedArrayBase->GetByteOffset();
  169. Assert(bufferIndex < typedArrayBase->GetArrayBuffer()->GetByteLength());
  170. SharedArrayBuffer *sharedArrayBuffer = typedArrayBase->GetArrayBuffer()->GetAsSharedArrayBuffer();
  171. WaiterList *waiterList = sharedArrayBuffer->GetWaiterList(bufferIndex);
  172. bool awoken = false;
  173. {
  174. AutoCriticalSection autoCS(waiterList->GetCriticalSectionForAccess());
  175. int32 w = JavascriptConversion::ToInt32(typedArrayBase->DirectGetItem(accessIndex), scriptContext);
  176. if (value != w)
  177. {
  178. return scriptContext->GetLibrary()->CreateStringFromCppLiteral(_u("not-equal"));
  179. }
  180. DWORD_PTR agent = (DWORD_PTR)scriptContext;
  181. Assert(sharedArrayBuffer->GetSharedContents()->IsValidAgent(agent));
  182. #pragma prefast(suppress:__WARNING_CALLER_FAILING_TO_HOLD, "This is a prefast false-positive caused by it being unable to identify that the critical section used here is the same as the one held by the AutoCriticalSection")
  183. awoken = waiterList->AddAndSuspendWaiter(agent, timeout);
  184. if (!awoken)
  185. {
  186. waiterList->RemoveWaiter(agent);
  187. }
  188. }
  189. return awoken ? scriptContext->GetLibrary()->CreateStringFromCppLiteral(_u("ok"))
  190. : scriptContext->GetLibrary()->CreateStringFromCppLiteral(_u("timed-out"));
  191. }
  192. Var AtomicsObject::EntryWake(RecyclableObject* function, CallInfo callInfo, ...)
  193. {
  194. ATOMICS_FUNCTION_ENTRY_CHECKS(2, "Atomics.wake");
  195. uint32 accessIndex = 0;
  196. TypedArrayBase *typedArrayBase = ValidateAndGetTypedArray(args[1], args[2], &accessIndex, scriptContext, true /*onlyInt32*/);
  197. int32 count = INT_MAX;
  198. if (args.Info.Count > 3 && !JavascriptOperators::IsUndefinedObject(args[3]))
  199. {
  200. double d = JavascriptConversion::ToInteger(args[3], scriptContext);
  201. if (!(NumberUtilities::IsNan(d) || JavascriptNumber::IsPosInf(d)))
  202. {
  203. int32 c = JavascriptConversion::ToInt32(d);
  204. count = max(0, c);
  205. }
  206. }
  207. Assert(typedArrayBase->GetBytesPerElement() == 4);
  208. uint32 bufferIndex = (accessIndex * 4) + typedArrayBase->GetByteOffset();
  209. Assert(bufferIndex < typedArrayBase->GetArrayBuffer()->GetByteLength());
  210. SharedArrayBuffer *sharedArrayBuffer = typedArrayBase->GetArrayBuffer()->GetAsSharedArrayBuffer();
  211. WaiterList *waiterList = sharedArrayBuffer->GetWaiterList(bufferIndex);
  212. uint32 removed = 0;
  213. {
  214. AutoCriticalSection autoCS(waiterList->GetCriticalSectionForAccess());
  215. removed = waiterList->RemoveAndWakeWaiters(count);
  216. }
  217. return JavascriptNumber::ToVar(removed, scriptContext);
  218. }
  219. Var AtomicsObject::EntryXor(RecyclableObject* function, CallInfo callInfo, ...)
  220. {
  221. ATOMICS_FUNCTION_ENTRY_CHECKS(3, "Atomics.xor");
  222. uint32 accessIndex = 0;
  223. TypedArrayBase *typedArrayBase = ValidateAndGetTypedArray(args[1], args[2], &accessIndex, scriptContext);
  224. return typedArrayBase->TypedXor(accessIndex, args[3]);
  225. }
  226. }