| 1 | /* |
| 2 | * Copyright (C) 2018 Francis Deslauriers <francis.deslauriers@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <stdbool.h> |
| 9 | #include <string.h> |
| 10 | |
| 11 | #include "ust-field-utils.h" |
| 12 | |
| 13 | /* |
| 14 | * The lttng_ust_ctl_field is made of a combination of C basic types |
| 15 | * lttng_ust_ctl_basic_type and _lttng_ust_ctl_basic_type. |
| 16 | * |
| 17 | * lttng_ust_ctl_basic_type contains an enumeration describing the abstract type. |
| 18 | * _lttng_ust_ctl_basic_type does _NOT_ contain an enumeration describing the |
| 19 | * abstract type. |
| 20 | * |
| 21 | * A layer is needed to use the same code for both structures. |
| 22 | * When dealing with _lttng_ust_ctl_basic_type, we need to use the abstract type of |
| 23 | * the lttng_ust_ctl_type struct. |
| 24 | */ |
| 25 | |
| 26 | /* |
| 27 | * Compare two lttng_ust_ctl_integer_type fields. |
| 28 | * Returns 1 if both are identical. |
| 29 | */ |
| 30 | static bool match_lttng_ust_ctl_field_integer(const struct lttng_ust_ctl_integer_type *first, |
| 31 | const struct lttng_ust_ctl_integer_type *second) |
| 32 | { |
| 33 | if (first->size != second->size) { |
| 34 | goto no_match; |
| 35 | } |
| 36 | if (first->alignment != second->alignment) { |
| 37 | goto no_match; |
| 38 | } |
| 39 | if (first->signedness != second->signedness) { |
| 40 | goto no_match; |
| 41 | } |
| 42 | if (first->encoding != second->encoding) { |
| 43 | goto no_match; |
| 44 | } |
| 45 | if (first->base != second->base) { |
| 46 | goto no_match; |
| 47 | } |
| 48 | if (first->reverse_byte_order != second->reverse_byte_order) { |
| 49 | goto no_match; |
| 50 | } |
| 51 | |
| 52 | return true; |
| 53 | |
| 54 | no_match: |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * Compare two _lttng_ust_ctl_basic_type fields known to be of type integer. |
| 60 | * Returns 1 if both are identical. |
| 61 | */ |
| 62 | static bool match_lttng_ust_ctl_field_integer_from_raw_basic_type( |
| 63 | const union _lttng_ust_ctl_basic_type *first, |
| 64 | const union _lttng_ust_ctl_basic_type *second) |
| 65 | { |
| 66 | return match_lttng_ust_ctl_field_integer(&first->integer, &second->integer); |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Compare two _lttng_ust_ctl_basic_type fields known to be of type enum. |
| 71 | * Returns 1 if both are identical. |
| 72 | */ |
| 73 | static bool match_lttng_ust_ctl_field_enum_from_raw_basic_type( |
| 74 | const union _lttng_ust_ctl_basic_type *first, |
| 75 | const union _lttng_ust_ctl_basic_type *second) |
| 76 | { |
| 77 | /* |
| 78 | * Compare enumeration ID. Enumeration ID is provided to the application by |
| 79 | * the session daemon before event registration. |
| 80 | */ |
| 81 | if (first->enumeration.id != second->enumeration.id) { |
| 82 | goto no_match; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Sanity check of the name and container type. Those were already checked |
| 87 | * during enum registration. |
| 88 | */ |
| 89 | if (strncmp(first->enumeration.name, second->enumeration.name, |
| 90 | LTTNG_UST_ABI_SYM_NAME_LEN)) { |
| 91 | goto no_match; |
| 92 | } |
| 93 | if (!match_lttng_ust_ctl_field_integer(&first->enumeration.container_type, |
| 94 | &second->enumeration.container_type)) { |
| 95 | goto no_match; |
| 96 | } |
| 97 | |
| 98 | return true; |
| 99 | |
| 100 | no_match: |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Compare two _lttng_ust_ctl_basic_type fields known to be of type string. |
| 106 | * Returns 1 if both are identical. |
| 107 | */ |
| 108 | static bool match_lttng_ust_ctl_field_string_from_raw_basic_type( |
| 109 | const union _lttng_ust_ctl_basic_type *first, |
| 110 | const union _lttng_ust_ctl_basic_type *second) |
| 111 | { |
| 112 | return first->string.encoding == second->string.encoding; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Compare two _lttng_ust_ctl_basic_type fields known to be of type float. |
| 117 | * Returns 1 if both are identical. |
| 118 | */ |
| 119 | static bool match_lttng_ust_ctl_field_float_from_raw_basic_type( |
| 120 | const union _lttng_ust_ctl_basic_type *first, |
| 121 | const union _lttng_ust_ctl_basic_type *second) |
| 122 | { |
| 123 | if (first->_float.exp_dig != second->_float.exp_dig) { |
| 124 | goto no_match; |
| 125 | } |
| 126 | |
| 127 | if (first->_float.mant_dig != second->_float.mant_dig) { |
| 128 | goto no_match; |
| 129 | } |
| 130 | |
| 131 | if (first->_float.reverse_byte_order != |
| 132 | second->_float.reverse_byte_order) { |
| 133 | goto no_match; |
| 134 | } |
| 135 | |
| 136 | if (first->_float.alignment != second->_float.alignment) { |
| 137 | goto no_match; |
| 138 | } |
| 139 | |
| 140 | return true; |
| 141 | |
| 142 | no_match: |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Compare two _lttng_ust_ctl_basic_type fields given their respective abstract types. |
| 148 | * Returns 1 if both are identical. |
| 149 | */ |
| 150 | static bool match_lttng_ust_ctl_field_raw_basic_type( |
| 151 | enum lttng_ust_ctl_abstract_types first_atype, |
| 152 | const union _lttng_ust_ctl_basic_type *first, |
| 153 | enum lttng_ust_ctl_abstract_types second_atype, |
| 154 | const union _lttng_ust_ctl_basic_type *second) |
| 155 | { |
| 156 | if (first_atype != second_atype) { |
| 157 | goto no_match; |
| 158 | } |
| 159 | |
| 160 | switch (first_atype) { |
| 161 | case lttng_ust_ctl_atype_integer: |
| 162 | if (!match_lttng_ust_ctl_field_integer_from_raw_basic_type(first, second)) { |
| 163 | goto no_match; |
| 164 | } |
| 165 | break; |
| 166 | case lttng_ust_ctl_atype_enum: |
| 167 | if (!match_lttng_ust_ctl_field_enum_from_raw_basic_type(first, second)) { |
| 168 | goto no_match; |
| 169 | } |
| 170 | break; |
| 171 | case lttng_ust_ctl_atype_string: |
| 172 | if (!match_lttng_ust_ctl_field_string_from_raw_basic_type(first, second)) { |
| 173 | goto no_match; |
| 174 | } |
| 175 | break; |
| 176 | case lttng_ust_ctl_atype_float: |
| 177 | if (!match_lttng_ust_ctl_field_float_from_raw_basic_type(first, second)) { |
| 178 | goto no_match; |
| 179 | } |
| 180 | break; |
| 181 | default: |
| 182 | goto no_match; |
| 183 | } |
| 184 | |
| 185 | return true; |
| 186 | |
| 187 | no_match: |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Compatibility layer between the lttng_ust_ctl_basic_type struct and |
| 193 | * _lttng_ust_ctl_basic_type union. |
| 194 | */ |
| 195 | static bool match_lttng_ust_ctl_field_basic_type(const struct lttng_ust_ctl_basic_type *first, |
| 196 | const struct lttng_ust_ctl_basic_type *second) |
| 197 | { |
| 198 | return match_lttng_ust_ctl_field_raw_basic_type(first->atype, &first->u.basic, |
| 199 | second->atype, &second->u.basic); |
| 200 | } |
| 201 | |
| 202 | int match_lttng_ust_ctl_field(const struct lttng_ust_ctl_field *first, |
| 203 | const struct lttng_ust_ctl_field *second) |
| 204 | { |
| 205 | /* Check the name of the field is identical. */ |
| 206 | if (strncmp(first->name, second->name, LTTNG_UST_ABI_SYM_NAME_LEN)) { |
| 207 | goto no_match; |
| 208 | } |
| 209 | |
| 210 | /* Check the field type is identical. */ |
| 211 | if (first->type.atype != second->type.atype) { |
| 212 | goto no_match; |
| 213 | } |
| 214 | |
| 215 | /* Check the field layout. */ |
| 216 | switch (first->type.atype) { |
| 217 | case lttng_ust_ctl_atype_integer: |
| 218 | case lttng_ust_ctl_atype_enum: |
| 219 | case lttng_ust_ctl_atype_string: |
| 220 | case lttng_ust_ctl_atype_float: |
| 221 | if (!match_lttng_ust_ctl_field_raw_basic_type(first->type.atype, |
| 222 | &first->type.u.legacy.basic, second->type.atype, |
| 223 | &second->type.u.legacy.basic)) { |
| 224 | goto no_match; |
| 225 | } |
| 226 | break; |
| 227 | case lttng_ust_ctl_atype_sequence: |
| 228 | /* Match element type of the sequence. */ |
| 229 | if (!match_lttng_ust_ctl_field_basic_type(&first->type.u.legacy.sequence.elem_type, |
| 230 | &second->type.u.legacy.sequence.elem_type)) { |
| 231 | goto no_match; |
| 232 | } |
| 233 | |
| 234 | /* Match length type of the sequence. */ |
| 235 | if (!match_lttng_ust_ctl_field_basic_type(&first->type.u.legacy.sequence.length_type, |
| 236 | &second->type.u.legacy.sequence.length_type)) { |
| 237 | goto no_match; |
| 238 | } |
| 239 | break; |
| 240 | case lttng_ust_ctl_atype_array: |
| 241 | /* Match element type of the array. */ |
| 242 | if (!match_lttng_ust_ctl_field_basic_type(&first->type.u.legacy.array.elem_type, |
| 243 | &second->type.u.legacy.array.elem_type)) { |
| 244 | goto no_match; |
| 245 | } |
| 246 | |
| 247 | /* Match length of the array. */ |
| 248 | if (first->type.u.legacy.array.length != second->type.u.legacy.array.length) { |
| 249 | goto no_match; |
| 250 | } |
| 251 | break; |
| 252 | case lttng_ust_ctl_atype_variant: |
| 253 | /* Compare number of choice of the variants. */ |
| 254 | if (first->type.u.legacy.variant.nr_choices != |
| 255 | second->type.u.legacy.variant.nr_choices) { |
| 256 | goto no_match; |
| 257 | } |
| 258 | |
| 259 | /* Compare tag name of the variants. */ |
| 260 | if (strncmp(first->type.u.legacy.variant.tag_name, |
| 261 | second->type.u.legacy.variant.tag_name, |
| 262 | LTTNG_UST_ABI_SYM_NAME_LEN)) { |
| 263 | goto no_match; |
| 264 | } |
| 265 | break; |
| 266 | case lttng_ust_ctl_atype_struct: |
| 267 | /* Compare number of fields of the structs. */ |
| 268 | if (first->type.u.legacy._struct.nr_fields != second->type.u.legacy._struct.nr_fields) { |
| 269 | goto no_match; |
| 270 | } |
| 271 | break; |
| 272 | case lttng_ust_ctl_atype_sequence_nestable: |
| 273 | if (first->type.u.sequence_nestable.alignment != second->type.u.sequence_nestable.alignment) { |
| 274 | goto no_match; |
| 275 | } |
| 276 | /* Compare length_name of the sequences. */ |
| 277 | if (strncmp(first->type.u.sequence_nestable.length_name, |
| 278 | second->type.u.sequence_nestable.length_name, |
| 279 | LTTNG_UST_ABI_SYM_NAME_LEN)) { |
| 280 | goto no_match; |
| 281 | } |
| 282 | /* Comparison will be done when marshalling following items. */ |
| 283 | break; |
| 284 | case lttng_ust_ctl_atype_array_nestable: |
| 285 | if (first->type.u.array_nestable.alignment != second->type.u.array_nestable.alignment) { |
| 286 | goto no_match; |
| 287 | } |
| 288 | /* Match length of the array. */ |
| 289 | if (first->type.u.array_nestable.length != second->type.u.array_nestable.length) { |
| 290 | goto no_match; |
| 291 | } |
| 292 | /* Comparison of element type will be done when marshalling following item. */ |
| 293 | break; |
| 294 | case lttng_ust_ctl_atype_enum_nestable: |
| 295 | if (first->type.u.enum_nestable.id != second->type.u.enum_nestable.id) { |
| 296 | goto no_match; |
| 297 | } |
| 298 | /* Compare name of the enums. */ |
| 299 | if (strncmp(first->type.u.enum_nestable.name, |
| 300 | second->type.u.enum_nestable.name, |
| 301 | LTTNG_UST_ABI_SYM_NAME_LEN)) { |
| 302 | goto no_match; |
| 303 | } |
| 304 | /* Comparison of element type will be done when marshalling following item. */ |
| 305 | break; |
| 306 | case lttng_ust_ctl_atype_struct_nestable: |
| 307 | if (first->type.u.struct_nestable.alignment != second->type.u.struct_nestable.alignment) { |
| 308 | goto no_match; |
| 309 | } |
| 310 | /* Compare number of fields of the structs. */ |
| 311 | if (first->type.u.struct_nestable.nr_fields != second->type.u.struct_nestable.nr_fields) { |
| 312 | goto no_match; |
| 313 | } |
| 314 | break; |
| 315 | case lttng_ust_ctl_atype_variant_nestable: |
| 316 | if (first->type.u.variant_nestable.alignment != second->type.u.variant_nestable.alignment) { |
| 317 | goto no_match; |
| 318 | } |
| 319 | /* Compare number of choice of the variants. */ |
| 320 | if (first->type.u.variant_nestable.nr_choices != |
| 321 | second->type.u.variant_nestable.nr_choices) { |
| 322 | goto no_match; |
| 323 | } |
| 324 | |
| 325 | /* Compare tag name of the variants. */ |
| 326 | if (strncmp(first->type.u.variant_nestable.tag_name, |
| 327 | second->type.u.variant_nestable.tag_name, |
| 328 | LTTNG_UST_ABI_SYM_NAME_LEN)) { |
| 329 | goto no_match; |
| 330 | } |
| 331 | break; |
| 332 | default: |
| 333 | goto no_match; |
| 334 | } |
| 335 | |
| 336 | return true; |
| 337 | |
| 338 | no_match: |
| 339 | return false; |
| 340 | } |