diff --git a/src/asahi/vulkan/hk_instance.c b/src/asahi/vulkan/hk_instance.c index 0212275b7d4..5fac9cabf66 100644 --- a/src/asahi/vulkan/hk_instance.c +++ b/src/asahi/vulkan/hk_instance.c @@ -170,14 +170,14 @@ hk_CreateInstance(const VkInstanceCreateInfo *pCreateInfo, } unsigned build_id_len = build_id_length(note); - if (build_id_len < 20) { + if (build_id_len < BUILD_ID_EXPECTED_HASH_LENGTH) { result = vk_errorf(NULL, VK_ERROR_INITIALIZATION_FAILED, "build-id too short. It needs to be a SHA"); goto fail_init; } static_assert(sizeof(instance->driver_build_sha) == SHA1_DIGEST_LENGTH); - memcpy(instance->driver_build_sha, build_id_data(note), SHA1_DIGEST_LENGTH); + copy_build_id_to_sha1(instance->driver_build_sha, note); *pInstance = hk_instance_to_handle(instance); return VK_SUCCESS; diff --git a/src/broadcom/vulkan/v3dv_device.c b/src/broadcom/vulkan/v3dv_device.c index 07f2b3eb59f..8481b4f16b4 100644 --- a/src/broadcom/vulkan/v3dv_device.c +++ b/src/broadcom/vulkan/v3dv_device.c @@ -767,13 +767,13 @@ init_uuids(struct v3dv_physical_device *device) } unsigned build_id_len = build_id_length(note); - if (build_id_len < 20) { + if (build_id_len < BUILD_ID_EXPECTED_HASH_LENGTH) { return vk_errorf(device->vk.instance, VK_ERROR_INITIALIZATION_FAILED, "build-id too short. It needs to be a SHA"); } - memcpy(device->driver_build_sha1, build_id_data(note), 20); + copy_build_id_to_sha1(device->driver_build_sha1, note); uint32_t vendor_id = v3dv_physical_device_vendor_id(device); uint32_t device_id = v3dv_physical_device_device_id(device); diff --git a/src/freedreno/ir3/ir3_disk_cache.c b/src/freedreno/ir3/ir3_disk_cache.c index b25ecbca8c6..43bb5fd6992 100644 --- a/src/freedreno/ir3/ir3_disk_cache.c +++ b/src/freedreno/ir3/ir3_disk_cache.c @@ -37,7 +37,7 @@ ir3_disk_cache_init(struct ir3_compiler *compiler) const struct build_id_note *note = build_id_find_nhdr_for_addr(ir3_disk_cache_init); unsigned build_id_len = build_id_length(note); - assert(note && build_id_len == 20); /* sha1 */ + assert(note && build_id_len == BUILD_ID_EXPECTED_HASH_LENGTH); /* sha1 */ const uint8_t *id_sha1 = build_id_data(note); assert(id_sha1); diff --git a/src/gallium/drivers/asahi/agx_disk_cache.c b/src/gallium/drivers/asahi/agx_disk_cache.c index 9bdcbb9a69e..eaafaf12515 100644 --- a/src/gallium/drivers/asahi/agx_disk_cache.c +++ b/src/gallium/drivers/asahi/agx_disk_cache.c @@ -210,7 +210,7 @@ agx_disk_cache_init(struct agx_screen *screen) const struct build_id_note *note = build_id_find_nhdr_for_addr(agx_disk_cache_init); - assert(note && build_id_length(note) == 20); + assert(note && build_id_length(note) == BUILD_ID_EXPECTED_HASH_LENGTH); const uint8_t *id_sha1 = build_id_data(note); assert(id_sha1); diff --git a/src/gallium/drivers/crocus/crocus_disk_cache.c b/src/gallium/drivers/crocus/crocus_disk_cache.c index 934ad386706..3fca4ba1099 100644 --- a/src/gallium/drivers/crocus/crocus_disk_cache.c +++ b/src/gallium/drivers/crocus/crocus_disk_cache.c @@ -245,7 +245,7 @@ crocus_disk_cache_init(struct crocus_screen *screen) const struct build_id_note *note = build_id_find_nhdr_for_addr(crocus_disk_cache_init); - assert(note && build_id_length(note) == 20); /* sha1 */ + assert(note && build_id_length(note) == BUILD_ID_EXPECTED_HASH_LENGTH); /* sha1 */ const uint8_t *id_sha1 = build_id_data(note); assert(id_sha1); diff --git a/src/gallium/drivers/etnaviv/etnaviv_disk_cache.c b/src/gallium/drivers/etnaviv/etnaviv_disk_cache.c index 68d54a8e747..052b342eaf6 100644 --- a/src/gallium/drivers/etnaviv/etnaviv_disk_cache.c +++ b/src/gallium/drivers/etnaviv/etnaviv_disk_cache.c @@ -39,7 +39,7 @@ etna_disk_cache_init(struct etna_compiler *compiler, const char *renderer) const struct build_id_note *note = build_id_find_nhdr_for_addr(etna_disk_cache_init); - assert(note && build_id_length(note) == 20); /* sha1 */ + assert(note && build_id_length(note) == BUILD_ID_EXPECTED_HASH_LENGTH); /* sha1 */ const uint8_t *id_sha1 = build_id_data(note); assert(id_sha1); diff --git a/src/gallium/drivers/iris/iris_disk_cache.c b/src/gallium/drivers/iris/iris_disk_cache.c index 4c623efe267..fda45163d81 100644 --- a/src/gallium/drivers/iris/iris_disk_cache.c +++ b/src/gallium/drivers/iris/iris_disk_cache.c @@ -374,7 +374,7 @@ iris_disk_cache_init(struct iris_screen *screen) const struct build_id_note *note = build_id_find_nhdr_for_addr(iris_disk_cache_init); - assert(note && build_id_length(note) == 20); /* sha1 */ + assert(note && build_id_length(note) == BUILD_ID_EXPECTED_HASH_LENGTH); /* sha1 */ const uint8_t *id_sha1 = build_id_data(note); assert(id_sha1); diff --git a/src/gallium/drivers/lima/lima_disk_cache.c b/src/gallium/drivers/lima/lima_disk_cache.c index e441bcbcc6e..ffd28fc2b70 100644 --- a/src/gallium/drivers/lima/lima_disk_cache.c +++ b/src/gallium/drivers/lima/lima_disk_cache.c @@ -193,7 +193,7 @@ lima_disk_cache_init(struct lima_screen *screen) { const struct build_id_note *note = build_id_find_nhdr_for_addr(lima_disk_cache_init); - assert(note && build_id_length(note) == 20); /* sha1 */ + assert(note && build_id_length(note) == BUILD_ID_EXPECTED_HASH_LENGTH); /* sha1 */ const uint8_t *id_sha1 = build_id_data(note); assert(id_sha1); diff --git a/src/gallium/drivers/panfrost/pan_disk_cache.c b/src/gallium/drivers/panfrost/pan_disk_cache.c index 6a1a3a0bcf4..75705c68d21 100644 --- a/src/gallium/drivers/panfrost/pan_disk_cache.c +++ b/src/gallium/drivers/panfrost/pan_disk_cache.c @@ -169,7 +169,7 @@ panfrost_disk_cache_init(struct panfrost_screen *screen) const struct build_id_note *note = build_id_find_nhdr_for_addr(panfrost_disk_cache_init); - assert(note && build_id_length(note) == 20); /* sha1 */ + assert(note && build_id_length(note) == BUILD_ID_EXPECTED_HASH_LENGTH); /* sha1 */ const uint8_t *id_sha1 = build_id_data(note); assert(id_sha1); diff --git a/src/gallium/drivers/v3d/v3d_disk_cache.c b/src/gallium/drivers/v3d/v3d_disk_cache.c index c508f03469c..2ab685547e9 100644 --- a/src/gallium/drivers/v3d/v3d_disk_cache.c +++ b/src/gallium/drivers/v3d/v3d_disk_cache.c @@ -54,7 +54,7 @@ void v3d_disk_cache_init(struct v3d_screen *screen) { const struct build_id_note *note = build_id_find_nhdr_for_addr(v3d_disk_cache_init); - assert(note && build_id_length(note) == 20); + assert(note && build_id_length(note) == BUILD_ID_EXPECTED_HASH_LENGTH); const uint8_t *id_sha1 = build_id_data(note); assert(id_sha1); diff --git a/src/gallium/drivers/virgl/virgl_screen.c b/src/gallium/drivers/virgl/virgl_screen.c index eadc8326247..487edd4e262 100644 --- a/src/gallium/drivers/virgl/virgl_screen.c +++ b/src/gallium/drivers/virgl/virgl_screen.c @@ -910,7 +910,7 @@ static void virgl_disk_cache_create(struct virgl_screen *screen) assert(note); unsigned build_id_len = build_id_length(note); - assert(build_id_len == 20); /* sha1 */ + assert(build_id_len == BUILD_ID_EXPECTED_HASH_LENGTH); /* sha1 */ const uint8_t *id_sha1 = build_id_data(note); assert(id_sha1); diff --git a/src/gallium/drivers/zink/zink_screen.c b/src/gallium/drivers/zink/zink_screen.c index c39b10ac5f3..bb6c837659f 100644 --- a/src/gallium/drivers/zink/zink_screen.c +++ b/src/gallium/drivers/zink/zink_screen.c @@ -309,7 +309,7 @@ disk_cache_init(struct zink_screen *screen) const struct build_id_note *note = build_id_find_nhdr_for_addr(disk_cache_init); unsigned build_id_len = build_id_length(note); - assert(note && build_id_len == 20); /* blake3 */ + assert(note && build_id_len == BUILD_ID_EXPECTED_HASH_LENGTH); _mesa_blake3_update(&ctx, build_id_data(note), build_id_len); #endif diff --git a/src/intel/vulkan/anv_physical_device.c b/src/intel/vulkan/anv_physical_device.c index d1c5ba4490b..5e012e83547 100644 --- a/src/intel/vulkan/anv_physical_device.c +++ b/src/intel/vulkan/anv_physical_device.c @@ -2354,12 +2354,12 @@ anv_physical_device_init_uuids(struct anv_physical_device *device) } unsigned build_id_len = build_id_length(note); - if (build_id_len < 20) { + if (build_id_len < BUILD_ID_EXPECTED_HASH_LENGTH) { return vk_errorf(device, VK_ERROR_INITIALIZATION_FAILED, "build-id too short. It needs to be a SHA"); } - memcpy(device->driver_build_sha1, build_id_data(note), 20); + copy_build_id_to_sha1(device->driver_build_sha1, note); struct mesa_sha1 sha1_ctx; uint8_t sha1[SHA1_DIGEST_LENGTH]; diff --git a/src/intel/vulkan_hasvk/anv_device.c b/src/intel/vulkan_hasvk/anv_device.c index fe91e6890fe..b7933ccce52 100644 --- a/src/intel/vulkan_hasvk/anv_device.c +++ b/src/intel/vulkan_hasvk/anv_device.c @@ -1404,12 +1404,12 @@ anv_physical_device_init_uuids(struct anv_physical_device *device) } unsigned build_id_len = build_id_length(note); - if (build_id_len < 20) { + if (build_id_len < BUILD_ID_EXPECTED_HASH_LENGTH) { return vk_errorf(device, VK_ERROR_INITIALIZATION_FAILED, "build-id too short. It needs to be a SHA"); } - memcpy(device->driver_build_sha1, build_id_data(note), 20); + copy_build_id_to_sha1(device->driver_build_sha1, note); struct mesa_sha1 sha1_ctx; uint8_t sha1[SHA1_DIGEST_LENGTH]; diff --git a/src/nouveau/vulkan/nvk_instance.c b/src/nouveau/vulkan/nvk_instance.c index 9fb7005d9ca..730fd293470 100644 --- a/src/nouveau/vulkan/nvk_instance.c +++ b/src/nouveau/vulkan/nvk_instance.c @@ -174,14 +174,14 @@ nvk_CreateInstance(const VkInstanceCreateInfo *pCreateInfo, } unsigned build_id_len = build_id_length(note); - if (build_id_len < 20) { + if (build_id_len < BUILD_ID_EXPECTED_HASH_LENGTH) { result = vk_errorf(NULL, VK_ERROR_INITIALIZATION_FAILED, "build-id too short. It needs to be a SHA"); goto fail_init; } STATIC_ASSERT(sizeof(instance->driver_build_sha) == SHA1_DIGEST_LENGTH); - memcpy(instance->driver_build_sha, build_id_data(note), SHA1_DIGEST_LENGTH); + copy_build_id_to_sha1(instance->driver_build_sha, note); *pInstance = nvk_instance_to_handle(instance); return VK_SUCCESS; diff --git a/src/panfrost/vulkan/panvk_instance.c b/src/panfrost/vulkan/panvk_instance.c index e57a9448847..e78765bd3ac 100644 --- a/src/panfrost/vulkan/panvk_instance.c +++ b/src/panfrost/vulkan/panvk_instance.c @@ -240,7 +240,7 @@ panvk_CreateInstance(const VkInstanceCreateInfo *pCreateInfo, } unsigned build_id_len = build_id_length(note); - if (build_id_len < 20) { + if (build_id_len < BUILD_ID_EXPECTED_HASH_LENGTH) { return panvk_errorf(NULL, VK_ERROR_INITIALIZATION_FAILED, "build-id too short. It needs to be a SHA"); } @@ -282,7 +282,7 @@ panvk_CreateInstance(const VkInstanceCreateInfo *pCreateInfo, VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false)); STATIC_ASSERT(sizeof(instance->driver_build_sha) == SHA1_DIGEST_LENGTH); - memcpy(instance->driver_build_sha, build_id_data(note), SHA1_DIGEST_LENGTH); + copy_build_id_to_sha1(instance->driver_build_sha, note); *pInstance = panvk_instance_to_handle(instance); diff --git a/src/util/build_id.c b/src/util/build_id.c index 80187fa6d03..b699161d9b7 100644 --- a/src/util/build_id.c +++ b/src/util/build_id.c @@ -188,4 +188,15 @@ build_id_data(const struct build_id_note *note) #endif /* DETECT_OS_APPLE */ } +void +copy_build_id_to_sha1(uint8_t sha1[SHA1_DIGEST_LENGTH], + const struct build_id_note *note) +{ + unsigned length = build_id_length(note); + + assert(length <= SHA1_DIGEST_LENGTH); + memcpy(sha1, build_id_data(note), length); + memset(sha1 + length, 0, SHA1_DIGEST_LENGTH - length); +} + #endif diff --git a/src/util/build_id.h b/src/util/build_id.h index f78c6ef98b2..5cd9766e63f 100644 --- a/src/util/build_id.h +++ b/src/util/build_id.h @@ -38,7 +38,10 @@ extern "C" { #if HAVE_BUILD_ID +#define BUILD_ID_EXPECTED_HASH_LENGTH 20 /* sha1 */ + #include +#include "sha1/sha1.h" struct build_id_note; @@ -51,6 +54,10 @@ build_id_length(const struct build_id_note *note); const uint8_t * build_id_data(const struct build_id_note *note); +void +copy_build_id_to_sha1(uint8_t sha1[SHA1_DIGEST_LENGTH], + const struct build_id_note *note); + #endif #ifdef __cplusplus