Skip to content

Commit 49359b1

Browse files
committed
2 parents 9f836e1 + fe7deac commit 49359b1

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

src/CLR/Core/CLR_RT_HeapBlock_Array.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,20 @@ HRESULT CLR_RT_HeapBlock_Array::Copy(
322322
dataSrc += indexSrc * sizeElem;
323323
dataDst += indexDst * sizeElem;
324324

325+
#if !defined(BUILD_RTM)
326+
// Validate pointers and memory ranges
327+
if (dataSrc == nullptr || dataDst == nullptr ||
328+
dataSrc + length * sizeElem > arraySrc->GetFirstElement() + arraySrc->m_numOfElements * sizeElem ||
329+
dataDst + length * sizeElem > arrayDst->GetFirstElement() + arrayDst->m_numOfElements * sizeElem)
330+
{
331+
#ifdef DEBUG
332+
_ASSERTE(FALSE);
333+
#endif
334+
335+
NANOCLR_SET_AND_LEAVE(CLR_E_OUT_OF_RANGE);
336+
}
337+
#endif
338+
325339
if (!arraySrc->m_fReference)
326340
{
327341
memmove(dataDst, dataSrc, length * sizeElem);

src/CLR/Core/GarbageCollector_Compaction.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,16 @@ void CLR_RT_GarbageCollector::Heap_Compact()
256256

257257
freeRegion->Unlink();
258258

259+
#ifdef _DEBUG
260+
261+
_ASSERTE(relocCurrent->m_destination >= (CLR_UINT8 *)g_CLR_RT_ExecutionEngine.m_heap.FirstNode());
262+
_ASSERTE(relocCurrent->m_destination < (CLR_UINT8 *)g_CLR_RT_ExecutionEngine.m_heap.LastNode());
263+
_ASSERTE(relocCurrent->m_start >= (CLR_UINT8 *)g_CLR_RT_ExecutionEngine.m_heap.FirstNode());
264+
_ASSERTE(relocCurrent->m_start < (CLR_UINT8 *)g_CLR_RT_ExecutionEngine.m_heap.LastNode());
265+
_ASSERTE(moveBytes <= freeRegion_Size);
266+
267+
#endif
268+
259269
memmove(relocCurrent->m_destination, relocCurrent->m_start, moveBytes);
260270

261271
if (freeRegion_Size)

src/CLR/WireProtocol/WireProtocol_Message.c

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,7 @@ bool IsMarkerMatched(void *header, const void *marker, size_t len)
6262
return memcmp(header, marker, len) == 0;
6363
}
6464

65-
void ShiftBufferToLeft(void *buffer, uint32_t len)
66-
{
67-
memmove((uint8_t *)buffer, ((uint8_t *)buffer + 1), len - 1);
68-
}
69-
70-
void SyncToMessageStart()
65+
bool SyncToMessageStart()
7166
{
7267
uint32_t len;
7368

@@ -88,16 +83,33 @@ void SyncToMessageStart()
8883
break;
8984
}
9085

91-
ShiftBufferToLeft(&_inboundMessage.m_header, len);
86+
// Calculate the source and destination pointers
87+
uint8_t *src = (uint8_t *)&_inboundMessage.m_header + 1;
88+
uint8_t *dst = (uint8_t *)&_inboundMessage.m_header;
89+
size_t moveLength = len - 1;
9290

93-
// update pointer and expected size
91+
// Ensure that the memory regions do not exceed allocated bounds
92+
if ((src + moveLength >= (uint8_t *)&_inboundMessage + sizeof(_inboundMessage)) ||
93+
(dst + moveLength >= (uint8_t *)&_inboundMessage + sizeof(_inboundMessage)))
94+
{
95+
return false;
96+
}
97+
98+
// Perform the memory move
99+
memmove(dst, src, moveLength);
100+
101+
// Update pointer and expected size
94102
_pos--;
95103
_size++;
96104

97-
// sanity checks
98-
_ASSERTE(_size <= sizeof(_inboundMessage.m_header));
99-
_ASSERTE(_pos >= (uint8_t *)&(_inboundMessage.m_header));
105+
// Sanity checks
106+
if (_size > sizeof(_inboundMessage.m_header) || _pos < (uint8_t *)&_inboundMessage.m_header)
107+
{
108+
return false;
109+
}
100110
}
111+
112+
return true;
101113
}
102114

103115
void WP_ReplyToCommand(WP_Message *message, uint8_t fSuccess, uint8_t fCritical, void *ptr, uint32_t size)
@@ -357,7 +369,16 @@ void WP_Message_Process()
357369
}
358370
}
359371

360-
SyncToMessageStart();
372+
if (!SyncToMessageStart())
373+
{
374+
// something went wrong
375+
TRACE0(TRACE_ERRORS, "RxError: Failed to sync to message start\n");
376+
377+
RestartStateMachine();
378+
379+
// exit the loop to allow other RTOS threads to run
380+
return;
381+
}
361382

362383
if (len >= sizeof(_inboundMessage.m_header.m_signature))
363384
{

0 commit comments

Comments
 (0)