From fad7e72fd45b9720897f5ac4f5589bb627150cad Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Tue, 19 May 2020 13:48:19 +0200 Subject: [PATCH] Harden against uninitialized rpmalloc. Initialize rpmalloc either by explicitly calling InitRPMallocThread(), or by forcing initialization of thread local variables block. --- TracyLua.hpp | 14 +++++--------- TracyOpenGL.hpp | 2 +- TracyVulkan.hpp | 1 + client/TracyProfiler.hpp | 5 +++-- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/TracyLua.hpp b/TracyLua.hpp index 6d27c814..5490467c 100644 --- a/TracyLua.hpp +++ b/TracyLua.hpp @@ -194,12 +194,11 @@ static inline int LuaZoneBeginS( lua_State* L ) if( !GetLuaZoneState().active ) return 0; #endif + TracyLfqPrepare( QueueType::ZoneBeginAllocSrcLocCallstack ); lua_Debug dbg; lua_getstack( L, 1, &dbg ); lua_getinfo( L, "Snl", &dbg ); const auto srcloc = Profiler::AllocSourceLocation( dbg.currentline, dbg.source, dbg.name ? dbg.name : dbg.short_src ); - - TracyLfqPrepare( QueueType::ZoneBeginAllocSrcLocCallstack ); MemWrite( &item->zoneBegin.time, Profiler::GetTime() ); MemWrite( &item->zoneBegin.srcloc, srcloc ); TracyLfqCommit; @@ -223,14 +222,13 @@ static inline int LuaZoneBeginNS( lua_State* L ) if( !GetLuaZoneState().active ) return 0; #endif + TracyLfqPrepare( QueueType::ZoneBeginAllocSrcLocCallstack ); lua_Debug dbg; lua_getstack( L, 1, &dbg ); lua_getinfo( L, "Snl", &dbg ); size_t nsz; const auto name = lua_tolstring( L, 1, &nsz ); const auto srcloc = Profiler::AllocSourceLocation( dbg.currentline, dbg.source, dbg.name ? dbg.name : dbg.short_src, name, nsz ); - - TracyLfqPrepare( QueueType::ZoneBeginAllocSrcLocCallstack ); MemWrite( &item->zoneBegin.time, Profiler::GetTime() ); MemWrite( &item->zoneBegin.srcloc, srcloc ); TracyLfqCommit; @@ -258,12 +256,11 @@ static inline int LuaZoneBegin( lua_State* L ) if( !GetLuaZoneState().active ) return 0; #endif + TracyLfqPrepare( QueueType::ZoneBeginAllocSrcLoc ); lua_Debug dbg; lua_getstack( L, 1, &dbg ); lua_getinfo( L, "Snl", &dbg ); const auto srcloc = Profiler::AllocSourceLocation( dbg.currentline, dbg.source, dbg.name ? dbg.name : dbg.short_src ); - - TracyLfqPrepare( QueueType::ZoneBeginAllocSrcLoc ); MemWrite( &item->zoneBegin.time, Profiler::GetTime() ); MemWrite( &item->zoneBegin.srcloc, srcloc ); TracyLfqCommit; @@ -283,14 +280,13 @@ static inline int LuaZoneBeginN( lua_State* L ) if( !GetLuaZoneState().active ) return 0; #endif + TracyLfqPrepare( QueueType::ZoneBeginAllocSrcLoc ); lua_Debug dbg; lua_getstack( L, 1, &dbg ); lua_getinfo( L, "Snl", &dbg ); size_t nsz; const auto name = lua_tolstring( L, 1, &nsz ); const auto srcloc = Profiler::AllocSourceLocation( dbg.currentline, dbg.source, dbg.name ? dbg.name : dbg.short_src, name, nsz ); - - TracyLfqPrepare( QueueType::ZoneBeginAllocSrcLoc ); MemWrite( &item->zoneBegin.time, Profiler::GetTime() ); MemWrite( &item->zoneBegin.srcloc, srcloc ); TracyLfqCommit; @@ -372,10 +368,10 @@ static inline int LuaMessage( lua_State* L ) auto txt = lua_tostring( L, 1 ); const auto size = strlen( txt ); + TracyLfqPrepare( QueueType::Message ); auto ptr = (char*)tracy_malloc( size+1 ); memcpy( ptr, txt, size ); ptr[size] = '\0'; - TracyLfqPrepare( QueueType::Message ); MemWrite( &item->message.time, Profiler::GetTime() ); MemWrite( &item->message.text, (uint64_t)ptr ); TracyLfqCommit; diff --git a/TracyOpenGL.hpp b/TracyOpenGL.hpp index 234e2504..9150b704 100644 --- a/TracyOpenGL.hpp +++ b/TracyOpenGL.hpp @@ -50,7 +50,7 @@ public: # define glQueryCounter glQueryCounterEXT #endif -#define TracyGpuContext tracy::GetGpuCtx().ptr = (tracy::GpuCtx*)tracy::tracy_malloc( sizeof( tracy::GpuCtx ) ); new(tracy::GetGpuCtx().ptr) tracy::GpuCtx; +#define TracyGpuContext tracy::InitRPMallocThread(); tracy::GetGpuCtx().ptr = (tracy::GpuCtx*)tracy::tracy_malloc( sizeof( tracy::GpuCtx ) ); new(tracy::GetGpuCtx().ptr) tracy::GpuCtx; #if defined TRACY_HAS_CALLSTACK && defined TRACY_CALLSTACK # define TracyGpuNamedZone( varname, name, active ) static const tracy::SourceLocationData TracyConcat(__tracy_gpu_source_location,__LINE__) { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, 0 }; tracy::GpuCtxScope varname( &TracyConcat(__tracy_gpu_source_location,__LINE__), TRACY_CALLSTACK, active ); # define TracyGpuNamedZoneC( varname, name, color, active ) static const tracy::SourceLocationData TracyConcat(__tracy_gpu_source_location,__LINE__) { name, __FUNCTION__, __FILE__, (uint32_t)__LINE__, color }; tracy::GpuCtxScope varname( &TracyConcat(__tracy_gpu_source_location,__LINE__), TRACY_CALLSTACK, active ); diff --git a/TracyVulkan.hpp b/TracyVulkan.hpp index e258f001..68efea58 100644 --- a/TracyVulkan.hpp +++ b/TracyVulkan.hpp @@ -271,6 +271,7 @@ private: static inline VkCtx* CreateVkContext( VkPhysicalDevice physdev, VkDevice device, VkQueue queue, VkCommandBuffer cmdbuf ) { + InitRPMallocThread(); auto ctx = (VkCtx*)tracy_malloc( sizeof( VkCtx ) ); new(ctx) VkCtx( physdev, device, queue, cmdbuf ); return ctx; diff --git a/client/TracyProfiler.hpp b/client/TracyProfiler.hpp index a03b63e7..b3c2e495 100644 --- a/client/TracyProfiler.hpp +++ b/client/TracyProfiler.hpp @@ -272,10 +272,10 @@ public: #ifdef TRACY_ON_DEMAND if( !GetProfiler().IsConnected() ) return; #endif + TracyLfqPrepare( callstack == 0 ? QueueType::Message : QueueType::MessageCallstack ); auto ptr = (char*)tracy_malloc( size+1 ); memcpy( ptr, txt, size ); ptr[size] = '\0'; - TracyLfqPrepare( callstack == 0 ? QueueType::Message : QueueType::MessageCallstack ); MemWrite( &item->message.time, GetTime() ); MemWrite( &item->message.text, (uint64_t)ptr ); TracyLfqCommit; @@ -301,10 +301,10 @@ public: #ifdef TRACY_ON_DEMAND if( !GetProfiler().IsConnected() ) return; #endif + TracyLfqPrepare( callstack == 0 ? QueueType::MessageColor : QueueType::MessageColorCallstack ); auto ptr = (char*)tracy_malloc( size+1 ); memcpy( ptr, txt, size ); ptr[size] = '\0'; - TracyLfqPrepare( callstack == 0 ? QueueType::MessageColor : QueueType::MessageColorCallstack ); MemWrite( &item->messageColor.time, GetTime() ); MemWrite( &item->messageColor.text, (uint64_t)ptr ); MemWrite( &item->messageColor.r, uint8_t( ( color ) & 0xFF ) ); @@ -333,6 +333,7 @@ public: static tracy_force_inline void MessageAppInfo( const char* txt, size_t size ) { + InitRPMallocThread(); auto ptr = (char*)tracy_malloc( size+1 ); memcpy( ptr, txt, size ); ptr[size] = '\0';