| 1 | /* |
| 2 | * filter-visitor-generate-bytecode.c |
| 3 | * |
| 4 | * LTTng filter bytecode generation |
| 5 | * |
| 6 | * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * SPDX-License-Identifier: LGPL-2.1-only |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <common/align.h> |
| 15 | #include <common/compat/errno.h> |
| 16 | #include <common/compat/string.h> |
| 17 | |
| 18 | #include "filter-bytecode.h" |
| 19 | #include "filter-ir.h" |
| 20 | #include "filter-ast.h" |
| 21 | |
| 22 | #include <common/macros.h> |
| 23 | |
| 24 | #ifndef max_t |
| 25 | #define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b))) |
| 26 | #endif |
| 27 | |
| 28 | #define INIT_ALLOC_SIZE 4 |
| 29 | |
| 30 | static |
| 31 | int recursive_visit_gen_bytecode(struct filter_parser_ctx *ctx, |
| 32 | struct ir_op *node); |
| 33 | |
| 34 | static inline int get_count_order(unsigned int count) |
| 35 | { |
| 36 | int order; |
| 37 | |
| 38 | order = lttng_fls(count) - 1; |
| 39 | if (count & (count - 1)) |
| 40 | order++; |
| 41 | return order; |
| 42 | } |
| 43 | |
| 44 | static |
| 45 | int bytecode_init(struct lttng_filter_bytecode_alloc **fb) |
| 46 | { |
| 47 | uint32_t alloc_len; |
| 48 | |
| 49 | alloc_len = sizeof(struct lttng_filter_bytecode_alloc) + INIT_ALLOC_SIZE; |
| 50 | *fb = calloc(alloc_len, 1); |
| 51 | if (!*fb) { |
| 52 | return -ENOMEM; |
| 53 | } else { |
| 54 | (*fb)->alloc_len = alloc_len; |
| 55 | return 0; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | static |
| 60 | int32_t bytecode_reserve(struct lttng_filter_bytecode_alloc **fb, uint32_t align, uint32_t len) |
| 61 | { |
| 62 | int32_t ret; |
| 63 | uint32_t padding = offset_align((*fb)->b.len, align); |
| 64 | uint32_t new_len = (*fb)->b.len + padding + len; |
| 65 | uint32_t new_alloc_len = sizeof(struct lttng_filter_bytecode_alloc) + new_len; |
| 66 | uint32_t old_alloc_len = (*fb)->alloc_len; |
| 67 | |
| 68 | if (new_len > LTTNG_FILTER_MAX_LEN) |
| 69 | return -EINVAL; |
| 70 | |
| 71 | if (new_alloc_len > old_alloc_len) { |
| 72 | struct lttng_filter_bytecode_alloc *newptr; |
| 73 | |
| 74 | new_alloc_len = |
| 75 | max_t(uint32_t, 1U << get_count_order(new_alloc_len), old_alloc_len << 1); |
| 76 | newptr = realloc(*fb, new_alloc_len); |
| 77 | if (!newptr) |
| 78 | return -ENOMEM; |
| 79 | *fb = newptr; |
| 80 | /* We zero directly the memory from start of allocation. */ |
| 81 | memset(&((char *) *fb)[old_alloc_len], 0, new_alloc_len - old_alloc_len); |
| 82 | (*fb)->alloc_len = new_alloc_len; |
| 83 | } |
| 84 | (*fb)->b.len += padding; |
| 85 | ret = (*fb)->b.len; |
| 86 | (*fb)->b.len += len; |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | static |
| 91 | int bytecode_push(struct lttng_filter_bytecode_alloc **fb, const void *data, |
| 92 | uint32_t align, uint32_t len) |
| 93 | { |
| 94 | int32_t offset; |
| 95 | |
| 96 | offset = bytecode_reserve(fb, align, len); |
| 97 | if (offset < 0) |
| 98 | return offset; |
| 99 | memcpy(&(*fb)->b.data[offset], data, len); |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static |
| 104 | int bytecode_push_logical(struct lttng_filter_bytecode_alloc **fb, |
| 105 | struct logical_op *data, |
| 106 | uint32_t align, uint32_t len, |
| 107 | uint16_t *skip_offset) |
| 108 | { |
| 109 | int32_t offset; |
| 110 | |
| 111 | offset = bytecode_reserve(fb, align, len); |
| 112 | if (offset < 0) |
| 113 | return offset; |
| 114 | memcpy(&(*fb)->b.data[offset], data, len); |
| 115 | *skip_offset = |
| 116 | (void *) &((struct logical_op *) &(*fb)->b.data[offset])->skip_offset |
| 117 | - (void *) &(*fb)->b.data[0]; |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static |
| 122 | int bytecode_patch(struct lttng_filter_bytecode_alloc **fb, |
| 123 | const void *data, |
| 124 | uint16_t offset, |
| 125 | uint32_t len) |
| 126 | { |
| 127 | if (offset >= (*fb)->b.len) { |
| 128 | return -EINVAL; |
| 129 | } |
| 130 | memcpy(&(*fb)->b.data[offset], data, len); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static |
| 135 | int visit_node_root(struct filter_parser_ctx *ctx, struct ir_op *node) |
| 136 | { |
| 137 | int ret; |
| 138 | struct return_op insn; |
| 139 | |
| 140 | /* Visit child */ |
| 141 | ret = recursive_visit_gen_bytecode(ctx, node->u.root.child); |
| 142 | if (ret) |
| 143 | return ret; |
| 144 | |
| 145 | /* Generate end of bytecode instruction */ |
| 146 | insn.op = FILTER_OP_RETURN; |
| 147 | return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn)); |
| 148 | } |
| 149 | |
| 150 | static |
| 151 | int append_str(char **s, const char *append) |
| 152 | { |
| 153 | char *old = *s; |
| 154 | char *new; |
| 155 | size_t oldlen = (old == NULL) ? 0 : strlen(old); |
| 156 | size_t appendlen = strlen(append); |
| 157 | |
| 158 | new = calloc(oldlen + appendlen + 1, 1); |
| 159 | if (!new) { |
| 160 | return -ENOMEM; |
| 161 | } |
| 162 | if (oldlen) { |
| 163 | strcpy(new, old); |
| 164 | } |
| 165 | strcat(new, append); |
| 166 | *s = new; |
| 167 | free(old); |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * 1: match |
| 173 | * 0: no match |
| 174 | * < 0: error |
| 175 | */ |
| 176 | static |
| 177 | int load_expression_legacy_match(const struct ir_load_expression *exp, |
| 178 | enum filter_op *op_type, |
| 179 | char **symbol) |
| 180 | { |
| 181 | const struct ir_load_expression_op *op; |
| 182 | bool need_dot = false; |
| 183 | |
| 184 | op = exp->child; |
| 185 | switch (op->type) { |
| 186 | case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT: |
| 187 | *op_type = FILTER_OP_GET_CONTEXT_REF; |
| 188 | if (append_str(symbol, "$ctx.")) { |
| 189 | return -ENOMEM; |
| 190 | } |
| 191 | need_dot = false; |
| 192 | break; |
| 193 | case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT: |
| 194 | *op_type = FILTER_OP_GET_CONTEXT_REF; |
| 195 | if (append_str(symbol, "$app.")) { |
| 196 | return -ENOMEM; |
| 197 | } |
| 198 | need_dot = false; |
| 199 | break; |
| 200 | case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT: |
| 201 | *op_type = FILTER_OP_LOAD_FIELD_REF; |
| 202 | need_dot = false; |
| 203 | break; |
| 204 | |
| 205 | case IR_LOAD_EXPRESSION_GET_SYMBOL: |
| 206 | case IR_LOAD_EXPRESSION_GET_INDEX: |
| 207 | case IR_LOAD_EXPRESSION_LOAD_FIELD: |
| 208 | default: |
| 209 | return 0; /* no match */ |
| 210 | } |
| 211 | |
| 212 | for (;;) { |
| 213 | op = op->next; |
| 214 | if (!op) { |
| 215 | return 0; /* no match */ |
| 216 | } |
| 217 | switch (op->type) { |
| 218 | case IR_LOAD_EXPRESSION_LOAD_FIELD: |
| 219 | goto end; |
| 220 | case IR_LOAD_EXPRESSION_GET_SYMBOL: |
| 221 | if (need_dot && append_str(symbol, ".")) { |
| 222 | return -ENOMEM; |
| 223 | } |
| 224 | if (append_str(symbol, op->u.symbol)) { |
| 225 | return -ENOMEM; |
| 226 | } |
| 227 | break; |
| 228 | default: |
| 229 | return 0; /* no match */ |
| 230 | } |
| 231 | need_dot = true; |
| 232 | } |
| 233 | end: |
| 234 | return 1; /* Legacy match */ |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * 1: legacy match |
| 239 | * 0: no legacy match |
| 240 | * < 0: error |
| 241 | */ |
| 242 | static |
| 243 | int visit_node_load_expression_legacy(struct filter_parser_ctx *ctx, |
| 244 | const struct ir_load_expression *exp, |
| 245 | const struct ir_load_expression_op *op) |
| 246 | { |
| 247 | struct load_op *insn = NULL; |
| 248 | uint32_t insn_len = sizeof(struct load_op) |
| 249 | + sizeof(struct field_ref); |
| 250 | struct field_ref ref_offset; |
| 251 | uint32_t reloc_offset_u32; |
| 252 | uint16_t reloc_offset; |
| 253 | enum filter_op op_type; |
| 254 | char *symbol = NULL; |
| 255 | int ret; |
| 256 | |
| 257 | ret = load_expression_legacy_match(exp, &op_type, &symbol); |
| 258 | if (ret <= 0) { |
| 259 | goto end; |
| 260 | } |
| 261 | insn = calloc(insn_len, 1); |
| 262 | if (!insn) { |
| 263 | ret = -ENOMEM; |
| 264 | goto end; |
| 265 | } |
| 266 | insn->op = op_type; |
| 267 | ref_offset.offset = (uint16_t) -1U; |
| 268 | memcpy(insn->data, &ref_offset, sizeof(ref_offset)); |
| 269 | /* reloc_offset points to struct load_op */ |
| 270 | reloc_offset_u32 = bytecode_get_len(&ctx->bytecode->b); |
| 271 | if (reloc_offset_u32 > LTTNG_FILTER_MAX_LEN - 1) { |
| 272 | ret = -EINVAL; |
| 273 | goto end; |
| 274 | } |
| 275 | reloc_offset = (uint16_t) reloc_offset_u32; |
| 276 | ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len); |
| 277 | if (ret) { |
| 278 | goto end; |
| 279 | } |
| 280 | /* append reloc */ |
| 281 | ret = bytecode_push(&ctx->bytecode_reloc, &reloc_offset, |
| 282 | 1, sizeof(reloc_offset)); |
| 283 | if (ret) { |
| 284 | goto end; |
| 285 | } |
| 286 | ret = bytecode_push(&ctx->bytecode_reloc, symbol, |
| 287 | 1, strlen(symbol) + 1); |
| 288 | if (ret) { |
| 289 | goto end; |
| 290 | } |
| 291 | ret = 1; /* legacy */ |
| 292 | end: |
| 293 | free(insn); |
| 294 | free(symbol); |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | static |
| 299 | int visit_node_load_expression(struct filter_parser_ctx *ctx, |
| 300 | const struct ir_op *node) |
| 301 | { |
| 302 | struct ir_load_expression *exp; |
| 303 | struct ir_load_expression_op *op; |
| 304 | int ret; |
| 305 | |
| 306 | exp = node->u.load.u.expression; |
| 307 | if (!exp) { |
| 308 | return -EINVAL; |
| 309 | } |
| 310 | op = exp->child; |
| 311 | if (!op) { |
| 312 | return -EINVAL; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * TODO: if we remove legacy load for application contexts, we |
| 317 | * need to update session bytecode parser as well. |
| 318 | */ |
| 319 | ret = visit_node_load_expression_legacy(ctx, exp, op); |
| 320 | if (ret < 0) { |
| 321 | return ret; |
| 322 | } |
| 323 | if (ret > 0) { |
| 324 | return 0; /* legacy */ |
| 325 | } |
| 326 | |
| 327 | for (; op != NULL; op = op->next) { |
| 328 | switch (op->type) { |
| 329 | case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT: |
| 330 | { |
| 331 | struct load_op *insn; |
| 332 | uint32_t insn_len = sizeof(struct load_op); |
| 333 | int ret; |
| 334 | |
| 335 | insn = calloc(insn_len, 1); |
| 336 | if (!insn) |
| 337 | return -ENOMEM; |
| 338 | insn->op = FILTER_OP_GET_CONTEXT_ROOT; |
| 339 | ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len); |
| 340 | free(insn); |
| 341 | if (ret) { |
| 342 | return ret; |
| 343 | } |
| 344 | break; |
| 345 | } |
| 346 | case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT: |
| 347 | { |
| 348 | struct load_op *insn; |
| 349 | uint32_t insn_len = sizeof(struct load_op); |
| 350 | int ret; |
| 351 | |
| 352 | insn = calloc(insn_len, 1); |
| 353 | if (!insn) |
| 354 | return -ENOMEM; |
| 355 | insn->op = FILTER_OP_GET_APP_CONTEXT_ROOT; |
| 356 | ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len); |
| 357 | free(insn); |
| 358 | if (ret) { |
| 359 | return ret; |
| 360 | } |
| 361 | break; |
| 362 | } |
| 363 | case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT: |
| 364 | { |
| 365 | struct load_op *insn; |
| 366 | uint32_t insn_len = sizeof(struct load_op); |
| 367 | int ret; |
| 368 | |
| 369 | insn = calloc(insn_len, 1); |
| 370 | if (!insn) |
| 371 | return -ENOMEM; |
| 372 | insn->op = FILTER_OP_GET_PAYLOAD_ROOT; |
| 373 | ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len); |
| 374 | free(insn); |
| 375 | if (ret) { |
| 376 | return ret; |
| 377 | } |
| 378 | break; |
| 379 | } |
| 380 | case IR_LOAD_EXPRESSION_GET_SYMBOL: |
| 381 | { |
| 382 | struct load_op *insn; |
| 383 | uint32_t insn_len = sizeof(struct load_op) |
| 384 | + sizeof(struct get_symbol); |
| 385 | struct get_symbol symbol_offset; |
| 386 | uint32_t reloc_offset_u32; |
| 387 | uint16_t reloc_offset; |
| 388 | uint32_t bytecode_reloc_offset_u32; |
| 389 | int ret; |
| 390 | |
| 391 | insn = calloc(insn_len, 1); |
| 392 | if (!insn) |
| 393 | return -ENOMEM; |
| 394 | insn->op = FILTER_OP_GET_SYMBOL; |
| 395 | bytecode_reloc_offset_u32 = |
| 396 | bytecode_get_len(&ctx->bytecode_reloc->b) |
| 397 | + sizeof(reloc_offset); |
| 398 | symbol_offset.offset = |
| 399 | (uint16_t) bytecode_reloc_offset_u32; |
| 400 | memcpy(insn->data, &symbol_offset, |
| 401 | sizeof(symbol_offset)); |
| 402 | /* reloc_offset points to struct load_op */ |
| 403 | reloc_offset_u32 = bytecode_get_len(&ctx->bytecode->b); |
| 404 | if (reloc_offset_u32 > LTTNG_FILTER_MAX_LEN - 1) { |
| 405 | free(insn); |
| 406 | return -EINVAL; |
| 407 | } |
| 408 | reloc_offset = (uint16_t) reloc_offset_u32; |
| 409 | ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len); |
| 410 | if (ret) { |
| 411 | free(insn); |
| 412 | return ret; |
| 413 | } |
| 414 | /* append reloc */ |
| 415 | ret = bytecode_push(&ctx->bytecode_reloc, &reloc_offset, |
| 416 | 1, sizeof(reloc_offset)); |
| 417 | if (ret) { |
| 418 | free(insn); |
| 419 | return ret; |
| 420 | } |
| 421 | ret = bytecode_push(&ctx->bytecode_reloc, |
| 422 | op->u.symbol, |
| 423 | 1, strlen(op->u.symbol) + 1); |
| 424 | free(insn); |
| 425 | if (ret) { |
| 426 | return ret; |
| 427 | } |
| 428 | break; |
| 429 | } |
| 430 | case IR_LOAD_EXPRESSION_GET_INDEX: |
| 431 | { |
| 432 | struct load_op *insn; |
| 433 | uint32_t insn_len = sizeof(struct load_op) |
| 434 | + sizeof(struct get_index_u64); |
| 435 | struct get_index_u64 index; |
| 436 | int ret; |
| 437 | |
| 438 | insn = calloc(insn_len, 1); |
| 439 | if (!insn) |
| 440 | return -ENOMEM; |
| 441 | insn->op = FILTER_OP_GET_INDEX_U64; |
| 442 | index.index = op->u.index; |
| 443 | memcpy(insn->data, &index, sizeof(index)); |
| 444 | ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len); |
| 445 | free(insn); |
| 446 | if (ret) { |
| 447 | return ret; |
| 448 | } |
| 449 | break; |
| 450 | } |
| 451 | case IR_LOAD_EXPRESSION_LOAD_FIELD: |
| 452 | { |
| 453 | struct load_op *insn; |
| 454 | uint32_t insn_len = sizeof(struct load_op); |
| 455 | int ret; |
| 456 | |
| 457 | insn = calloc(insn_len, 1); |
| 458 | if (!insn) |
| 459 | return -ENOMEM; |
| 460 | insn->op = FILTER_OP_LOAD_FIELD; |
| 461 | ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len); |
| 462 | free(insn); |
| 463 | if (ret) { |
| 464 | return ret; |
| 465 | } |
| 466 | break; |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | static |
| 474 | int visit_node_load(struct filter_parser_ctx *ctx, struct ir_op *node) |
| 475 | { |
| 476 | int ret; |
| 477 | |
| 478 | switch (node->data_type) { |
| 479 | case IR_DATA_UNKNOWN: |
| 480 | default: |
| 481 | fprintf(stderr, "[error] Unknown data type in %s\n", |
| 482 | __func__); |
| 483 | return -EINVAL; |
| 484 | |
| 485 | case IR_DATA_STRING: |
| 486 | { |
| 487 | struct load_op *insn; |
| 488 | uint32_t insn_len = sizeof(struct load_op) |
| 489 | + strlen(node->u.load.u.string.value) + 1; |
| 490 | |
| 491 | insn = calloc(insn_len, 1); |
| 492 | if (!insn) |
| 493 | return -ENOMEM; |
| 494 | |
| 495 | switch (node->u.load.u.string.type) { |
| 496 | case IR_LOAD_STRING_TYPE_GLOB_STAR: |
| 497 | /* |
| 498 | * We explicitly tell the interpreter here that |
| 499 | * this load is a full star globbing pattern so |
| 500 | * that the appropriate matching function can be |
| 501 | * called. Also, see comment below. |
| 502 | */ |
| 503 | insn->op = FILTER_OP_LOAD_STAR_GLOB_STRING; |
| 504 | break; |
| 505 | default: |
| 506 | /* |
| 507 | * This is the "legacy" string, which includes |
| 508 | * star globbing patterns with a star only at |
| 509 | * the end. Both "plain" and "star at the end" |
| 510 | * literal strings are handled at the same place |
| 511 | * by the tracer's filter bytecode interpreter, |
| 512 | * whereas full star globbing patterns (stars |
| 513 | * can be anywhere in the string) is a special |
| 514 | * case. |
| 515 | */ |
| 516 | insn->op = FILTER_OP_LOAD_STRING; |
| 517 | break; |
| 518 | } |
| 519 | |
| 520 | strcpy(insn->data, node->u.load.u.string.value); |
| 521 | ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len); |
| 522 | free(insn); |
| 523 | return ret; |
| 524 | } |
| 525 | case IR_DATA_NUMERIC: |
| 526 | { |
| 527 | struct load_op *insn; |
| 528 | uint32_t insn_len = sizeof(struct load_op) |
| 529 | + sizeof(struct literal_numeric); |
| 530 | |
| 531 | insn = calloc(insn_len, 1); |
| 532 | if (!insn) |
| 533 | return -ENOMEM; |
| 534 | insn->op = FILTER_OP_LOAD_S64; |
| 535 | memcpy(insn->data, &node->u.load.u.num, sizeof(int64_t)); |
| 536 | ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len); |
| 537 | free(insn); |
| 538 | return ret; |
| 539 | } |
| 540 | case IR_DATA_FLOAT: |
| 541 | { |
| 542 | struct load_op *insn; |
| 543 | uint32_t insn_len = sizeof(struct load_op) |
| 544 | + sizeof(struct literal_double); |
| 545 | |
| 546 | insn = calloc(insn_len, 1); |
| 547 | if (!insn) |
| 548 | return -ENOMEM; |
| 549 | insn->op = FILTER_OP_LOAD_DOUBLE; |
| 550 | memcpy(insn->data, &node->u.load.u.flt, sizeof(double)); |
| 551 | ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len); |
| 552 | free(insn); |
| 553 | return ret; |
| 554 | } |
| 555 | case IR_DATA_EXPRESSION: |
| 556 | return visit_node_load_expression(ctx, node); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | static |
| 561 | int visit_node_unary(struct filter_parser_ctx *ctx, struct ir_op *node) |
| 562 | { |
| 563 | int ret; |
| 564 | struct unary_op insn; |
| 565 | |
| 566 | /* Visit child */ |
| 567 | ret = recursive_visit_gen_bytecode(ctx, node->u.unary.child); |
| 568 | if (ret) |
| 569 | return ret; |
| 570 | |
| 571 | /* Generate end of bytecode instruction */ |
| 572 | switch (node->u.unary.type) { |
| 573 | case AST_UNARY_UNKNOWN: |
| 574 | default: |
| 575 | fprintf(stderr, "[error] Unknown unary node type in %s\n", |
| 576 | __func__); |
| 577 | return -EINVAL; |
| 578 | case AST_UNARY_PLUS: |
| 579 | /* Nothing to do. */ |
| 580 | return 0; |
| 581 | case AST_UNARY_MINUS: |
| 582 | insn.op = FILTER_OP_UNARY_MINUS; |
| 583 | return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn)); |
| 584 | case AST_UNARY_NOT: |
| 585 | insn.op = FILTER_OP_UNARY_NOT; |
| 586 | return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn)); |
| 587 | case AST_UNARY_BIT_NOT: |
| 588 | insn.op = FILTER_OP_UNARY_BIT_NOT; |
| 589 | return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn)); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * Binary comparator nesting is disallowed. This allows fitting into |
| 595 | * only 2 registers. |
| 596 | */ |
| 597 | static |
| 598 | int visit_node_binary(struct filter_parser_ctx *ctx, struct ir_op *node) |
| 599 | { |
| 600 | int ret; |
| 601 | struct binary_op insn; |
| 602 | |
| 603 | /* Visit child */ |
| 604 | ret = recursive_visit_gen_bytecode(ctx, node->u.binary.left); |
| 605 | if (ret) |
| 606 | return ret; |
| 607 | ret = recursive_visit_gen_bytecode(ctx, node->u.binary.right); |
| 608 | if (ret) |
| 609 | return ret; |
| 610 | |
| 611 | switch (node->u.binary.type) { |
| 612 | case AST_OP_UNKNOWN: |
| 613 | default: |
| 614 | fprintf(stderr, "[error] Unknown unary node type in %s\n", |
| 615 | __func__); |
| 616 | return -EINVAL; |
| 617 | |
| 618 | case AST_OP_AND: |
| 619 | case AST_OP_OR: |
| 620 | fprintf(stderr, "[error] Unexpected logical node type in %s\n", |
| 621 | __func__); |
| 622 | return -EINVAL; |
| 623 | |
| 624 | case AST_OP_MUL: |
| 625 | insn.op = FILTER_OP_MUL; |
| 626 | break; |
| 627 | case AST_OP_DIV: |
| 628 | insn.op = FILTER_OP_DIV; |
| 629 | break; |
| 630 | case AST_OP_MOD: |
| 631 | insn.op = FILTER_OP_MOD; |
| 632 | break; |
| 633 | case AST_OP_PLUS: |
| 634 | insn.op = FILTER_OP_PLUS; |
| 635 | break; |
| 636 | case AST_OP_MINUS: |
| 637 | insn.op = FILTER_OP_MINUS; |
| 638 | break; |
| 639 | case AST_OP_BIT_RSHIFT: |
| 640 | insn.op = FILTER_OP_BIT_RSHIFT; |
| 641 | break; |
| 642 | case AST_OP_BIT_LSHIFT: |
| 643 | insn.op = FILTER_OP_BIT_LSHIFT; |
| 644 | break; |
| 645 | case AST_OP_BIT_AND: |
| 646 | insn.op = FILTER_OP_BIT_AND; |
| 647 | break; |
| 648 | case AST_OP_BIT_OR: |
| 649 | insn.op = FILTER_OP_BIT_OR; |
| 650 | break; |
| 651 | case AST_OP_BIT_XOR: |
| 652 | insn.op = FILTER_OP_BIT_XOR; |
| 653 | break; |
| 654 | |
| 655 | case AST_OP_EQ: |
| 656 | insn.op = FILTER_OP_EQ; |
| 657 | break; |
| 658 | case AST_OP_NE: |
| 659 | insn.op = FILTER_OP_NE; |
| 660 | break; |
| 661 | case AST_OP_GT: |
| 662 | insn.op = FILTER_OP_GT; |
| 663 | break; |
| 664 | case AST_OP_LT: |
| 665 | insn.op = FILTER_OP_LT; |
| 666 | break; |
| 667 | case AST_OP_GE: |
| 668 | insn.op = FILTER_OP_GE; |
| 669 | break; |
| 670 | case AST_OP_LE: |
| 671 | insn.op = FILTER_OP_LE; |
| 672 | break; |
| 673 | } |
| 674 | return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn)); |
| 675 | } |
| 676 | |
| 677 | /* |
| 678 | * A logical op always return a s64 (1 or 0). |
| 679 | */ |
| 680 | static |
| 681 | int visit_node_logical(struct filter_parser_ctx *ctx, struct ir_op *node) |
| 682 | { |
| 683 | int ret; |
| 684 | struct logical_op insn; |
| 685 | uint16_t skip_offset_loc; |
| 686 | uint16_t target_loc; |
| 687 | |
| 688 | /* Visit left child */ |
| 689 | ret = recursive_visit_gen_bytecode(ctx, node->u.binary.left); |
| 690 | if (ret) |
| 691 | return ret; |
| 692 | /* Cast to s64 if float or field ref */ |
| 693 | if ((node->u.binary.left->data_type == IR_DATA_FIELD_REF |
| 694 | || node->u.binary.left->data_type == IR_DATA_GET_CONTEXT_REF |
| 695 | || node->u.binary.left->data_type == IR_DATA_EXPRESSION) |
| 696 | || node->u.binary.left->data_type == IR_DATA_FLOAT) { |
| 697 | struct cast_op cast_insn; |
| 698 | |
| 699 | if (node->u.binary.left->data_type == IR_DATA_FIELD_REF |
| 700 | || node->u.binary.left->data_type == IR_DATA_GET_CONTEXT_REF |
| 701 | || node->u.binary.left->data_type == IR_DATA_EXPRESSION) { |
| 702 | cast_insn.op = FILTER_OP_CAST_TO_S64; |
| 703 | } else { |
| 704 | cast_insn.op = FILTER_OP_CAST_DOUBLE_TO_S64; |
| 705 | } |
| 706 | ret = bytecode_push(&ctx->bytecode, &cast_insn, |
| 707 | 1, sizeof(cast_insn)); |
| 708 | if (ret) |
| 709 | return ret; |
| 710 | } |
| 711 | switch (node->u.logical.type) { |
| 712 | default: |
| 713 | fprintf(stderr, "[error] Unknown node type in %s\n", |
| 714 | __func__); |
| 715 | return -EINVAL; |
| 716 | |
| 717 | case AST_OP_AND: |
| 718 | insn.op = FILTER_OP_AND; |
| 719 | break; |
| 720 | case AST_OP_OR: |
| 721 | insn.op = FILTER_OP_OR; |
| 722 | break; |
| 723 | } |
| 724 | insn.skip_offset = (uint16_t) -1UL; /* Temporary */ |
| 725 | ret = bytecode_push_logical(&ctx->bytecode, &insn, 1, sizeof(insn), |
| 726 | &skip_offset_loc); |
| 727 | if (ret) |
| 728 | return ret; |
| 729 | /* Visit right child */ |
| 730 | ret = recursive_visit_gen_bytecode(ctx, node->u.binary.right); |
| 731 | if (ret) |
| 732 | return ret; |
| 733 | /* Cast to s64 if float or field ref */ |
| 734 | if ((node->u.binary.right->data_type == IR_DATA_FIELD_REF |
| 735 | || node->u.binary.right->data_type == IR_DATA_GET_CONTEXT_REF |
| 736 | || node->u.binary.right->data_type == IR_DATA_EXPRESSION) |
| 737 | || node->u.binary.right->data_type == IR_DATA_FLOAT) { |
| 738 | struct cast_op cast_insn; |
| 739 | |
| 740 | if (node->u.binary.right->data_type == IR_DATA_FIELD_REF |
| 741 | || node->u.binary.right->data_type == IR_DATA_GET_CONTEXT_REF |
| 742 | || node->u.binary.right->data_type == IR_DATA_EXPRESSION) { |
| 743 | cast_insn.op = FILTER_OP_CAST_TO_S64; |
| 744 | } else { |
| 745 | cast_insn.op = FILTER_OP_CAST_DOUBLE_TO_S64; |
| 746 | } |
| 747 | ret = bytecode_push(&ctx->bytecode, &cast_insn, |
| 748 | 1, sizeof(cast_insn)); |
| 749 | if (ret) |
| 750 | return ret; |
| 751 | } |
| 752 | /* We now know where the logical op can skip. */ |
| 753 | target_loc = (uint16_t) bytecode_get_len(&ctx->bytecode->b); |
| 754 | ret = bytecode_patch(&ctx->bytecode, |
| 755 | &target_loc, /* Offset to jump to */ |
| 756 | skip_offset_loc, /* Where to patch */ |
| 757 | sizeof(uint16_t)); |
| 758 | return ret; |
| 759 | } |
| 760 | |
| 761 | /* |
| 762 | * Postorder traversal of the tree. We need the children result before |
| 763 | * we can evaluate the parent. |
| 764 | */ |
| 765 | static |
| 766 | int recursive_visit_gen_bytecode(struct filter_parser_ctx *ctx, |
| 767 | struct ir_op *node) |
| 768 | { |
| 769 | switch (node->op) { |
| 770 | case IR_OP_UNKNOWN: |
| 771 | default: |
| 772 | fprintf(stderr, "[error] Unknown node type in %s\n", |
| 773 | __func__); |
| 774 | return -EINVAL; |
| 775 | |
| 776 | case IR_OP_ROOT: |
| 777 | return visit_node_root(ctx, node); |
| 778 | case IR_OP_LOAD: |
| 779 | return visit_node_load(ctx, node); |
| 780 | case IR_OP_UNARY: |
| 781 | return visit_node_unary(ctx, node); |
| 782 | case IR_OP_BINARY: |
| 783 | return visit_node_binary(ctx, node); |
| 784 | case IR_OP_LOGICAL: |
| 785 | return visit_node_logical(ctx, node); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | LTTNG_HIDDEN |
| 790 | void filter_bytecode_free(struct filter_parser_ctx *ctx) |
| 791 | { |
| 792 | if (!ctx) { |
| 793 | return; |
| 794 | } |
| 795 | |
| 796 | if (ctx->bytecode) { |
| 797 | free(ctx->bytecode); |
| 798 | ctx->bytecode = NULL; |
| 799 | } |
| 800 | |
| 801 | if (ctx->bytecode_reloc) { |
| 802 | free(ctx->bytecode_reloc); |
| 803 | ctx->bytecode_reloc = NULL; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | LTTNG_HIDDEN |
| 808 | int filter_visitor_bytecode_generate(struct filter_parser_ctx *ctx) |
| 809 | { |
| 810 | int ret; |
| 811 | |
| 812 | ret = bytecode_init(&ctx->bytecode); |
| 813 | if (ret) |
| 814 | return ret; |
| 815 | ret = bytecode_init(&ctx->bytecode_reloc); |
| 816 | if (ret) |
| 817 | goto error; |
| 818 | ret = recursive_visit_gen_bytecode(ctx, ctx->ir_root); |
| 819 | if (ret) |
| 820 | goto error; |
| 821 | |
| 822 | /* Finally, append symbol table to bytecode */ |
| 823 | ctx->bytecode->b.reloc_table_offset = bytecode_get_len(&ctx->bytecode->b); |
| 824 | return bytecode_push(&ctx->bytecode, ctx->bytecode_reloc->b.data, |
| 825 | 1, bytecode_get_len(&ctx->bytecode_reloc->b)); |
| 826 | |
| 827 | error: |
| 828 | filter_bytecode_free(ctx); |
| 829 | return ret; |
| 830 | } |