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