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
15 #include <common/align.h>
16 #include <common/compat/string.h>
18 #include "filter-bytecode.h"
19 #include "filter-ir.h"
20 #include "filter-ast.h"
22 #include <common/macros.h>
25 #define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b)))
28 #define INIT_ALLOC_SIZE 4
31 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
34 static inline int get_count_order(unsigned int count
)
38 order
= lttng_fls(count
) - 1;
39 if (count
& (count
- 1))
45 int bytecode_init(struct lttng_filter_bytecode_alloc
**fb
)
49 alloc_len
= sizeof(struct lttng_filter_bytecode_alloc
) + INIT_ALLOC_SIZE
;
50 *fb
= calloc(alloc_len
, 1);
54 (*fb
)->alloc_len
= alloc_len
;
60 int32_t bytecode_reserve(struct lttng_filter_bytecode_alloc
**fb
, uint32_t align
, uint32_t len
)
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
;
68 if (new_len
> LTTNG_FILTER_MAX_LEN
)
71 if (new_alloc_len
> old_alloc_len
) {
72 struct lttng_filter_bytecode_alloc
*newptr
;
75 max_t(uint32_t, 1U << get_count_order(new_alloc_len
), old_alloc_len
<< 1);
76 newptr
= realloc(*fb
, new_alloc_len
);
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
;
84 (*fb
)->b
.len
+= padding
;
91 int bytecode_push(struct lttng_filter_bytecode_alloc
**fb
, const void *data
,
92 uint32_t align
, uint32_t len
)
96 offset
= bytecode_reserve(fb
, align
, len
);
99 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
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
)
111 offset
= bytecode_reserve(fb
, align
, len
);
114 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
116 (void *) &((struct logical_op
*) &(*fb
)->b
.data
[offset
])->skip_offset
117 - (void *) &(*fb
)->b
.data
[0];
122 int bytecode_patch(struct lttng_filter_bytecode_alloc
**fb
,
127 if (offset
>= (*fb
)->b
.len
) {
130 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
135 int visit_node_root(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
138 struct return_op insn
;
141 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.root
.child
);
145 /* Generate end of bytecode instruction */
146 insn
.op
= FILTER_OP_RETURN
;
147 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
151 int append_str(char **s
, const char *append
)
155 size_t oldlen
= (old
== NULL
) ? 0 : strlen(old
);
156 size_t appendlen
= strlen(append
);
158 new = calloc(oldlen
+ appendlen
+ 1, 1);
177 int load_expression_legacy_match(const struct ir_load_expression
*exp
,
178 enum filter_op
*op_type
,
181 const struct ir_load_expression_op
*op
;
182 bool need_dot
= false;
186 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
187 *op_type
= FILTER_OP_GET_CONTEXT_REF
;
188 if (append_str(symbol
, "$ctx.")) {
193 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
194 *op_type
= FILTER_OP_GET_CONTEXT_REF
;
195 if (append_str(symbol
, "$app.")) {
200 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
201 *op_type
= FILTER_OP_LOAD_FIELD_REF
;
205 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
206 case IR_LOAD_EXPRESSION_GET_INDEX
:
207 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
209 return 0; /* no match */
215 return 0; /* no match */
218 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
220 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
221 if (need_dot
&& append_str(symbol
, ".")) {
224 if (append_str(symbol
, op
->u
.symbol
)) {
229 return 0; /* no match */
234 return 1; /* Legacy match */
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
)
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
;
257 ret
= load_expression_legacy_match(exp
, &op_type
, &symbol
);
261 insn
= calloc(insn_len
, 1);
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) {
275 reloc_offset
= (uint16_t) reloc_offset_u32
;
276 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
281 ret
= bytecode_push(&ctx
->bytecode_reloc
, &reloc_offset
,
282 1, sizeof(reloc_offset
));
286 ret
= bytecode_push(&ctx
->bytecode_reloc
, symbol
,
287 1, strlen(symbol
) + 1);
291 ret
= 1; /* legacy */
299 int visit_node_load_expression(struct filter_parser_ctx
*ctx
,
300 const struct ir_op
*node
)
302 struct ir_load_expression
*exp
;
303 struct ir_load_expression_op
*op
;
306 exp
= node
->u
.load
.u
.expression
;
316 * TODO: if we remove legacy load for application contexts, we
317 * need to update session bytecode parser as well.
319 ret
= visit_node_load_expression_legacy(ctx
, exp
, op
);
324 return 0; /* legacy */
327 for (; op
!= NULL
; op
= op
->next
) {
329 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
331 struct load_op
*insn
;
332 uint32_t insn_len
= sizeof(struct load_op
);
335 insn
= calloc(insn_len
, 1);
338 insn
->op
= FILTER_OP_GET_CONTEXT_ROOT
;
339 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
346 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
348 struct load_op
*insn
;
349 uint32_t insn_len
= sizeof(struct load_op
);
352 insn
= calloc(insn_len
, 1);
355 insn
->op
= FILTER_OP_GET_APP_CONTEXT_ROOT
;
356 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
363 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
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_GET_PAYLOAD_ROOT
;
373 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
380 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
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
;
391 insn
= calloc(insn_len
, 1);
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) {
408 reloc_offset
= (uint16_t) reloc_offset_u32
;
409 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
415 ret
= bytecode_push(&ctx
->bytecode_reloc
, &reloc_offset
,
416 1, sizeof(reloc_offset
));
421 ret
= bytecode_push(&ctx
->bytecode_reloc
,
423 1, strlen(op
->u
.symbol
) + 1);
430 case IR_LOAD_EXPRESSION_GET_INDEX
:
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
;
438 insn
= calloc(insn_len
, 1);
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
);
451 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
453 struct load_op
*insn
;
454 uint32_t insn_len
= sizeof(struct load_op
);
457 insn
= calloc(insn_len
, 1);
460 insn
->op
= FILTER_OP_LOAD_FIELD
;
461 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
474 int visit_node_load(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
478 switch (node
->data_type
) {
479 case IR_DATA_UNKNOWN
:
481 fprintf(stderr
, "[error] Unknown data type in %s\n",
487 struct load_op
*insn
;
488 uint32_t insn_len
= sizeof(struct load_op
)
489 + strlen(node
->u
.load
.u
.string
.value
) + 1;
491 insn
= calloc(insn_len
, 1);
495 switch (node
->u
.load
.u
.string
.type
) {
496 case IR_LOAD_STRING_TYPE_GLOB_STAR
:
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.
503 insn
->op
= FILTER_OP_LOAD_STAR_GLOB_STRING
;
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
516 insn
->op
= FILTER_OP_LOAD_STRING
;
520 strcpy(insn
->data
, node
->u
.load
.u
.string
.value
);
521 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
525 case IR_DATA_NUMERIC
:
527 struct load_op
*insn
;
528 uint32_t insn_len
= sizeof(struct load_op
)
529 + sizeof(struct literal_numeric
);
531 insn
= calloc(insn_len
, 1);
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
);
542 struct load_op
*insn
;
543 uint32_t insn_len
= sizeof(struct load_op
)
544 + sizeof(struct literal_double
);
546 insn
= calloc(insn_len
, 1);
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
);
555 case IR_DATA_EXPRESSION
:
556 return visit_node_load_expression(ctx
, node
);
561 int visit_node_unary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
564 struct unary_op insn
;
567 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.unary
.child
);
571 /* Generate end of bytecode instruction */
572 switch (node
->u
.unary
.type
) {
573 case AST_UNARY_UNKNOWN
:
575 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
581 case AST_UNARY_MINUS
:
582 insn
.op
= FILTER_OP_UNARY_MINUS
;
583 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
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
));
594 * Binary comparator nesting is disallowed. This allows fitting into
598 int visit_node_binary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
601 struct binary_op insn
;
604 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
607 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
611 switch (node
->u
.binary
.type
) {
614 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
620 fprintf(stderr
, "[error] Unexpected logical node type in %s\n",
625 insn
.op
= FILTER_OP_MUL
;
628 insn
.op
= FILTER_OP_DIV
;
631 insn
.op
= FILTER_OP_MOD
;
634 insn
.op
= FILTER_OP_PLUS
;
637 insn
.op
= FILTER_OP_MINUS
;
639 case AST_OP_BIT_RSHIFT
:
640 insn
.op
= FILTER_OP_BIT_RSHIFT
;
642 case AST_OP_BIT_LSHIFT
:
643 insn
.op
= FILTER_OP_BIT_LSHIFT
;
646 insn
.op
= FILTER_OP_BIT_AND
;
649 insn
.op
= FILTER_OP_BIT_OR
;
652 insn
.op
= FILTER_OP_BIT_XOR
;
656 insn
.op
= FILTER_OP_EQ
;
659 insn
.op
= FILTER_OP_NE
;
662 insn
.op
= FILTER_OP_GT
;
665 insn
.op
= FILTER_OP_LT
;
668 insn
.op
= FILTER_OP_GE
;
671 insn
.op
= FILTER_OP_LE
;
674 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
678 * A logical op always return a s64 (1 or 0).
681 int visit_node_logical(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
684 struct logical_op insn
;
685 uint16_t skip_offset_loc
;
688 /* Visit left child */
689 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
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
;
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
;
704 cast_insn
.op
= FILTER_OP_CAST_DOUBLE_TO_S64
;
706 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
707 1, sizeof(cast_insn
));
711 switch (node
->u
.logical
.type
) {
713 fprintf(stderr
, "[error] Unknown node type in %s\n",
718 insn
.op
= FILTER_OP_AND
;
721 insn
.op
= FILTER_OP_OR
;
724 insn
.skip_offset
= (uint16_t) -1UL; /* Temporary */
725 ret
= bytecode_push_logical(&ctx
->bytecode
, &insn
, 1, sizeof(insn
),
729 /* Visit right child */
730 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
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
;
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
;
745 cast_insn
.op
= FILTER_OP_CAST_DOUBLE_TO_S64
;
747 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
748 1, sizeof(cast_insn
));
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 */
762 * Postorder traversal of the tree. We need the children result before
763 * we can evaluate the parent.
766 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
772 fprintf(stderr
, "[error] Unknown node type in %s\n",
777 return visit_node_root(ctx
, node
);
779 return visit_node_load(ctx
, node
);
781 return visit_node_unary(ctx
, node
);
783 return visit_node_binary(ctx
, node
);
785 return visit_node_logical(ctx
, node
);
790 void filter_bytecode_free(struct filter_parser_ctx
*ctx
)
798 ctx
->bytecode
= NULL
;
801 if (ctx
->bytecode_reloc
) {
802 free(ctx
->bytecode_reloc
);
803 ctx
->bytecode_reloc
= NULL
;
808 int filter_visitor_bytecode_generate(struct filter_parser_ctx
*ctx
)
812 ret
= bytecode_init(&ctx
->bytecode
);
815 ret
= bytecode_init(&ctx
->bytecode_reloc
);
818 ret
= recursive_visit_gen_bytecode(ctx
, ctx
->ir_root
);
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
));
828 filter_bytecode_free(ctx
);