From 29df4e8218ee7d147542f767d899c5c639932e06 Mon Sep 17 00:00:00 2001 From: Andrew Au Date: Wed, 8 May 2019 10:35:43 -0700 Subject: Return the required padding size instead of the current position to avoid integer overflow. --- src/vm/eventpipeblock.h | 9 ++++----- src/vm/fastserializer.cpp | 11 ++--------- src/vm/fastserializer.h | 6 +++--- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/vm/eventpipeblock.h b/src/vm/eventpipeblock.h index 78b04af805..c85a84e461 100644 --- a/src/vm/eventpipeblock.h +++ b/src/vm/eventpipeblock.h @@ -51,14 +51,13 @@ public: if (eventsSize == 0) return; - size_t currentPosition = pSerializer->GetCurrentPosition(); - if (currentPosition % ALIGNMENT_SIZE != 0) + unsigned int requiredPadding = pSerializer->GetRequiredPadding(); + if (requiredPadding != 0) { BYTE maxPadding[ALIGNMENT_SIZE - 1] = {}; // it's longest possible padding, we are going to use only part of it - unsigned int paddingLength = ALIGNMENT_SIZE - (currentPosition % ALIGNMENT_SIZE); - pSerializer->WriteBuffer(maxPadding, paddingLength); // we write zeros here, the reader is going to always read from the first aligned address of the serialized content + pSerializer->WriteBuffer(maxPadding, requiredPadding); // we write zeros here, the reader is going to always read from the first aligned address of the serialized content - _ASSERTE(pSerializer->HasWriteErrors() || (pSerializer->GetCurrentPosition() % ALIGNMENT_SIZE == 0)); + _ASSERTE(pSerializer->HasWriteErrors() || (pSerializer->GetRequiredPadding() == 0)); } pSerializer->WriteBuffer(m_pBlock, eventsSize); diff --git a/src/vm/fastserializer.cpp b/src/vm/fastserializer.cpp index 3330bffc2f..73a8da553e 100644 --- a/src/vm/fastserializer.cpp +++ b/src/vm/fastserializer.cpp @@ -132,7 +132,7 @@ FastSerializer::FastSerializer(StreamWriter *pStreamWriter) : m_pStreamWriter(pS CONTRACTL_END; m_writeErrorEncountered = false; - m_currentPos = 0; + m_requiredPadding = 0; WriteFileHeader(); } @@ -190,19 +190,12 @@ void FastSerializer::WriteBuffer(BYTE *pBuffer, unsigned int length) uint32_t outCount; bool fSuccess = m_pStreamWriter->Write(pBuffer, length, outCount); -#ifdef _DEBUG - size_t prevPos = m_currentPos; -#endif - m_currentPos += outCount; + m_requiredPadding = (ALIGNMENT_SIZE + m_requiredPadding - (outCount % ALIGNMENT_SIZE)) % ALIGNMENT_SIZE; // This will cause us to stop writing to the file. // The file will still remain open until shutdown so that we don't // have to take a lock at this level when we touch the file stream. m_writeErrorEncountered = (length != outCount) || !fSuccess; - -#ifdef _DEBUG - _ASSERTE(m_writeErrorEncountered || (prevPos < m_currentPos)); -#endif } EX_CATCH { diff --git a/src/vm/fastserializer.h b/src/vm/fastserializer.h index f62bb62b52..aa0501f0ae 100644 --- a/src/vm/fastserializer.h +++ b/src/vm/fastserializer.h @@ -86,10 +86,10 @@ public: void WriteTag(FastSerializerTags tag, BYTE *payload = NULL, unsigned int payloadLength = 0); void WriteString(const char *strContents, unsigned int length); - size_t GetCurrentPosition() const + unsigned int GetRequiredPadding() const { LIMITED_METHOD_CONTRACT; - return m_currentPos; + return m_requiredPadding; } bool HasWriteErrors() const @@ -104,7 +104,7 @@ private: StreamWriter *const m_pStreamWriter; bool m_writeErrorEncountered; - size_t m_currentPos; + unsigned int m_requiredPadding; }; #endif // FEATURE_PERFTRACING -- cgit v1.2.3