1
0
mirror of https://github.com/wolfpld/tracy synced 2025-04-29 12:23:53 +00:00

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.
This commit is contained in:
Bartosz Taudul 2018-04-21 14:53:40 +02:00
parent a63f214964
commit 764792d8db
5 changed files with 35 additions and 2 deletions

View File

@ -0,0 +1,11 @@
#ifndef __TRACYFILEHEADER_HPP__
#define __TRACYFILEHEADER_HPP__
namespace tracy
{
static const char Lz4Header[4] = { 't', 'l', 'Z', 4 };
}
#endif

View File

@ -2,15 +2,19 @@
#define __TRACYFILEREAD_HPP__
#include <algorithm>
#include <stdexcept>
#include <stdio.h>
#include <string.h>
#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 )
{

View File

@ -5,6 +5,7 @@
#include <stdio.h>
#include <string.h>
#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 )
{

View File

@ -117,6 +117,7 @@
<ClInclude Include="..\..\..\nfd\nfd_common.h" />
<ClInclude Include="..\..\..\server\TracyCharUtil.hpp" />
<ClInclude Include="..\..\..\server\TracyEvent.hpp" />
<ClInclude Include="..\..\..\server\TracyFileHeader.hpp" />
<ClInclude Include="..\..\..\server\TracyFileRead.hpp" />
<ClInclude Include="..\..\..\server\TracyFileWrite.hpp" />
<ClInclude Include="..\..\..\server\TracyImGui.hpp" />

View File

@ -167,6 +167,9 @@
<ClInclude Include="..\..\..\common\tracy_sema.h">
<Filter>imgui</Filter>
</ClInclude>
<ClInclude Include="..\..\..\server\TracyFileHeader.hpp">
<Filter>server</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Natvis Include="DebugVis.natvis" />