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_filter_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
= FILTER_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 filter_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
= FILTER_OP_GET_CONTEXT_REF
;
100 if (append_str(symbol
, "$ctx.")) {
105 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
106 *op_type
= FILTER_OP_GET_CONTEXT_REF
;
107 if (append_str(symbol
, "$app.")) {
112 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
113 *op_type
= FILTER_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 filter_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 struct load_op
*insn
;
244 uint32_t insn_len
= sizeof(struct load_op
);
247 insn
= calloc(insn_len
, 1);
250 insn
->op
= FILTER_OP_GET_CONTEXT_ROOT
;
251 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
258 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
260 struct load_op
*insn
;
261 uint32_t insn_len
= sizeof(struct load_op
);
264 insn
= calloc(insn_len
, 1);
267 insn
->op
= FILTER_OP_GET_APP_CONTEXT_ROOT
;
268 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
275 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
277 struct load_op
*insn
;
278 uint32_t insn_len
= sizeof(struct load_op
);
281 insn
= calloc(insn_len
, 1);
284 insn
->op
= FILTER_OP_GET_PAYLOAD_ROOT
;
285 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
292 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
294 struct load_op
*insn
;
295 uint32_t insn_len
= sizeof(struct load_op
)
296 + sizeof(struct get_symbol
);
297 struct get_symbol symbol_offset
;
298 uint32_t reloc_offset_u32
;
299 uint16_t reloc_offset
;
300 uint32_t bytecode_reloc_offset_u32
;
303 insn
= calloc(insn_len
, 1);
306 insn
->op
= FILTER_OP_GET_SYMBOL
;
307 bytecode_reloc_offset_u32
=
308 bytecode_get_len(&ctx
->bytecode_reloc
->b
)
309 + sizeof(reloc_offset
);
310 symbol_offset
.offset
=
311 (uint16_t) bytecode_reloc_offset_u32
;
312 memcpy(insn
->data
, &symbol_offset
,
313 sizeof(symbol_offset
));
314 /* reloc_offset points to struct load_op */
315 reloc_offset_u32
= bytecode_get_len(&ctx
->bytecode
->b
);
316 if (reloc_offset_u32
> LTTNG_FILTER_MAX_LEN
- 1) {
320 reloc_offset
= (uint16_t) reloc_offset_u32
;
321 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
327 ret
= bytecode_push(&ctx
->bytecode_reloc
, &reloc_offset
,
328 1, sizeof(reloc_offset
));
333 ret
= bytecode_push(&ctx
->bytecode_reloc
,
335 1, strlen(op
->u
.symbol
) + 1);
342 case IR_LOAD_EXPRESSION_GET_INDEX
:
344 struct load_op
*insn
;
345 uint32_t insn_len
= sizeof(struct load_op
)
346 + sizeof(struct get_index_u64
);
347 struct get_index_u64 index
;
350 insn
= calloc(insn_len
, 1);
353 insn
->op
= FILTER_OP_GET_INDEX_U64
;
354 index
.index
= op
->u
.index
;
355 memcpy(insn
->data
, &index
, sizeof(index
));
356 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
363 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
365 struct load_op
*insn
;
366 uint32_t insn_len
= sizeof(struct load_op
);
369 insn
= calloc(insn_len
, 1);
372 insn
->op
= FILTER_OP_LOAD_FIELD
;
373 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
386 int visit_node_load(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
390 switch (node
->data_type
) {
391 case IR_DATA_UNKNOWN
:
393 fprintf(stderr
, "[error] Unknown data type in %s\n",
399 struct load_op
*insn
;
400 uint32_t insn_len
= sizeof(struct load_op
)
401 + strlen(node
->u
.load
.u
.string
.value
) + 1;
403 insn
= calloc(insn_len
, 1);
407 switch (node
->u
.load
.u
.string
.type
) {
408 case IR_LOAD_STRING_TYPE_GLOB_STAR
:
410 * We explicitly tell the interpreter here that
411 * this load is a full star globbing pattern so
412 * that the appropriate matching function can be
413 * called. Also, see comment below.
415 insn
->op
= FILTER_OP_LOAD_STAR_GLOB_STRING
;
419 * This is the "legacy" string, which includes
420 * star globbing patterns with a star only at
421 * the end. Both "plain" and "star at the end"
422 * literal strings are handled at the same place
423 * by the tracer's filter bytecode interpreter,
424 * whereas full star globbing patterns (stars
425 * can be anywhere in the string) is a special
428 insn
->op
= FILTER_OP_LOAD_STRING
;
432 strcpy(insn
->data
, node
->u
.load
.u
.string
.value
);
433 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
437 case IR_DATA_NUMERIC
:
439 struct load_op
*insn
;
440 uint32_t insn_len
= sizeof(struct load_op
)
441 + sizeof(struct literal_numeric
);
443 insn
= calloc(insn_len
, 1);
446 insn
->op
= FILTER_OP_LOAD_S64
;
447 memcpy(insn
->data
, &node
->u
.load
.u
.num
, sizeof(int64_t));
448 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
454 struct load_op
*insn
;
455 uint32_t insn_len
= sizeof(struct load_op
)
456 + sizeof(struct literal_double
);
458 insn
= calloc(insn_len
, 1);
461 insn
->op
= FILTER_OP_LOAD_DOUBLE
;
462 memcpy(insn
->data
, &node
->u
.load
.u
.flt
, sizeof(double));
463 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
467 case IR_DATA_EXPRESSION
:
468 return visit_node_load_expression(ctx
, node
);
473 int visit_node_unary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
476 struct unary_op insn
;
479 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.unary
.child
);
483 /* Generate end of bytecode instruction */
484 switch (node
->u
.unary
.type
) {
485 case AST_UNARY_UNKNOWN
:
487 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
493 case AST_UNARY_MINUS
:
494 insn
.op
= FILTER_OP_UNARY_MINUS
;
495 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
497 insn
.op
= FILTER_OP_UNARY_NOT
;
498 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
499 case AST_UNARY_BIT_NOT
:
500 insn
.op
= FILTER_OP_UNARY_BIT_NOT
;
501 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
506 * Binary comparator nesting is disallowed. This allows fitting into
510 int visit_node_binary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
513 struct binary_op insn
;
516 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
519 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
523 switch (node
->u
.binary
.type
) {
526 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
532 fprintf(stderr
, "[error] Unexpected logical node type in %s\n",
537 insn
.op
= FILTER_OP_MUL
;
540 insn
.op
= FILTER_OP_DIV
;
543 insn
.op
= FILTER_OP_MOD
;
546 insn
.op
= FILTER_OP_PLUS
;
549 insn
.op
= FILTER_OP_MINUS
;
551 case AST_OP_BIT_RSHIFT
:
552 insn
.op
= FILTER_OP_BIT_RSHIFT
;
554 case AST_OP_BIT_LSHIFT
:
555 insn
.op
= FILTER_OP_BIT_LSHIFT
;
558 insn
.op
= FILTER_OP_BIT_AND
;
561 insn
.op
= FILTER_OP_BIT_OR
;
564 insn
.op
= FILTER_OP_BIT_XOR
;
568 insn
.op
= FILTER_OP_EQ
;
571 insn
.op
= FILTER_OP_NE
;
574 insn
.op
= FILTER_OP_GT
;
577 insn
.op
= FILTER_OP_LT
;
580 insn
.op
= FILTER_OP_GE
;
583 insn
.op
= FILTER_OP_LE
;
586 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
590 * A logical op always return a s64 (1 or 0).
593 int visit_node_logical(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
596 struct logical_op insn
;
597 uint16_t skip_offset_loc
;
600 /* Visit left child */
601 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
604 /* Cast to s64 if float or field ref */
605 if ((node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
606 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
607 || node
->u
.binary
.left
->data_type
== IR_DATA_EXPRESSION
)
608 || node
->u
.binary
.left
->data_type
== IR_DATA_FLOAT
) {
609 struct cast_op cast_insn
;
611 if (node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
612 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
613 || node
->u
.binary
.left
->data_type
== IR_DATA_EXPRESSION
) {
614 cast_insn
.op
= FILTER_OP_CAST_TO_S64
;
616 cast_insn
.op
= FILTER_OP_CAST_DOUBLE_TO_S64
;
618 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
619 1, sizeof(cast_insn
));
623 switch (node
->u
.logical
.type
) {
625 fprintf(stderr
, "[error] Unknown node type in %s\n",
630 insn
.op
= FILTER_OP_AND
;
633 insn
.op
= FILTER_OP_OR
;
636 insn
.skip_offset
= (uint16_t) -1UL; /* Temporary */
637 ret
= bytecode_push_logical(&ctx
->bytecode
, &insn
, 1, sizeof(insn
),
641 /* Visit right child */
642 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
645 /* Cast to s64 if float or field ref */
646 if ((node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
647 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
648 || node
->u
.binary
.right
->data_type
== IR_DATA_EXPRESSION
)
649 || node
->u
.binary
.right
->data_type
== IR_DATA_FLOAT
) {
650 struct cast_op cast_insn
;
652 if (node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
653 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
654 || node
->u
.binary
.right
->data_type
== IR_DATA_EXPRESSION
) {
655 cast_insn
.op
= FILTER_OP_CAST_TO_S64
;
657 cast_insn
.op
= FILTER_OP_CAST_DOUBLE_TO_S64
;
659 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
660 1, sizeof(cast_insn
));
664 /* We now know where the logical op can skip. */
665 target_loc
= (uint16_t) bytecode_get_len(&ctx
->bytecode
->b
);
666 ret
= bytecode_patch(&ctx
->bytecode
,
667 &target_loc
, /* Offset to jump to */
668 skip_offset_loc
, /* Where to patch */
674 * Postorder traversal of the tree. We need the children result before
675 * we can evaluate the parent.
678 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
684 fprintf(stderr
, "[error] Unknown node type in %s\n",
689 return visit_node_root(ctx
, node
);
691 return visit_node_load(ctx
, node
);
693 return visit_node_unary(ctx
, node
);
695 return visit_node_binary(ctx
, node
);
697 return visit_node_logical(ctx
, node
);
702 void filter_bytecode_free(struct filter_parser_ctx
*ctx
)
710 ctx
->bytecode
= NULL
;
713 if (ctx
->bytecode_reloc
) {
714 free(ctx
->bytecode_reloc
);
715 ctx
->bytecode_reloc
= NULL
;
720 int filter_visitor_bytecode_generate(struct filter_parser_ctx
*ctx
)
724 ret
= bytecode_init(&ctx
->bytecode
);
727 ret
= bytecode_init(&ctx
->bytecode_reloc
);
730 ret
= recursive_visit_gen_bytecode(ctx
, ctx
->ir_root
);
734 /* Finally, append symbol table to bytecode */
735 ctx
->bytecode
->b
.reloc_table_offset
= bytecode_get_len(&ctx
->bytecode
->b
);
736 return bytecode_push(&ctx
->bytecode
, ctx
->bytecode_reloc
->b
.data
,
737 1, bytecode_get_len(&ctx
->bytecode_reloc
->b
));
740 filter_bytecode_free(ctx
);