Path: blob/21.2-virgl/src/gbm/main/gbm_abi_check.c
4564 views
/*1* Copyright © 2021 NVIDIA Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT18* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,19* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER21* DEALINGS IN THE SOFTWARE.22*/2324#include "gbm_backend_abi.h" /* Current GBM backend ABI implementation */2526#include <stddef.h> /* offsetof */27#include <stdio.h> /* printf */2829/*30* The following are previous implementations of the structures defined in31* gbm_backend_abi.h, with their ABI version appended.32*33* DO NOT EVER CHANGE EXISTING DEFINITIONS HERE!34*35* Changing them implies breaking the GBM backend ABI. Instead, to extend the36* ABI, in gbm_backend_abi.h:37*38* -Add a new versioned struct39* -Append it to the associated top-level object's struct40* -Increment GBM_BACKEND_ABI_VERSION41*42* Then, here:43*44* -Add a new block of definitions below for the new ABI content45* -Add a new block of checks in main()46*/4748/* From: 49a7331cb02 - James Jones - gbm: Version the backend interface */49#define GBM_BACKEND_ABI_VERSION_abi0 050struct gbm_device_v0_abi0 {51const struct gbm_backend_desc *backend_desc;52uint32_t backend_version;53int fd;54const char *name;55void (*destroy)(struct gbm_device *gbm);56int (*is_format_supported)(struct gbm_device *gbm,57uint32_t format,58uint32_t usage);59int (*get_format_modifier_plane_count)(struct gbm_device *device,60uint32_t format,61uint64_t modifier);62struct gbm_bo *(*bo_create)(struct gbm_device *gbm,63uint32_t width, uint32_t height,64uint32_t format,65uint32_t usage,66const uint64_t *modifiers,67const unsigned int count);68struct gbm_bo *(*bo_import)(struct gbm_device *gbm, uint32_t type,69void *buffer, uint32_t usage);70void *(*bo_map)(struct gbm_bo *bo,71uint32_t x, uint32_t y,72uint32_t width, uint32_t height,73uint32_t flags, uint32_t *stride,74void **map_data);75void (*bo_unmap)(struct gbm_bo *bo, void *map_data);76int (*bo_write)(struct gbm_bo *bo, const void *buf, size_t data);77int (*bo_get_fd)(struct gbm_bo *bo);78int (*bo_get_planes)(struct gbm_bo *bo);79union gbm_bo_handle (*bo_get_handle)(struct gbm_bo *bo, int plane);80int (*bo_get_plane_fd)(struct gbm_bo *bo, int plane);81uint32_t (*bo_get_stride)(struct gbm_bo *bo, int plane);82uint32_t (*bo_get_offset)(struct gbm_bo *bo, int plane);83uint64_t (*bo_get_modifier)(struct gbm_bo *bo);84void (*bo_destroy)(struct gbm_bo *bo);85struct gbm_surface *(*surface_create)(struct gbm_device *gbm,86uint32_t width, uint32_t height,87uint32_t format, uint32_t flags,88const uint64_t *modifiers,89const unsigned count);90struct gbm_bo *(*surface_lock_front_buffer)(struct gbm_surface *surface);91void (*surface_release_buffer)(struct gbm_surface *surface,92struct gbm_bo *bo);93int (*surface_has_free_buffers)(struct gbm_surface *surface);94void (*surface_destroy)(struct gbm_surface *surface);95};9697struct gbm_device_abi0 {98/* Hack to make a gbm_device detectable by its first element. */99struct gbm_device *(*dummy)(int);100struct gbm_device_v0_abi0 v0;101};102103/**104* GBM buffer object interface corresponding to GBM_BACKEND_ABI_VERSION = 0105*106* DO NOT MODIFY THIS STRUCT. Instead, introduce a gbm_bo_v1, increment107* GBM_BACKEND_ABI_VERSION, and append gbm_bo_v1 to gbm_bo.108*/109struct gbm_bo_v0_abi0 {110uint32_t width;111uint32_t height;112uint32_t stride;113uint32_t format;114union gbm_bo_handle handle;115void *user_data;116void (*destroy_user_data)(struct gbm_bo *, void *);117};118119/**120* The allocated buffer object.121*122* The members in this structure should not be accessed directly.123*124* To modify this structure, introduce a new gbm_bo_v<N> structure, add it to125* the end of this structure, and increment GBM_BACKEND_ABI_VERSION.126*/127struct gbm_bo_abi0 {128struct gbm_device *gbm;129struct gbm_bo_v0_abi0 v0;130};131132/**133* GBM surface interface corresponding to GBM_BACKEND_ABI_VERSION = 0134*135* DO NOT MODIFY THIS STRUCT. Instead, introduce a gbm_surface_v1, increment136* GBM_BACKEND_ABI_VERSION, and append gbm_surface_v1 to gbm_surface.137*/138struct gbm_surface_v0_abi0 {139uint32_t width;140uint32_t height;141uint32_t format;142uint32_t flags;143struct {144uint64_t *modifiers;145unsigned count;146};147};148149/**150* An allocated GBM surface.151*152* To modify this structure, introduce a new gbm_surface_v<N> structure, add it153* to the end of this structure, and increment GBM_BACKEND_ABI_VERSION.154*/155struct gbm_surface_abi0 {156struct gbm_device *gbm;157struct gbm_surface_v0_abi0 v0;158};159160/**161* GBM backend interfaces corresponding to GBM_BACKEND_ABI_VERSION = 0162*163* DO NOT MODIFY THIS STRUCT. Instead, introduce a gbm_backend_v1, increment164* GBM_BACKEND_ABI_VERSION, append gbm_backend_v1 to gbm_backend.165*/166struct gbm_backend_v0_abi0 {167/**168* The version of the GBM backend interface supported by this backend. This169* is set by the backend itself, and may be greater or less than the version170* supported by the loader. It is the responsibility of the GBM loader to171* respect this version when accessing fields in this structure.172*/173uint32_t backend_version;174175const char *backend_name;176struct gbm_device *(*create_device)(int fd, uint32_t gbm_backend_version);177};178179/**180* The interface exposed by an external GBM backend.181*182* To modify this structure, introduce a new gbm_backend_v<N> structure, add it183* to the end of this structure, and increment GBM_BACKEND_ABI_VERSION.184*/185struct gbm_backend_abi0 {186struct gbm_backend_v0_abi0 v0;187};188189/**190* GBM interfaces exposed to GBM backends at GBM_BACKEND_ABI_VERSION >= 0191*192* DO NOT MODIFY THIS STRUCT. Instead, introduce a gbm_core_v1, increment193* GBM_BACKEND_ABI_VERSION, and append gbm_core_v1 to gbm_backend.194*/195struct gbm_core_v0_abi0 {196/**197* The version of the GBM backend interface supported by the GBM loader. This198* is set by the loader, and may be greater or less than the version199* supported by a given backend. It is the responsibility of the backend to200* respect this version when accessing fields in this structure and other201* structures allocated or modified by the loader.202*/203uint32_t core_version;204205uint32_t (*format_canonicalize)(uint32_t gbm_format);206};207208/**209* The interface exposed by the GBM core/loader code to GBM backends.210*211* To modify this structure, introduce a new gbm_core_v<N> structure, add it212* to the end of this structure, and increment GBM_BACKEND_ABI_VERSION.213*/214struct gbm_core_abi0 {215struct gbm_core_v0_abi0 v0;216};217218typedef const struct gbm_backend *(*GBM_GET_BACKEND_PROC_PTR_abi0)(const struct gbm_core *gbm_core);219220/*221* Structure/member ABI-checking helper macros222*/223#define MEMBER_SIZE(type, member) sizeof(((type *)0)->member)224225#define CHECK_RENAMED_MEMBER_BASE(type, a_ver, b_ver, a_member, b_member) \226do { \227if (offsetof(struct type ## a_ver, a_member) != \228offsetof(struct type ## b_ver, b_member)) { \229printf("Backards incompatible change detected!\n " \230"offsetof(struct " #type #a_ver "::" #a_member ") != " \231"offsetof(struct " #type #b_ver "::" #b_member ")\n"); \232return 1; \233} \234\235if (MEMBER_SIZE(struct type ## a_ver, a_member) != \236MEMBER_SIZE(struct type ## b_ver, b_member)) { \237printf("Backards incompatible change detected!\n " \238"MEMBER_SIZE(struct " #type #a_ver "::" #a_member ") != " \239"MEMBER_SIZE(struct " #type #b_ver "::" #b_member ")\n"); \240return 1; \241} \242} while (0)243244#define CHECK_RENAMED_MEMBER_TYPE(type, a_ver, b_ver, a_member, b_member) \245do { \246/* Compile-time type compatibility check */ \247struct type ## a_ver a; \248struct type ## b_ver b = {0}; \249a.a_member = b.b_member; \250(void)a; \251} while (0)252253#define CHECK_RENAMED_MEMBER(type, a_ver, b_ver, a_member, b_member) \254do { \255CHECK_RENAMED_MEMBER_BASE(type, a_ver, b_ver, a_member, b_member); \256CHECK_RENAMED_MEMBER_TYPE(type, a_ver, b_ver, a_member, b_member); \257} while (0)258#define CHECK_RENAMED_MEMBER_NO_TYPE(type, a_ver, b_ver, a_member, b_member) \259CHECK_RENAMED_MEMBER_BASE(type, a_ver, b_ver, a_member, b_member);260261#define CHECK_MEMBER(type, a_ver, b_ver, member) \262CHECK_RENAMED_MEMBER(type, a_ver, b_ver, member, member)263#define CHECK_MEMBER_NO_TYPE(type, a_ver, b_ver, member) \264CHECK_RENAMED_MEMBER_NO_TYPE(type, a_ver, b_ver, member, member)265#define CHECK_MEMBER_CURRENT(type, a_ver, member) \266CHECK_MEMBER(type, a_ver,, member)267#define CHECK_MEMBER_CURRENT_NO_TYPE(type, a_ver, member) \268CHECK_MEMBER_NO_TYPE(type, a_ver,, member)269270#define CHECK_SIZE(type, a_ver, b_ver) \271do { \272if (sizeof(struct type ## a_ver) > \273sizeof(struct type ## b_ver)) { \274printf("Backards incompatible change detected!\n " \275"sizeof(struct " #type #a_ver ") > " \276"sizeof(struct " #type #b_ver ")\n"); \277return 1; \278} \279} while (0)280281#define CHECK_SIZE_CURRENT(type, a_ver) \282do { \283if (sizeof(struct type ## a_ver) != \284sizeof(struct type)) { \285printf("Backards incompatible change detected!\n " \286"sizeof(struct " #type #a_ver ") != " \287"sizeof(struct " #type ")\n"); \288return 1; \289} \290} while (0)291292#define CHECK_VERSION(a_ver, b_ver) \293do { \294if ((GBM_BACKEND_ABI_VERSION ## a_ver) >= \295(GBM_BACKEND_ABI_VERSION ## b_ver)) { \296printf("Backards incompatible change detected!\n " \297"GBM_BACKEND_ABI_VERSION" #a_ver " >= " \298"GBM_BACKEND_ABI_VERSION" #b_ver "\n"); \299return 1; \300} \301} while (0)302303#define CHECK_VERSION_CURRENT(a_ver) \304do { \305if ((GBM_BACKEND_ABI_VERSION ## a_ver) != \306(GBM_BACKEND_ABI_VERSION)) { \307printf("Backards incompatible change detected!\n " \308"GBM_BACKEND_ABI_VERSION" #a_ver " != " \309"GBM_BACKEND_ABI_VERSION\n"); \310return 1; \311} \312} while (0)313314#define CHECK_PROC(proc, a_ver, b_ver) \315do { \316proc ## a_ver a; \317proc ## b_ver b = NULL; \318a = b; \319(void)a; \320} while (0)321322#define CHECK_PROC_CURRENT(proc, a_ver) \323CHECK_PROC(proc, a_ver,)324325int main(int argc, char **argv)326{327/********************************************/328/*** Compare Current ABI to ABI version 0 ***/329/********************************************/330331/* Check current gbm_device ABI against gbm_device_abi0*/332CHECK_MEMBER_CURRENT(gbm_device, _abi0, dummy);333CHECK_MEMBER_CURRENT_NO_TYPE(gbm_device, _abi0, v0);334335CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, backend_desc);336CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, backend_version);337CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, fd);338CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, name);339CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, destroy);340CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, is_format_supported);341CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, get_format_modifier_plane_count);342CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, bo_create);343CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, bo_import);344CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, bo_map);345CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, bo_unmap);346CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, bo_write);347CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, bo_get_fd);348CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, bo_get_planes);349CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, bo_get_handle);350CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, bo_get_plane_fd);351CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, bo_get_stride);352CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, bo_get_offset);353CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, bo_get_modifier);354CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, bo_destroy);355CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, surface_create);356CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, surface_lock_front_buffer);357CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, surface_release_buffer);358CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, surface_has_free_buffers);359CHECK_MEMBER_CURRENT(gbm_device_v0, _abi0, surface_destroy);360361/* Size of ABI-versioned substructures verified by above member checks */362CHECK_SIZE_CURRENT (gbm_device, _abi0);363364365/* Check current gbm_bo ABI against gbm_bo_abi0*/366CHECK_MEMBER_CURRENT(gbm_bo, _abi0, gbm);367CHECK_MEMBER_CURRENT_NO_TYPE(gbm_bo, _abi0, v0);368369CHECK_MEMBER_CURRENT(gbm_bo_v0, _abi0, width);370CHECK_MEMBER_CURRENT(gbm_bo_v0, _abi0, height);371CHECK_MEMBER_CURRENT(gbm_bo_v0, _abi0, stride);372CHECK_MEMBER_CURRENT(gbm_bo_v0, _abi0, format);373CHECK_MEMBER_CURRENT(gbm_bo_v0, _abi0, handle);374CHECK_MEMBER_CURRENT(gbm_bo_v0, _abi0, user_data);375CHECK_MEMBER_CURRENT(gbm_bo_v0, _abi0, destroy_user_data);376377/* Size of ABI-versioned substructures verified by above member checks */378CHECK_SIZE_CURRENT (gbm_bo, _abi0);379380381/* Check current gbm_surface ABI against gbm_surface_abi0 */382CHECK_MEMBER_CURRENT(gbm_surface, _abi0, gbm);383CHECK_MEMBER_CURRENT_NO_TYPE(gbm_surface, _abi0, v0);384385CHECK_MEMBER_CURRENT(gbm_surface_v0, _abi0, width);386CHECK_MEMBER_CURRENT(gbm_surface_v0, _abi0, height);387CHECK_MEMBER_CURRENT(gbm_surface_v0, _abi0, format);388CHECK_MEMBER_CURRENT(gbm_surface_v0, _abi0, flags);389CHECK_MEMBER_CURRENT(gbm_surface_v0, _abi0, modifiers);390CHECK_MEMBER_CURRENT(gbm_surface_v0, _abi0, count);391392/* Size of ABI-versioned substructures verified by above member checks */393CHECK_SIZE_CURRENT (gbm_surface, _abi0);394395396/* Check current gbm_backend ABI against gbm_backend_abi0 */397CHECK_MEMBER_CURRENT_NO_TYPE(gbm_backend, _abi0, v0);398399CHECK_MEMBER_CURRENT(gbm_backend_v0, _abi0, backend_version);400CHECK_MEMBER_CURRENT(gbm_backend_v0, _abi0, backend_name);401CHECK_MEMBER_CURRENT(gbm_backend_v0, _abi0, create_device);402403/* Size of ABI-versioned substructures verified by above member checks */404CHECK_SIZE_CURRENT (gbm_backend, _abi0);405406407/* Check current gbm_core ABI against gbm_core_abi0 */408CHECK_MEMBER_CURRENT_NO_TYPE(gbm_core, _abi0, v0);409410CHECK_MEMBER_CURRENT(gbm_core_v0, _abi0, core_version);411CHECK_MEMBER_CURRENT(gbm_core_v0, _abi0, format_canonicalize);412413/* Size of ABI-versioned substructures verified by above member checks */414CHECK_SIZE_CURRENT (gbm_core, _abi0);415416417CHECK_PROC_CURRENT (GBM_GET_BACKEND_PROC_PTR, _abi0);418419return 0;420}421422423