panfrost: Make pan_merge macro more robust

Consider the following innocuous-looking code:

   pan_merge(packed, vtx->attributes[i], ATTRIBUTE);

Under the current implementation, this code is completely broken. Why?
The current implemention is a macro which hardcodes the loop index i,
which shadows the i used to index attributes. Pull out a helper method
so we do the right thing without resulting to further preprocessor abuse
(__COUNTER__).

While making things more robust, assert the crucial pan_merge
invariant that the total size is a multiple of 4; if this fails, the
routine risks memory corruption.

Signed-off-by: Alyssa Rosenzweig <alyssa@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/14119>
This commit is contained in:
Alyssa Rosenzweig 2021-12-06 15:06:23 -05:00 committed by Marge Bot
parent 36f8f9b882
commit 311077c483
1 changed files with 9 additions and 4 deletions

View File

@ -176,11 +176,16 @@ __gen_unpack_padded(const uint8_t *restrict cl, uint32_t start, uint32_t end)
#define pan_section_print(fp, A, S, var, indent) \\
PREFIX4(A, SECTION, S, print)(fp, &(var), indent)
static inline void pan_merge_helper(uint32_t *dst, const uint32_t *src, size_t bytes)
{
assert((bytes & 3) == 0);
for (unsigned i = 0; i < (bytes / 4); ++i)
dst[i] |= src[i];
}
#define pan_merge(packed1, packed2, type) \
do { \
for (unsigned i = 0; i < (PREFIX2(type, LENGTH) / 4); ++i) \
(packed1).opaque[i] |= (packed2).opaque[i]; \
} while(0)
pan_merge_helper((packed1).opaque, (packed2).opaque, pan_size(type))
/* From presentations, 16x16 tiles externally. Use shift for fast computation
* of tile numbers. */