From: Jason Ekstrand Date: Tue, 21 Apr 2020 20:51:01 +0000 (-0500) Subject: vulkan: Add run-time object type asserts in handle casts X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=32f20783a512129fc2d7dd8a11ffa8670cef0068;p=mesa.git vulkan: Add run-time object type asserts in handle casts Reviewed-by: Lionel Landwerlin Acked-by: Kristian H. Kristensen Acked-by: Bas Nieuwenhuizen Reviewed-by: Samuel Pitoiset Part-of: --- diff --git a/src/vulkan/util/vk_object.c b/src/vulkan/util/vk_object.c index d6965158914..f84a68034b0 100644 --- a/src/vulkan/util/vk_object.c +++ b/src/vulkan/util/vk_object.c @@ -29,6 +29,9 @@ vk_object_base_init(UNUSED struct vk_device *device, UNUSED VkObjectType obj_type) { base->_loader_data.loaderMagic = ICD_LOADER_MAGIC; +#ifndef NDEBUG + base->type = obj_type; +#endif } void diff --git a/src/vulkan/util/vk_object.h b/src/vulkan/util/vk_object.h index f91cc1640a0..bd32f1ad681 100644 --- a/src/vulkan/util/vk_object.h +++ b/src/vulkan/util/vk_object.h @@ -36,6 +36,9 @@ struct vk_device; struct vk_object_base { VK_LOADER_DATA _loader_data; +#ifndef NDEBUG + VkObjectType type; +#endif }; void vk_object_base_init(UNUSED struct vk_device *device, @@ -43,6 +46,13 @@ void vk_object_base_init(UNUSED struct vk_device *device, UNUSED VkObjectType obj_type); void vk_object_base_finish(UNUSED struct vk_object_base *base); +static inline void +vk_object_base_assert_valid(ASSERTED struct vk_object_base *base, + ASSERTED VkObjectType obj_type) +{ + assert(base == NULL || base->type == obj_type); +} + struct vk_device { struct vk_object_base base; @@ -59,13 +69,16 @@ void vk_device_finish(struct vk_device *device); static inline struct __driver_type * \ __driver_type ## _from_handle(__VkType _handle) \ { \ + struct vk_object_base *base = (struct vk_object_base *)_handle; \ + vk_object_base_assert_valid(base, __VK_TYPE); \ STATIC_ASSERT(offsetof(struct __driver_type, __base) == 0); \ - return (struct __driver_type *) _handle; \ + return (struct __driver_type *) base; \ } \ \ static inline __VkType \ __driver_type ## _to_handle(struct __driver_type *_obj) \ { \ + vk_object_base_assert_valid(&_obj->__base, __VK_TYPE); \ return (__VkType) _obj; \ } @@ -73,13 +86,17 @@ void vk_device_finish(struct vk_device *device); static inline struct __driver_type * \ __driver_type ## _from_handle(__VkType _handle) \ { \ + struct vk_object_base *base = \ + (struct vk_object_base *)(uintptr_t)_handle; \ + vk_object_base_assert_valid(base, __VK_TYPE); \ STATIC_ASSERT(offsetof(struct __driver_type, __base) == 0); \ - return (struct __driver_type *)(uintptr_t) _handle; \ + return (struct __driver_type *)base; \ } \ \ static inline __VkType \ __driver_type ## _to_handle(struct __driver_type *_obj) \ { \ + vk_object_base_assert_valid(&_obj->__base, __VK_TYPE); \ return (__VkType)(uintptr_t) _obj; \ }