| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2010 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * Common Trace Format Metadata Semantic Validator. |
| 7 | */ |
| 8 | |
| 9 | #define BT_COMP_LOG_SELF_COMP (log_cfg->self_comp) |
| 10 | #define BT_LOG_OUTPUT_LEVEL (log_cfg->log_level) |
| 11 | #define BT_LOG_TAG "PLUGIN/CTF/META/SEMANTIC-VALIDATOR-VISITOR" |
| 12 | #include "logging/comp-logging.h" |
| 13 | |
| 14 | #include <stdio.h> |
| 15 | #include <unistd.h> |
| 16 | #include <string.h> |
| 17 | #include <stdlib.h> |
| 18 | #include "common/assert.h" |
| 19 | #include <glib.h> |
| 20 | #include <inttypes.h> |
| 21 | #include <errno.h> |
| 22 | #include "common/list.h" |
| 23 | #include "scanner.h" |
| 24 | #include "ast.h" |
| 25 | #include "logging.h" |
| 26 | |
| 27 | #define _bt_list_first_entry(ptr, type, member) \ |
| 28 | bt_list_entry((ptr)->next, type, member) |
| 29 | |
| 30 | static |
| 31 | int _ctf_visitor_semantic_check(int depth, struct ctf_node *node, |
| 32 | struct meta_log_config *log_cfg); |
| 33 | |
| 34 | static |
| 35 | int ctf_visitor_unary_expression(int depth, struct ctf_node *node, |
| 36 | struct meta_log_config *log_cfg) |
| 37 | { |
| 38 | struct ctf_node *iter; |
| 39 | int is_ctf_exp = 0, is_ctf_exp_left = 0; |
| 40 | |
| 41 | switch (node->parent->type) { |
| 42 | case NODE_CTF_EXPRESSION: |
| 43 | is_ctf_exp = 1; |
| 44 | bt_list_for_each_entry(iter, &node->parent->u.ctf_expression.left, |
| 45 | siblings) { |
| 46 | if (iter == node) { |
| 47 | is_ctf_exp_left = 1; |
| 48 | /* |
| 49 | * We are a left child of a ctf expression. |
| 50 | * We are only allowed to be a string. |
| 51 | */ |
| 52 | if (node->u.unary_expression.type != UNARY_STRING) { |
| 53 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 54 | "Left child of a CTF expression is only allowed to be a string."); |
| 55 | goto errperm; |
| 56 | } |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | /* Right child of a ctf expression can be any type of unary exp. */ |
| 61 | break; /* OK */ |
| 62 | case NODE_TYPE_DECLARATOR: |
| 63 | /* |
| 64 | * We are the length of a type declarator. |
| 65 | */ |
| 66 | switch (node->u.unary_expression.type) { |
| 67 | case UNARY_UNSIGNED_CONSTANT: |
| 68 | case UNARY_STRING: |
| 69 | break; |
| 70 | default: |
| 71 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 72 | "Children of field class declarator and `enum` can only be unsigned numeric constants or references to fields (e.g., `a.b.c`)."); |
| 73 | goto errperm; |
| 74 | } |
| 75 | break; /* OK */ |
| 76 | |
| 77 | case NODE_STRUCT: |
| 78 | /* |
| 79 | * We are the size of a struct align attribute. |
| 80 | */ |
| 81 | switch (node->u.unary_expression.type) { |
| 82 | case UNARY_UNSIGNED_CONSTANT: |
| 83 | break; |
| 84 | default: |
| 85 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 86 | "Structure alignment attribute can only be an unsigned numeric constant."); |
| 87 | goto errperm; |
| 88 | } |
| 89 | break; |
| 90 | |
| 91 | case NODE_ENUMERATOR: |
| 92 | /* The enumerator's parent has validated its validity already. */ |
| 93 | break; /* OK */ |
| 94 | |
| 95 | case NODE_UNARY_EXPRESSION: |
| 96 | /* |
| 97 | * We disallow nested unary expressions and "sbrac" unary |
| 98 | * expressions. |
| 99 | */ |
| 100 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 101 | "Nested unary expressions not allowed (`()` and `[]`)."); |
| 102 | goto errperm; |
| 103 | |
| 104 | case NODE_ROOT: |
| 105 | case NODE_EVENT: |
| 106 | case NODE_STREAM: |
| 107 | case NODE_ENV: |
| 108 | case NODE_TRACE: |
| 109 | case NODE_CLOCK: |
| 110 | case NODE_CALLSITE: |
| 111 | case NODE_TYPEDEF: |
| 112 | case NODE_TYPEALIAS_TARGET: |
| 113 | case NODE_TYPEALIAS_ALIAS: |
| 114 | case NODE_TYPEALIAS: |
| 115 | case NODE_TYPE_SPECIFIER: |
| 116 | case NODE_POINTER: |
| 117 | case NODE_FLOATING_POINT: |
| 118 | case NODE_INTEGER: |
| 119 | case NODE_STRING: |
| 120 | case NODE_ENUM: |
| 121 | case NODE_STRUCT_OR_VARIANT_DECLARATION: |
| 122 | case NODE_VARIANT: |
| 123 | default: |
| 124 | goto errinval; |
| 125 | } |
| 126 | |
| 127 | switch (node->u.unary_expression.link) { |
| 128 | case UNARY_LINK_UNKNOWN: |
| 129 | /* We don't allow empty link except on the first node of the list */ |
| 130 | if (is_ctf_exp && _bt_list_first_entry(is_ctf_exp_left ? |
| 131 | &node->parent->u.ctf_expression.left : |
| 132 | &node->parent->u.ctf_expression.right, |
| 133 | struct ctf_node, |
| 134 | siblings) != node) { |
| 135 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 136 | "Empty link is not allowed except on first node of unary expression (need to separate nodes with `.` or `->`)."); |
| 137 | goto errperm; |
| 138 | } |
| 139 | break; /* OK */ |
| 140 | case UNARY_DOTLINK: |
| 141 | case UNARY_ARROWLINK: |
| 142 | /* We only allow -> and . links between children of ctf_expression. */ |
| 143 | if (node->parent->type != NODE_CTF_EXPRESSION) { |
| 144 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 145 | "Links `.` and `->` are only allowed as children of CTF expression."); |
| 146 | goto errperm; |
| 147 | } |
| 148 | /* |
| 149 | * Only strings can be separated linked by . or ->. |
| 150 | * This includes "", '' and non-quoted identifiers. |
| 151 | */ |
| 152 | if (node->u.unary_expression.type != UNARY_STRING) { |
| 153 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 154 | "Links `.` and `->` are only allowed to separate strings and identifiers."); |
| 155 | goto errperm; |
| 156 | } |
| 157 | /* We don't allow link on the first node of the list */ |
| 158 | if (is_ctf_exp && _bt_list_first_entry(is_ctf_exp_left ? |
| 159 | &node->parent->u.ctf_expression.left : |
| 160 | &node->parent->u.ctf_expression.right, |
| 161 | struct ctf_node, |
| 162 | siblings) == node) { |
| 163 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 164 | "Links `.` and `->` are not allowed before first node of the unary expression list."); |
| 165 | goto errperm; |
| 166 | } |
| 167 | break; |
| 168 | case UNARY_DOTDOTDOT: |
| 169 | /* We only allow ... link between children of enumerator. */ |
| 170 | if (node->parent->type != NODE_ENUMERATOR) { |
| 171 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 172 | "Link `...` is only allowed within enumerator."); |
| 173 | goto errperm; |
| 174 | } |
| 175 | /* We don't allow link on the first node of the list */ |
| 176 | if (_bt_list_first_entry(&node->parent->u.enumerator.values, |
| 177 | struct ctf_node, |
| 178 | siblings) == node) { |
| 179 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 180 | "Link `...` is not allowed on the first node of the unary expression list."); |
| 181 | goto errperm; |
| 182 | } |
| 183 | break; |
| 184 | default: |
| 185 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 186 | "Unknown expression link type: type=%d", |
| 187 | node->u.unary_expression.link); |
| 188 | return -EINVAL; |
| 189 | } |
| 190 | return 0; |
| 191 | |
| 192 | errinval: |
| 193 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 194 | "Incoherent parent node's type: node-type=%s, parent-node-type=%s", |
| 195 | node_type(node), node_type(node->parent)); |
| 196 | return -EINVAL; /* Incoherent structure */ |
| 197 | |
| 198 | errperm: |
| 199 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 200 | "Semantic error: node-type=%s, parent-node-type=%s", |
| 201 | node_type(node), node_type(node->parent)); |
| 202 | return -EPERM; /* Structure not allowed */ |
| 203 | } |
| 204 | |
| 205 | static |
| 206 | int ctf_visitor_field_class_specifier_list(int depth, struct ctf_node *node, |
| 207 | struct meta_log_config *log_cfg) |
| 208 | { |
| 209 | switch (node->parent->type) { |
| 210 | case NODE_CTF_EXPRESSION: |
| 211 | case NODE_TYPE_DECLARATOR: |
| 212 | case NODE_TYPEDEF: |
| 213 | case NODE_TYPEALIAS_TARGET: |
| 214 | case NODE_TYPEALIAS_ALIAS: |
| 215 | case NODE_ENUM: |
| 216 | case NODE_STRUCT_OR_VARIANT_DECLARATION: |
| 217 | case NODE_ROOT: |
| 218 | break; /* OK */ |
| 219 | |
| 220 | case NODE_EVENT: |
| 221 | case NODE_STREAM: |
| 222 | case NODE_ENV: |
| 223 | case NODE_TRACE: |
| 224 | case NODE_CLOCK: |
| 225 | case NODE_CALLSITE: |
| 226 | case NODE_UNARY_EXPRESSION: |
| 227 | case NODE_TYPEALIAS: |
| 228 | case NODE_TYPE_SPECIFIER: |
| 229 | case NODE_TYPE_SPECIFIER_LIST: |
| 230 | case NODE_POINTER: |
| 231 | case NODE_FLOATING_POINT: |
| 232 | case NODE_INTEGER: |
| 233 | case NODE_STRING: |
| 234 | case NODE_ENUMERATOR: |
| 235 | case NODE_VARIANT: |
| 236 | case NODE_STRUCT: |
| 237 | default: |
| 238 | goto errinval; |
| 239 | } |
| 240 | return 0; |
| 241 | errinval: |
| 242 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 243 | "Incoherent parent node's type: node-type=%s, parent-node-type=%s", |
| 244 | node_type(node), node_type(node->parent)); |
| 245 | return -EINVAL; /* Incoherent structure */ |
| 246 | } |
| 247 | |
| 248 | static |
| 249 | int ctf_visitor_field_class_specifier(int depth, struct ctf_node *node, |
| 250 | struct meta_log_config *log_cfg) |
| 251 | { |
| 252 | switch (node->parent->type) { |
| 253 | case NODE_TYPE_SPECIFIER_LIST: |
| 254 | break; /* OK */ |
| 255 | |
| 256 | case NODE_CTF_EXPRESSION: |
| 257 | case NODE_TYPE_DECLARATOR: |
| 258 | case NODE_TYPEDEF: |
| 259 | case NODE_TYPEALIAS_TARGET: |
| 260 | case NODE_TYPEALIAS_ALIAS: |
| 261 | case NODE_ENUM: |
| 262 | case NODE_STRUCT_OR_VARIANT_DECLARATION: |
| 263 | case NODE_ROOT: |
| 264 | case NODE_EVENT: |
| 265 | case NODE_STREAM: |
| 266 | case NODE_ENV: |
| 267 | case NODE_TRACE: |
| 268 | case NODE_CLOCK: |
| 269 | case NODE_CALLSITE: |
| 270 | case NODE_UNARY_EXPRESSION: |
| 271 | case NODE_TYPEALIAS: |
| 272 | case NODE_TYPE_SPECIFIER: |
| 273 | case NODE_POINTER: |
| 274 | case NODE_FLOATING_POINT: |
| 275 | case NODE_INTEGER: |
| 276 | case NODE_STRING: |
| 277 | case NODE_ENUMERATOR: |
| 278 | case NODE_VARIANT: |
| 279 | case NODE_STRUCT: |
| 280 | default: |
| 281 | goto errinval; |
| 282 | } |
| 283 | return 0; |
| 284 | errinval: |
| 285 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 286 | "Incoherent parent node's type: node-type=%s, parent-node-type=%s", |
| 287 | node_type(node), node_type(node->parent)); |
| 288 | return -EINVAL; /* Incoherent structure */ |
| 289 | } |
| 290 | |
| 291 | static |
| 292 | int ctf_visitor_field_class_declarator(int depth, struct ctf_node *node, |
| 293 | struct meta_log_config *log_cfg) |
| 294 | { |
| 295 | int ret = 0; |
| 296 | struct ctf_node *iter; |
| 297 | |
| 298 | depth++; |
| 299 | |
| 300 | switch (node->parent->type) { |
| 301 | case NODE_TYPE_DECLARATOR: |
| 302 | /* |
| 303 | * A nested field class declarator is not allowed to |
| 304 | * contain pointers. |
| 305 | */ |
| 306 | if (!bt_list_empty(&node->u.field_class_declarator.pointers)) |
| 307 | goto errperm; |
| 308 | break; /* OK */ |
| 309 | case NODE_TYPEALIAS_TARGET: |
| 310 | break; /* OK */ |
| 311 | case NODE_TYPEALIAS_ALIAS: |
| 312 | /* |
| 313 | * Only accept alias name containing: |
| 314 | * - identifier |
| 315 | * - identifier * (any number of pointers) |
| 316 | * NOT accepting alias names containing [] (would otherwise |
| 317 | * cause semantic clash for later declarations of |
| 318 | * arrays/sequences of elements, where elements could be |
| 319 | * arrays/sequences themselves (if allowed in field class alias). |
| 320 | * NOT accepting alias with identifier. The declarator should |
| 321 | * be either empty or contain pointer(s). |
| 322 | */ |
| 323 | if (node->u.field_class_declarator.type == TYPEDEC_NESTED) |
| 324 | goto errperm; |
| 325 | bt_list_for_each_entry(iter, &node->parent->u.field_class_alias_name.field_class_specifier_list->u.field_class_specifier_list.head, |
| 326 | siblings) { |
| 327 | switch (iter->u.field_class_specifier.type) { |
| 328 | case TYPESPEC_FLOATING_POINT: |
| 329 | case TYPESPEC_INTEGER: |
| 330 | case TYPESPEC_STRING: |
| 331 | case TYPESPEC_STRUCT: |
| 332 | case TYPESPEC_VARIANT: |
| 333 | case TYPESPEC_ENUM: |
| 334 | if (bt_list_empty(&node->u.field_class_declarator.pointers)) |
| 335 | goto errperm; |
| 336 | break; |
| 337 | default: |
| 338 | break; |
| 339 | } |
| 340 | } |
| 341 | if (node->u.field_class_declarator.type == TYPEDEC_ID && |
| 342 | node->u.field_class_declarator.u.id) |
| 343 | goto errperm; |
| 344 | break; /* OK */ |
| 345 | case NODE_TYPEDEF: |
| 346 | case NODE_STRUCT_OR_VARIANT_DECLARATION: |
| 347 | break; /* OK */ |
| 348 | |
| 349 | case NODE_ROOT: |
| 350 | case NODE_EVENT: |
| 351 | case NODE_STREAM: |
| 352 | case NODE_ENV: |
| 353 | case NODE_TRACE: |
| 354 | case NODE_CLOCK: |
| 355 | case NODE_CALLSITE: |
| 356 | case NODE_CTF_EXPRESSION: |
| 357 | case NODE_UNARY_EXPRESSION: |
| 358 | case NODE_TYPEALIAS: |
| 359 | case NODE_TYPE_SPECIFIER: |
| 360 | case NODE_POINTER: |
| 361 | case NODE_FLOATING_POINT: |
| 362 | case NODE_INTEGER: |
| 363 | case NODE_STRING: |
| 364 | case NODE_ENUMERATOR: |
| 365 | case NODE_ENUM: |
| 366 | case NODE_VARIANT: |
| 367 | case NODE_STRUCT: |
| 368 | default: |
| 369 | goto errinval; |
| 370 | } |
| 371 | |
| 372 | bt_list_for_each_entry(iter, &node->u.field_class_declarator.pointers, |
| 373 | siblings) { |
| 374 | ret = _ctf_visitor_semantic_check(depth + 1, iter, log_cfg); |
| 375 | if (ret) |
| 376 | return ret; |
| 377 | } |
| 378 | |
| 379 | switch (node->u.field_class_declarator.type) { |
| 380 | case TYPEDEC_ID: |
| 381 | break; |
| 382 | case TYPEDEC_NESTED: |
| 383 | { |
| 384 | if (node->u.field_class_declarator.u.nested.field_class_declarator) { |
| 385 | ret = _ctf_visitor_semantic_check(depth + 1, |
| 386 | node->u.field_class_declarator.u.nested.field_class_declarator, |
| 387 | log_cfg); |
| 388 | if (ret) |
| 389 | return ret; |
| 390 | } |
| 391 | if (!node->u.field_class_declarator.u.nested.abstract_array) { |
| 392 | bt_list_for_each_entry(iter, &node->u.field_class_declarator.u.nested.length, |
| 393 | siblings) { |
| 394 | if (iter->type != NODE_UNARY_EXPRESSION) { |
| 395 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 396 | "Expecting unary expression as length: node-type=%s", |
| 397 | node_type(iter)); |
| 398 | return -EINVAL; |
| 399 | } |
| 400 | ret = _ctf_visitor_semantic_check(depth + 1, |
| 401 | iter, log_cfg); |
| 402 | if (ret) |
| 403 | return ret; |
| 404 | } |
| 405 | } else { |
| 406 | if (node->parent->type == NODE_TYPEALIAS_TARGET) { |
| 407 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 408 | "Abstract array declarator not permitted as target of field class alias."); |
| 409 | return -EINVAL; |
| 410 | } |
| 411 | } |
| 412 | if (node->u.field_class_declarator.bitfield_len) { |
| 413 | ret = _ctf_visitor_semantic_check(depth + 1, |
| 414 | node->u.field_class_declarator.bitfield_len, |
| 415 | log_cfg); |
| 416 | if (ret) |
| 417 | return ret; |
| 418 | } |
| 419 | break; |
| 420 | } |
| 421 | case TYPEDEC_UNKNOWN: |
| 422 | default: |
| 423 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 424 | "Unknown field class declarator: type=%d", |
| 425 | node->u.field_class_declarator.type); |
| 426 | return -EINVAL; |
| 427 | } |
| 428 | depth--; |
| 429 | return 0; |
| 430 | |
| 431 | errinval: |
| 432 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 433 | "Incoherent parent node's type: node-type=%s, parent-node-type=%s", |
| 434 | node_type(node), node_type(node->parent)); |
| 435 | return -EINVAL; /* Incoherent structure */ |
| 436 | |
| 437 | errperm: |
| 438 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 439 | "Semantic error: node-type=%s, parent-node-type=%s", |
| 440 | node_type(node), node_type(node->parent)); |
| 441 | return -EPERM; /* Structure not allowed */ |
| 442 | } |
| 443 | |
| 444 | static |
| 445 | int _ctf_visitor_semantic_check(int depth, struct ctf_node *node, |
| 446 | struct meta_log_config *log_cfg) |
| 447 | { |
| 448 | int ret = 0; |
| 449 | struct ctf_node *iter; |
| 450 | |
| 451 | if (node->visited) |
| 452 | return 0; |
| 453 | |
| 454 | switch (node->type) { |
| 455 | case NODE_ROOT: |
| 456 | bt_list_for_each_entry(iter, &node->u.root.declaration_list, siblings) { |
| 457 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 458 | log_cfg); |
| 459 | if (ret) |
| 460 | return ret; |
| 461 | } |
| 462 | bt_list_for_each_entry(iter, &node->u.root.trace, siblings) { |
| 463 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 464 | log_cfg); |
| 465 | if (ret) |
| 466 | return ret; |
| 467 | } |
| 468 | bt_list_for_each_entry(iter, &node->u.root.stream, siblings) { |
| 469 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 470 | log_cfg); |
| 471 | if (ret) |
| 472 | return ret; |
| 473 | } |
| 474 | bt_list_for_each_entry(iter, &node->u.root.event, siblings) { |
| 475 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 476 | log_cfg); |
| 477 | if (ret) |
| 478 | return ret; |
| 479 | } |
| 480 | break; |
| 481 | |
| 482 | case NODE_EVENT: |
| 483 | switch (node->parent->type) { |
| 484 | case NODE_ROOT: |
| 485 | break; /* OK */ |
| 486 | default: |
| 487 | goto errinval; |
| 488 | } |
| 489 | |
| 490 | bt_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) { |
| 491 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 492 | log_cfg); |
| 493 | if (ret) |
| 494 | return ret; |
| 495 | } |
| 496 | break; |
| 497 | case NODE_STREAM: |
| 498 | switch (node->parent->type) { |
| 499 | case NODE_ROOT: |
| 500 | break; /* OK */ |
| 501 | default: |
| 502 | goto errinval; |
| 503 | } |
| 504 | |
| 505 | bt_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) { |
| 506 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 507 | log_cfg); |
| 508 | if (ret) |
| 509 | return ret; |
| 510 | } |
| 511 | break; |
| 512 | case NODE_ENV: |
| 513 | switch (node->parent->type) { |
| 514 | case NODE_ROOT: |
| 515 | break; /* OK */ |
| 516 | default: |
| 517 | goto errinval; |
| 518 | } |
| 519 | |
| 520 | bt_list_for_each_entry(iter, &node->u.env.declaration_list, siblings) { |
| 521 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 522 | log_cfg); |
| 523 | if (ret) |
| 524 | return ret; |
| 525 | } |
| 526 | break; |
| 527 | case NODE_TRACE: |
| 528 | switch (node->parent->type) { |
| 529 | case NODE_ROOT: |
| 530 | break; /* OK */ |
| 531 | default: |
| 532 | goto errinval; |
| 533 | } |
| 534 | |
| 535 | bt_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) { |
| 536 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 537 | log_cfg); |
| 538 | if (ret) |
| 539 | return ret; |
| 540 | } |
| 541 | break; |
| 542 | case NODE_CLOCK: |
| 543 | switch (node->parent->type) { |
| 544 | case NODE_ROOT: |
| 545 | break; /* OK */ |
| 546 | default: |
| 547 | goto errinval; |
| 548 | } |
| 549 | |
| 550 | bt_list_for_each_entry(iter, &node->u.clock.declaration_list, siblings) { |
| 551 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 552 | log_cfg); |
| 553 | if (ret) |
| 554 | return ret; |
| 555 | } |
| 556 | break; |
| 557 | case NODE_CALLSITE: |
| 558 | switch (node->parent->type) { |
| 559 | case NODE_ROOT: |
| 560 | break; /* OK */ |
| 561 | default: |
| 562 | goto errinval; |
| 563 | } |
| 564 | |
| 565 | bt_list_for_each_entry(iter, &node->u.callsite.declaration_list, siblings) { |
| 566 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 567 | log_cfg); |
| 568 | if (ret) |
| 569 | return ret; |
| 570 | } |
| 571 | break; |
| 572 | |
| 573 | case NODE_CTF_EXPRESSION: |
| 574 | switch (node->parent->type) { |
| 575 | case NODE_ROOT: |
| 576 | case NODE_EVENT: |
| 577 | case NODE_STREAM: |
| 578 | case NODE_ENV: |
| 579 | case NODE_TRACE: |
| 580 | case NODE_CLOCK: |
| 581 | case NODE_CALLSITE: |
| 582 | case NODE_FLOATING_POINT: |
| 583 | case NODE_INTEGER: |
| 584 | case NODE_STRING: |
| 585 | break; /* OK */ |
| 586 | |
| 587 | case NODE_CTF_EXPRESSION: |
| 588 | case NODE_UNARY_EXPRESSION: |
| 589 | case NODE_TYPEDEF: |
| 590 | case NODE_TYPEALIAS_TARGET: |
| 591 | case NODE_TYPEALIAS_ALIAS: |
| 592 | case NODE_STRUCT_OR_VARIANT_DECLARATION: |
| 593 | case NODE_TYPEALIAS: |
| 594 | case NODE_TYPE_SPECIFIER: |
| 595 | case NODE_TYPE_SPECIFIER_LIST: |
| 596 | case NODE_POINTER: |
| 597 | case NODE_TYPE_DECLARATOR: |
| 598 | case NODE_ENUMERATOR: |
| 599 | case NODE_ENUM: |
| 600 | case NODE_VARIANT: |
| 601 | case NODE_STRUCT: |
| 602 | default: |
| 603 | goto errinval; |
| 604 | } |
| 605 | |
| 606 | depth++; |
| 607 | bt_list_for_each_entry(iter, &node->u.ctf_expression.left, siblings) { |
| 608 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 609 | log_cfg); |
| 610 | if (ret) |
| 611 | return ret; |
| 612 | } |
| 613 | bt_list_for_each_entry(iter, &node->u.ctf_expression.right, siblings) { |
| 614 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 615 | log_cfg); |
| 616 | if (ret) |
| 617 | return ret; |
| 618 | } |
| 619 | depth--; |
| 620 | break; |
| 621 | case NODE_UNARY_EXPRESSION: |
| 622 | return ctf_visitor_unary_expression(depth, node, log_cfg); |
| 623 | |
| 624 | case NODE_TYPEDEF: |
| 625 | switch (node->parent->type) { |
| 626 | case NODE_ROOT: |
| 627 | case NODE_EVENT: |
| 628 | case NODE_STREAM: |
| 629 | case NODE_TRACE: |
| 630 | case NODE_VARIANT: |
| 631 | case NODE_STRUCT: |
| 632 | break; /* OK */ |
| 633 | |
| 634 | case NODE_CTF_EXPRESSION: |
| 635 | case NODE_UNARY_EXPRESSION: |
| 636 | case NODE_TYPEDEF: |
| 637 | case NODE_TYPEALIAS_TARGET: |
| 638 | case NODE_TYPEALIAS_ALIAS: |
| 639 | case NODE_TYPEALIAS: |
| 640 | case NODE_STRUCT_OR_VARIANT_DECLARATION: |
| 641 | case NODE_TYPE_SPECIFIER: |
| 642 | case NODE_TYPE_SPECIFIER_LIST: |
| 643 | case NODE_POINTER: |
| 644 | case NODE_TYPE_DECLARATOR: |
| 645 | case NODE_FLOATING_POINT: |
| 646 | case NODE_INTEGER: |
| 647 | case NODE_STRING: |
| 648 | case NODE_ENUMERATOR: |
| 649 | case NODE_ENUM: |
| 650 | case NODE_CLOCK: |
| 651 | case NODE_CALLSITE: |
| 652 | case NODE_ENV: |
| 653 | default: |
| 654 | goto errinval; |
| 655 | } |
| 656 | |
| 657 | depth++; |
| 658 | ret = _ctf_visitor_semantic_check(depth + 1, |
| 659 | node->u.field_class_def.field_class_specifier_list, |
| 660 | log_cfg); |
| 661 | if (ret) |
| 662 | return ret; |
| 663 | bt_list_for_each_entry(iter, &node->u.field_class_def.field_class_declarators, siblings) { |
| 664 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 665 | log_cfg); |
| 666 | if (ret) |
| 667 | return ret; |
| 668 | } |
| 669 | depth--; |
| 670 | break; |
| 671 | case NODE_TYPEALIAS_TARGET: |
| 672 | { |
| 673 | int nr_declarators; |
| 674 | |
| 675 | switch (node->parent->type) { |
| 676 | case NODE_TYPEALIAS: |
| 677 | break; /* OK */ |
| 678 | default: |
| 679 | goto errinval; |
| 680 | } |
| 681 | |
| 682 | depth++; |
| 683 | ret = _ctf_visitor_semantic_check(depth + 1, |
| 684 | node->u.field_class_alias_target.field_class_specifier_list, |
| 685 | log_cfg); |
| 686 | if (ret) |
| 687 | return ret; |
| 688 | nr_declarators = 0; |
| 689 | bt_list_for_each_entry(iter, &node->u.field_class_alias_target.field_class_declarators, siblings) { |
| 690 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 691 | log_cfg); |
| 692 | if (ret) |
| 693 | return ret; |
| 694 | nr_declarators++; |
| 695 | } |
| 696 | if (nr_declarators > 1) { |
| 697 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 698 | "Too many declarators in field class alias's name (maximum is 1): count=%d", |
| 699 | nr_declarators); |
| 700 | return -EINVAL; |
| 701 | } |
| 702 | depth--; |
| 703 | break; |
| 704 | } |
| 705 | case NODE_TYPEALIAS_ALIAS: |
| 706 | { |
| 707 | int nr_declarators; |
| 708 | |
| 709 | switch (node->parent->type) { |
| 710 | case NODE_TYPEALIAS: |
| 711 | break; /* OK */ |
| 712 | default: |
| 713 | goto errinval; |
| 714 | } |
| 715 | |
| 716 | depth++; |
| 717 | ret = _ctf_visitor_semantic_check(depth + 1, |
| 718 | node->u.field_class_alias_name.field_class_specifier_list, |
| 719 | log_cfg); |
| 720 | if (ret) |
| 721 | return ret; |
| 722 | nr_declarators = 0; |
| 723 | bt_list_for_each_entry(iter, &node->u.field_class_alias_name.field_class_declarators, siblings) { |
| 724 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 725 | log_cfg); |
| 726 | if (ret) |
| 727 | return ret; |
| 728 | nr_declarators++; |
| 729 | } |
| 730 | if (nr_declarators > 1) { |
| 731 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 732 | "Too many declarators in field class alias's name (maximum is 1): count=%d", |
| 733 | nr_declarators); |
| 734 | return -EINVAL; |
| 735 | } |
| 736 | depth--; |
| 737 | break; |
| 738 | } |
| 739 | case NODE_TYPEALIAS: |
| 740 | switch (node->parent->type) { |
| 741 | case NODE_ROOT: |
| 742 | case NODE_EVENT: |
| 743 | case NODE_STREAM: |
| 744 | case NODE_TRACE: |
| 745 | case NODE_VARIANT: |
| 746 | case NODE_STRUCT: |
| 747 | break; /* OK */ |
| 748 | |
| 749 | case NODE_CTF_EXPRESSION: |
| 750 | case NODE_UNARY_EXPRESSION: |
| 751 | case NODE_TYPEDEF: |
| 752 | case NODE_TYPEALIAS_TARGET: |
| 753 | case NODE_TYPEALIAS_ALIAS: |
| 754 | case NODE_TYPEALIAS: |
| 755 | case NODE_STRUCT_OR_VARIANT_DECLARATION: |
| 756 | case NODE_TYPE_SPECIFIER: |
| 757 | case NODE_TYPE_SPECIFIER_LIST: |
| 758 | case NODE_POINTER: |
| 759 | case NODE_TYPE_DECLARATOR: |
| 760 | case NODE_FLOATING_POINT: |
| 761 | case NODE_INTEGER: |
| 762 | case NODE_STRING: |
| 763 | case NODE_ENUMERATOR: |
| 764 | case NODE_ENUM: |
| 765 | case NODE_CLOCK: |
| 766 | case NODE_CALLSITE: |
| 767 | case NODE_ENV: |
| 768 | default: |
| 769 | goto errinval; |
| 770 | } |
| 771 | |
| 772 | ret = _ctf_visitor_semantic_check(depth + 1, |
| 773 | node->u.field_class_alias.target, log_cfg); |
| 774 | if (ret) |
| 775 | return ret; |
| 776 | ret = _ctf_visitor_semantic_check(depth + 1, |
| 777 | node->u.field_class_alias.alias, log_cfg); |
| 778 | if (ret) |
| 779 | return ret; |
| 780 | break; |
| 781 | |
| 782 | case NODE_TYPE_SPECIFIER_LIST: |
| 783 | ret = ctf_visitor_field_class_specifier_list(depth, node, |
| 784 | log_cfg); |
| 785 | if (ret) |
| 786 | return ret; |
| 787 | break; |
| 788 | case NODE_TYPE_SPECIFIER: |
| 789 | ret = ctf_visitor_field_class_specifier(depth, node, |
| 790 | log_cfg); |
| 791 | if (ret) |
| 792 | return ret; |
| 793 | break; |
| 794 | case NODE_POINTER: |
| 795 | switch (node->parent->type) { |
| 796 | case NODE_TYPE_DECLARATOR: |
| 797 | break; /* OK */ |
| 798 | default: |
| 799 | goto errinval; |
| 800 | } |
| 801 | break; |
| 802 | case NODE_TYPE_DECLARATOR: |
| 803 | ret = ctf_visitor_field_class_declarator(depth, node, |
| 804 | log_cfg); |
| 805 | if (ret) |
| 806 | return ret; |
| 807 | break; |
| 808 | |
| 809 | case NODE_FLOATING_POINT: |
| 810 | switch (node->parent->type) { |
| 811 | case NODE_TYPE_SPECIFIER: |
| 812 | break; /* OK */ |
| 813 | default: |
| 814 | goto errinval; |
| 815 | |
| 816 | case NODE_UNARY_EXPRESSION: |
| 817 | goto errperm; |
| 818 | } |
| 819 | bt_list_for_each_entry(iter, &node->u.floating_point.expressions, siblings) { |
| 820 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 821 | log_cfg); |
| 822 | if (ret) |
| 823 | return ret; |
| 824 | } |
| 825 | break; |
| 826 | case NODE_INTEGER: |
| 827 | switch (node->parent->type) { |
| 828 | case NODE_TYPE_SPECIFIER: |
| 829 | break; /* OK */ |
| 830 | default: |
| 831 | goto errinval; |
| 832 | |
| 833 | } |
| 834 | |
| 835 | bt_list_for_each_entry(iter, &node->u.integer.expressions, siblings) { |
| 836 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 837 | log_cfg); |
| 838 | if (ret) |
| 839 | return ret; |
| 840 | } |
| 841 | break; |
| 842 | case NODE_STRING: |
| 843 | switch (node->parent->type) { |
| 844 | case NODE_TYPE_SPECIFIER: |
| 845 | break; /* OK */ |
| 846 | default: |
| 847 | goto errinval; |
| 848 | |
| 849 | case NODE_UNARY_EXPRESSION: |
| 850 | goto errperm; |
| 851 | } |
| 852 | |
| 853 | bt_list_for_each_entry(iter, &node->u.string.expressions, siblings) { |
| 854 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 855 | log_cfg); |
| 856 | if (ret) |
| 857 | return ret; |
| 858 | } |
| 859 | break; |
| 860 | case NODE_ENUMERATOR: |
| 861 | switch (node->parent->type) { |
| 862 | case NODE_ENUM: |
| 863 | break; |
| 864 | default: |
| 865 | goto errinval; |
| 866 | } |
| 867 | /* |
| 868 | * Enumerators are only allows to contain: |
| 869 | * numeric unary expression |
| 870 | * or num. unary exp. ... num. unary exp |
| 871 | */ |
| 872 | { |
| 873 | int count = 0; |
| 874 | |
| 875 | bt_list_for_each_entry(iter, &node->u.enumerator.values, |
| 876 | siblings) { |
| 877 | switch (count++) { |
| 878 | case 0: if (iter->type != NODE_UNARY_EXPRESSION |
| 879 | || (iter->u.unary_expression.type != UNARY_SIGNED_CONSTANT |
| 880 | && iter->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) |
| 881 | || iter->u.unary_expression.link != UNARY_LINK_UNKNOWN) { |
| 882 | _BT_COMP_LOGE_LINENO(iter->lineno, |
| 883 | "First unary expression of enumerator is unexpected."); |
| 884 | goto errperm; |
| 885 | } |
| 886 | break; |
| 887 | case 1: if (iter->type != NODE_UNARY_EXPRESSION |
| 888 | || (iter->u.unary_expression.type != UNARY_SIGNED_CONSTANT |
| 889 | && iter->u.unary_expression.type != UNARY_UNSIGNED_CONSTANT) |
| 890 | || iter->u.unary_expression.link != UNARY_DOTDOTDOT) { |
| 891 | _BT_COMP_LOGE_LINENO(iter->lineno, |
| 892 | "Second unary expression of enumerator is unexpected."); |
| 893 | goto errperm; |
| 894 | } |
| 895 | break; |
| 896 | default: |
| 897 | goto errperm; |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | bt_list_for_each_entry(iter, &node->u.enumerator.values, siblings) { |
| 903 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 904 | log_cfg); |
| 905 | if (ret) |
| 906 | return ret; |
| 907 | } |
| 908 | break; |
| 909 | case NODE_ENUM: |
| 910 | switch (node->parent->type) { |
| 911 | case NODE_TYPE_SPECIFIER: |
| 912 | break; /* OK */ |
| 913 | default: |
| 914 | goto errinval; |
| 915 | |
| 916 | case NODE_UNARY_EXPRESSION: |
| 917 | goto errperm; |
| 918 | } |
| 919 | |
| 920 | depth++; |
| 921 | ret = _ctf_visitor_semantic_check(depth + 1, |
| 922 | node->u._enum.container_field_class, log_cfg); |
| 923 | if (ret) |
| 924 | return ret; |
| 925 | |
| 926 | bt_list_for_each_entry(iter, &node->u._enum.enumerator_list, siblings) { |
| 927 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 928 | log_cfg); |
| 929 | if (ret) |
| 930 | return ret; |
| 931 | } |
| 932 | depth--; |
| 933 | break; |
| 934 | case NODE_STRUCT_OR_VARIANT_DECLARATION: |
| 935 | switch (node->parent->type) { |
| 936 | case NODE_STRUCT: |
| 937 | case NODE_VARIANT: |
| 938 | break; |
| 939 | default: |
| 940 | goto errinval; |
| 941 | } |
| 942 | ret = _ctf_visitor_semantic_check(depth + 1, |
| 943 | node->u.struct_or_variant_declaration.field_class_specifier_list, |
| 944 | log_cfg); |
| 945 | if (ret) |
| 946 | return ret; |
| 947 | bt_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.field_class_declarators, siblings) { |
| 948 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 949 | log_cfg); |
| 950 | if (ret) |
| 951 | return ret; |
| 952 | } |
| 953 | break; |
| 954 | case NODE_VARIANT: |
| 955 | switch (node->parent->type) { |
| 956 | case NODE_TYPE_SPECIFIER: |
| 957 | break; /* OK */ |
| 958 | default: |
| 959 | goto errinval; |
| 960 | |
| 961 | case NODE_UNARY_EXPRESSION: |
| 962 | goto errperm; |
| 963 | } |
| 964 | bt_list_for_each_entry(iter, &node->u.variant.declaration_list, siblings) { |
| 965 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 966 | log_cfg); |
| 967 | if (ret) |
| 968 | return ret; |
| 969 | } |
| 970 | break; |
| 971 | |
| 972 | case NODE_STRUCT: |
| 973 | switch (node->parent->type) { |
| 974 | case NODE_TYPE_SPECIFIER: |
| 975 | break; /* OK */ |
| 976 | default: |
| 977 | goto errinval; |
| 978 | |
| 979 | case NODE_UNARY_EXPRESSION: |
| 980 | goto errperm; |
| 981 | } |
| 982 | bt_list_for_each_entry(iter, &node->u._struct.declaration_list, siblings) { |
| 983 | ret = _ctf_visitor_semantic_check(depth + 1, iter, |
| 984 | log_cfg); |
| 985 | if (ret) |
| 986 | return ret; |
| 987 | } |
| 988 | break; |
| 989 | |
| 990 | case NODE_UNKNOWN: |
| 991 | default: |
| 992 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 993 | "Unknown node type: type=%d", node->type); |
| 994 | return -EINVAL; |
| 995 | } |
| 996 | return ret; |
| 997 | |
| 998 | errinval: |
| 999 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 1000 | "Incoherent parent node's type: node-type=%s, parent-node-type=%s", |
| 1001 | node_type(node), node_type(node->parent)); |
| 1002 | return -EINVAL; /* Incoherent structure */ |
| 1003 | |
| 1004 | errperm: |
| 1005 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 1006 | "Semantic error: node-type=%s, parent-node-type=%s", |
| 1007 | node_type(node), node_type(node->parent)); |
| 1008 | return -EPERM; /* Structure not allowed */ |
| 1009 | } |
| 1010 | |
| 1011 | int ctf_visitor_semantic_check(int depth, struct ctf_node *node, |
| 1012 | struct meta_log_config *log_cfg) |
| 1013 | { |
| 1014 | int ret = 0; |
| 1015 | |
| 1016 | /* |
| 1017 | * First make sure we create the parent links for all children. Let's |
| 1018 | * take the safe route and recreate them at each validation, just in |
| 1019 | * case the structure has changed. |
| 1020 | */ |
| 1021 | ret = ctf_visitor_parent_links(depth, node, log_cfg); |
| 1022 | if (ret) { |
| 1023 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 1024 | "Cannot create parent links in metadata's AST: " |
| 1025 | "ret=%d", ret); |
| 1026 | goto end; |
| 1027 | } |
| 1028 | |
| 1029 | ret = _ctf_visitor_semantic_check(depth, node, log_cfg); |
| 1030 | if (ret) { |
| 1031 | _BT_COMP_LOGE_LINENO(node->lineno, |
| 1032 | "Cannot check metadata's AST semantics: " |
| 1033 | "ret=%d", ret); |
| 1034 | goto end; |
| 1035 | } |
| 1036 | |
| 1037 | end: |
| 1038 | return ret; |
| 1039 | } |