diff --git a/tools/tests/mali_compat_layer_test.c b/tools/tests/mali_compat_layer_test.c index 640460685c..3584a66000 100644 --- a/tools/tests/mali_compat_layer_test.c +++ b/tools/tests/mali_compat_layer_test.c @@ -182,6 +182,25 @@ static void test_spirv(const char *directory) assert(!spirv_strip_dual_source(truncated, 5, &out)); } +static void test_handle_map_null_key(void) +{ + struct handle_map map = {0}; + int first, second; + map_put(&map, 0x1000, &first); + map_put(&map, 0x2000, &second); + for (int i = 0; i < 1000; i++) + assert(!map_remove(&map, 0)); + map_put(&map, 0, &first); + assert(map.count == 2 && !map_get(&map, 0)); + for (uint64_t key = 1; key <= 30; key++) { + map_put(&map, 0x100000 + key, &first); + assert(map_remove(&map, 0x100000 + key) == &first); + } + assert(map.count == 2 && map.capacity == 64); + assert(map_get(&map, 0x1000) == &first && map_get(&map, 0x2000) == &second); + map_free(&map); +} + static VkResult VKAPI_CALL mock_create_instance(const VkInstanceCreateInfo *info, const VkAllocationCallbacks *allocator, VkInstance *instance) { @@ -464,6 +483,7 @@ static void test_capable_device(void) int main(int argc, char **argv) { test_spirv(argc > 1 ? argv[1] : NULL); + test_handle_map_null_key(); setenv("WINEPREFIX", "/nonexistent", 1); unsetenv("DROIDDECK_MALI_COMPAT"); diff --git a/tools/venus/layer/layer.c b/tools/venus/layer/layer.c index a4e35e0a32..21778b859e 100644 --- a/tools/venus/layer/layer.c +++ b/tools/venus/layer/layer.c @@ -60,9 +60,11 @@ static uint64_t hash(uint64_t key) return key; } +/* Key 0 marks an empty slot, so VK_NULL_HANDLE is never looked up, stored or removed. Destroying a + * null handle is valid Vulkan, and map_remove(0) would otherwise match an empty slot and drop count. */ void *map_get(const struct handle_map *map, uint64_t key) { - if (!map->capacity) + if (!map->capacity || !key) return NULL; for (uint32_t i = hash(key) & (map->capacity - 1);; i = (i + 1) & (map->capacity - 1)) { if (map->keys[i] == key) @@ -90,6 +92,8 @@ static void map_grow(struct handle_map *map) void map_put(struct handle_map *map, uint64_t key, void *value) { + if (!key) + return; if ((map->count + 1) * 4 > map->capacity * 3) map_grow(map); uint32_t i = hash(key) & (map->capacity - 1); @@ -103,7 +107,7 @@ void map_put(struct handle_map *map, uint64_t key, void *value) void *map_remove(struct handle_map *map, uint64_t key) { - if (!map->capacity) + if (!map->capacity || !key) return NULL; uint32_t mask = map->capacity - 1; uint32_t i = hash(key) & mask;