Harden against uninitialized rpmalloc.

Initialize rpmalloc either by explicitly calling InitRPMallocThread(),
or by forcing initialization of thread local variables block.
This commit is contained in:
Bartosz Taudul 2020-05-19 13:48:19 +02:00
parent dd4c2cf9fa
commit fad7e72fd4
4 changed files with 10 additions and 12 deletions

View File

@ -194,12 +194,11 @@ static inline int LuaZoneBeginS( lua_State* L )
if( !GetLuaZoneState().active ) return 0; if( !GetLuaZoneState().active ) return 0;
#endif #endif
TracyLfqPrepare( QueueType::ZoneBeginAllocSrcLocCallstack );
lua_Debug dbg; lua_Debug dbg;
lua_getstack( L, 1, &dbg ); lua_getstack( L, 1, &dbg );
lua_getinfo( L, "Snl", &dbg ); lua_getinfo( L, "Snl", &dbg );
const auto srcloc = Profiler::AllocSourceLocation( dbg.currentline, dbg.source, dbg.name ? dbg.name : dbg.short_src ); 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.time, Profiler::GetTime() );
MemWrite( &item->zoneBegin.srcloc, srcloc ); MemWrite( &item->zoneBegin.srcloc, srcloc );
TracyLfqCommit; TracyLfqCommit;
@ -223,14 +222,13 @@ static inline int LuaZoneBeginNS( lua_State* L )
if( !GetLuaZoneState().active ) return 0; if( !GetLuaZoneState().active ) return 0;
#endif #endif
TracyLfqPrepare( QueueType::ZoneBeginAllocSrcLocCallstack );
lua_Debug dbg; lua_Debug dbg;
lua_getstack( L, 1, &dbg ); lua_getstack( L, 1, &dbg );
lua_getinfo( L, "Snl", &dbg ); lua_getinfo( L, "Snl", &dbg );
size_t nsz; size_t nsz;
const auto name = lua_tolstring( L, 1, &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 ); 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.time, Profiler::GetTime() );
MemWrite( &item->zoneBegin.srcloc, srcloc ); MemWrite( &item->zoneBegin.srcloc, srcloc );
TracyLfqCommit; TracyLfqCommit;
@ -258,12 +256,11 @@ static inline int LuaZoneBegin( lua_State* L )
if( !GetLuaZoneState().active ) return 0; if( !GetLuaZoneState().active ) return 0;
#endif #endif
TracyLfqPrepare( QueueType::ZoneBeginAllocSrcLoc );
lua_Debug dbg; lua_Debug dbg;
lua_getstack( L, 1, &dbg ); lua_getstack( L, 1, &dbg );
lua_getinfo( L, "Snl", &dbg ); lua_getinfo( L, "Snl", &dbg );
const auto srcloc = Profiler::AllocSourceLocation( dbg.currentline, dbg.source, dbg.name ? dbg.name : dbg.short_src ); 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.time, Profiler::GetTime() );
MemWrite( &item->zoneBegin.srcloc, srcloc ); MemWrite( &item->zoneBegin.srcloc, srcloc );
TracyLfqCommit; TracyLfqCommit;
@ -283,14 +280,13 @@ static inline int LuaZoneBeginN( lua_State* L )
if( !GetLuaZoneState().active ) return 0; if( !GetLuaZoneState().active ) return 0;
#endif #endif
TracyLfqPrepare( QueueType::ZoneBeginAllocSrcLoc );
lua_Debug dbg; lua_Debug dbg;
lua_getstack( L, 1, &dbg ); lua_getstack( L, 1, &dbg );
lua_getinfo( L, "Snl", &dbg ); lua_getinfo( L, "Snl", &dbg );
size_t nsz; size_t nsz;
const auto name = lua_tolstring( L, 1, &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 ); 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.time, Profiler::GetTime() );
MemWrite( &item->zoneBegin.srcloc, srcloc ); MemWrite( &item->zoneBegin.srcloc, srcloc );
TracyLfqCommit; TracyLfqCommit;
@ -372,10 +368,10 @@ static inline int LuaMessage( lua_State* L )
auto txt = lua_tostring( L, 1 ); auto txt = lua_tostring( L, 1 );
const auto size = strlen( txt ); const auto size = strlen( txt );
TracyLfqPrepare( QueueType::Message );
auto ptr = (char*)tracy_malloc( size+1 ); auto ptr = (char*)tracy_malloc( size+1 );
memcpy( ptr, txt, size ); memcpy( ptr, txt, size );
ptr[size] = '\0'; ptr[size] = '\0';
TracyLfqPrepare( QueueType::Message );
MemWrite( &item->message.time, Profiler::GetTime() ); MemWrite( &item->message.time, Profiler::GetTime() );
MemWrite( &item->message.text, (uint64_t)ptr ); MemWrite( &item->message.text, (uint64_t)ptr );
TracyLfqCommit; TracyLfqCommit;

View File

@ -50,7 +50,7 @@ public:
# define glQueryCounter glQueryCounterEXT # define glQueryCounter glQueryCounterEXT
#endif #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 #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 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 ); # 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 );

View File

@ -271,6 +271,7 @@ private:
static inline VkCtx* CreateVkContext( VkPhysicalDevice physdev, VkDevice device, VkQueue queue, VkCommandBuffer cmdbuf ) static inline VkCtx* CreateVkContext( VkPhysicalDevice physdev, VkDevice device, VkQueue queue, VkCommandBuffer cmdbuf )
{ {
InitRPMallocThread();
auto ctx = (VkCtx*)tracy_malloc( sizeof( VkCtx ) ); auto ctx = (VkCtx*)tracy_malloc( sizeof( VkCtx ) );
new(ctx) VkCtx( physdev, device, queue, cmdbuf ); new(ctx) VkCtx( physdev, device, queue, cmdbuf );
return ctx; return ctx;

View File

@ -272,10 +272,10 @@ public:
#ifdef TRACY_ON_DEMAND #ifdef TRACY_ON_DEMAND
if( !GetProfiler().IsConnected() ) return; if( !GetProfiler().IsConnected() ) return;
#endif #endif
TracyLfqPrepare( callstack == 0 ? QueueType::Message : QueueType::MessageCallstack );
auto ptr = (char*)tracy_malloc( size+1 ); auto ptr = (char*)tracy_malloc( size+1 );
memcpy( ptr, txt, size ); memcpy( ptr, txt, size );
ptr[size] = '\0'; ptr[size] = '\0';
TracyLfqPrepare( callstack == 0 ? QueueType::Message : QueueType::MessageCallstack );
MemWrite( &item->message.time, GetTime() ); MemWrite( &item->message.time, GetTime() );
MemWrite( &item->message.text, (uint64_t)ptr ); MemWrite( &item->message.text, (uint64_t)ptr );
TracyLfqCommit; TracyLfqCommit;
@ -301,10 +301,10 @@ public:
#ifdef TRACY_ON_DEMAND #ifdef TRACY_ON_DEMAND
if( !GetProfiler().IsConnected() ) return; if( !GetProfiler().IsConnected() ) return;
#endif #endif
TracyLfqPrepare( callstack == 0 ? QueueType::MessageColor : QueueType::MessageColorCallstack );
auto ptr = (char*)tracy_malloc( size+1 ); auto ptr = (char*)tracy_malloc( size+1 );
memcpy( ptr, txt, size ); memcpy( ptr, txt, size );
ptr[size] = '\0'; ptr[size] = '\0';
TracyLfqPrepare( callstack == 0 ? QueueType::MessageColor : QueueType::MessageColorCallstack );
MemWrite( &item->messageColor.time, GetTime() ); MemWrite( &item->messageColor.time, GetTime() );
MemWrite( &item->messageColor.text, (uint64_t)ptr ); MemWrite( &item->messageColor.text, (uint64_t)ptr );
MemWrite( &item->messageColor.r, uint8_t( ( color ) & 0xFF ) ); 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 ) static tracy_force_inline void MessageAppInfo( const char* txt, size_t size )
{ {
InitRPMallocThread();
auto ptr = (char*)tracy_malloc( size+1 ); auto ptr = (char*)tracy_malloc( size+1 );
memcpy( ptr, txt, size ); memcpy( ptr, txt, size );
ptr[size] = '\0'; ptr[size] = '\0';