vulkan: Add run-time object type asserts in handle casts
authorJason Ekstrand <jason@jlekstrand.net>
Tue, 21 Apr 2020 20:51:01 +0000 (15:51 -0500)
committerMarge Bot <eric+marge@anholt.net>
Mon, 4 May 2020 14:06:27 +0000 (14:06 +0000)
Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
Acked-by: Kristian H. Kristensen <hoegsberg@google.com>
Acked-by: Bas Nieuwenhuizen <bas@basnieuwenhuizen.nl>
Reviewed-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4690>

src/vulkan/util/vk_object.c
src/vulkan/util/vk_object.h

index d696515891421a538552dbf6f9ba4c5b92002d86..f84a68034b07d0e3a7ee414656eae4713397f66b 100644 (file)
@@ -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
index f91cc1640a0c9c9437cd3b008c1cd6f54867b572..bd32f1ad68124924121266a0d65376763365e760 100644 (file)
@@ -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;                                   \
    }