2 * filter-visitor-generate-bytecode.c
4 * LTTng filter bytecode generation
6 * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * SPDX-License-Identifier: LGPL-2.1-only
14 #include <common/align.h>
15 #include <common/compat/errno.h>
16 #include <common/compat/string.h>
18 #include "common/align.h"
19 #include "common/bytecode/bytecode.h"
20 #include "common/compat/string.h"
21 #include "common/macros.h"
22 #include "filter-ast.h"
23 #include "filter-ir.h"
26 #define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b)))
30 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
34 int bytecode_patch(struct lttng_bytecode_alloc
**fb
,
39 if (offset
>= (*fb
)->b
.len
) {
42 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
47 int visit_node_root(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
50 struct return_op insn
;
53 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.root
.child
);
57 /* Generate end of bytecode instruction */
58 insn
.op
= BYTECODE_OP_RETURN
;
59 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
63 int append_str(char **s
, const char *append
)
67 size_t oldlen
= (old
== NULL
) ? 0 : strlen(old
);
68 size_t appendlen
= strlen(append
);
70 new = calloc(oldlen
+ appendlen
+ 1, 1);
89 int load_expression_legacy_match(const struct ir_load_expression
*exp
,
90 enum bytecode_op
*op_type
,
93 const struct ir_load_expression_op
*op
;
94 bool need_dot
= false;
98 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
99 *op_type
= BYTECODE_OP_GET_CONTEXT_REF
;
100 if (append_str(symbol
, "$ctx.")) {
105 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
106 *op_type
= BYTECODE_OP_GET_CONTEXT_REF
;
107 if (append_str(symbol
, "$app.")) {
112 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
113 *op_type
= BYTECODE_OP_LOAD_FIELD_REF
;
117 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
118 case IR_LOAD_EXPRESSION_GET_INDEX
:
119 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
121 return 0; /* no match */
127 return 0; /* no match */
130 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
132 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
133 if (need_dot
&& append_str(symbol
, ".")) {
136 if (append_str(symbol
, op
->u
.symbol
)) {
141 return 0; /* no match */
146 return 1; /* Legacy match */
155 int visit_node_load_expression_legacy(struct filter_parser_ctx
*ctx
,
156 const struct ir_load_expression
*exp
,
157 const struct ir_load_expression_op
*op
)
159 struct load_op
*insn
= NULL
;
160 uint32_t insn_len
= sizeof(struct load_op
)
161 + sizeof(struct field_ref
);
162 struct field_ref ref_offset
;
163 uint32_t reloc_offset_u32
;
164 uint16_t reloc_offset
;
165 enum bytecode_op op_type
;
169 ret
= load_expression_legacy_match(exp
, &op_type
, &symbol
);
173 insn
= calloc(insn_len
, 1);
179 ref_offset
.offset
= (uint16_t) -1U;
180 memcpy(insn
->data
, &ref_offset
, sizeof(ref_offset
));
181 /* reloc_offset points to struct load_op */
182 reloc_offset_u32
= bytecode_get_len(&ctx
->bytecode
->b
);
183 if (reloc_offset_u32
> LTTNG_FILTER_MAX_LEN
- 1) {
187 reloc_offset
= (uint16_t) reloc_offset_u32
;
188 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
193 ret
= bytecode_push(&ctx
->bytecode_reloc
, &reloc_offset
,
194 1, sizeof(reloc_offset
));
198 ret
= bytecode_push(&ctx
->bytecode_reloc
, symbol
,
199 1, strlen(symbol
) + 1);
203 ret
= 1; /* legacy */
211 int visit_node_load_expression(struct filter_parser_ctx
*ctx
,
212 const struct ir_op
*node
)
214 struct ir_load_expression
*exp
;
215 struct ir_load_expression_op
*op
;
218 exp
= node
->u
.load
.u
.expression
;
228 * TODO: if we remove legacy load for application contexts, we
229 * need to update session bytecode parser as well.
231 ret
= visit_node_load_expression_legacy(ctx
, exp
, op
);
236 return 0; /* legacy */
239 for (; op
!= NULL
; op
= op
->next
) {
241 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
243 ret
= bytecode_push_get_context_root(&ctx
->bytecode
);
251 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
253 ret
= bytecode_push_get_app_context_root(
262 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
264 ret
= bytecode_push_get_payload_root(&ctx
->bytecode
);
272 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
274 ret
= bytecode_push_get_symbol(&ctx
->bytecode
,
275 &ctx
->bytecode_reloc
, op
->u
.symbol
);
283 case IR_LOAD_EXPRESSION_GET_INDEX
:
285 ret
= bytecode_push_get_index_u64(
286 &ctx
->bytecode
, op
->u
.index
);
294 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
296 struct load_op
*insn
;
297 uint32_t insn_len
= sizeof(struct load_op
);
299 insn
= calloc(insn_len
, 1);
302 insn
->op
= BYTECODE_OP_LOAD_FIELD
;
303 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
316 int visit_node_load(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
320 switch (node
->data_type
) {
321 case IR_DATA_UNKNOWN
:
323 fprintf(stderr
, "[error] Unknown data type in %s\n",
329 struct load_op
*insn
;
330 uint32_t insn_len
= sizeof(struct load_op
)
331 + strlen(node
->u
.load
.u
.string
.value
) + 1;
333 insn
= calloc(insn_len
, 1);
337 switch (node
->u
.load
.u
.string
.type
) {
338 case IR_LOAD_STRING_TYPE_GLOB_STAR
:
340 * We explicitly tell the interpreter here that
341 * this load is a full star globbing pattern so
342 * that the appropriate matching function can be
343 * called. Also, see comment below.
345 insn
->op
= BYTECODE_OP_LOAD_STAR_GLOB_STRING
;
349 * This is the "legacy" string, which includes
350 * star globbing patterns with a star only at
351 * the end. Both "plain" and "star at the end"
352 * literal strings are handled at the same place
353 * by the tracer's filter bytecode interpreter,
354 * whereas full star globbing patterns (stars
355 * can be anywhere in the string) is a special
358 insn
->op
= BYTECODE_OP_LOAD_STRING
;
362 strcpy(insn
->data
, node
->u
.load
.u
.string
.value
);
363 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
367 case IR_DATA_NUMERIC
:
369 struct load_op
*insn
;
370 uint32_t insn_len
= sizeof(struct load_op
)
371 + sizeof(struct literal_numeric
);
373 insn
= calloc(insn_len
, 1);
376 insn
->op
= BYTECODE_OP_LOAD_S64
;
377 memcpy(insn
->data
, &node
->u
.load
.u
.num
, sizeof(int64_t));
378 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
384 struct load_op
*insn
;
385 uint32_t insn_len
= sizeof(struct load_op
)
386 + sizeof(struct literal_double
);
388 insn
= calloc(insn_len
, 1);
391 insn
->op
= BYTECODE_OP_LOAD_DOUBLE
;
392 memcpy(insn
->data
, &node
->u
.load
.u
.flt
, sizeof(double));
393 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
397 case IR_DATA_EXPRESSION
:
398 return visit_node_load_expression(ctx
, node
);
403 int visit_node_unary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
406 struct unary_op insn
;
409 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.unary
.child
);
413 /* Generate end of bytecode instruction */
414 switch (node
->u
.unary
.type
) {
415 case AST_UNARY_UNKNOWN
:
417 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
423 case AST_UNARY_MINUS
:
424 insn
.op
= BYTECODE_OP_UNARY_MINUS
;
425 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
427 insn
.op
= BYTECODE_OP_UNARY_NOT
;
428 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
429 case AST_UNARY_BIT_NOT
:
430 insn
.op
= BYTECODE_OP_UNARY_BIT_NOT
;
431 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
436 * Binary comparator nesting is disallowed. This allows fitting into
440 int visit_node_binary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
443 struct binary_op insn
;
446 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
449 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
453 switch (node
->u
.binary
.type
) {
456 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
462 fprintf(stderr
, "[error] Unexpected logical node type in %s\n",
467 insn
.op
= BYTECODE_OP_MUL
;
470 insn
.op
= BYTECODE_OP_DIV
;
473 insn
.op
= BYTECODE_OP_MOD
;
476 insn
.op
= BYTECODE_OP_PLUS
;
479 insn
.op
= BYTECODE_OP_MINUS
;
481 case AST_OP_BIT_RSHIFT
:
482 insn
.op
= BYTECODE_OP_BIT_RSHIFT
;
484 case AST_OP_BIT_LSHIFT
:
485 insn
.op
= BYTECODE_OP_BIT_LSHIFT
;
488 insn
.op
= BYTECODE_OP_BIT_AND
;
491 insn
.op
= BYTECODE_OP_BIT_OR
;
494 insn
.op
= BYTECODE_OP_BIT_XOR
;
498 insn
.op
= BYTECODE_OP_EQ
;
501 insn
.op
= BYTECODE_OP_NE
;
504 insn
.op
= BYTECODE_OP_GT
;
507 insn
.op
= BYTECODE_OP_LT
;
510 insn
.op
= BYTECODE_OP_GE
;
513 insn
.op
= BYTECODE_OP_LE
;
516 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
520 * A logical op always return a s64 (1 or 0).
523 int visit_node_logical(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
526 struct logical_op insn
;
527 uint16_t skip_offset_loc
;
530 /* Visit left child */
531 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
534 /* Cast to s64 if float or field ref */
535 if ((node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
536 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
537 || node
->u
.binary
.left
->data_type
== IR_DATA_EXPRESSION
)
538 || node
->u
.binary
.left
->data_type
== IR_DATA_FLOAT
) {
539 struct cast_op cast_insn
;
541 if (node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
542 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
543 || node
->u
.binary
.left
->data_type
== IR_DATA_EXPRESSION
) {
544 cast_insn
.op
= BYTECODE_OP_CAST_TO_S64
;
546 cast_insn
.op
= BYTECODE_OP_CAST_DOUBLE_TO_S64
;
548 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
549 1, sizeof(cast_insn
));
553 switch (node
->u
.logical
.type
) {
555 fprintf(stderr
, "[error] Unknown node type in %s\n",
560 insn
.op
= BYTECODE_OP_AND
;
563 insn
.op
= BYTECODE_OP_OR
;
566 insn
.skip_offset
= (uint16_t) -1UL; /* Temporary */
567 ret
= bytecode_push_logical(&ctx
->bytecode
, &insn
, 1, sizeof(insn
),
571 /* Visit right child */
572 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
575 /* Cast to s64 if float or field ref */
576 if ((node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
577 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
578 || node
->u
.binary
.right
->data_type
== IR_DATA_EXPRESSION
)
579 || node
->u
.binary
.right
->data_type
== IR_DATA_FLOAT
) {
580 struct cast_op cast_insn
;
582 if (node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
583 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
584 || node
->u
.binary
.right
->data_type
== IR_DATA_EXPRESSION
) {
585 cast_insn
.op
= BYTECODE_OP_CAST_TO_S64
;
587 cast_insn
.op
= BYTECODE_OP_CAST_DOUBLE_TO_S64
;
589 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
590 1, sizeof(cast_insn
));
594 /* We now know where the logical op can skip. */
595 target_loc
= (uint16_t) bytecode_get_len(&ctx
->bytecode
->b
);
596 ret
= bytecode_patch(&ctx
->bytecode
,
597 &target_loc
, /* Offset to jump to */
598 skip_offset_loc
, /* Where to patch */
604 * Postorder traversal of the tree. We need the children result before
605 * we can evaluate the parent.
608 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
614 fprintf(stderr
, "[error] Unknown node type in %s\n",
619 return visit_node_root(ctx
, node
);
621 return visit_node_load(ctx
, node
);
623 return visit_node_unary(ctx
, node
);
625 return visit_node_binary(ctx
, node
);
627 return visit_node_logical(ctx
, node
);
632 void filter_bytecode_free(struct filter_parser_ctx
*ctx
)
640 ctx
->bytecode
= NULL
;
643 if (ctx
->bytecode_reloc
) {
644 free(ctx
->bytecode_reloc
);
645 ctx
->bytecode_reloc
= NULL
;
650 int filter_visitor_bytecode_generate(struct filter_parser_ctx
*ctx
)
654 ret
= bytecode_init(&ctx
->bytecode
);
657 ret
= bytecode_init(&ctx
->bytecode_reloc
);
660 ret
= recursive_visit_gen_bytecode(ctx
, ctx
->ir_root
);
664 /* Finally, append symbol table to bytecode */
665 ctx
->bytecode
->b
.reloc_table_offset
= bytecode_get_len(&ctx
->bytecode
->b
);
666 return bytecode_push(&ctx
->bytecode
, ctx
->bytecode_reloc
->b
.data
,
667 1, bytecode_get_len(&ctx
->bytecode_reloc
->b
));
670 filter_bytecode_free(ctx
);