From a4b4e8a38059242f34b203e167a51d0e9cd7c883 Mon Sep 17 00:00:00 2001 From: James Dolan Date: Tue, 30 Dec 2025 19:43:22 -0800 Subject: [PATCH 1/2] fix alignment of guard blocks --- glslang/Include/PoolAlloc.h | 26 ++++++++++++------------ glslang/MachineIndependent/PoolAlloc.cpp | 4 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/glslang/Include/PoolAlloc.h b/glslang/Include/PoolAlloc.h index 8a9284144a..d7bf8b5ad7 100644 --- a/glslang/Include/PoolAlloc.h +++ b/glslang/Include/PoolAlloc.h @@ -84,9 +84,9 @@ class TAllocation { // makes the compiler print warnings about 0 length memsets, // even with the if() protecting them. # ifdef GUARD_BLOCKS - memset(preGuard(), guardBlockBeginVal, guardBlockSize); + memset(preGuard(), guardBlockBeginVal, guardBlockSize()); memset(data(), userDataFill, size); - memset(postGuard(), guardBlockEndVal, guardBlockSize); + memset(postGuard(), guardBlockEndVal, guardBlockSize()); # endif } @@ -100,12 +100,12 @@ class TAllocation { // Return total size needed to accommodate user buffer of 'size', // plus our tracking data. inline static size_t allocationSize(size_t size) { - return size + 2 * guardBlockSize + headerSize(); + return size + 2 * guardBlockSize() + headerSize(); } // Offset from surrounding buffer to get to user data buffer. inline static unsigned char* offsetAllocation(unsigned char* m) { - return m + guardBlockSize + headerSize(); + return m + guardBlockSize() + headerSize(); } private: @@ -113,7 +113,7 @@ class TAllocation { // Find offsets to pre and post guard blocks, and user data buffer unsigned char* preGuard() const { return mem + headerSize(); } - unsigned char* data() const { return preGuard() + guardBlockSize; } + unsigned char* data() const { return preGuard() + guardBlockSize(); } unsigned char* postGuard() const { return data() + size; } size_t size; // size of the user data area @@ -125,15 +125,15 @@ class TAllocation { static inline constexpr unsigned char userDataFill = 0xcd; # ifdef GUARD_BLOCKS - static inline constexpr size_t guardBlockSize = 16; -# else - static inline constexpr size_t guardBlockSize = 0; -# endif - -# ifdef GUARD_BLOCKS - inline static size_t headerSize() { return sizeof(TAllocation); } + inline static constexpr size_t headerSize() { return sizeof(TAllocation); } + inline static constexpr size_t guardBlockSize() { + constexpr size_t kMinGuard = 16; + constexpr size_t kAlignment = 16; + return ((kMinGuard + sizeof(TAllocation) + kAlignment - 1) & ~(kAlignment - 1)) - sizeof(TAllocation); + } # else - inline static size_t headerSize() { return 0; } + inline static constexpr size_t headerSize() { return 0; } + inline static constexpr size_t guardBlockSize() { return 0; } # endif }; diff --git a/glslang/MachineIndependent/PoolAlloc.cpp b/glslang/MachineIndependent/PoolAlloc.cpp index 93a3b0d12f..1ac9fbb28c 100644 --- a/glslang/MachineIndependent/PoolAlloc.cpp +++ b/glslang/MachineIndependent/PoolAlloc.cpp @@ -148,7 +148,7 @@ void TAllocation::checkGuardBlock(unsigned char*, unsigned char, const char*) co #endif { #ifdef GUARD_BLOCKS - for (size_t x = 0; x < guardBlockSize; x++) { + for (size_t x = 0; x < guardBlockSize(); x++) { if (blockMem[x] != val) { const int maxSize = 80; char assertMsg[maxSize]; @@ -160,7 +160,7 @@ void TAllocation::checkGuardBlock(unsigned char*, unsigned char, const char*) co } } #else - assert(guardBlockSize == 0); + assert(guardBlockSize() == 0); #endif } From 5e9d7b46a332178c48c2ed36073930bb948ac3b3 Mon Sep 17 00:00:00 2001 From: James Dolan Date: Sun, 15 Mar 2026 10:04:14 -0700 Subject: [PATCH 2/2] code review: naming convention and static_assert --- glslang/Include/PoolAlloc.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/glslang/Include/PoolAlloc.h b/glslang/Include/PoolAlloc.h index d7bf8b5ad7..1c51a08210 100644 --- a/glslang/Include/PoolAlloc.h +++ b/glslang/Include/PoolAlloc.h @@ -127,9 +127,13 @@ class TAllocation { # ifdef GUARD_BLOCKS inline static constexpr size_t headerSize() { return sizeof(TAllocation); } inline static constexpr size_t guardBlockSize() { - constexpr size_t kMinGuard = 16; - constexpr size_t kAlignment = 16; - return ((kMinGuard + sizeof(TAllocation) + kAlignment - 1) & ~(kAlignment - 1)) - sizeof(TAllocation); + constexpr size_t minGuardSize = 16; + constexpr size_t alignmentSize = 16; + constexpr size_t guardLayoutSize = + (minGuardSize + sizeof(TAllocation) + alignmentSize - 1) & ~(alignmentSize - 1); + static_assert((guardLayoutSize % alignmentSize) == 0, + "Guard block layout is not 16-byte aligned."); + return guardLayoutSize - sizeof(TAllocation); } # else inline static constexpr size_t headerSize() { return 0; }