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
16 #include "common/align.h"
17 #include "common/bytecode/bytecode.h"
18 #include "common/compat/string.h"
19 #include "common/macros.h"
20 #include "filter-ast.h"
21 #include "filter-ir.h"
24 #define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b)))
28 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
32 int bytecode_patch(struct lttng_bytecode_alloc
**fb
,
37 if (offset
>= (*fb
)->b
.len
) {
40 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
45 int visit_node_root(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
48 struct return_op insn
;
51 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.root
.child
);
55 /* Generate end of bytecode instruction */
56 insn
.op
= BYTECODE_OP_RETURN
;
57 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
61 int append_str(char **s
, const char *append
)
65 size_t oldlen
= (old
== NULL
) ? 0 : strlen(old
);
66 size_t appendlen
= strlen(append
);
68 new = calloc(oldlen
+ appendlen
+ 1, 1);
87 int load_expression_legacy_match(const struct ir_load_expression
*exp
,
88 enum bytecode_op
*op_type
,
91 const struct ir_load_expression_op
*op
;
92 bool need_dot
= false;
96 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
97 *op_type
= BYTECODE_OP_GET_CONTEXT_REF
;
98 if (append_str(symbol
, "$ctx.")) {
103 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
104 *op_type
= BYTECODE_OP_GET_CONTEXT_REF
;
105 if (append_str(symbol
, "$app.")) {
110 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
111 *op_type
= BYTECODE_OP_LOAD_FIELD_REF
;
115 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
116 case IR_LOAD_EXPRESSION_GET_INDEX
:
117 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
119 return 0; /* no match */
125 return 0; /* no match */
128 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
130 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
131 if (need_dot
&& append_str(symbol
, ".")) {
134 if (append_str(symbol
, op
->u
.symbol
)) {
139 return 0; /* no match */
144 return 1; /* Legacy match */
153 int visit_node_load_expression_legacy(struct filter_parser_ctx
*ctx
,
154 const struct ir_load_expression
*exp
,
155 const struct ir_load_expression_op
*op
)
157 struct load_op
*insn
= NULL
;
158 uint32_t insn_len
= sizeof(struct load_op
)
159 + sizeof(struct field_ref
);
160 struct field_ref ref_offset
;
161 uint32_t reloc_offset_u32
;
162 uint16_t reloc_offset
;
163 enum bytecode_op op_type
;
167 ret
= load_expression_legacy_match(exp
, &op_type
, &symbol
);
171 insn
= calloc(insn_len
, 1);
177 ref_offset
.offset
= (uint16_t) -1U;
178 memcpy(insn
->data
, &ref_offset
, sizeof(ref_offset
));
179 /* reloc_offset points to struct load_op */
180 reloc_offset_u32
= bytecode_get_len(&ctx
->bytecode
->b
);
181 if (reloc_offset_u32
> LTTNG_FILTER_MAX_LEN
- 1) {
185 reloc_offset
= (uint16_t) reloc_offset_u32
;
186 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
191 ret
= bytecode_push(&ctx
->bytecode_reloc
, &reloc_offset
,
192 1, sizeof(reloc_offset
));
196 ret
= bytecode_push(&ctx
->bytecode_reloc
, symbol
,
197 1, strlen(symbol
) + 1);
201 ret
= 1; /* legacy */
209 int visit_node_load_expression(struct filter_parser_ctx
*ctx
,
210 const struct ir_op
*node
)
212 struct ir_load_expression
*exp
;
213 struct ir_load_expression_op
*op
;
216 exp
= node
->u
.load
.u
.expression
;
226 * TODO: if we remove legacy load for application contexts, we
227 * need to update session bytecode parser as well.
229 ret
= visit_node_load_expression_legacy(ctx
, exp
, op
);
234 return 0; /* legacy */
237 for (; op
!= NULL
; op
= op
->next
) {
239 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
241 int ret
= bytecode_push_get_context_root(&ctx
->bytecode
);
248 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
250 int ret
= bytecode_push_get_app_context_root(&ctx
->bytecode
);
257 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
259 int ret
= bytecode_push_get_payload_root(&ctx
->bytecode
);
266 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
268 int ret
= bytecode_push_get_symbol(
270 &ctx
->bytecode_reloc
,
277 case IR_LOAD_EXPRESSION_GET_INDEX
:
279 int ret
= bytecode_push_get_index_u64(&ctx
->bytecode
, op
->u
.index
);
285 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
287 struct load_op
*insn
;
288 uint32_t insn_len
= sizeof(struct load_op
);
291 insn
= calloc(insn_len
, 1);
294 insn
->op
= BYTECODE_OP_LOAD_FIELD
;
295 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
308 int visit_node_load(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
312 switch (node
->data_type
) {
313 case IR_DATA_UNKNOWN
:
315 fprintf(stderr
, "[error] Unknown data type in %s\n",
321 struct load_op
*insn
;
322 uint32_t insn_len
= sizeof(struct load_op
)
323 + strlen(node
->u
.load
.u
.string
.value
) + 1;
325 insn
= calloc(insn_len
, 1);
329 switch (node
->u
.load
.u
.string
.type
) {
330 case IR_LOAD_STRING_TYPE_GLOB_STAR
:
332 * We explicitly tell the interpreter here that
333 * this load is a full star globbing pattern so
334 * that the appropriate matching function can be
335 * called. Also, see comment below.
337 insn
->op
= BYTECODE_OP_LOAD_STAR_GLOB_STRING
;
341 * This is the "legacy" string, which includes
342 * star globbing patterns with a star only at
343 * the end. Both "plain" and "star at the end"
344 * literal strings are handled at the same place
345 * by the tracer's filter bytecode interpreter,
346 * whereas full star globbing patterns (stars
347 * can be anywhere in the string) is a special
350 insn
->op
= BYTECODE_OP_LOAD_STRING
;
354 strcpy(insn
->data
, node
->u
.load
.u
.string
.value
);
355 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
359 case IR_DATA_NUMERIC
:
361 struct load_op
*insn
;
362 uint32_t insn_len
= sizeof(struct load_op
)
363 + sizeof(struct literal_numeric
);
365 insn
= calloc(insn_len
, 1);
368 insn
->op
= BYTECODE_OP_LOAD_S64
;
369 memcpy(insn
->data
, &node
->u
.load
.u
.num
, sizeof(int64_t));
370 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
376 struct load_op
*insn
;
377 uint32_t insn_len
= sizeof(struct load_op
)
378 + sizeof(struct literal_double
);
380 insn
= calloc(insn_len
, 1);
383 insn
->op
= BYTECODE_OP_LOAD_DOUBLE
;
384 memcpy(insn
->data
, &node
->u
.load
.u
.flt
, sizeof(double));
385 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
389 case IR_DATA_EXPRESSION
:
390 return visit_node_load_expression(ctx
, node
);
395 int visit_node_unary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
398 struct unary_op insn
;
401 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.unary
.child
);
405 /* Generate end of bytecode instruction */
406 switch (node
->u
.unary
.type
) {
407 case AST_UNARY_UNKNOWN
:
409 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
415 case AST_UNARY_MINUS
:
416 insn
.op
= BYTECODE_OP_UNARY_MINUS
;
417 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
419 insn
.op
= BYTECODE_OP_UNARY_NOT
;
420 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
421 case AST_UNARY_BIT_NOT
:
422 insn
.op
= BYTECODE_OP_UNARY_BIT_NOT
;
423 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
428 * Binary comparator nesting is disallowed. This allows fitting into
432 int visit_node_binary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
435 struct binary_op insn
;
438 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
441 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
445 switch (node
->u
.binary
.type
) {
448 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
454 fprintf(stderr
, "[error] Unexpected logical node type in %s\n",
459 insn
.op
= BYTECODE_OP_MUL
;
462 insn
.op
= BYTECODE_OP_DIV
;
465 insn
.op
= BYTECODE_OP_MOD
;
468 insn
.op
= BYTECODE_OP_PLUS
;
471 insn
.op
= BYTECODE_OP_MINUS
;
473 case AST_OP_BIT_RSHIFT
:
474 insn
.op
= BYTECODE_OP_BIT_RSHIFT
;
476 case AST_OP_BIT_LSHIFT
:
477 insn
.op
= BYTECODE_OP_BIT_LSHIFT
;
480 insn
.op
= BYTECODE_OP_BIT_AND
;
483 insn
.op
= BYTECODE_OP_BIT_OR
;
486 insn
.op
= BYTECODE_OP_BIT_XOR
;
490 insn
.op
= BYTECODE_OP_EQ
;
493 insn
.op
= BYTECODE_OP_NE
;
496 insn
.op
= BYTECODE_OP_GT
;
499 insn
.op
= BYTECODE_OP_LT
;
502 insn
.op
= BYTECODE_OP_GE
;
505 insn
.op
= BYTECODE_OP_LE
;
508 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
512 * A logical op always return a s64 (1 or 0).
515 int visit_node_logical(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
518 struct logical_op insn
;
519 uint16_t skip_offset_loc
;
522 /* Visit left child */
523 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
526 /* Cast to s64 if float or field ref */
527 if ((node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
528 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
529 || node
->u
.binary
.left
->data_type
== IR_DATA_EXPRESSION
)
530 || node
->u
.binary
.left
->data_type
== IR_DATA_FLOAT
) {
531 struct cast_op cast_insn
;
533 if (node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
534 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
535 || node
->u
.binary
.left
->data_type
== IR_DATA_EXPRESSION
) {
536 cast_insn
.op
= BYTECODE_OP_CAST_TO_S64
;
538 cast_insn
.op
= BYTECODE_OP_CAST_DOUBLE_TO_S64
;
540 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
541 1, sizeof(cast_insn
));
545 switch (node
->u
.logical
.type
) {
547 fprintf(stderr
, "[error] Unknown node type in %s\n",
552 insn
.op
= BYTECODE_OP_AND
;
555 insn
.op
= BYTECODE_OP_OR
;
558 insn
.skip_offset
= (uint16_t) -1UL; /* Temporary */
559 ret
= bytecode_push_logical(&ctx
->bytecode
, &insn
, 1, sizeof(insn
),
563 /* Visit right child */
564 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
567 /* Cast to s64 if float or field ref */
568 if ((node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
569 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
570 || node
->u
.binary
.right
->data_type
== IR_DATA_EXPRESSION
)
571 || node
->u
.binary
.right
->data_type
== IR_DATA_FLOAT
) {
572 struct cast_op cast_insn
;
574 if (node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
575 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
576 || node
->u
.binary
.right
->data_type
== IR_DATA_EXPRESSION
) {
577 cast_insn
.op
= BYTECODE_OP_CAST_TO_S64
;
579 cast_insn
.op
= BYTECODE_OP_CAST_DOUBLE_TO_S64
;
581 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
582 1, sizeof(cast_insn
));
586 /* We now know where the logical op can skip. */
587 target_loc
= (uint16_t) bytecode_get_len(&ctx
->bytecode
->b
);
588 ret
= bytecode_patch(&ctx
->bytecode
,
589 &target_loc
, /* Offset to jump to */
590 skip_offset_loc
, /* Where to patch */
596 * Postorder traversal of the tree. We need the children result before
597 * we can evaluate the parent.
600 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
606 fprintf(stderr
, "[error] Unknown node type in %s\n",
611 return visit_node_root(ctx
, node
);
613 return visit_node_load(ctx
, node
);
615 return visit_node_unary(ctx
, node
);
617 return visit_node_binary(ctx
, node
);
619 return visit_node_logical(ctx
, node
);
624 void filter_bytecode_free(struct filter_parser_ctx
*ctx
)
632 ctx
->bytecode
= NULL
;
635 if (ctx
->bytecode_reloc
) {
636 free(ctx
->bytecode_reloc
);
637 ctx
->bytecode_reloc
= NULL
;
642 int filter_visitor_bytecode_generate(struct filter_parser_ctx
*ctx
)
646 ret
= bytecode_init(&ctx
->bytecode
);
649 ret
= bytecode_init(&ctx
->bytecode_reloc
);
652 ret
= recursive_visit_gen_bytecode(ctx
, ctx
->ir_root
);
656 /* Finally, append symbol table to bytecode */
657 ctx
->bytecode
->b
.reloc_table_offset
= bytecode_get_len(&ctx
->bytecode
->b
);
658 return bytecode_push(&ctx
->bytecode
, ctx
->bytecode_reloc
->b
.data
,
659 1, bytecode_get_len(&ctx
->bytecode_reloc
->b
));
662 filter_bytecode_free(ctx
);