瀏覽代碼

should not Assert for memory operation fail on client process (might be crashed or killed)

Lei Shi 9 年之前
父節點
當前提交
aa1fbe0ea4

+ 1 - 2
lib/Backend/CodeGenNumberAllocator.cpp

@@ -331,8 +331,7 @@ Js::JavascriptNumber* XProcNumberPageSegmentImpl::AllocateNumber(Func* func, dou
             // initialize number by WriteProcessMemory
             if (!WriteProcessMemory(hProcess, (void*)number, pLocalNumber, sizeCat, NULL))
             {
-                MemoryOperationLastError::RecordLastError();
-                Js::Throw::InternalError();
+                MemoryOperationLastError::RecordLastErrorAndThrow();
             }
 
             return (Js::JavascriptNumber*) number;

+ 3 - 6
lib/Common/Memory/CustomHeap.cpp

@@ -225,10 +225,9 @@ Allocation* Heap::Alloc(size_t bytes, ushort pdataCount, ushort xdataSize, bool
             size_t resultBytes = VirtualQueryEx(this->processHandle, allocation->address, &memBasicInfo, sizeof(memBasicInfo));
             if (resultBytes == 0)
             {
-                MemoryOperationLastError::RecordLastError();
                 if (this->processHandle != GetCurrentProcess())
                 {
-                    Js::Throw::InternalError();
+                    MemoryOperationLastError::RecordLastErrorAndThrow();
                 }
             }
             Assert(memBasicInfo.Protect == PAGE_EXECUTE);
@@ -264,10 +263,9 @@ Allocation* Heap::Alloc(size_t bytes, ushort pdataCount, ushort xdataSize, bool
         size_t resultBytes = VirtualQueryEx(this->processHandle, page->address, &memBasicInfo, sizeof(memBasicInfo));
         if (resultBytes == 0)
         {
-            MemoryOperationLastError::RecordLastError();
             if (this->processHandle != GetCurrentProcess())
             {
-                Js::Throw::InternalError();
+                MemoryOperationLastError::RecordLastErrorAndThrow();
             }
         }
         Assert(memBasicInfo.Protect == PAGE_EXECUTE);
@@ -1077,8 +1075,7 @@ void FillDebugBreak(_In_ BYTE* buffer, __in size_t byteCount, HANDLE processHand
     {
         if (!WriteProcessMemory(processHandle, buffer, writeBuffer, byteCount, NULL))
         {
-            MemoryOperationLastError::RecordLastError();
-            Js::Throw::InternalError();
+            MemoryOperationLastError::RecordLastErrorAndThrow();
         }
         HeapDeleteArray(byteCount, writeBuffer);
     }

+ 4 - 8
lib/Common/Memory/MemUtils.cpp

@@ -24,8 +24,7 @@ Memory::ChakraMemSet(_In_ void *dst, int val, size_t sizeInBytes, HANDLE process
     {
         if (!WriteProcessMemory(processHandle, dst, writeBuffer, sizeInBytes, NULL))
         {
-            MemoryOperationLastError::RecordLastError();
-            Js::Throw::InternalError();
+            MemoryOperationLastError::RecordLastErrorAndThrow();
         }
         HeapDeleteArray(sizeInBytes, writeBuffer);
     }
@@ -44,12 +43,9 @@ Memory::ChakraMemCopy(_In_ void *dst, size_t sizeInBytes, _In_reads_bytes_(count
     {
         memcpy(dst, src, count);
     }
-    else
+    else if (!WriteProcessMemory(processHandle, dst, src, count, NULL))
     {
-        if (!WriteProcessMemory(processHandle, dst, src, count, NULL))
-        {
-            MemoryOperationLastError::RecordLastError();
-            Js::Throw::InternalError();
-        }
+        MemoryOperationLastError::RecordLastErrorAndThrow();
     }
+
 }

+ 1 - 2
lib/Common/Memory/PageAllocator.cpp

@@ -952,8 +952,7 @@ PageAllocatorBase<T>::FillAllocPages(__in void * address, uint pageCount)
         readBuffer = HeapNewArray(byte, bufferSize);
         if (!ReadProcessMemory(this->processHandle, address, readBuffer, bufferSize, NULL))
         {
-            MemoryOperationLastError::RecordLastError();
-            Js::Throw::InternalError();
+            MemoryOperationLastError::RecordLastErrorAndThrow();
         }
     }
     for (size_t i = 0; i < bufferSize; i++)

+ 8 - 0
lib/Common/Memory/PageAllocator.h

@@ -389,6 +389,14 @@ public:
             MemOpLastError = GetLastError();
         }
     }
+    static void RecordLastErrorAndThrow()
+    {
+        if (MemOpLastError == 0)
+        {
+            MemOpLastError = GetLastError();
+            throw Js::InternalErrorException();
+        }
+    }
     static void ClearLastError()
     {
         MemOpLastError = 0;

+ 1 - 2
lib/Common/Memory/VirtualAllocWrapper.cpp

@@ -146,10 +146,9 @@ PreReservedVirtualAllocWrapper::IsInRange(void * address)
     size_t bytes = VirtualQueryEx(processHandle, address, &memBasicInfo, sizeof(memBasicInfo));
     if (bytes == 0)
     {
-        MemoryOperationLastError::RecordLastError();
         if (this->processHandle != GetCurrentProcess())
         {
-            Js::Throw::InternalError();
+            MemoryOperationLastError::RecordLastErrorAndThrow();
         }
         return false;
     }