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
;
322 ret
= visit_node_load_expression_legacy(ctx
, exp
, op
);
327 return 0; /* legacy */
330 for (; op
!= NULL
; op
= op
->next
) {
332 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
334 struct load_op
*insn
;
335 uint32_t insn_len
= sizeof(struct load_op
);
338 insn
= calloc(insn_len
, 1);
341 insn
->op
= FILTER_OP_GET_CONTEXT_ROOT
;
342 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
349 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
351 struct load_op
*insn
;
352 uint32_t insn_len
= sizeof(struct load_op
);
355 insn
= calloc(insn_len
, 1);
358 insn
->op
= FILTER_OP_GET_APP_CONTEXT_ROOT
;
359 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
366 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
368 struct load_op
*insn
;
369 uint32_t insn_len
= sizeof(struct load_op
);
372 insn
= calloc(insn_len
, 1);
375 insn
->op
= FILTER_OP_GET_PAYLOAD_ROOT
;
376 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
383 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
385 struct load_op
*insn
;
386 uint32_t insn_len
= sizeof(struct load_op
)
387 + sizeof(struct get_symbol
);
388 struct get_symbol symbol_offset
;
389 uint32_t reloc_offset_u32
;
390 uint16_t reloc_offset
;
391 uint32_t bytecode_reloc_offset_u32
;
394 insn
= calloc(insn_len
, 1);
397 insn
->op
= FILTER_OP_GET_SYMBOL
;
398 bytecode_reloc_offset_u32
=
399 bytecode_get_len(&ctx
->bytecode_reloc
->b
)
400 + sizeof(reloc_offset
);
401 symbol_offset
.offset
=
402 (uint16_t) bytecode_reloc_offset_u32
;
403 memcpy(insn
->data
, &symbol_offset
,
404 sizeof(symbol_offset
));
405 /* reloc_offset points to struct load_op */
406 reloc_offset_u32
= bytecode_get_len(&ctx
->bytecode
->b
);
407 if (reloc_offset_u32
> LTTNG_FILTER_MAX_LEN
- 1) {
411 reloc_offset
= (uint16_t) reloc_offset_u32
;
412 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
418 ret
= bytecode_push(&ctx
->bytecode_reloc
, &reloc_offset
,
419 1, sizeof(reloc_offset
));
424 ret
= bytecode_push(&ctx
->bytecode_reloc
,
426 1, strlen(op
->u
.symbol
) + 1);
433 case IR_LOAD_EXPRESSION_GET_INDEX
:
435 struct load_op
*insn
;
436 uint32_t insn_len
= sizeof(struct load_op
)
437 + sizeof(struct get_index_u64
);
438 struct get_index_u64 index
;
441 insn
= calloc(insn_len
, 1);
444 insn
->op
= FILTER_OP_GET_INDEX_U64
;
445 index
.index
= op
->u
.index
;
446 memcpy(insn
->data
, &index
, sizeof(index
));
447 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
454 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
456 struct load_op
*insn
;
457 uint32_t insn_len
= sizeof(struct load_op
);
460 insn
= calloc(insn_len
, 1);
463 insn
->op
= FILTER_OP_LOAD_FIELD
;
464 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
477 int visit_node_load(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
481 switch (node
->data_type
) {
482 case IR_DATA_UNKNOWN
:
484 fprintf(stderr
, "[error] Unknown data type in %s\n",
490 struct load_op
*insn
;
491 uint32_t insn_len
= sizeof(struct load_op
)
492 + strlen(node
->u
.load
.u
.string
.value
) + 1;
494 insn
= calloc(insn_len
, 1);
498 switch (node
->u
.load
.u
.string
.type
) {
499 case IR_LOAD_STRING_TYPE_GLOB_STAR
:
501 * We explicitly tell the interpreter here that
502 * this load is a full star globbing pattern so
503 * that the appropriate matching function can be
504 * called. Also, see comment below.
506 insn
->op
= FILTER_OP_LOAD_STAR_GLOB_STRING
;
510 * This is the "legacy" string, which includes
511 * star globbing patterns with a star only at
512 * the end. Both "plain" and "star at the end"
513 * literal strings are handled at the same place
514 * by the tracer's filter bytecode interpreter,
515 * whereas full star globbing patterns (stars
516 * can be anywhere in the string) is a special
519 insn
->op
= FILTER_OP_LOAD_STRING
;
523 strcpy(insn
->data
, node
->u
.load
.u
.string
.value
);
524 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
528 case IR_DATA_NUMERIC
:
530 struct load_op
*insn
;
531 uint32_t insn_len
= sizeof(struct load_op
)
532 + sizeof(struct literal_numeric
);
534 insn
= calloc(insn_len
, 1);
537 insn
->op
= FILTER_OP_LOAD_S64
;
538 memcpy(insn
->data
, &node
->u
.load
.u
.num
, sizeof(int64_t));
539 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
545 struct load_op
*insn
;
546 uint32_t insn_len
= sizeof(struct load_op
)
547 + sizeof(struct literal_double
);
549 insn
= calloc(insn_len
, 1);
552 insn
->op
= FILTER_OP_LOAD_DOUBLE
;
553 memcpy(insn
->data
, &node
->u
.load
.u
.flt
, sizeof(double));
554 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
558 case IR_DATA_EXPRESSION
:
559 return visit_node_load_expression(ctx
, node
);
564 int visit_node_unary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
567 struct unary_op insn
;
570 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.unary
.child
);
574 /* Generate end of bytecode instruction */
575 switch (node
->u
.unary
.type
) {
576 case AST_UNARY_UNKNOWN
:
578 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
584 case AST_UNARY_MINUS
:
585 insn
.op
= FILTER_OP_UNARY_MINUS
;
586 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
588 insn
.op
= FILTER_OP_UNARY_NOT
;
589 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
590 case AST_UNARY_BIT_NOT
:
591 insn
.op
= FILTER_OP_UNARY_BIT_NOT
;
592 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
597 * Binary comparator nesting is disallowed. This allows fitting into
601 int visit_node_binary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
604 struct binary_op insn
;
607 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
610 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
614 switch (node
->u
.binary
.type
) {
617 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
623 fprintf(stderr
, "[error] Unexpected logical node type in %s\n",
628 insn
.op
= FILTER_OP_MUL
;
631 insn
.op
= FILTER_OP_DIV
;
634 insn
.op
= FILTER_OP_MOD
;
637 insn
.op
= FILTER_OP_PLUS
;
640 insn
.op
= FILTER_OP_MINUS
;
642 case AST_OP_BIT_RSHIFT
:
643 insn
.op
= FILTER_OP_BIT_RSHIFT
;
645 case AST_OP_BIT_LSHIFT
:
646 insn
.op
= FILTER_OP_BIT_LSHIFT
;
649 insn
.op
= FILTER_OP_BIT_AND
;
652 insn
.op
= FILTER_OP_BIT_OR
;
655 insn
.op
= FILTER_OP_BIT_XOR
;
659 insn
.op
= FILTER_OP_EQ
;
662 insn
.op
= FILTER_OP_NE
;
665 insn
.op
= FILTER_OP_GT
;
668 insn
.op
= FILTER_OP_LT
;
671 insn
.op
= FILTER_OP_GE
;
674 insn
.op
= FILTER_OP_LE
;
677 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
681 * A logical op always return a s64 (1 or 0).
684 int visit_node_logical(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
687 struct logical_op insn
;
688 uint16_t skip_offset_loc
;
691 /* Visit left child */
692 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
695 /* Cast to s64 if float or field ref */
696 if ((node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
697 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
698 || node
->u
.binary
.left
->data_type
== IR_DATA_EXPRESSION
)
699 || node
->u
.binary
.left
->data_type
== IR_DATA_FLOAT
) {
700 struct cast_op cast_insn
;
702 if (node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
703 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
704 || node
->u
.binary
.left
->data_type
== IR_DATA_EXPRESSION
) {
705 cast_insn
.op
= FILTER_OP_CAST_TO_S64
;
707 cast_insn
.op
= FILTER_OP_CAST_DOUBLE_TO_S64
;
709 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
710 1, sizeof(cast_insn
));
714 switch (node
->u
.logical
.type
) {
716 fprintf(stderr
, "[error] Unknown node type in %s\n",
721 insn
.op
= FILTER_OP_AND
;
724 insn
.op
= FILTER_OP_OR
;
727 insn
.skip_offset
= (uint16_t) -1UL; /* Temporary */
728 ret
= bytecode_push_logical(&ctx
->bytecode
, &insn
, 1, sizeof(insn
),
732 /* Visit right child */
733 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
736 /* Cast to s64 if float or field ref */
737 if ((node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
738 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
739 || node
->u
.binary
.right
->data_type
== IR_DATA_EXPRESSION
)
740 || node
->u
.binary
.right
->data_type
== IR_DATA_FLOAT
) {
741 struct cast_op cast_insn
;
743 if (node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
744 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
745 || node
->u
.binary
.right
->data_type
== IR_DATA_EXPRESSION
) {
746 cast_insn
.op
= FILTER_OP_CAST_TO_S64
;
748 cast_insn
.op
= FILTER_OP_CAST_DOUBLE_TO_S64
;
750 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
751 1, sizeof(cast_insn
));
755 /* We now know where the logical op can skip. */
756 target_loc
= (uint16_t) bytecode_get_len(&ctx
->bytecode
->b
);
757 ret
= bytecode_patch(&ctx
->bytecode
,
758 &target_loc
, /* Offset to jump to */
759 skip_offset_loc
, /* Where to patch */
765 * Postorder traversal of the tree. We need the children result before
766 * we can evaluate the parent.
769 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
775 fprintf(stderr
, "[error] Unknown node type in %s\n",
780 return visit_node_root(ctx
, node
);
782 return visit_node_load(ctx
, node
);
784 return visit_node_unary(ctx
, node
);
786 return visit_node_binary(ctx
, node
);
788 return visit_node_logical(ctx
, node
);
793 void filter_bytecode_free(struct filter_parser_ctx
*ctx
)
801 ctx
->bytecode
= NULL
;
804 if (ctx
->bytecode_reloc
) {
805 free(ctx
->bytecode_reloc
);
806 ctx
->bytecode_reloc
= NULL
;
811 int filter_visitor_bytecode_generate(struct filter_parser_ctx
*ctx
)
815 ret
= bytecode_init(&ctx
->bytecode
);
818 ret
= bytecode_init(&ctx
->bytecode_reloc
);
821 ret
= recursive_visit_gen_bytecode(ctx
, ctx
->ir_root
);
825 /* Finally, append symbol table to bytecode */
826 ctx
->bytecode
->b
.reloc_table_offset
= bytecode_get_len(&ctx
->bytecode
->b
);
827 return bytecode_push(&ctx
->bytecode
, ctx
->bytecode_reloc
->b
.data
,
828 1, bytecode_get_len(&ctx
->bytecode_reloc
->b
));
831 filter_bytecode_free(ctx
);