Mesa (master): util: Fix disk_cache index calculation on big endian

Matt Turner mattst88 at kemper.freedesktop.org
Sat Nov 25 20:30:58 UTC 2017


Module: Mesa
Branch: master
Commit: c690a7a8cdfb6425547bbb782020098405851194
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=c690a7a8cdfb6425547bbb782020098405851194

Author: Matt Turner <mattst88 at gmail.com>
Date:   Thu Nov 23 10:41:34 2017 -0800

util: Fix disk_cache index calculation on big endian

The cache-test test program attempts to create a collision (using key_a
and key_a_collide) by making the first two bytes identical. The idea is
fine -- the shader cache wants to use the first four characters of a
SHA1 hex digest as the index.

The following program

        unsigned char array[4] = {1, 2, 3, 4};
        int *ptr = (int *)array;

        for (int i = 0; i < 4; i++) {
            printf("%02x", array[i]);
        }
        printf("\n");

        printf("%08x\n", *ptr);

prints

   01020304
   04030201

on little endian, and

   01020304
   01020304

on big endian.

On big endian platforms reading the character array back as an int (as
is done in disk_cache.c) does not yield the same results as reading the
byte array.

To get the first four characters of the SHA1 hex digest when we mask
with CACHE_INDEX_KEY_MASK, we need to byte swap the int on big endian
platforms.

Bugzilla: https://bugs.freedesktop.org/103668
Bugzilla: https://bugs.gentoo.org/637060
Bugzilla: https://bugs.gentoo.org/636326
Fixes: 87ab26b2ab35 ("glsl: Add initial functions to implement an
                      on-disk cache")
Reviewed-by: Emil Velikov <emil.velikov at collabora.com>

---

 src/util/disk_cache.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/util/disk_cache.c b/src/util/disk_cache.c
index fde6e2e097..e95406565e 100644
--- a/src/util/disk_cache.c
+++ b/src/util/disk_cache.c
@@ -1170,7 +1170,7 @@ void
 disk_cache_put_key(struct disk_cache *cache, const cache_key key)
 {
    const uint32_t *key_chunk = (const uint32_t *) key;
-   int i = *key_chunk & CACHE_INDEX_KEY_MASK;
+   int i = CPU_TO_LE32(*key_chunk) & CACHE_INDEX_KEY_MASK;
    unsigned char *entry;
 
    entry = &cache->stored_keys[i * CACHE_KEY_SIZE];
@@ -1189,7 +1189,7 @@ bool
 disk_cache_has_key(struct disk_cache *cache, const cache_key key)
 {
    const uint32_t *key_chunk = (const uint32_t *) key;
-   int i = *key_chunk & CACHE_INDEX_KEY_MASK;
+   int i = CPU_TO_LE32(*key_chunk) & CACHE_INDEX_KEY_MASK;
    unsigned char *entry;
 
    entry = &cache->stored_keys[i * CACHE_KEY_SIZE];




More information about the mesa-commit mailing list