From 764792d8db77b84ecbd981d71e8c5fa1d808d801 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Sat, 21 Apr 2018 14:53:40 +0200 Subject: [PATCH] Try to not crash when opening invalid files. Tracy will now perform a number of checks when trying to read a dump file: 1. The file must have at least 4 bytes of data. 2. There should be a 4 byte header to indicate the file was saved by tracy. This is a breaking change in file format. 3. Old header-less files are still supported, but there's a new check for data validity. The first 4 bytes of file (as an uint32) must be less or equal to max LZ4 data packet size. This requires the first two bytes to be 00 00 or 00 01, which should catch most invalid files. --- server/TracyFileHeader.hpp | 11 +++++++++++ server/TracyFileRead.hpp | 17 ++++++++++++++++- server/TracyFileWrite.hpp | 5 ++++- standalone/build/win32/Tracy.vcxproj | 1 + standalone/build/win32/Tracy.vcxproj.filters | 3 +++ 5 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 server/TracyFileHeader.hpp diff --git a/server/TracyFileHeader.hpp b/server/TracyFileHeader.hpp new file mode 100644 index 00000000..67ca8afc --- /dev/null +++ b/server/TracyFileHeader.hpp @@ -0,0 +1,11 @@ +#ifndef __TRACYFILEHEADER_HPP__ +#define __TRACYFILEHEADER_HPP__ + +namespace tracy +{ + +static const char Lz4Header[4] = { 't', 'l', 'Z', 4 }; + +} + +#endif diff --git a/server/TracyFileRead.hpp b/server/TracyFileRead.hpp index 45451b34..fb851167 100644 --- a/server/TracyFileRead.hpp +++ b/server/TracyFileRead.hpp @@ -2,15 +2,19 @@ #define __TRACYFILEREAD_HPP__ #include +#include #include #include +#include "TracyFileHeader.hpp" #include "../common/tracy_lz4.hpp" #include "../common/TracyForceInline.hpp" namespace tracy { +struct NotTracyDump : public std::exception {}; + class FileRead { public: @@ -84,7 +88,18 @@ private: , m_offset( BufSize ) , m_active( 1 ) , m_lastBlock( 0 ) - {} + { + char hdr[4]; + if( fread( hdr, 1, sizeof( hdr ), m_file ) != sizeof( hdr ) ) throw NotTracyDump(); + if( memcmp( hdr, Lz4Header, sizeof( hdr ) ) != 0 ) + { + fseek( m_file, 0, SEEK_SET ); + uint32_t sz; + static_assert( sizeof( sz ) == sizeof( hdr ), "Size mismatch" ); + memcpy( &sz, hdr, sizeof( sz ) ); + if( sz > LZ4Size ) throw NotTracyDump(); + } + } tracy_force_inline void ReadSmall( void* ptr, size_t size ) { diff --git a/server/TracyFileWrite.hpp b/server/TracyFileWrite.hpp index 71269f89..9d3f8537 100644 --- a/server/TracyFileWrite.hpp +++ b/server/TracyFileWrite.hpp @@ -5,6 +5,7 @@ #include #include +#include "TracyFileHeader.hpp" #include "../common/tracy_lz4.hpp" #include "../common/TracyForceInline.hpp" @@ -48,7 +49,9 @@ private: , m_file( f ) , m_offset( 0 ) , m_active( 0 ) - {} + { + fwrite( Lz4Header, 1, sizeof( Lz4Header ), m_file ); + } tracy_force_inline void WriteSmall( const void* ptr, size_t size ) { diff --git a/standalone/build/win32/Tracy.vcxproj b/standalone/build/win32/Tracy.vcxproj index 9b5101fc..8aa4e9a7 100644 --- a/standalone/build/win32/Tracy.vcxproj +++ b/standalone/build/win32/Tracy.vcxproj @@ -117,6 +117,7 @@ + diff --git a/standalone/build/win32/Tracy.vcxproj.filters b/standalone/build/win32/Tracy.vcxproj.filters index fe6814c7..963db2b8 100644 --- a/standalone/build/win32/Tracy.vcxproj.filters +++ b/standalone/build/win32/Tracy.vcxproj.filters @@ -167,6 +167,9 @@ imgui + + server +