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
12 #include "common/align.hpp"
13 #include "common/bytecode/bytecode.hpp"
14 #include "common/compat/string.hpp"
15 #include "common/macros.hpp"
16 #include "common/string-utils/string-utils.hpp"
17 #include "filter-ast.hpp"
18 #include "filter-ir.hpp"
20 #include <common/align.hpp>
21 #include <common/compat/errno.hpp>
22 #include <common/compat/string.hpp>
27 static int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
, struct ir_op
*node
);
30 bytecode_patch(struct lttng_bytecode_alloc
**fb
, const void *data
, uint16_t offset
, uint32_t len
)
32 if (offset
>= (*fb
)->b
.len
) {
35 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
39 static int visit_node_root(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
42 struct return_op insn
;
45 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.root
.child
);
49 /* Generate end of bytecode instruction */
50 insn
.op
= BYTECODE_OP_RETURN
;
51 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
59 static int load_expression_legacy_match(const struct ir_load_expression
*exp
,
60 enum bytecode_op
*op_type
,
63 const struct ir_load_expression_op
*op
;
64 bool need_dot
= false;
68 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
69 *op_type
= BYTECODE_OP_GET_CONTEXT_REF
;
70 if (strutils_append_str(symbol
, "$ctx.")) {
75 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
76 *op_type
= BYTECODE_OP_GET_CONTEXT_REF
;
77 if (strutils_append_str(symbol
, "$app.")) {
82 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
83 *op_type
= BYTECODE_OP_LOAD_FIELD_REF
;
87 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
88 case IR_LOAD_EXPRESSION_GET_INDEX
:
89 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
91 return 0; /* no match */
97 return 0; /* no match */
100 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
102 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
103 if (need_dot
&& strutils_append_str(symbol
, ".")) {
106 if (strutils_append_str(symbol
, op
->u
.symbol
)) {
111 return 0; /* no match */
116 return 1; /* Legacy match */
124 static int visit_node_load_expression_legacy(struct filter_parser_ctx
*ctx
,
125 const struct ir_load_expression
*exp
,
126 const struct ir_load_expression_op
*op
)
128 struct load_op
*insn
= NULL
;
129 uint32_t insn_len
= sizeof(struct load_op
) + sizeof(struct field_ref
);
130 struct field_ref ref_offset
;
131 uint32_t reloc_offset_u32
;
132 uint16_t reloc_offset
;
133 enum bytecode_op op_type
;
137 ret
= load_expression_legacy_match(exp
, &op_type
, &symbol
);
141 insn
= (load_op
*) calloc(insn_len
, 1);
147 ref_offset
.offset
= (uint16_t) -1U;
148 memcpy(insn
->data
, &ref_offset
, sizeof(ref_offset
));
149 /* reloc_offset points to struct load_op */
150 reloc_offset_u32
= bytecode_get_len(&ctx
->bytecode
->b
);
151 if (reloc_offset_u32
> LTTNG_FILTER_MAX_LEN
- 1) {
155 reloc_offset
= (uint16_t) reloc_offset_u32
;
156 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
161 ret
= bytecode_push(&ctx
->bytecode_reloc
, &reloc_offset
, 1, sizeof(reloc_offset
));
165 ret
= bytecode_push(&ctx
->bytecode_reloc
, symbol
, 1, strlen(symbol
) + 1);
169 ret
= 1; /* legacy */
176 static int visit_node_load_expression(struct filter_parser_ctx
*ctx
, const struct ir_op
*node
)
178 struct ir_load_expression
*exp
;
179 struct ir_load_expression_op
*op
;
182 exp
= node
->u
.load
.u
.expression
;
192 * TODO: if we remove legacy load for application contexts, we
193 * need to update session bytecode parser as well.
195 ret
= visit_node_load_expression_legacy(ctx
, exp
, op
);
200 return 0; /* legacy */
203 for (; op
!= NULL
; op
= op
->next
) {
205 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
207 ret
= bytecode_push_get_context_root(&ctx
->bytecode
);
215 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
217 ret
= bytecode_push_get_app_context_root(&ctx
->bytecode
);
225 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
227 ret
= bytecode_push_get_payload_root(&ctx
->bytecode
);
235 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
237 ret
= bytecode_push_get_symbol(
238 &ctx
->bytecode
, &ctx
->bytecode_reloc
, op
->u
.symbol
);
246 case IR_LOAD_EXPRESSION_GET_INDEX
:
248 ret
= bytecode_push_get_index_u64(&ctx
->bytecode
, op
->u
.index
);
256 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
258 struct load_op
*insn
;
259 uint32_t insn_len
= sizeof(struct load_op
);
261 insn
= (load_op
*) calloc(insn_len
, 1);
264 insn
->op
= BYTECODE_OP_LOAD_FIELD
;
265 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
277 static int visit_node_load(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
281 switch (node
->data_type
) {
282 case IR_DATA_UNKNOWN
:
284 fprintf(stderr
, "[error] Unknown data type in %s\n", __func__
);
289 struct load_op
*insn
;
291 sizeof(struct load_op
) + strlen(node
->u
.load
.u
.string
.value
) + 1;
293 insn
= (load_op
*) calloc(insn_len
, 1);
297 switch (node
->u
.load
.u
.string
.type
) {
298 case IR_LOAD_STRING_TYPE_GLOB_STAR
:
300 * We explicitly tell the interpreter here that
301 * this load is a full star globbing pattern so
302 * that the appropriate matching function can be
303 * called. Also, see comment below.
305 insn
->op
= BYTECODE_OP_LOAD_STAR_GLOB_STRING
;
309 * This is the "legacy" string, which includes
310 * star globbing patterns with a star only at
311 * the end. Both "plain" and "star at the end"
312 * literal strings are handled at the same place
313 * by the tracer's filter bytecode interpreter,
314 * whereas full star globbing patterns (stars
315 * can be anywhere in the string) is a special
318 insn
->op
= BYTECODE_OP_LOAD_STRING
;
322 strcpy(insn
->data
, node
->u
.load
.u
.string
.value
);
323 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
327 case IR_DATA_NUMERIC
:
329 struct load_op
*insn
;
330 uint32_t insn_len
= sizeof(struct load_op
) + sizeof(struct literal_numeric
);
332 insn
= (load_op
*) calloc(insn_len
, 1);
335 insn
->op
= BYTECODE_OP_LOAD_S64
;
336 memcpy(insn
->data
, &node
->u
.load
.u
.num
, sizeof(int64_t));
337 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
343 struct load_op
*insn
;
344 uint32_t insn_len
= sizeof(struct load_op
) + sizeof(struct literal_double
);
346 insn
= (load_op
*) calloc(insn_len
, 1);
349 insn
->op
= BYTECODE_OP_LOAD_DOUBLE
;
350 memcpy(insn
->data
, &node
->u
.load
.u
.flt
, sizeof(double));
351 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
355 case IR_DATA_EXPRESSION
:
356 return visit_node_load_expression(ctx
, node
);
360 static int visit_node_unary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
363 struct unary_op insn
;
366 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.unary
.child
);
370 /* Generate end of bytecode instruction */
371 switch (node
->u
.unary
.type
) {
372 case AST_UNARY_UNKNOWN
:
374 fprintf(stderr
, "[error] Unknown unary node type in %s\n", __func__
);
379 case AST_UNARY_MINUS
:
380 insn
.op
= BYTECODE_OP_UNARY_MINUS
;
381 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
383 insn
.op
= BYTECODE_OP_UNARY_NOT
;
384 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
385 case AST_UNARY_BIT_NOT
:
386 insn
.op
= BYTECODE_OP_UNARY_BIT_NOT
;
387 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
392 * Binary comparator nesting is disallowed. This allows fitting into
395 static int visit_node_binary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
398 struct binary_op insn
;
401 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
404 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
408 switch (node
->u
.binary
.type
) {
411 fprintf(stderr
, "[error] Unknown unary node type in %s\n", __func__
);
416 fprintf(stderr
, "[error] Unexpected logical node type in %s\n", __func__
);
420 insn
.op
= BYTECODE_OP_MUL
;
423 insn
.op
= BYTECODE_OP_DIV
;
426 insn
.op
= BYTECODE_OP_MOD
;
429 insn
.op
= BYTECODE_OP_PLUS
;
432 insn
.op
= BYTECODE_OP_MINUS
;
434 case AST_OP_BIT_RSHIFT
:
435 insn
.op
= BYTECODE_OP_BIT_RSHIFT
;
437 case AST_OP_BIT_LSHIFT
:
438 insn
.op
= BYTECODE_OP_BIT_LSHIFT
;
441 insn
.op
= BYTECODE_OP_BIT_AND
;
444 insn
.op
= BYTECODE_OP_BIT_OR
;
447 insn
.op
= BYTECODE_OP_BIT_XOR
;
451 insn
.op
= BYTECODE_OP_EQ
;
454 insn
.op
= BYTECODE_OP_NE
;
457 insn
.op
= BYTECODE_OP_GT
;
460 insn
.op
= BYTECODE_OP_LT
;
463 insn
.op
= BYTECODE_OP_GE
;
466 insn
.op
= BYTECODE_OP_LE
;
469 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
473 * A logical op always return a s64 (1 or 0).
475 static int visit_node_logical(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
478 struct logical_op insn
;
479 uint16_t skip_offset_loc
;
482 /* Visit left child */
483 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
486 /* Cast to s64 if float or field ref */
487 if ((node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
||
488 node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
||
489 node
->u
.binary
.left
->data_type
== IR_DATA_EXPRESSION
) ||
490 node
->u
.binary
.left
->data_type
== IR_DATA_FLOAT
) {
491 struct cast_op cast_insn
;
493 if (node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
||
494 node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
||
495 node
->u
.binary
.left
->data_type
== IR_DATA_EXPRESSION
) {
496 cast_insn
.op
= BYTECODE_OP_CAST_TO_S64
;
498 cast_insn
.op
= BYTECODE_OP_CAST_DOUBLE_TO_S64
;
500 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
, 1, sizeof(cast_insn
));
504 switch (node
->u
.logical
.type
) {
506 fprintf(stderr
, "[error] Unknown node type in %s\n", __func__
);
510 insn
.op
= BYTECODE_OP_AND
;
513 insn
.op
= BYTECODE_OP_OR
;
516 insn
.skip_offset
= (uint16_t) -1UL; /* Temporary */
517 ret
= bytecode_push_logical(&ctx
->bytecode
, &insn
, 1, sizeof(insn
), &skip_offset_loc
);
520 /* Visit right child */
521 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
524 /* Cast to s64 if float or field ref */
525 if ((node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
||
526 node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
||
527 node
->u
.binary
.right
->data_type
== IR_DATA_EXPRESSION
) ||
528 node
->u
.binary
.right
->data_type
== IR_DATA_FLOAT
) {
529 struct cast_op cast_insn
;
531 if (node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
||
532 node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
||
533 node
->u
.binary
.right
->data_type
== IR_DATA_EXPRESSION
) {
534 cast_insn
.op
= BYTECODE_OP_CAST_TO_S64
;
536 cast_insn
.op
= BYTECODE_OP_CAST_DOUBLE_TO_S64
;
538 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
, 1, sizeof(cast_insn
));
542 /* We now know where the logical op can skip. */
543 target_loc
= (uint16_t) bytecode_get_len(&ctx
->bytecode
->b
);
544 ret
= bytecode_patch(&ctx
->bytecode
,
545 &target_loc
, /* Offset to jump to */
546 skip_offset_loc
, /* Where to patch */
552 * Postorder traversal of the tree. We need the children result before
553 * we can evaluate the parent.
555 static int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
560 fprintf(stderr
, "[error] Unknown node type in %s\n", __func__
);
564 return visit_node_root(ctx
, node
);
566 return visit_node_load(ctx
, node
);
568 return visit_node_unary(ctx
, node
);
570 return visit_node_binary(ctx
, node
);
572 return visit_node_logical(ctx
, node
);
576 void filter_bytecode_free(struct filter_parser_ctx
*ctx
)
584 ctx
->bytecode
= NULL
;
587 if (ctx
->bytecode_reloc
) {
588 free(ctx
->bytecode_reloc
);
589 ctx
->bytecode_reloc
= NULL
;
593 int filter_visitor_bytecode_generate(struct filter_parser_ctx
*ctx
)
597 ret
= bytecode_init(&ctx
->bytecode
);
600 ret
= bytecode_init(&ctx
->bytecode_reloc
);
603 ret
= recursive_visit_gen_bytecode(ctx
, ctx
->ir_root
);
607 /* Finally, append symbol table to bytecode */
608 ctx
->bytecode
->b
.reloc_table_offset
= bytecode_get_len(&ctx
->bytecode
->b
);
609 return bytecode_push(&ctx
->bytecode
,
610 ctx
->bytecode_reloc
->b
.data
,
612 bytecode_get_len(&ctx
->bytecode_reloc
->b
));
615 filter_bytecode_free(ctx
);