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.hpp>
15 #include <common/compat/errno.hpp>
16 #include <common/compat/string.hpp>
18 #include "common/align.hpp"
19 #include "common/bytecode/bytecode.hpp"
20 #include "common/compat/string.hpp"
21 #include "common/macros.hpp"
22 #include "common/string-utils/string-utils.hpp"
23 #include "filter-ast.hpp"
24 #include "filter-ir.hpp"
27 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
31 int bytecode_patch(struct lttng_bytecode_alloc
**fb
,
36 if (offset
>= (*fb
)->b
.len
) {
39 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
44 int visit_node_root(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
47 struct return_op insn
;
50 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.root
.child
);
54 /* Generate end of bytecode instruction */
55 insn
.op
= BYTECODE_OP_RETURN
;
56 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
65 int load_expression_legacy_match(const struct ir_load_expression
*exp
,
66 enum bytecode_op
*op_type
,
69 const struct ir_load_expression_op
*op
;
70 bool need_dot
= false;
74 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
75 *op_type
= BYTECODE_OP_GET_CONTEXT_REF
;
76 if (strutils_append_str(symbol
, "$ctx.")) {
81 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
82 *op_type
= BYTECODE_OP_GET_CONTEXT_REF
;
83 if (strutils_append_str(symbol
, "$app.")) {
88 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
89 *op_type
= BYTECODE_OP_LOAD_FIELD_REF
;
93 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
94 case IR_LOAD_EXPRESSION_GET_INDEX
:
95 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
97 return 0; /* no match */
103 return 0; /* no match */
106 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
108 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
109 if (need_dot
&& strutils_append_str(symbol
, ".")) {
112 if (strutils_append_str(symbol
, op
->u
.symbol
)) {
117 return 0; /* no match */
122 return 1; /* Legacy match */
131 int visit_node_load_expression_legacy(struct filter_parser_ctx
*ctx
,
132 const struct ir_load_expression
*exp
,
133 const struct ir_load_expression_op
*op
)
135 struct load_op
*insn
= NULL
;
136 uint32_t insn_len
= sizeof(struct load_op
)
137 + sizeof(struct field_ref
);
138 struct field_ref ref_offset
;
139 uint32_t reloc_offset_u32
;
140 uint16_t reloc_offset
;
141 enum bytecode_op op_type
;
145 ret
= load_expression_legacy_match(exp
, &op_type
, &symbol
);
149 insn
= (load_op
*) calloc(insn_len
, 1);
155 ref_offset
.offset
= (uint16_t) -1U;
156 memcpy(insn
->data
, &ref_offset
, sizeof(ref_offset
));
157 /* reloc_offset points to struct load_op */
158 reloc_offset_u32
= bytecode_get_len(&ctx
->bytecode
->b
);
159 if (reloc_offset_u32
> LTTNG_FILTER_MAX_LEN
- 1) {
163 reloc_offset
= (uint16_t) reloc_offset_u32
;
164 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
169 ret
= bytecode_push(&ctx
->bytecode_reloc
, &reloc_offset
,
170 1, sizeof(reloc_offset
));
174 ret
= bytecode_push(&ctx
->bytecode_reloc
, symbol
,
175 1, strlen(symbol
) + 1);
179 ret
= 1; /* legacy */
187 int visit_node_load_expression(struct filter_parser_ctx
*ctx
,
188 const struct ir_op
*node
)
190 struct ir_load_expression
*exp
;
191 struct ir_load_expression_op
*op
;
194 exp
= node
->u
.load
.u
.expression
;
204 * TODO: if we remove legacy load for application contexts, we
205 * need to update session bytecode parser as well.
207 ret
= visit_node_load_expression_legacy(ctx
, exp
, op
);
212 return 0; /* legacy */
215 for (; op
!= NULL
; op
= op
->next
) {
217 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
219 ret
= bytecode_push_get_context_root(&ctx
->bytecode
);
227 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
229 ret
= bytecode_push_get_app_context_root(
238 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
240 ret
= bytecode_push_get_payload_root(&ctx
->bytecode
);
248 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
250 ret
= bytecode_push_get_symbol(&ctx
->bytecode
,
251 &ctx
->bytecode_reloc
, op
->u
.symbol
);
259 case IR_LOAD_EXPRESSION_GET_INDEX
:
261 ret
= bytecode_push_get_index_u64(
262 &ctx
->bytecode
, op
->u
.index
);
270 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
272 struct load_op
*insn
;
273 uint32_t insn_len
= sizeof(struct load_op
);
275 insn
= (load_op
*) calloc(insn_len
, 1);
278 insn
->op
= BYTECODE_OP_LOAD_FIELD
;
279 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
292 int visit_node_load(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
296 switch (node
->data_type
) {
297 case IR_DATA_UNKNOWN
:
299 fprintf(stderr
, "[error] Unknown data type in %s\n",
305 struct load_op
*insn
;
306 uint32_t insn_len
= sizeof(struct load_op
)
307 + strlen(node
->u
.load
.u
.string
.value
) + 1;
309 insn
= (load_op
*) calloc(insn_len
, 1);
313 switch (node
->u
.load
.u
.string
.type
) {
314 case IR_LOAD_STRING_TYPE_GLOB_STAR
:
316 * We explicitly tell the interpreter here that
317 * this load is a full star globbing pattern so
318 * that the appropriate matching function can be
319 * called. Also, see comment below.
321 insn
->op
= BYTECODE_OP_LOAD_STAR_GLOB_STRING
;
325 * This is the "legacy" string, which includes
326 * star globbing patterns with a star only at
327 * the end. Both "plain" and "star at the end"
328 * literal strings are handled at the same place
329 * by the tracer's filter bytecode interpreter,
330 * whereas full star globbing patterns (stars
331 * can be anywhere in the string) is a special
334 insn
->op
= BYTECODE_OP_LOAD_STRING
;
338 strcpy(insn
->data
, node
->u
.load
.u
.string
.value
);
339 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
343 case IR_DATA_NUMERIC
:
345 struct load_op
*insn
;
346 uint32_t insn_len
= sizeof(struct load_op
)
347 + sizeof(struct literal_numeric
);
349 insn
= (load_op
*) calloc(insn_len
, 1);
352 insn
->op
= BYTECODE_OP_LOAD_S64
;
353 memcpy(insn
->data
, &node
->u
.load
.u
.num
, sizeof(int64_t));
354 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
360 struct load_op
*insn
;
361 uint32_t insn_len
= sizeof(struct load_op
)
362 + sizeof(struct literal_double
);
364 insn
= (load_op
*) calloc(insn_len
, 1);
367 insn
->op
= BYTECODE_OP_LOAD_DOUBLE
;
368 memcpy(insn
->data
, &node
->u
.load
.u
.flt
, sizeof(double));
369 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
373 case IR_DATA_EXPRESSION
:
374 return visit_node_load_expression(ctx
, node
);
379 int visit_node_unary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
382 struct unary_op insn
;
385 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.unary
.child
);
389 /* Generate end of bytecode instruction */
390 switch (node
->u
.unary
.type
) {
391 case AST_UNARY_UNKNOWN
:
393 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
399 case AST_UNARY_MINUS
:
400 insn
.op
= BYTECODE_OP_UNARY_MINUS
;
401 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
403 insn
.op
= BYTECODE_OP_UNARY_NOT
;
404 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
405 case AST_UNARY_BIT_NOT
:
406 insn
.op
= BYTECODE_OP_UNARY_BIT_NOT
;
407 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
412 * Binary comparator nesting is disallowed. This allows fitting into
416 int visit_node_binary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
419 struct binary_op insn
;
422 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
425 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
429 switch (node
->u
.binary
.type
) {
432 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
438 fprintf(stderr
, "[error] Unexpected logical node type in %s\n",
443 insn
.op
= BYTECODE_OP_MUL
;
446 insn
.op
= BYTECODE_OP_DIV
;
449 insn
.op
= BYTECODE_OP_MOD
;
452 insn
.op
= BYTECODE_OP_PLUS
;
455 insn
.op
= BYTECODE_OP_MINUS
;
457 case AST_OP_BIT_RSHIFT
:
458 insn
.op
= BYTECODE_OP_BIT_RSHIFT
;
460 case AST_OP_BIT_LSHIFT
:
461 insn
.op
= BYTECODE_OP_BIT_LSHIFT
;
464 insn
.op
= BYTECODE_OP_BIT_AND
;
467 insn
.op
= BYTECODE_OP_BIT_OR
;
470 insn
.op
= BYTECODE_OP_BIT_XOR
;
474 insn
.op
= BYTECODE_OP_EQ
;
477 insn
.op
= BYTECODE_OP_NE
;
480 insn
.op
= BYTECODE_OP_GT
;
483 insn
.op
= BYTECODE_OP_LT
;
486 insn
.op
= BYTECODE_OP_GE
;
489 insn
.op
= BYTECODE_OP_LE
;
492 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
496 * A logical op always return a s64 (1 or 0).
499 int visit_node_logical(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
502 struct logical_op insn
;
503 uint16_t skip_offset_loc
;
506 /* Visit left child */
507 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
510 /* Cast to s64 if float or field ref */
511 if ((node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
512 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
513 || node
->u
.binary
.left
->data_type
== IR_DATA_EXPRESSION
)
514 || node
->u
.binary
.left
->data_type
== IR_DATA_FLOAT
) {
515 struct cast_op cast_insn
;
517 if (node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
518 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
519 || node
->u
.binary
.left
->data_type
== IR_DATA_EXPRESSION
) {
520 cast_insn
.op
= BYTECODE_OP_CAST_TO_S64
;
522 cast_insn
.op
= BYTECODE_OP_CAST_DOUBLE_TO_S64
;
524 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
525 1, sizeof(cast_insn
));
529 switch (node
->u
.logical
.type
) {
531 fprintf(stderr
, "[error] Unknown node type in %s\n",
536 insn
.op
= BYTECODE_OP_AND
;
539 insn
.op
= BYTECODE_OP_OR
;
542 insn
.skip_offset
= (uint16_t) -1UL; /* Temporary */
543 ret
= bytecode_push_logical(&ctx
->bytecode
, &insn
, 1, sizeof(insn
),
547 /* Visit right child */
548 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
551 /* Cast to s64 if float or field ref */
552 if ((node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
553 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
554 || node
->u
.binary
.right
->data_type
== IR_DATA_EXPRESSION
)
555 || node
->u
.binary
.right
->data_type
== IR_DATA_FLOAT
) {
556 struct cast_op cast_insn
;
558 if (node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
559 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
560 || node
->u
.binary
.right
->data_type
== IR_DATA_EXPRESSION
) {
561 cast_insn
.op
= BYTECODE_OP_CAST_TO_S64
;
563 cast_insn
.op
= BYTECODE_OP_CAST_DOUBLE_TO_S64
;
565 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
566 1, sizeof(cast_insn
));
570 /* We now know where the logical op can skip. */
571 target_loc
= (uint16_t) bytecode_get_len(&ctx
->bytecode
->b
);
572 ret
= bytecode_patch(&ctx
->bytecode
,
573 &target_loc
, /* Offset to jump to */
574 skip_offset_loc
, /* Where to patch */
580 * Postorder traversal of the tree. We need the children result before
581 * we can evaluate the parent.
584 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
590 fprintf(stderr
, "[error] Unknown node type in %s\n",
595 return visit_node_root(ctx
, node
);
597 return visit_node_load(ctx
, node
);
599 return visit_node_unary(ctx
, node
);
601 return visit_node_binary(ctx
, node
);
603 return visit_node_logical(ctx
, node
);
607 void filter_bytecode_free(struct filter_parser_ctx
*ctx
)
615 ctx
->bytecode
= NULL
;
618 if (ctx
->bytecode_reloc
) {
619 free(ctx
->bytecode_reloc
);
620 ctx
->bytecode_reloc
= NULL
;
624 int filter_visitor_bytecode_generate(struct filter_parser_ctx
*ctx
)
628 ret
= bytecode_init(&ctx
->bytecode
);
631 ret
= bytecode_init(&ctx
->bytecode_reloc
);
634 ret
= recursive_visit_gen_bytecode(ctx
, ctx
->ir_root
);
638 /* Finally, append symbol table to bytecode */
639 ctx
->bytecode
->b
.reloc_table_offset
= bytecode_get_len(&ctx
->bytecode
->b
);
640 return bytecode_push(&ctx
->bytecode
, ctx
->bytecode_reloc
->b
.data
,
641 1, bytecode_get_len(&ctx
->bytecode_reloc
->b
));
644 filter_bytecode_free(ctx
);