Skip to content

Commit 36ee985

Browse files
authored
fix alignment of guard blocks (KhronosGroup#4137)
1 parent 09c541e commit 36ee985

2 files changed

Lines changed: 19 additions & 15 deletions

File tree

glslang/Include/PoolAlloc.h

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ class TAllocation {
8484
// makes the compiler print warnings about 0 length memsets,
8585
// even with the if() protecting them.
8686
# ifdef GUARD_BLOCKS
87-
memset(preGuard(), guardBlockBeginVal, guardBlockSize);
87+
memset(preGuard(), guardBlockBeginVal, guardBlockSize());
8888
memset(data(), userDataFill, size);
89-
memset(postGuard(), guardBlockEndVal, guardBlockSize);
89+
memset(postGuard(), guardBlockEndVal, guardBlockSize());
9090
# endif
9191
}
9292

@@ -100,20 +100,20 @@ class TAllocation {
100100
// Return total size needed to accommodate user buffer of 'size',
101101
// plus our tracking data.
102102
inline static size_t allocationSize(size_t size) {
103-
return size + 2 * guardBlockSize + headerSize();
103+
return size + 2 * guardBlockSize() + headerSize();
104104
}
105105

106106
// Offset from surrounding buffer to get to user data buffer.
107107
inline static unsigned char* offsetAllocation(unsigned char* m) {
108-
return m + guardBlockSize + headerSize();
108+
return m + guardBlockSize() + headerSize();
109109
}
110110

111111
private:
112112
void checkGuardBlock(unsigned char* blockMem, unsigned char val, const char* locText) const;
113113

114114
// Find offsets to pre and post guard blocks, and user data buffer
115115
unsigned char* preGuard() const { return mem + headerSize(); }
116-
unsigned char* data() const { return preGuard() + guardBlockSize; }
116+
unsigned char* data() const { return preGuard() + guardBlockSize(); }
117117
unsigned char* postGuard() const { return data() + size; }
118118

119119
size_t size; // size of the user data area
@@ -125,15 +125,19 @@ class TAllocation {
125125
static inline constexpr unsigned char userDataFill = 0xcd;
126126

127127
# ifdef GUARD_BLOCKS
128-
static inline constexpr size_t guardBlockSize = 16;
129-
# else
130-
static inline constexpr size_t guardBlockSize = 0;
131-
# endif
132-
133-
# ifdef GUARD_BLOCKS
134-
inline static size_t headerSize() { return sizeof(TAllocation); }
128+
inline static constexpr size_t headerSize() { return sizeof(TAllocation); }
129+
inline static constexpr size_t guardBlockSize() {
130+
constexpr size_t minGuardSize = 16;
131+
constexpr size_t alignmentSize = 16;
132+
constexpr size_t guardLayoutSize =
133+
(minGuardSize + sizeof(TAllocation) + alignmentSize - 1) & ~(alignmentSize - 1);
134+
static_assert((guardLayoutSize % alignmentSize) == 0,
135+
"Guard block layout is not 16-byte aligned.");
136+
return guardLayoutSize - sizeof(TAllocation);
137+
}
135138
# else
136-
inline static size_t headerSize() { return 0; }
139+
inline static constexpr size_t headerSize() { return 0; }
140+
inline static constexpr size_t guardBlockSize() { return 0; }
137141
# endif
138142
};
139143

glslang/MachineIndependent/PoolAlloc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ void TAllocation::checkGuardBlock(unsigned char*, unsigned char, const char*) co
148148
#endif
149149
{
150150
#ifdef GUARD_BLOCKS
151-
for (size_t x = 0; x < guardBlockSize; x++) {
151+
for (size_t x = 0; x < guardBlockSize(); x++) {
152152
if (blockMem[x] != val) {
153153
const int maxSize = 80;
154154
char assertMsg[maxSize];
@@ -160,7 +160,7 @@ void TAllocation::checkGuardBlock(unsigned char*, unsigned char, const char*) co
160160
}
161161
}
162162
#else
163-
assert(guardBlockSize == 0);
163+
assert(guardBlockSize() == 0);
164164
#endif
165165
}
166166

0 commit comments

Comments
 (0)