ALL: use #define and a copy helper to check and copy build_id

preparation for changing SHA1_DIGEST_LENGTH to BLAKE3_KEY_LEN

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/39110>
This commit is contained in:
Marek Olšák 2025-12-30 12:58:42 -05:00 committed by Marge Bot
parent 1912a00a91
commit 0b7ee3b981
18 changed files with 40 additions and 22 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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

View file

@ -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];

View file

@ -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];

View file

@ -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;

View file

@ -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);

View file

@ -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

View file

@ -38,7 +38,10 @@ extern "C" {
#if HAVE_BUILD_ID
#define BUILD_ID_EXPECTED_HASH_LENGTH 20 /* sha1 */
#include <stdint.h>
#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