| 1 | /* |
| 2 | * Copyright 2018 - Philippe Proulx <pproulx@efficios.com> |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | */ |
| 14 | |
| 15 | #define BT_LOG_TAG "PLUGIN/CTF/META/VALIDATE" |
| 16 | #include "logging.h" |
| 17 | |
| 18 | #include <babeltrace2/babeltrace.h> |
| 19 | #include "common/macros.h" |
| 20 | #include "common/assert.h" |
| 21 | #include <glib.h> |
| 22 | #include <stdint.h> |
| 23 | #include <string.h> |
| 24 | #include <inttypes.h> |
| 25 | |
| 26 | #include "ctf-meta-visitors.h" |
| 27 | |
| 28 | static |
| 29 | int validate_stream_class(struct ctf_stream_class *sc) |
| 30 | { |
| 31 | int ret = 0; |
| 32 | struct ctf_field_class_int *int_fc; |
| 33 | struct ctf_field_class *fc; |
| 34 | |
| 35 | if (sc->is_translated) { |
| 36 | goto end; |
| 37 | } |
| 38 | |
| 39 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( |
| 40 | (void *) sc->packet_context_fc, "timestamp_begin"); |
| 41 | if (fc) { |
| 42 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
| 43 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { |
| 44 | BT_LOGE_STR("Invalid packet context field class: " |
| 45 | "`timestamp_begin` member is not an integer field class."); |
| 46 | goto invalid; |
| 47 | } |
| 48 | |
| 49 | int_fc = (void *) fc; |
| 50 | |
| 51 | if (int_fc->is_signed) { |
| 52 | BT_LOGE_STR("Invalid packet context field class: " |
| 53 | "`timestamp_begin` member is signed."); |
| 54 | goto invalid; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( |
| 59 | (void *) sc->packet_context_fc, "timestamp_end"); |
| 60 | if (fc) { |
| 61 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
| 62 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { |
| 63 | BT_LOGE_STR("Invalid packet context field class: " |
| 64 | "`timestamp_end` member is not an integer field class."); |
| 65 | goto invalid; |
| 66 | } |
| 67 | |
| 68 | int_fc = (void *) fc; |
| 69 | |
| 70 | if (int_fc->is_signed) { |
| 71 | BT_LOGE_STR("Invalid packet context field class: " |
| 72 | "`timestamp_end` member is signed."); |
| 73 | goto invalid; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( |
| 78 | (void *) sc->packet_context_fc, "events_discarded"); |
| 79 | if (fc) { |
| 80 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
| 81 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { |
| 82 | BT_LOGE_STR("Invalid packet context field class: " |
| 83 | "`events_discarded` member is not an integer field class."); |
| 84 | goto invalid; |
| 85 | } |
| 86 | |
| 87 | int_fc = (void *) fc; |
| 88 | |
| 89 | if (int_fc->is_signed) { |
| 90 | BT_LOGE_STR("Invalid packet context field class: " |
| 91 | "`events_discarded` member is signed."); |
| 92 | goto invalid; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( |
| 97 | (void *) sc->packet_context_fc, "packet_seq_num"); |
| 98 | if (fc) { |
| 99 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
| 100 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { |
| 101 | BT_LOGE_STR("Invalid packet context field class: " |
| 102 | "`packet_seq_num` member is not an integer field class."); |
| 103 | goto invalid; |
| 104 | } |
| 105 | |
| 106 | int_fc = (void *) fc; |
| 107 | |
| 108 | if (int_fc->is_signed) { |
| 109 | BT_LOGE_STR("Invalid packet context field class: " |
| 110 | "`packet_seq_num` member is signed."); |
| 111 | goto invalid; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( |
| 116 | (void *) sc->packet_context_fc, "packet_size"); |
| 117 | if (fc) { |
| 118 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
| 119 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { |
| 120 | BT_LOGE_STR("Invalid packet context field class: " |
| 121 | "`packet_size` member is not an integer field class."); |
| 122 | goto invalid; |
| 123 | } |
| 124 | |
| 125 | int_fc = (void *) fc; |
| 126 | |
| 127 | if (int_fc->is_signed) { |
| 128 | BT_LOGE_STR("Invalid packet context field class: " |
| 129 | "`packet_size` member is signed."); |
| 130 | goto invalid; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( |
| 135 | (void *) sc->packet_context_fc, "content_size"); |
| 136 | if (fc) { |
| 137 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
| 138 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { |
| 139 | BT_LOGE_STR("Invalid packet context field class: " |
| 140 | "`content_size` member is not an integer field class."); |
| 141 | goto invalid; |
| 142 | } |
| 143 | |
| 144 | int_fc = (void *) fc; |
| 145 | |
| 146 | if (int_fc->is_signed) { |
| 147 | BT_LOGE_STR("Invalid packet context field class: " |
| 148 | "`content_size` member is signed."); |
| 149 | goto invalid; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( |
| 154 | (void *) sc->event_header_fc, "id"); |
| 155 | if (fc) { |
| 156 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
| 157 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { |
| 158 | BT_LOGE_STR("Invalid event header field class: " |
| 159 | "`id` member is not an integer field class."); |
| 160 | goto invalid; |
| 161 | } |
| 162 | |
| 163 | int_fc = (void *) fc; |
| 164 | |
| 165 | if (int_fc->is_signed) { |
| 166 | BT_LOGE_STR("Invalid event header field class: " |
| 167 | "`id` member is signed."); |
| 168 | goto invalid; |
| 169 | } |
| 170 | } else { |
| 171 | if (sc->event_classes->len > 1) { |
| 172 | BT_LOGE_STR("Invalid event header field class: " |
| 173 | "missing `id` member as there's " |
| 174 | "more than one event class."); |
| 175 | goto invalid; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | goto end; |
| 180 | |
| 181 | invalid: |
| 182 | ret = -1; |
| 183 | |
| 184 | end: |
| 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | BT_HIDDEN |
| 189 | int ctf_trace_class_validate(struct ctf_trace_class *ctf_tc) |
| 190 | { |
| 191 | int ret = 0; |
| 192 | struct ctf_field_class_int *int_fc; |
| 193 | uint64_t i; |
| 194 | |
| 195 | if (!ctf_tc->is_translated) { |
| 196 | struct ctf_field_class *fc; |
| 197 | |
| 198 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( |
| 199 | (void *) ctf_tc->packet_header_fc, "magic"); |
| 200 | if (fc) { |
| 201 | struct ctf_named_field_class *named_fc = |
| 202 | ctf_field_class_struct_borrow_member_by_index( |
| 203 | (void *) ctf_tc->packet_header_fc, |
| 204 | 0); |
| 205 | |
| 206 | if (named_fc->fc != fc) { |
| 207 | BT_LOGE_STR("Invalid packet header field class: " |
| 208 | "`magic` member is not the first member."); |
| 209 | goto invalid; |
| 210 | } |
| 211 | |
| 212 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
| 213 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { |
| 214 | BT_LOGE_STR("Invalid packet header field class: " |
| 215 | "`magic` member is not an integer field class."); |
| 216 | goto invalid; |
| 217 | } |
| 218 | |
| 219 | int_fc = (void *) fc; |
| 220 | |
| 221 | if (int_fc->is_signed) { |
| 222 | BT_LOGE_STR("Invalid packet header field class: " |
| 223 | "`magic` member is signed."); |
| 224 | goto invalid; |
| 225 | } |
| 226 | |
| 227 | if (int_fc->base.size != 32) { |
| 228 | BT_LOGE_STR("Invalid packet header field class: " |
| 229 | "`magic` member is not 32-bit."); |
| 230 | goto invalid; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( |
| 235 | (void *) ctf_tc->packet_header_fc, "stream_id"); |
| 236 | if (fc) { |
| 237 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
| 238 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { |
| 239 | BT_LOGE_STR("Invalid packet header field class: " |
| 240 | "`stream_id` member is not an integer field class."); |
| 241 | goto invalid; |
| 242 | } |
| 243 | |
| 244 | int_fc = (void *) fc; |
| 245 | |
| 246 | if (int_fc->is_signed) { |
| 247 | BT_LOGE_STR("Invalid packet header field class: " |
| 248 | "`stream_id` member is signed."); |
| 249 | goto invalid; |
| 250 | } |
| 251 | } else { |
| 252 | if (ctf_tc->stream_classes->len > 1) { |
| 253 | BT_LOGE_STR("Invalid packet header field class: " |
| 254 | "missing `stream_id` member as there's " |
| 255 | "more than one stream class."); |
| 256 | goto invalid; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( |
| 261 | (void *) ctf_tc->packet_header_fc, |
| 262 | "stream_instance_id"); |
| 263 | if (fc) { |
| 264 | if (fc->type != CTF_FIELD_CLASS_TYPE_INT && |
| 265 | fc->type != CTF_FIELD_CLASS_TYPE_ENUM) { |
| 266 | BT_LOGE_STR("Invalid packet header field class: " |
| 267 | "`stream_instance_id` member is not an integer field class."); |
| 268 | goto invalid; |
| 269 | } |
| 270 | |
| 271 | int_fc = (void *) fc; |
| 272 | |
| 273 | if (int_fc->is_signed) { |
| 274 | BT_LOGE_STR("Invalid packet header field class: " |
| 275 | "`stream_instance_id` member is signed."); |
| 276 | goto invalid; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | fc = ctf_field_class_struct_borrow_member_field_class_by_name( |
| 281 | (void *) ctf_tc->packet_header_fc, "uuid"); |
| 282 | if (fc) { |
| 283 | struct ctf_field_class_array *array_fc = (void *) fc; |
| 284 | |
| 285 | if (fc->type != CTF_FIELD_CLASS_TYPE_ARRAY) { |
| 286 | BT_LOGE_STR("Invalid packet header field class: " |
| 287 | "`uuid` member is not an array field class."); |
| 288 | goto invalid; |
| 289 | } |
| 290 | |
| 291 | array_fc = (void *) fc; |
| 292 | |
| 293 | if (array_fc->length != 16) { |
| 294 | BT_LOGE_STR("Invalid packet header field class: " |
| 295 | "`uuid` member is not a 16-element array field class."); |
| 296 | goto invalid; |
| 297 | } |
| 298 | |
| 299 | if (array_fc->base.elem_fc->type != CTF_FIELD_CLASS_TYPE_INT) { |
| 300 | BT_LOGE_STR("Invalid packet header field class: " |
| 301 | "`uuid` member's element field class is not " |
| 302 | "an integer field class."); |
| 303 | goto invalid; |
| 304 | } |
| 305 | |
| 306 | int_fc = (void *) array_fc->base.elem_fc; |
| 307 | |
| 308 | if (int_fc->is_signed) { |
| 309 | BT_LOGE_STR("Invalid packet header field class: " |
| 310 | "`uuid` member's element field class " |
| 311 | "is a signed integer field class."); |
| 312 | goto invalid; |
| 313 | } |
| 314 | |
| 315 | if (int_fc->base.size != 8) { |
| 316 | BT_LOGE_STR("Invalid packet header field class: " |
| 317 | "`uuid` member's element field class " |
| 318 | "is not an 8-bit integer field class."); |
| 319 | goto invalid; |
| 320 | } |
| 321 | |
| 322 | if (int_fc->base.base.alignment != 8) { |
| 323 | BT_LOGE_STR("Invalid packet header field class: " |
| 324 | "`uuid` member's element field class's " |
| 325 | "alignment is not 8."); |
| 326 | goto invalid; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | for (i = 0; i < ctf_tc->stream_classes->len; i++) { |
| 332 | struct ctf_stream_class *sc = |
| 333 | ctf_tc->stream_classes->pdata[i]; |
| 334 | |
| 335 | ret = validate_stream_class(sc); |
| 336 | if (ret) { |
| 337 | BT_LOGE("Invalid stream class: sc-id=%" PRIu64, sc->id); |
| 338 | goto invalid; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | goto end; |
| 343 | |
| 344 | invalid: |
| 345 | ret = -1; |
| 346 | |
| 347 | end: |
| 348 | return ret; |
| 349 | } |