2 * filter-visitor-generate-bytecode.c
4 * LTTng filter bytecode generation
6 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License, version 2.1 only,
10 * as published by the Free Software Foundation.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <common/align.h>
26 #include <common/compat/string.h>
28 #include "filter-bytecode.h"
29 #include "filter-ir.h"
30 #include "filter-ast.h"
32 #include <common/macros.h>
35 #define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b)))
38 #define INIT_ALLOC_SIZE 4
41 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
44 static inline int get_count_order(unsigned int count
)
48 order
= lttng_fls(count
) - 1;
49 if (count
& (count
- 1))
55 int bytecode_init(struct lttng_filter_bytecode_alloc
**fb
)
59 alloc_len
= sizeof(struct lttng_filter_bytecode_alloc
) + INIT_ALLOC_SIZE
;
60 *fb
= calloc(alloc_len
, 1);
64 (*fb
)->alloc_len
= alloc_len
;
70 int32_t bytecode_reserve(struct lttng_filter_bytecode_alloc
**fb
, uint32_t align
, uint32_t len
)
73 uint32_t padding
= offset_align((*fb
)->b
.len
, align
);
74 uint32_t new_len
= (*fb
)->b
.len
+ padding
+ len
;
75 uint32_t new_alloc_len
= sizeof(struct lttng_filter_bytecode_alloc
) + new_len
;
76 uint32_t old_alloc_len
= (*fb
)->alloc_len
;
78 if (new_len
> LTTNG_FILTER_MAX_LEN
)
81 if (new_alloc_len
> old_alloc_len
) {
82 struct lttng_filter_bytecode_alloc
*newptr
;
85 max_t(uint32_t, 1U << get_count_order(new_alloc_len
), old_alloc_len
<< 1);
86 newptr
= realloc(*fb
, new_alloc_len
);
90 /* We zero directly the memory from start of allocation. */
91 memset(&((char *) *fb
)[old_alloc_len
], 0, new_alloc_len
- old_alloc_len
);
92 (*fb
)->alloc_len
= new_alloc_len
;
94 (*fb
)->b
.len
+= padding
;
101 int bytecode_push(struct lttng_filter_bytecode_alloc
**fb
, const void *data
,
102 uint32_t align
, uint32_t len
)
106 offset
= bytecode_reserve(fb
, align
, len
);
109 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
114 int bytecode_push_logical(struct lttng_filter_bytecode_alloc
**fb
,
115 struct logical_op
*data
,
116 uint32_t align
, uint32_t len
,
117 uint16_t *skip_offset
)
121 offset
= bytecode_reserve(fb
, align
, len
);
124 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
126 (void *) &((struct logical_op
*) &(*fb
)->b
.data
[offset
])->skip_offset
127 - (void *) &(*fb
)->b
.data
[0];
132 int bytecode_patch(struct lttng_filter_bytecode_alloc
**fb
,
137 if (offset
>= (*fb
)->b
.len
) {
140 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
145 int visit_node_root(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
148 struct return_op insn
;
151 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.root
.child
);
155 /* Generate end of bytecode instruction */
156 insn
.op
= FILTER_OP_RETURN
;
157 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
161 int append_str(char **s
, const char *append
)
165 size_t oldlen
= (old
== NULL
) ? 0 : strlen(old
);
166 size_t appendlen
= strlen(append
);
168 new = calloc(oldlen
+ appendlen
+ 1, 1);
187 int load_expression_legacy_match(const struct ir_load_expression
*exp
,
188 enum filter_op
*op_type
,
191 const struct ir_load_expression_op
*op
;
192 bool need_dot
= false;
196 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
197 *op_type
= FILTER_OP_GET_CONTEXT_REF
;
198 if (append_str(symbol
, "$ctx.")) {
203 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
204 *op_type
= FILTER_OP_GET_CONTEXT_REF
;
205 if (append_str(symbol
, "$app.")) {
210 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
211 *op_type
= FILTER_OP_LOAD_FIELD_REF
;
215 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
216 case IR_LOAD_EXPRESSION_GET_INDEX
:
217 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
219 return 0; /* no match */
225 return 0; /* no match */
228 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
230 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
231 if (need_dot
&& append_str(symbol
, ".")) {
234 if (append_str(symbol
, op
->u
.symbol
)) {
239 return 0; /* no match */
244 return 1; /* Legacy match */
253 int visit_node_load_expression_legacy(struct filter_parser_ctx
*ctx
,
254 const struct ir_load_expression
*exp
,
255 const struct ir_load_expression_op
*op
)
257 struct load_op
*insn
= NULL
;
258 uint32_t insn_len
= sizeof(struct load_op
)
259 + sizeof(struct field_ref
);
260 struct field_ref ref_offset
;
261 uint32_t reloc_offset_u32
;
262 uint16_t reloc_offset
;
263 enum filter_op op_type
;
267 ret
= load_expression_legacy_match(exp
, &op_type
, &symbol
);
271 insn
= calloc(insn_len
, 1);
277 ref_offset
.offset
= (uint16_t) -1U;
278 memcpy(insn
->data
, &ref_offset
, sizeof(ref_offset
));
279 /* reloc_offset points to struct load_op */
280 reloc_offset_u32
= bytecode_get_len(&ctx
->bytecode
->b
);
281 if (reloc_offset_u32
> LTTNG_FILTER_MAX_LEN
- 1) {
285 reloc_offset
= (uint16_t) reloc_offset_u32
;
286 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
291 ret
= bytecode_push(&ctx
->bytecode_reloc
, &reloc_offset
,
292 1, sizeof(reloc_offset
));
296 ret
= bytecode_push(&ctx
->bytecode_reloc
, symbol
,
297 1, strlen(symbol
) + 1);
298 ret
= 1; /* legacy */
306 int visit_node_load_expression(struct filter_parser_ctx
*ctx
,
307 const struct ir_op
*node
)
309 struct ir_load_expression
*exp
;
310 struct ir_load_expression_op
*op
;
313 exp
= node
->u
.load
.u
.expression
;
323 * TODO: if we remove legacy load for application contexts, we
324 * need to update session bytecode parser as well.
326 ret
= visit_node_load_expression_legacy(ctx
, exp
, op
);
331 return 0; /* legacy */
334 for (; op
!= NULL
; op
= op
->next
) {
336 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
338 struct load_op
*insn
;
339 uint32_t insn_len
= sizeof(struct load_op
);
342 insn
= calloc(insn_len
, 1);
345 insn
->op
= FILTER_OP_GET_CONTEXT_ROOT
;
346 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
353 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
355 struct load_op
*insn
;
356 uint32_t insn_len
= sizeof(struct load_op
);
359 insn
= calloc(insn_len
, 1);
362 insn
->op
= FILTER_OP_GET_APP_CONTEXT_ROOT
;
363 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
370 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
372 struct load_op
*insn
;
373 uint32_t insn_len
= sizeof(struct load_op
);
376 insn
= calloc(insn_len
, 1);
379 insn
->op
= FILTER_OP_GET_PAYLOAD_ROOT
;
380 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
387 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
389 struct load_op
*insn
;
390 uint32_t insn_len
= sizeof(struct load_op
)
391 + sizeof(struct get_symbol
);
392 struct get_symbol symbol_offset
;
393 uint32_t reloc_offset_u32
;
394 uint16_t reloc_offset
;
395 uint32_t bytecode_reloc_offset_u32
;
398 insn
= calloc(insn_len
, 1);
401 insn
->op
= FILTER_OP_GET_SYMBOL
;
402 bytecode_reloc_offset_u32
=
403 bytecode_get_len(&ctx
->bytecode_reloc
->b
)
404 + sizeof(reloc_offset
);
405 symbol_offset
.offset
=
406 (uint16_t) bytecode_reloc_offset_u32
;
407 memcpy(insn
->data
, &symbol_offset
,
408 sizeof(symbol_offset
));
409 /* reloc_offset points to struct load_op */
410 reloc_offset_u32
= bytecode_get_len(&ctx
->bytecode
->b
);
411 if (reloc_offset_u32
> LTTNG_FILTER_MAX_LEN
- 1) {
415 reloc_offset
= (uint16_t) reloc_offset_u32
;
416 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
422 ret
= bytecode_push(&ctx
->bytecode_reloc
, &reloc_offset
,
423 1, sizeof(reloc_offset
));
428 ret
= bytecode_push(&ctx
->bytecode_reloc
,
430 1, strlen(op
->u
.symbol
) + 1);
437 case IR_LOAD_EXPRESSION_GET_INDEX
:
439 struct load_op
*insn
;
440 uint32_t insn_len
= sizeof(struct load_op
)
441 + sizeof(struct get_index_u64
);
442 struct get_index_u64 index
;
445 insn
= calloc(insn_len
, 1);
448 insn
->op
= FILTER_OP_GET_INDEX_U64
;
449 index
.index
= op
->u
.index
;
450 memcpy(insn
->data
, &index
, sizeof(index
));
451 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
458 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
460 struct load_op
*insn
;
461 uint32_t insn_len
= sizeof(struct load_op
);
464 insn
= calloc(insn_len
, 1);
467 insn
->op
= FILTER_OP_LOAD_FIELD
;
468 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
481 int visit_node_load(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
485 switch (node
->data_type
) {
486 case IR_DATA_UNKNOWN
:
488 fprintf(stderr
, "[error] Unknown data type in %s\n",
494 struct load_op
*insn
;
495 uint32_t insn_len
= sizeof(struct load_op
)
496 + strlen(node
->u
.load
.u
.string
.value
) + 1;
498 insn
= calloc(insn_len
, 1);
502 switch (node
->u
.load
.u
.string
.type
) {
503 case IR_LOAD_STRING_TYPE_GLOB_STAR
:
505 * We explicitly tell the interpreter here that
506 * this load is a full star globbing pattern so
507 * that the appropriate matching function can be
508 * called. Also, see comment below.
510 insn
->op
= FILTER_OP_LOAD_STAR_GLOB_STRING
;
514 * This is the "legacy" string, which includes
515 * star globbing patterns with a star only at
516 * the end. Both "plain" and "star at the end"
517 * literal strings are handled at the same place
518 * by the tracer's filter bytecode interpreter,
519 * whereas full star globbing patterns (stars
520 * can be anywhere in the string) is a special
523 insn
->op
= FILTER_OP_LOAD_STRING
;
527 strcpy(insn
->data
, node
->u
.load
.u
.string
.value
);
528 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
532 case IR_DATA_NUMERIC
:
534 struct load_op
*insn
;
535 uint32_t insn_len
= sizeof(struct load_op
)
536 + sizeof(struct literal_numeric
);
538 insn
= calloc(insn_len
, 1);
541 insn
->op
= FILTER_OP_LOAD_S64
;
542 memcpy(insn
->data
, &node
->u
.load
.u
.num
, sizeof(int64_t));
543 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
549 struct load_op
*insn
;
550 uint32_t insn_len
= sizeof(struct load_op
)
551 + sizeof(struct literal_double
);
553 insn
= calloc(insn_len
, 1);
556 insn
->op
= FILTER_OP_LOAD_DOUBLE
;
557 memcpy(insn
->data
, &node
->u
.load
.u
.flt
, sizeof(double));
558 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
562 case IR_DATA_EXPRESSION
:
563 return visit_node_load_expression(ctx
, node
);
568 int visit_node_unary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
571 struct unary_op insn
;
574 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.unary
.child
);
578 /* Generate end of bytecode instruction */
579 switch (node
->u
.unary
.type
) {
580 case AST_UNARY_UNKNOWN
:
582 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
588 case AST_UNARY_MINUS
:
589 insn
.op
= FILTER_OP_UNARY_MINUS
;
590 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
592 insn
.op
= FILTER_OP_UNARY_NOT
;
593 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
594 case AST_UNARY_BIT_NOT
:
595 insn
.op
= FILTER_OP_UNARY_BIT_NOT
;
596 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
601 * Binary comparator nesting is disallowed. This allows fitting into
605 int visit_node_binary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
608 struct binary_op insn
;
611 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
614 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
618 switch (node
->u
.binary
.type
) {
621 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
627 fprintf(stderr
, "[error] Unexpected logical node type in %s\n",
632 insn
.op
= FILTER_OP_MUL
;
635 insn
.op
= FILTER_OP_DIV
;
638 insn
.op
= FILTER_OP_MOD
;
641 insn
.op
= FILTER_OP_PLUS
;
644 insn
.op
= FILTER_OP_MINUS
;
646 case AST_OP_BIT_RSHIFT
:
647 insn
.op
= FILTER_OP_BIT_RSHIFT
;
649 case AST_OP_BIT_LSHIFT
:
650 insn
.op
= FILTER_OP_BIT_LSHIFT
;
653 insn
.op
= FILTER_OP_BIT_AND
;
656 insn
.op
= FILTER_OP_BIT_OR
;
659 insn
.op
= FILTER_OP_BIT_XOR
;
663 insn
.op
= FILTER_OP_EQ
;
666 insn
.op
= FILTER_OP_NE
;
669 insn
.op
= FILTER_OP_GT
;
672 insn
.op
= FILTER_OP_LT
;
675 insn
.op
= FILTER_OP_GE
;
678 insn
.op
= FILTER_OP_LE
;
681 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
685 * A logical op always return a s64 (1 or 0).
688 int visit_node_logical(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
691 struct logical_op insn
;
692 uint16_t skip_offset_loc
;
695 /* Visit left child */
696 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
699 /* Cast to s64 if float or field ref */
700 if ((node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
701 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
702 || node
->u
.binary
.left
->data_type
== IR_DATA_EXPRESSION
)
703 || node
->u
.binary
.left
->data_type
== IR_DATA_FLOAT
) {
704 struct cast_op cast_insn
;
706 if (node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
707 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
708 || node
->u
.binary
.left
->data_type
== IR_DATA_EXPRESSION
) {
709 cast_insn
.op
= FILTER_OP_CAST_TO_S64
;
711 cast_insn
.op
= FILTER_OP_CAST_DOUBLE_TO_S64
;
713 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
714 1, sizeof(cast_insn
));
718 switch (node
->u
.logical
.type
) {
720 fprintf(stderr
, "[error] Unknown node type in %s\n",
725 insn
.op
= FILTER_OP_AND
;
728 insn
.op
= FILTER_OP_OR
;
731 insn
.skip_offset
= (uint16_t) -1UL; /* Temporary */
732 ret
= bytecode_push_logical(&ctx
->bytecode
, &insn
, 1, sizeof(insn
),
736 /* Visit right child */
737 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
740 /* Cast to s64 if float or field ref */
741 if ((node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
742 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
743 || node
->u
.binary
.right
->data_type
== IR_DATA_EXPRESSION
)
744 || node
->u
.binary
.right
->data_type
== IR_DATA_FLOAT
) {
745 struct cast_op cast_insn
;
747 if (node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
748 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
749 || node
->u
.binary
.right
->data_type
== IR_DATA_EXPRESSION
) {
750 cast_insn
.op
= FILTER_OP_CAST_TO_S64
;
752 cast_insn
.op
= FILTER_OP_CAST_DOUBLE_TO_S64
;
754 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
755 1, sizeof(cast_insn
));
759 /* We now know where the logical op can skip. */
760 target_loc
= (uint16_t) bytecode_get_len(&ctx
->bytecode
->b
);
761 ret
= bytecode_patch(&ctx
->bytecode
,
762 &target_loc
, /* Offset to jump to */
763 skip_offset_loc
, /* Where to patch */
769 * Postorder traversal of the tree. We need the children result before
770 * we can evaluate the parent.
773 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
779 fprintf(stderr
, "[error] Unknown node type in %s\n",
784 return visit_node_root(ctx
, node
);
786 return visit_node_load(ctx
, node
);
788 return visit_node_unary(ctx
, node
);
790 return visit_node_binary(ctx
, node
);
792 return visit_node_logical(ctx
, node
);
797 void filter_bytecode_free(struct filter_parser_ctx
*ctx
)
805 ctx
->bytecode
= NULL
;
808 if (ctx
->bytecode_reloc
) {
809 free(ctx
->bytecode_reloc
);
810 ctx
->bytecode_reloc
= NULL
;
815 int filter_visitor_bytecode_generate(struct filter_parser_ctx
*ctx
)
819 ret
= bytecode_init(&ctx
->bytecode
);
822 ret
= bytecode_init(&ctx
->bytecode_reloc
);
825 ret
= recursive_visit_gen_bytecode(ctx
, ctx
->ir_root
);
829 /* Finally, append symbol table to bytecode */
830 ctx
->bytecode
->b
.reloc_table_offset
= bytecode_get_len(&ctx
->bytecode
->b
);
831 return bytecode_push(&ctx
->bytecode
, ctx
->bytecode_reloc
->b
.data
,
832 1, bytecode_get_len(&ctx
->bytecode_reloc
->b
));
835 filter_bytecode_free(ctx
);