2 * filter-visitor-generate-bytecode.c
4 * LTTng filter bytecode generation
6 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License, version 2.1 only,
10 * as published by the Free Software Foundation.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <common/align.h>
26 #include <common/compat/string.h>
28 #include "filter-bytecode.h"
29 #include "filter-ir.h"
30 #include "filter-ast.h"
32 #include <common/macros.h>
35 #define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b)))
38 #define INIT_ALLOC_SIZE 4
41 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
44 static inline int get_count_order(unsigned int count
)
48 order
= lttng_fls(count
) - 1;
49 if (count
& (count
- 1))
55 int bytecode_init(struct lttng_filter_bytecode_alloc
**fb
)
59 alloc_len
= sizeof(struct lttng_filter_bytecode_alloc
) + INIT_ALLOC_SIZE
;
60 *fb
= calloc(alloc_len
, 1);
64 (*fb
)->alloc_len
= alloc_len
;
70 int32_t bytecode_reserve(struct lttng_filter_bytecode_alloc
**fb
, uint32_t align
, uint32_t len
)
73 uint32_t padding
= offset_align((*fb
)->b
.len
, align
);
74 uint32_t new_len
= (*fb
)->b
.len
+ padding
+ len
;
75 uint32_t new_alloc_len
= sizeof(struct lttng_filter_bytecode_alloc
) + new_len
;
76 uint32_t old_alloc_len
= (*fb
)->alloc_len
;
78 if (new_len
> LTTNG_FILTER_MAX_LEN
)
81 if (new_alloc_len
> old_alloc_len
) {
82 struct lttng_filter_bytecode_alloc
*newptr
;
85 max_t(uint32_t, 1U << get_count_order(new_alloc_len
), old_alloc_len
<< 1);
86 newptr
= realloc(*fb
, new_alloc_len
);
90 /* We zero directly the memory from start of allocation. */
91 memset(&((char *) *fb
)[old_alloc_len
], 0, new_alloc_len
- old_alloc_len
);
92 (*fb
)->alloc_len
= new_alloc_len
;
94 (*fb
)->b
.len
+= padding
;
101 int bytecode_push(struct lttng_filter_bytecode_alloc
**fb
, const void *data
,
102 uint32_t align
, uint32_t len
)
106 offset
= bytecode_reserve(fb
, align
, len
);
109 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
114 int bytecode_push_logical(struct lttng_filter_bytecode_alloc
**fb
,
115 struct logical_op
*data
,
116 uint32_t align
, uint32_t len
,
117 uint16_t *skip_offset
)
121 offset
= bytecode_reserve(fb
, align
, len
);
124 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
126 (void *) &((struct logical_op
*) &(*fb
)->b
.data
[offset
])->skip_offset
127 - (void *) &(*fb
)->b
.data
[0];
132 int bytecode_patch(struct lttng_filter_bytecode_alloc
**fb
,
137 if (offset
>= (*fb
)->b
.len
) {
140 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
145 int visit_node_root(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
148 struct return_op insn
;
151 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.root
.child
);
155 /* Generate end of bytecode instruction */
156 insn
.op
= FILTER_OP_RETURN
;
157 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
161 int visit_node_load_expression(struct filter_parser_ctx
*ctx
,
162 const struct ir_op
*node
)
164 struct ir_load_expression
*exp
;
165 struct ir_load_expression_op
*op
;
167 exp
= node
->u
.load
.u
.expression
;
175 for (; op
!= NULL
; op
= op
->next
) {
177 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
179 struct load_op
*insn
;
180 uint32_t insn_len
= sizeof(struct load_op
);
183 insn
= calloc(insn_len
, 1);
186 insn
->op
= FILTER_OP_GET_CONTEXT_ROOT
;
187 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
194 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
196 struct load_op
*insn
;
197 uint32_t insn_len
= sizeof(struct load_op
);
200 insn
= calloc(insn_len
, 1);
203 insn
->op
= FILTER_OP_GET_APP_CONTEXT_ROOT
;
204 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
211 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
213 struct load_op
*insn
;
214 uint32_t insn_len
= sizeof(struct load_op
);
217 insn
= calloc(insn_len
, 1);
220 insn
->op
= FILTER_OP_GET_PAYLOAD_ROOT
;
221 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
228 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
230 struct load_op
*insn
;
231 uint32_t insn_len
= sizeof(struct load_op
)
232 + sizeof(struct get_symbol
);
233 struct get_symbol symbol_offset
;
234 uint32_t reloc_offset_u32
;
235 uint16_t reloc_offset
;
236 uint32_t bytecode_reloc_offset_u32
;
239 insn
= calloc(insn_len
, 1);
242 insn
->op
= FILTER_OP_GET_SYMBOL
;
243 bytecode_reloc_offset_u32
=
244 bytecode_get_len(&ctx
->bytecode_reloc
->b
)
245 + sizeof(reloc_offset
);
246 symbol_offset
.offset
=
247 (uint16_t) bytecode_reloc_offset_u32
;
248 memcpy(insn
->data
, &symbol_offset
,
249 sizeof(symbol_offset
));
250 /* reloc_offset points to struct load_op */
251 reloc_offset_u32
= bytecode_get_len(&ctx
->bytecode
->b
);
252 if (reloc_offset_u32
> LTTNG_FILTER_MAX_LEN
- 1) {
256 reloc_offset
= (uint16_t) reloc_offset_u32
;
257 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
263 ret
= bytecode_push(&ctx
->bytecode_reloc
, &reloc_offset
,
264 1, sizeof(reloc_offset
));
269 ret
= bytecode_push(&ctx
->bytecode_reloc
,
271 1, strlen(op
->u
.symbol
) + 1);
278 case IR_LOAD_EXPRESSION_GET_INDEX
:
280 struct load_op
*insn
;
281 uint32_t insn_len
= sizeof(struct load_op
)
282 + sizeof(struct get_index_u64
);
283 struct get_index_u64 index
;
286 insn
= calloc(insn_len
, 1);
289 insn
->op
= FILTER_OP_GET_INDEX_U64
;
290 index
.index
= op
->u
.index
;
291 memcpy(insn
->data
, &index
, sizeof(index
));
292 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
299 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
301 struct load_op
*insn
;
302 uint32_t insn_len
= sizeof(struct load_op
);
305 insn
= calloc(insn_len
, 1);
308 insn
->op
= FILTER_OP_LOAD_FIELD
;
309 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
322 int visit_node_load(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
326 switch (node
->data_type
) {
327 case IR_DATA_UNKNOWN
:
329 fprintf(stderr
, "[error] Unknown data type in %s\n",
335 struct load_op
*insn
;
336 uint32_t insn_len
= sizeof(struct load_op
)
337 + strlen(node
->u
.load
.u
.string
.value
) + 1;
339 insn
= calloc(insn_len
, 1);
343 switch (node
->u
.load
.u
.string
.type
) {
344 case IR_LOAD_STRING_TYPE_GLOB_STAR
:
346 * We explicitly tell the interpreter here that
347 * this load is a full star globbing pattern so
348 * that the appropriate matching function can be
349 * called. Also, see comment below.
351 insn
->op
= FILTER_OP_LOAD_STAR_GLOB_STRING
;
355 * This is the "legacy" string, which includes
356 * star globbing patterns with a star only at
357 * the end. Both "plain" and "star at the end"
358 * literal strings are handled at the same place
359 * by the tracer's filter bytecode interpreter,
360 * whereas full star globbing patterns (stars
361 * can be anywhere in the string) is a special
364 insn
->op
= FILTER_OP_LOAD_STRING
;
368 strcpy(insn
->data
, node
->u
.load
.u
.string
.value
);
369 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
373 case IR_DATA_NUMERIC
:
375 struct load_op
*insn
;
376 uint32_t insn_len
= sizeof(struct load_op
)
377 + sizeof(struct literal_numeric
);
379 insn
= calloc(insn_len
, 1);
382 insn
->op
= FILTER_OP_LOAD_S64
;
383 memcpy(insn
->data
, &node
->u
.load
.u
.num
, sizeof(int64_t));
384 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
390 struct load_op
*insn
;
391 uint32_t insn_len
= sizeof(struct load_op
)
392 + sizeof(struct literal_double
);
394 insn
= calloc(insn_len
, 1);
397 insn
->op
= FILTER_OP_LOAD_DOUBLE
;
398 memcpy(insn
->data
, &node
->u
.load
.u
.flt
, sizeof(double));
399 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
403 case IR_DATA_EXPRESSION
:
404 return visit_node_load_expression(ctx
, node
);
409 int visit_node_unary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
412 struct unary_op insn
;
415 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.unary
.child
);
419 /* Generate end of bytecode instruction */
420 switch (node
->u
.unary
.type
) {
421 case AST_UNARY_UNKNOWN
:
423 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
429 case AST_UNARY_MINUS
:
430 insn
.op
= FILTER_OP_UNARY_MINUS
;
431 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
433 insn
.op
= FILTER_OP_UNARY_NOT
;
434 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
439 * Binary comparator nesting is disallowed. This allows fitting into
443 int visit_node_binary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
446 struct binary_op insn
;
449 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
452 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
456 switch (node
->u
.binary
.type
) {
459 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
465 fprintf(stderr
, "[error] Unexpected logical node type in %s\n",
470 insn
.op
= FILTER_OP_MUL
;
473 insn
.op
= FILTER_OP_DIV
;
476 insn
.op
= FILTER_OP_MOD
;
479 insn
.op
= FILTER_OP_PLUS
;
482 insn
.op
= FILTER_OP_MINUS
;
485 insn
.op
= FILTER_OP_RSHIFT
;
488 insn
.op
= FILTER_OP_LSHIFT
;
491 insn
.op
= FILTER_OP_BIT_AND
;
494 insn
.op
= FILTER_OP_BIT_OR
;
497 insn
.op
= FILTER_OP_BIT_XOR
;
501 insn
.op
= FILTER_OP_EQ
;
504 insn
.op
= FILTER_OP_NE
;
507 insn
.op
= FILTER_OP_GT
;
510 insn
.op
= FILTER_OP_LT
;
513 insn
.op
= FILTER_OP_GE
;
516 insn
.op
= FILTER_OP_LE
;
519 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
523 * A logical op always return a s64 (1 or 0).
526 int visit_node_logical(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
529 struct logical_op insn
;
530 uint16_t skip_offset_loc
;
533 /* Visit left child */
534 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
537 /* Cast to s64 if float or field ref */
538 if ((node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
539 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
540 || node
->u
.binary
.left
->data_type
== IR_DATA_EXPRESSION
)
541 || node
->u
.binary
.left
->data_type
== IR_DATA_FLOAT
) {
542 struct cast_op cast_insn
;
544 if (node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
545 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
546 || node
->u
.binary
.left
->data_type
== IR_DATA_EXPRESSION
) {
547 cast_insn
.op
= FILTER_OP_CAST_TO_S64
;
549 cast_insn
.op
= FILTER_OP_CAST_DOUBLE_TO_S64
;
551 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
552 1, sizeof(cast_insn
));
556 switch (node
->u
.logical
.type
) {
558 fprintf(stderr
, "[error] Unknown node type in %s\n",
563 insn
.op
= FILTER_OP_AND
;
566 insn
.op
= FILTER_OP_OR
;
569 insn
.skip_offset
= (uint16_t) -1UL; /* Temporary */
570 ret
= bytecode_push_logical(&ctx
->bytecode
, &insn
, 1, sizeof(insn
),
574 /* Visit right child */
575 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
578 /* Cast to s64 if float or field ref */
579 if ((node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
580 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
581 || node
->u
.binary
.right
->data_type
== IR_DATA_EXPRESSION
)
582 || node
->u
.binary
.right
->data_type
== IR_DATA_FLOAT
) {
583 struct cast_op cast_insn
;
585 if (node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
586 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
587 || node
->u
.binary
.right
->data_type
== IR_DATA_EXPRESSION
) {
588 cast_insn
.op
= FILTER_OP_CAST_TO_S64
;
590 cast_insn
.op
= FILTER_OP_CAST_DOUBLE_TO_S64
;
592 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
593 1, sizeof(cast_insn
));
597 /* We now know where the logical op can skip. */
598 target_loc
= (uint16_t) bytecode_get_len(&ctx
->bytecode
->b
);
599 ret
= bytecode_patch(&ctx
->bytecode
,
600 &target_loc
, /* Offset to jump to */
601 skip_offset_loc
, /* Where to patch */
607 * Postorder traversal of the tree. We need the children result before
608 * we can evaluate the parent.
611 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
617 fprintf(stderr
, "[error] Unknown node type in %s\n",
622 return visit_node_root(ctx
, node
);
624 return visit_node_load(ctx
, node
);
626 return visit_node_unary(ctx
, node
);
628 return visit_node_binary(ctx
, node
);
630 return visit_node_logical(ctx
, node
);
635 void filter_bytecode_free(struct filter_parser_ctx
*ctx
)
643 ctx
->bytecode
= NULL
;
646 if (ctx
->bytecode_reloc
) {
647 free(ctx
->bytecode_reloc
);
648 ctx
->bytecode_reloc
= NULL
;
653 int filter_visitor_bytecode_generate(struct filter_parser_ctx
*ctx
)
657 ret
= bytecode_init(&ctx
->bytecode
);
660 ret
= bytecode_init(&ctx
->bytecode_reloc
);
663 ret
= recursive_visit_gen_bytecode(ctx
, ctx
->ir_root
);
667 /* Finally, append symbol table to bytecode */
668 ctx
->bytecode
->b
.reloc_table_offset
= bytecode_get_len(&ctx
->bytecode
->b
);
669 return bytecode_push(&ctx
->bytecode
, ctx
->bytecode_reloc
->b
.data
,
670 1, bytecode_get_len(&ctx
->bytecode_reloc
->b
));
673 filter_bytecode_free(ctx
);