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 "common/string-utils/string-utils.h"
23 #include "filter-ast.h"
24 #include "filter-ir.h"
27 #define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b)))
31 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
35 int bytecode_patch(struct lttng_bytecode_alloc
**fb
,
40 if (offset
>= (*fb
)->b
.len
) {
43 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
48 int visit_node_root(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
51 struct return_op insn
;
54 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.root
.child
);
58 /* Generate end of bytecode instruction */
59 insn
.op
= BYTECODE_OP_RETURN
;
60 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
69 int load_expression_legacy_match(const struct ir_load_expression
*exp
,
70 enum bytecode_op
*op_type
,
73 const struct ir_load_expression_op
*op
;
74 bool need_dot
= false;
78 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
79 *op_type
= BYTECODE_OP_GET_CONTEXT_REF
;
80 if (strutils_append_str(symbol
, "$ctx.")) {
85 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
86 *op_type
= BYTECODE_OP_GET_CONTEXT_REF
;
87 if (strutils_append_str(symbol
, "$app.")) {
92 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
93 *op_type
= BYTECODE_OP_LOAD_FIELD_REF
;
97 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
98 case IR_LOAD_EXPRESSION_GET_INDEX
:
99 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
101 return 0; /* no match */
107 return 0; /* no match */
110 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
112 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
113 if (need_dot
&& strutils_append_str(symbol
, ".")) {
116 if (strutils_append_str(symbol
, op
->u
.symbol
)) {
121 return 0; /* no match */
126 return 1; /* Legacy match */
135 int visit_node_load_expression_legacy(struct filter_parser_ctx
*ctx
,
136 const struct ir_load_expression
*exp
,
137 const struct ir_load_expression_op
*op
)
139 struct load_op
*insn
= NULL
;
140 uint32_t insn_len
= sizeof(struct load_op
)
141 + sizeof(struct field_ref
);
142 struct field_ref ref_offset
;
143 uint32_t reloc_offset_u32
;
144 uint16_t reloc_offset
;
145 enum bytecode_op op_type
;
149 ret
= load_expression_legacy_match(exp
, &op_type
, &symbol
);
153 insn
= (load_op
*) calloc(insn_len
, 1);
159 ref_offset
.offset
= (uint16_t) -1U;
160 memcpy(insn
->data
, &ref_offset
, sizeof(ref_offset
));
161 /* reloc_offset points to struct load_op */
162 reloc_offset_u32
= bytecode_get_len(&ctx
->bytecode
->b
);
163 if (reloc_offset_u32
> LTTNG_FILTER_MAX_LEN
- 1) {
167 reloc_offset
= (uint16_t) reloc_offset_u32
;
168 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
173 ret
= bytecode_push(&ctx
->bytecode_reloc
, &reloc_offset
,
174 1, sizeof(reloc_offset
));
178 ret
= bytecode_push(&ctx
->bytecode_reloc
, symbol
,
179 1, strlen(symbol
) + 1);
183 ret
= 1; /* legacy */
191 int visit_node_load_expression(struct filter_parser_ctx
*ctx
,
192 const struct ir_op
*node
)
194 struct ir_load_expression
*exp
;
195 struct ir_load_expression_op
*op
;
198 exp
= node
->u
.load
.u
.expression
;
208 * TODO: if we remove legacy load for application contexts, we
209 * need to update session bytecode parser as well.
211 ret
= visit_node_load_expression_legacy(ctx
, exp
, op
);
216 return 0; /* legacy */
219 for (; op
!= NULL
; op
= op
->next
) {
221 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
223 ret
= bytecode_push_get_context_root(&ctx
->bytecode
);
231 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
233 ret
= bytecode_push_get_app_context_root(
242 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
244 ret
= bytecode_push_get_payload_root(&ctx
->bytecode
);
252 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
254 ret
= bytecode_push_get_symbol(&ctx
->bytecode
,
255 &ctx
->bytecode_reloc
, op
->u
.symbol
);
263 case IR_LOAD_EXPRESSION_GET_INDEX
:
265 ret
= bytecode_push_get_index_u64(
266 &ctx
->bytecode
, op
->u
.index
);
274 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
276 struct load_op
*insn
;
277 uint32_t insn_len
= sizeof(struct load_op
);
279 insn
= (load_op
*) calloc(insn_len
, 1);
282 insn
->op
= BYTECODE_OP_LOAD_FIELD
;
283 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
296 int visit_node_load(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
300 switch (node
->data_type
) {
301 case IR_DATA_UNKNOWN
:
303 fprintf(stderr
, "[error] Unknown data type in %s\n",
309 struct load_op
*insn
;
310 uint32_t insn_len
= sizeof(struct load_op
)
311 + strlen(node
->u
.load
.u
.string
.value
) + 1;
313 insn
= (load_op
*) calloc(insn_len
, 1);
317 switch (node
->u
.load
.u
.string
.type
) {
318 case IR_LOAD_STRING_TYPE_GLOB_STAR
:
320 * We explicitly tell the interpreter here that
321 * this load is a full star globbing pattern so
322 * that the appropriate matching function can be
323 * called. Also, see comment below.
325 insn
->op
= BYTECODE_OP_LOAD_STAR_GLOB_STRING
;
329 * This is the "legacy" string, which includes
330 * star globbing patterns with a star only at
331 * the end. Both "plain" and "star at the end"
332 * literal strings are handled at the same place
333 * by the tracer's filter bytecode interpreter,
334 * whereas full star globbing patterns (stars
335 * can be anywhere in the string) is a special
338 insn
->op
= BYTECODE_OP_LOAD_STRING
;
342 strcpy(insn
->data
, node
->u
.load
.u
.string
.value
);
343 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
347 case IR_DATA_NUMERIC
:
349 struct load_op
*insn
;
350 uint32_t insn_len
= sizeof(struct load_op
)
351 + sizeof(struct literal_numeric
);
353 insn
= (load_op
*) calloc(insn_len
, 1);
356 insn
->op
= BYTECODE_OP_LOAD_S64
;
357 memcpy(insn
->data
, &node
->u
.load
.u
.num
, sizeof(int64_t));
358 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
364 struct load_op
*insn
;
365 uint32_t insn_len
= sizeof(struct load_op
)
366 + sizeof(struct literal_double
);
368 insn
= (load_op
*) calloc(insn_len
, 1);
371 insn
->op
= BYTECODE_OP_LOAD_DOUBLE
;
372 memcpy(insn
->data
, &node
->u
.load
.u
.flt
, sizeof(double));
373 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
377 case IR_DATA_EXPRESSION
:
378 return visit_node_load_expression(ctx
, node
);
383 int visit_node_unary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
386 struct unary_op insn
;
389 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.unary
.child
);
393 /* Generate end of bytecode instruction */
394 switch (node
->u
.unary
.type
) {
395 case AST_UNARY_UNKNOWN
:
397 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
403 case AST_UNARY_MINUS
:
404 insn
.op
= BYTECODE_OP_UNARY_MINUS
;
405 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
407 insn
.op
= BYTECODE_OP_UNARY_NOT
;
408 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
409 case AST_UNARY_BIT_NOT
:
410 insn
.op
= BYTECODE_OP_UNARY_BIT_NOT
;
411 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
416 * Binary comparator nesting is disallowed. This allows fitting into
420 int visit_node_binary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
423 struct binary_op insn
;
426 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
429 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
433 switch (node
->u
.binary
.type
) {
436 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
442 fprintf(stderr
, "[error] Unexpected logical node type in %s\n",
447 insn
.op
= BYTECODE_OP_MUL
;
450 insn
.op
= BYTECODE_OP_DIV
;
453 insn
.op
= BYTECODE_OP_MOD
;
456 insn
.op
= BYTECODE_OP_PLUS
;
459 insn
.op
= BYTECODE_OP_MINUS
;
461 case AST_OP_BIT_RSHIFT
:
462 insn
.op
= BYTECODE_OP_BIT_RSHIFT
;
464 case AST_OP_BIT_LSHIFT
:
465 insn
.op
= BYTECODE_OP_BIT_LSHIFT
;
468 insn
.op
= BYTECODE_OP_BIT_AND
;
471 insn
.op
= BYTECODE_OP_BIT_OR
;
474 insn
.op
= BYTECODE_OP_BIT_XOR
;
478 insn
.op
= BYTECODE_OP_EQ
;
481 insn
.op
= BYTECODE_OP_NE
;
484 insn
.op
= BYTECODE_OP_GT
;
487 insn
.op
= BYTECODE_OP_LT
;
490 insn
.op
= BYTECODE_OP_GE
;
493 insn
.op
= BYTECODE_OP_LE
;
496 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
500 * A logical op always return a s64 (1 or 0).
503 int visit_node_logical(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
506 struct logical_op insn
;
507 uint16_t skip_offset_loc
;
510 /* Visit left child */
511 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
514 /* Cast to s64 if float or field ref */
515 if ((node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
516 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
517 || node
->u
.binary
.left
->data_type
== IR_DATA_EXPRESSION
)
518 || node
->u
.binary
.left
->data_type
== IR_DATA_FLOAT
) {
519 struct cast_op cast_insn
;
521 if (node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
522 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
523 || node
->u
.binary
.left
->data_type
== IR_DATA_EXPRESSION
) {
524 cast_insn
.op
= BYTECODE_OP_CAST_TO_S64
;
526 cast_insn
.op
= BYTECODE_OP_CAST_DOUBLE_TO_S64
;
528 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
529 1, sizeof(cast_insn
));
533 switch (node
->u
.logical
.type
) {
535 fprintf(stderr
, "[error] Unknown node type in %s\n",
540 insn
.op
= BYTECODE_OP_AND
;
543 insn
.op
= BYTECODE_OP_OR
;
546 insn
.skip_offset
= (uint16_t) -1UL; /* Temporary */
547 ret
= bytecode_push_logical(&ctx
->bytecode
, &insn
, 1, sizeof(insn
),
551 /* Visit right child */
552 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
555 /* Cast to s64 if float or field ref */
556 if ((node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
557 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
558 || node
->u
.binary
.right
->data_type
== IR_DATA_EXPRESSION
)
559 || node
->u
.binary
.right
->data_type
== IR_DATA_FLOAT
) {
560 struct cast_op cast_insn
;
562 if (node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
563 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
564 || node
->u
.binary
.right
->data_type
== IR_DATA_EXPRESSION
) {
565 cast_insn
.op
= BYTECODE_OP_CAST_TO_S64
;
567 cast_insn
.op
= BYTECODE_OP_CAST_DOUBLE_TO_S64
;
569 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
570 1, sizeof(cast_insn
));
574 /* We now know where the logical op can skip. */
575 target_loc
= (uint16_t) bytecode_get_len(&ctx
->bytecode
->b
);
576 ret
= bytecode_patch(&ctx
->bytecode
,
577 &target_loc
, /* Offset to jump to */
578 skip_offset_loc
, /* Where to patch */
584 * Postorder traversal of the tree. We need the children result before
585 * we can evaluate the parent.
588 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
594 fprintf(stderr
, "[error] Unknown node type in %s\n",
599 return visit_node_root(ctx
, node
);
601 return visit_node_load(ctx
, node
);
603 return visit_node_unary(ctx
, node
);
605 return visit_node_binary(ctx
, node
);
607 return visit_node_logical(ctx
, node
);
611 void filter_bytecode_free(struct filter_parser_ctx
*ctx
)
619 ctx
->bytecode
= NULL
;
622 if (ctx
->bytecode_reloc
) {
623 free(ctx
->bytecode_reloc
);
624 ctx
->bytecode_reloc
= NULL
;
628 int filter_visitor_bytecode_generate(struct filter_parser_ctx
*ctx
)
632 ret
= bytecode_init(&ctx
->bytecode
);
635 ret
= bytecode_init(&ctx
->bytecode_reloc
);
638 ret
= recursive_visit_gen_bytecode(ctx
, ctx
->ir_root
);
642 /* Finally, append symbol table to bytecode */
643 ctx
->bytecode
->b
.reloc_table_offset
= bytecode_get_len(&ctx
->bytecode
->b
);
644 return bytecode_push(&ctx
->bytecode
, ctx
->bytecode_reloc
->b
.data
,
645 1, bytecode_get_len(&ctx
->bytecode_reloc
->b
));
648 filter_bytecode_free(ctx
);