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