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