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 const int ret
= bytecode_push_get_context_root(&ctx
->bytecode
);
251 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
253 const int ret
= bytecode_push_get_app_context_root(&ctx
->bytecode
);
261 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
263 const int ret
= bytecode_push_get_payload_root(&ctx
->bytecode
);
271 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
273 const int ret
= bytecode_push_get_symbol(
275 &ctx
->bytecode_reloc
,
284 case IR_LOAD_EXPRESSION_GET_INDEX
:
286 const int ret
= bytecode_push_get_index_u64(&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
);
300 insn
= calloc(insn_len
, 1);
303 insn
->op
= BYTECODE_OP_LOAD_FIELD
;
304 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
317 int visit_node_load(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
321 switch (node
->data_type
) {
322 case IR_DATA_UNKNOWN
:
324 fprintf(stderr
, "[error] Unknown data type in %s\n",
330 struct load_op
*insn
;
331 uint32_t insn_len
= sizeof(struct load_op
)
332 + strlen(node
->u
.load
.u
.string
.value
) + 1;
334 insn
= calloc(insn_len
, 1);
338 switch (node
->u
.load
.u
.string
.type
) {
339 case IR_LOAD_STRING_TYPE_GLOB_STAR
:
341 * We explicitly tell the interpreter here that
342 * this load is a full star globbing pattern so
343 * that the appropriate matching function can be
344 * called. Also, see comment below.
346 insn
->op
= BYTECODE_OP_LOAD_STAR_GLOB_STRING
;
350 * This is the "legacy" string, which includes
351 * star globbing patterns with a star only at
352 * the end. Both "plain" and "star at the end"
353 * literal strings are handled at the same place
354 * by the tracer's filter bytecode interpreter,
355 * whereas full star globbing patterns (stars
356 * can be anywhere in the string) is a special
359 insn
->op
= BYTECODE_OP_LOAD_STRING
;
363 strcpy(insn
->data
, node
->u
.load
.u
.string
.value
);
364 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
368 case IR_DATA_NUMERIC
:
370 struct load_op
*insn
;
371 uint32_t insn_len
= sizeof(struct load_op
)
372 + sizeof(struct literal_numeric
);
374 insn
= calloc(insn_len
, 1);
377 insn
->op
= BYTECODE_OP_LOAD_S64
;
378 memcpy(insn
->data
, &node
->u
.load
.u
.num
, sizeof(int64_t));
379 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
385 struct load_op
*insn
;
386 uint32_t insn_len
= sizeof(struct load_op
)
387 + sizeof(struct literal_double
);
389 insn
= calloc(insn_len
, 1);
392 insn
->op
= BYTECODE_OP_LOAD_DOUBLE
;
393 memcpy(insn
->data
, &node
->u
.load
.u
.flt
, sizeof(double));
394 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
398 case IR_DATA_EXPRESSION
:
399 return visit_node_load_expression(ctx
, node
);
404 int visit_node_unary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
407 struct unary_op insn
;
410 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.unary
.child
);
414 /* Generate end of bytecode instruction */
415 switch (node
->u
.unary
.type
) {
416 case AST_UNARY_UNKNOWN
:
418 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
424 case AST_UNARY_MINUS
:
425 insn
.op
= BYTECODE_OP_UNARY_MINUS
;
426 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
428 insn
.op
= BYTECODE_OP_UNARY_NOT
;
429 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
430 case AST_UNARY_BIT_NOT
:
431 insn
.op
= BYTECODE_OP_UNARY_BIT_NOT
;
432 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
437 * Binary comparator nesting is disallowed. This allows fitting into
441 int visit_node_binary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
444 struct binary_op insn
;
447 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
450 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
454 switch (node
->u
.binary
.type
) {
457 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
463 fprintf(stderr
, "[error] Unexpected logical node type in %s\n",
468 insn
.op
= BYTECODE_OP_MUL
;
471 insn
.op
= BYTECODE_OP_DIV
;
474 insn
.op
= BYTECODE_OP_MOD
;
477 insn
.op
= BYTECODE_OP_PLUS
;
480 insn
.op
= BYTECODE_OP_MINUS
;
482 case AST_OP_BIT_RSHIFT
:
483 insn
.op
= BYTECODE_OP_BIT_RSHIFT
;
485 case AST_OP_BIT_LSHIFT
:
486 insn
.op
= BYTECODE_OP_BIT_LSHIFT
;
489 insn
.op
= BYTECODE_OP_BIT_AND
;
492 insn
.op
= BYTECODE_OP_BIT_OR
;
495 insn
.op
= BYTECODE_OP_BIT_XOR
;
499 insn
.op
= BYTECODE_OP_EQ
;
502 insn
.op
= BYTECODE_OP_NE
;
505 insn
.op
= BYTECODE_OP_GT
;
508 insn
.op
= BYTECODE_OP_LT
;
511 insn
.op
= BYTECODE_OP_GE
;
514 insn
.op
= BYTECODE_OP_LE
;
517 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
521 * A logical op always return a s64 (1 or 0).
524 int visit_node_logical(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
527 struct logical_op insn
;
528 uint16_t skip_offset_loc
;
531 /* Visit left child */
532 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
535 /* Cast to s64 if float or field ref */
536 if ((node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
537 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
538 || node
->u
.binary
.left
->data_type
== IR_DATA_EXPRESSION
)
539 || node
->u
.binary
.left
->data_type
== IR_DATA_FLOAT
) {
540 struct cast_op cast_insn
;
542 if (node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
543 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
544 || node
->u
.binary
.left
->data_type
== IR_DATA_EXPRESSION
) {
545 cast_insn
.op
= BYTECODE_OP_CAST_TO_S64
;
547 cast_insn
.op
= BYTECODE_OP_CAST_DOUBLE_TO_S64
;
549 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
550 1, sizeof(cast_insn
));
554 switch (node
->u
.logical
.type
) {
556 fprintf(stderr
, "[error] Unknown node type in %s\n",
561 insn
.op
= BYTECODE_OP_AND
;
564 insn
.op
= BYTECODE_OP_OR
;
567 insn
.skip_offset
= (uint16_t) -1UL; /* Temporary */
568 ret
= bytecode_push_logical(&ctx
->bytecode
, &insn
, 1, sizeof(insn
),
572 /* Visit right child */
573 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
576 /* Cast to s64 if float or field ref */
577 if ((node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
578 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
579 || node
->u
.binary
.right
->data_type
== IR_DATA_EXPRESSION
)
580 || node
->u
.binary
.right
->data_type
== IR_DATA_FLOAT
) {
581 struct cast_op cast_insn
;
583 if (node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
584 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
585 || node
->u
.binary
.right
->data_type
== IR_DATA_EXPRESSION
) {
586 cast_insn
.op
= BYTECODE_OP_CAST_TO_S64
;
588 cast_insn
.op
= BYTECODE_OP_CAST_DOUBLE_TO_S64
;
590 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
591 1, sizeof(cast_insn
));
595 /* We now know where the logical op can skip. */
596 target_loc
= (uint16_t) bytecode_get_len(&ctx
->bytecode
->b
);
597 ret
= bytecode_patch(&ctx
->bytecode
,
598 &target_loc
, /* Offset to jump to */
599 skip_offset_loc
, /* Where to patch */
605 * Postorder traversal of the tree. We need the children result before
606 * we can evaluate the parent.
609 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
615 fprintf(stderr
, "[error] Unknown node type in %s\n",
620 return visit_node_root(ctx
, node
);
622 return visit_node_load(ctx
, node
);
624 return visit_node_unary(ctx
, node
);
626 return visit_node_binary(ctx
, node
);
628 return visit_node_logical(ctx
, node
);
633 void filter_bytecode_free(struct filter_parser_ctx
*ctx
)
641 ctx
->bytecode
= NULL
;
644 if (ctx
->bytecode_reloc
) {
645 free(ctx
->bytecode_reloc
);
646 ctx
->bytecode_reloc
= NULL
;
651 int filter_visitor_bytecode_generate(struct filter_parser_ctx
*ctx
)
655 ret
= bytecode_init(&ctx
->bytecode
);
658 ret
= bytecode_init(&ctx
->bytecode_reloc
);
661 ret
= recursive_visit_gen_bytecode(ctx
, ctx
->ir_root
);
665 /* Finally, append symbol table to bytecode */
666 ctx
->bytecode
->b
.reloc_table_offset
= bytecode_get_len(&ctx
->bytecode
->b
);
667 return bytecode_push(&ctx
->bytecode
, ctx
->bytecode_reloc
->b
.data
,
668 1, bytecode_get_len(&ctx
->bytecode_reloc
->b
));
671 filter_bytecode_free(ctx
);