Monado OpenXR Runtime
oxr_api_verify.h
Go to the documentation of this file.
1 // Copyright 2018-2019, Collabora, Ltd.
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4  * @file
5  * @brief File for verifying app input into api functions.
6  * @author Jakob Bornecrantz <jakob@collabora.com>
7  * @ingroup oxr_api
8  */
9 
10 #pragma once
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 
17 #define _OXR_VERIFY_AND_SET_AND_INIT(log, thing, new_thing, THING, name, \
18  lookup) \
19  do { \
20  oxr_log_init(log, name); \
21  if ((void *)thing == NULL) { \
22  return oxr_error(log, XR_ERROR_HANDLE_INVALID, \
23  "(" #thing " == NULL)"); \
24  } \
25  new_thing = (__typeof__(new_thing))thing; \
26  if (new_thing->handle.debug != OXR_XR_DEBUG_##THING) { \
27  return oxr_error(log, XR_ERROR_HANDLE_INVALID, \
28  "(" #thing " == %p)", \
29  (void *)new_thing); \
30  } \
31  if (new_thing->handle.state != OXR_HANDLE_STATE_LIVE) { \
32  return oxr_error(log, XR_ERROR_HANDLE_INVALID, \
33  " state == %s (" #thing " == %p)", \
34  oxr_handle_state_to_string( \
35  new_thing->handle.state), \
36  (void *)new_thing); \
37  } \
38  oxr_log_set_instance(log, lookup); \
39  } while (0)
40 
41 #define _OXR_VERIFY_SET(log, arg, new_arg, THING) \
42  do { \
43  if ((void *)arg == NULL) { \
44  return oxr_error(log, XR_ERROR_HANDLE_INVALID, \
45  "(" #arg " == NULL)"); \
46  } \
47  new_arg = (__typeof__(new_arg))arg; \
48  if (new_arg->handle.debug != OXR_XR_DEBUG_##THING) { \
49  return oxr_error(log, XR_ERROR_HANDLE_INVALID, \
50  "(" #arg " == %p)", (void *)new_arg); \
51  } \
52  } while (0)
53 
54 
55 /*!
56  * @ingroup oxr_api
57  * @{
58  */
59 
60 // clang-format off
61 #define OXR_VERIFY_INSTANCE_AND_INIT_LOG(log, thing, new_thing, name) \
62  _OXR_VERIFY_AND_SET_AND_INIT(log, thing, new_thing, INSTANCE, name, new_thing)
63 #define OXR_VERIFY_MESSENGER_AND_INIT_LOG(log, thing, new_thing, name) \
64  _OXR_VERIFY_AND_SET_AND_INIT(log, thing, new_thing, MESSENGER, name, new_thing->inst)
65 #define OXR_VERIFY_SESSION_AND_INIT_LOG(log, thing, new_thing, name) \
66  _OXR_VERIFY_AND_SET_AND_INIT(log, thing, new_thing, SESSION, name, new_thing->sys->inst)
67 #define OXR_VERIFY_SPACE_AND_INIT_LOG(log, thing, new_thing, name) \
68  _OXR_VERIFY_AND_SET_AND_INIT(log, thing, new_thing, SPACE, name, new_thing->sess->sys->inst)
69 #define OXR_VERIFY_ACTION_AND_INIT_LOG(log, thing, new_thing, name) \
70  _OXR_VERIFY_AND_SET_AND_INIT(log, thing, new_thing, ACTION, name, new_thing->act_set->inst)
71 #define OXR_VERIFY_SWAPCHAIN_AND_INIT_LOG(log, thing, new_thing, name) \
72  _OXR_VERIFY_AND_SET_AND_INIT(log, thing, new_thing, SWAPCHAIN, name, new_thing->sess->sys->inst)
73 #define OXR_VERIFY_ACTIONSET_AND_INIT_LOG(log, thing, new_thing, name) \
74  _OXR_VERIFY_AND_SET_AND_INIT(log, thing, new_thing, ACTIONSET, name, new_thing->inst)
75 
76 #define OXR_VERIFY_INSTANCE_NOT_NULL(log, arg, new_arg) _OXR_VERIFY_SET(log, arg, new_arg, INSTANCE);
77 #define OXR_VERIFY_MESSENGER_NOT_NULL(log, arg, new_arg) _OXR_VERIFY_SET(log, arg, new_arg, MESSENGER);
78 #define OXR_VERIFY_SESSION_NOT_NULL(log, arg, new_arg) _OXR_VERIFY_SET(log, arg, new_arg, SESSION);
79 #define OXR_VERIFY_SPACE_NOT_NULL(log, arg, new_arg) _OXR_VERIFY_SET(log, arg, new_arg, SPACE);
80 #define OXR_VERIFY_ACTION_NOT_NULL(log, arg, new_arg) _OXR_VERIFY_SET(log, arg, new_arg, ACTION);
81 #define OXR_VERIFY_SWAPCHAIN_NOT_NULL(log, arg, new_arg) _OXR_VERIFY_SET(log, arg, new_arg, SWAPCHAIN);
82 #define OXR_VERIFY_ACTIONSET_NOT_NULL(log, arg, new_arg) _OXR_VERIFY_SET(log, arg, new_arg, ACTIONSET);
83 // clang-format on
84 
85 /*!
86  * Checks if a required extension is enabled.
87  *
88  * mixed_case_name should be the extension name without the XR_ prefix.
89  */
90 #define OXR_VERIFY_EXTENSION(log, inst, mixed_case_name) \
91  do { \
92  if (!(inst)->extensions.mixed_case_name) { \
93  return oxr_error((log), XR_ERROR_FUNCTION_UNSUPPORTED, \
94  " Requires XR_" #mixed_case_name \
95  " extension enabled"); \
96  } \
97  } while (false)
98 
99 #define OXR_VERIFY_ARG_NOT_NULL(log, arg) \
100  do { \
101  if (arg == NULL) { \
102  return oxr_error(log, XR_ERROR_VALIDATION_FAILURE, \
103  "(" #arg " == NULL)"); \
104  } \
105  } while (false)
106 
107 #define OXR_VERIFY_ARG_NOT_ZERO(log, arg) \
108  do { \
109  if (arg == 0) { \
110  return oxr_error(log, XR_ERROR_VALIDATION_FAILURE, \
111  "(" #arg " == 0) must be non-zero"); \
112  } \
113  } while (false)
114 
115 #define OXR_VERIFY_ARG_TYPE_CAN_BE_NULL(log, arg, type_enum) \
116  do { \
117  if (arg != NULL && arg->type != type_enum) { \
118  return oxr_error(log, XR_ERROR_VALIDATION_FAILURE, \
119  "(" #arg "->type == %u)", arg->type); \
120  } \
121  } while (false)
122 
123 #define OXR_VERIFY_ARG_TYPE_AND_NOT_NULL(log, arg, type_enum) \
124  do { \
125  if (arg == NULL) { \
126  return oxr_error(log, XR_ERROR_VALIDATION_FAILURE, \
127  "(" #arg " == NULL)"); \
128  } \
129  OXR_VERIFY_ARG_TYPE_CAN_BE_NULL(log, arg, type_enum); \
130  } while (false)
131 
132 #define OXR_VERIFY_SUBACTION_PATHS(log, count, paths) \
133  do { \
134  if (count > 0 && paths == NULL) { \
135  return oxr_error(log, XR_ERROR_VALIDATION_FAILURE, \
136  " " #count " is not zero but " #paths \
137  " is NULL"); \
138  } \
139  } while (false)
140 
141 #define OXR_VERIFY_ARG_SINGLE_LEVEL_FIXED_LENGTH_PATH(log, path) \
142  do { \
143  XrResult verify_ret = oxr_verify_fixed_size_single_level_path( \
144  log, path, ARRAY_SIZE(path), #path); \
145  if (verify_ret != XR_SUCCESS) { \
146  return verify_ret; \
147  } \
148  } while (false)
149 
150 #define OXR_VERIFY_ARG_LOCALIZED_NAME(log, string) \
151  do { \
152  XrResult verify_ret = oxr_verify_localized_name( \
153  log, string, ARRAY_SIZE(string), #string); \
154  if (verify_ret != XR_SUCCESS) { \
155  return verify_ret; \
156  } \
157  } while (false)
158 
159 #define OXR_VERIFY_POSE(log, p) \
160  do { \
161  if (!math_quat_validate((struct xrt_quat *)&p.orientation)) { \
162  return oxr_error(log, XR_ERROR_POSE_INVALID, \
163  "(" #p \
164  ".orientation) is not a valid quat"); \
165  } \
166  \
167  if (!math_vec3_validate((struct xrt_vec3 *)&p.position)) { \
168  return oxr_error(log, XR_ERROR_POSE_INVALID, \
169  "(" #p ".position) is not valid"); \
170  } \
171  } while (false)
172 
173 /*
174  *
175  * Implementation in oxr_verify.cpp
176  *
177  */
178 
179 XrResult
181  const char *path,
182  const char *name);
183 
184 /*!
185  * Verify a full path.
186  *
187  * Length not including zero terminator character but must be there.
188  */
189 XrResult
190 oxr_verify_full_path(struct oxr_logger *log,
191  const char *path,
192  size_t length,
193  const char *name);
194 
195 /*!
196  * Verify a single path level that sits inside of a fixed sized array.
197  */
198 XrResult
200  const char *path,
201  uint32_t array_size,
202  const char *name);
203 
204 /*!
205  * Verify an arbitrary UTF-8 string that sits inside of a fixed sized array.
206  */
207 XrResult
209  const char *string,
210  uint32_t array_size,
211  const char *name);
212 
213 /*!
214  * Verify a set of subaction paths for action creation.
215  */
216 XrResult
218  struct oxr_instance *inst,
219  uint32_t countSubactionPaths,
220  const XrPath *subactionPaths,
221  const char *variable);
222 
223 /*!
224  * Verify a set of subaction paths for action sync.
225  */
226 XrResult
228  struct oxr_instance *inst,
229  XrPath path,
230  uint32_t index);
231 
232 /*!
233  * Verify a set of subaction paths for action state get.
234  */
235 XrResult
237  struct oxr_instance *inst,
238  XrPath path,
239  const struct oxr_sub_paths *act_sub_paths,
240  struct oxr_sub_paths *out_sub_paths,
241  const char *variable);
242 
243 XrResult
245  const struct oxr_instance *,
246  const XrSessionCreateInfo *);
247 
248 #if defined(XR_USE_PLATFORM_XLIB) && defined(XR_USE_GRAPHICS_API_OPENGL)
249 XrResult
251  struct oxr_logger *, const XrGraphicsBindingOpenGLXlibKHR *);
252 #endif // defined(XR_USE_PLATFORM_XLIB) && defined(XR_USE_GRAPHICS_API_OPENGL)
253 
254 #if defined(XR_USE_GRAPHICS_API_VULKAN)
255 XrResult
257  const XrGraphicsBindingVulkanKHR *);
258 #endif // defined(XR_USE_GRAPHICS_API_VULKAN)
259 
260 #if defined(XR_USE_PLATFORM_EGL) && defined(XR_USE_GRAPHICS_API_OPENGL)
261 XrResult
262 oxr_verify_XrGraphicsBindingEGLMND(struct oxr_logger *log,
263  const XrGraphicsBindingEGLMND *next);
264 #endif // defined(XR_USE_PLATFORM_EGL) && defined(XR_USE_GRAPHICS_API_OPENGL)
265 
266 /*!
267  * @}
268  */
269 
270 
271 #ifdef __cplusplus
272 }
273 #endif
XrResult oxr_verify_localized_name(struct oxr_logger *, const char *string, uint32_t array_size, const char *name)
Verify an arbitrary UTF-8 string that sits inside of a fixed sized array.
Definition: oxr_verify.c:102
XrResult oxr_verify_subaction_paths_create(struct oxr_logger *log, struct oxr_instance *inst, uint32_t countSubactionPaths, const XrPath *subactionPaths, const char *variable)
Verify a set of subaction paths for action creation.
Definition: oxr_verify.c:353
XrResult oxr_verify_subaction_path_get(struct oxr_logger *log, struct oxr_instance *inst, XrPath path, const struct oxr_sub_paths *act_sub_paths, struct oxr_sub_paths *out_sub_paths, const char *variable)
Verify a set of subaction paths for action state get.
Definition: oxr_verify.c:397
XrResult oxr_verify_fixed_size_single_level_path(struct oxr_logger *, const char *path, uint32_t array_size, const char *name)
Verify a single path level that sits inside of a fixed sized array.
Definition: oxr_verify.c:62
XrResult oxr_verify_XrGraphicsBindingVulkanKHR(struct oxr_logger *, const XrGraphicsBindingVulkanKHR *)
Definition: oxr_verify.c:566
Logger struct that lives on the stack, one for each call client call.
Definition: oxr_logger.h:36
XrResult oxr_verify_full_path(struct oxr_logger *log, const char *path, size_t length, const char *name)
Verify a full path.
Definition: oxr_verify.c:154
XrResult oxr_verify_subaction_path_sync(struct oxr_logger *log, struct oxr_instance *inst, XrPath path, uint32_t index)
Verify a set of subaction paths for action sync.
Definition: oxr_verify.c:375
To carry around a sementic selection of sub action paths.
Definition: oxr_objects.h:1073
XrResult oxr_verify_full_path_c(struct oxr_logger *log, const char *path, const char *name)
Definition: oxr_verify.c:136
XrResult oxr_verify_XrSessionCreateInfo(struct oxr_logger *, const struct oxr_instance *, const XrSessionCreateInfo *)
Definition: oxr_verify.c:458
XrResult oxr_verify_XrGraphicsBindingOpenGLXlibKHR(struct oxr_logger *, const XrGraphicsBindingOpenGLXlibKHR *)
Definition: oxr_verify.c:533
struct oxr_instance * inst
Definition: oxr_logger.h:38
Main object that ties everything together.
Definition: oxr_objects.h:899