1 /* SPDX-License-Identifier: MIT
5 * LTTng modules bytecode code.
7 * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 #include <linux/list.h>
11 #include <linux/slab.h>
13 #include <lttng/lttng-bytecode.h>
15 static const char *opnames
[] = {
16 [ BYTECODE_OP_UNKNOWN
] = "UNKNOWN",
18 [ BYTECODE_OP_RETURN
] = "RETURN",
21 [ BYTECODE_OP_MUL
] = "MUL",
22 [ BYTECODE_OP_DIV
] = "DIV",
23 [ BYTECODE_OP_MOD
] = "MOD",
24 [ BYTECODE_OP_PLUS
] = "PLUS",
25 [ BYTECODE_OP_MINUS
] = "MINUS",
26 [ BYTECODE_OP_BIT_RSHIFT
] = "BIT_RSHIFT",
27 [ BYTECODE_OP_BIT_LSHIFT
] = "BIT_LSHIFT",
28 [ BYTECODE_OP_BIT_AND
] = "BIT_AND",
29 [ BYTECODE_OP_BIT_OR
] = "BIT_OR",
30 [ BYTECODE_OP_BIT_XOR
] = "BIT_XOR",
32 /* binary comparators */
33 [ BYTECODE_OP_EQ
] = "EQ",
34 [ BYTECODE_OP_NE
] = "NE",
35 [ BYTECODE_OP_GT
] = "GT",
36 [ BYTECODE_OP_LT
] = "LT",
37 [ BYTECODE_OP_GE
] = "GE",
38 [ BYTECODE_OP_LE
] = "LE",
40 /* string binary comparators */
41 [ BYTECODE_OP_EQ_STRING
] = "EQ_STRING",
42 [ BYTECODE_OP_NE_STRING
] = "NE_STRING",
43 [ BYTECODE_OP_GT_STRING
] = "GT_STRING",
44 [ BYTECODE_OP_LT_STRING
] = "LT_STRING",
45 [ BYTECODE_OP_GE_STRING
] = "GE_STRING",
46 [ BYTECODE_OP_LE_STRING
] = "LE_STRING",
48 /* s64 binary comparators */
49 [ BYTECODE_OP_EQ_S64
] = "EQ_S64",
50 [ BYTECODE_OP_NE_S64
] = "NE_S64",
51 [ BYTECODE_OP_GT_S64
] = "GT_S64",
52 [ BYTECODE_OP_LT_S64
] = "LT_S64",
53 [ BYTECODE_OP_GE_S64
] = "GE_S64",
54 [ BYTECODE_OP_LE_S64
] = "LE_S64",
56 /* double binary comparators */
57 [ BYTECODE_OP_EQ_DOUBLE
] = "EQ_DOUBLE",
58 [ BYTECODE_OP_NE_DOUBLE
] = "NE_DOUBLE",
59 [ BYTECODE_OP_GT_DOUBLE
] = "GT_DOUBLE",
60 [ BYTECODE_OP_LT_DOUBLE
] = "LT_DOUBLE",
61 [ BYTECODE_OP_GE_DOUBLE
] = "GE_DOUBLE",
62 [ BYTECODE_OP_LE_DOUBLE
] = "LE_DOUBLE",
64 /* Mixed S64-double binary comparators */
65 [ BYTECODE_OP_EQ_DOUBLE_S64
] = "EQ_DOUBLE_S64",
66 [ BYTECODE_OP_NE_DOUBLE_S64
] = "NE_DOUBLE_S64",
67 [ BYTECODE_OP_GT_DOUBLE_S64
] = "GT_DOUBLE_S64",
68 [ BYTECODE_OP_LT_DOUBLE_S64
] = "LT_DOUBLE_S64",
69 [ BYTECODE_OP_GE_DOUBLE_S64
] = "GE_DOUBLE_S64",
70 [ BYTECODE_OP_LE_DOUBLE_S64
] = "LE_DOUBLE_S64",
72 [ BYTECODE_OP_EQ_S64_DOUBLE
] = "EQ_S64_DOUBLE",
73 [ BYTECODE_OP_NE_S64_DOUBLE
] = "NE_S64_DOUBLE",
74 [ BYTECODE_OP_GT_S64_DOUBLE
] = "GT_S64_DOUBLE",
75 [ BYTECODE_OP_LT_S64_DOUBLE
] = "LT_S64_DOUBLE",
76 [ BYTECODE_OP_GE_S64_DOUBLE
] = "GE_S64_DOUBLE",
77 [ BYTECODE_OP_LE_S64_DOUBLE
] = "LE_S64_DOUBLE",
80 [ BYTECODE_OP_UNARY_PLUS
] = "UNARY_PLUS",
81 [ BYTECODE_OP_UNARY_MINUS
] = "UNARY_MINUS",
82 [ BYTECODE_OP_UNARY_NOT
] = "UNARY_NOT",
83 [ BYTECODE_OP_UNARY_PLUS_S64
] = "UNARY_PLUS_S64",
84 [ BYTECODE_OP_UNARY_MINUS_S64
] = "UNARY_MINUS_S64",
85 [ BYTECODE_OP_UNARY_NOT_S64
] = "UNARY_NOT_S64",
86 [ BYTECODE_OP_UNARY_PLUS_DOUBLE
] = "UNARY_PLUS_DOUBLE",
87 [ BYTECODE_OP_UNARY_MINUS_DOUBLE
] = "UNARY_MINUS_DOUBLE",
88 [ BYTECODE_OP_UNARY_NOT_DOUBLE
] = "UNARY_NOT_DOUBLE",
91 [ BYTECODE_OP_AND
] = "AND",
92 [ BYTECODE_OP_OR
] = "OR",
95 [ BYTECODE_OP_LOAD_FIELD_REF
] = "LOAD_FIELD_REF",
96 [ BYTECODE_OP_LOAD_FIELD_REF_STRING
] = "LOAD_FIELD_REF_STRING",
97 [ BYTECODE_OP_LOAD_FIELD_REF_SEQUENCE
] = "LOAD_FIELD_REF_SEQUENCE",
98 [ BYTECODE_OP_LOAD_FIELD_REF_S64
] = "LOAD_FIELD_REF_S64",
99 [ BYTECODE_OP_LOAD_FIELD_REF_DOUBLE
] = "LOAD_FIELD_REF_DOUBLE",
101 /* load from immediate operand */
102 [ BYTECODE_OP_LOAD_STRING
] = "LOAD_STRING",
103 [ BYTECODE_OP_LOAD_S64
] = "LOAD_S64",
104 [ BYTECODE_OP_LOAD_DOUBLE
] = "LOAD_DOUBLE",
107 [ BYTECODE_OP_CAST_TO_S64
] = "CAST_TO_S64",
108 [ BYTECODE_OP_CAST_DOUBLE_TO_S64
] = "CAST_DOUBLE_TO_S64",
109 [ BYTECODE_OP_CAST_NOP
] = "CAST_NOP",
111 /* get context ref */
112 [ BYTECODE_OP_GET_CONTEXT_REF
] = "GET_CONTEXT_REF",
113 [ BYTECODE_OP_GET_CONTEXT_REF_STRING
] = "GET_CONTEXT_REF_STRING",
114 [ BYTECODE_OP_GET_CONTEXT_REF_S64
] = "GET_CONTEXT_REF_S64",
115 [ BYTECODE_OP_GET_CONTEXT_REF_DOUBLE
] = "GET_CONTEXT_REF_DOUBLE",
117 /* load userspace field ref */
118 [ BYTECODE_OP_LOAD_FIELD_REF_USER_STRING
] = "LOAD_FIELD_REF_USER_STRING",
119 [ BYTECODE_OP_LOAD_FIELD_REF_USER_SEQUENCE
] = "LOAD_FIELD_REF_USER_SEQUENCE",
122 * load immediate star globbing pattern (literal string)
125 [ BYTECODE_OP_LOAD_STAR_GLOB_STRING
] = "LOAD_STAR_GLOB_STRING",
127 /* globbing pattern binary operator: apply to */
128 [ BYTECODE_OP_EQ_STAR_GLOB_STRING
] = "EQ_STAR_GLOB_STRING",
129 [ BYTECODE_OP_NE_STAR_GLOB_STRING
] = "NE_STAR_GLOB_STRING",
132 * Instructions for recursive traversal through composed types.
134 [ BYTECODE_OP_GET_CONTEXT_ROOT
] = "GET_CONTEXT_ROOT",
135 [ BYTECODE_OP_GET_APP_CONTEXT_ROOT
] = "GET_APP_CONTEXT_ROOT",
136 [ BYTECODE_OP_GET_PAYLOAD_ROOT
] = "GET_PAYLOAD_ROOT",
138 [ BYTECODE_OP_GET_SYMBOL
] = "GET_SYMBOL",
139 [ BYTECODE_OP_GET_SYMBOL_FIELD
] = "GET_SYMBOL_FIELD",
140 [ BYTECODE_OP_GET_INDEX_U16
] = "GET_INDEX_U16",
141 [ BYTECODE_OP_GET_INDEX_U64
] = "GET_INDEX_U64",
143 [ BYTECODE_OP_LOAD_FIELD
] = "LOAD_FIELD",
144 [ BYTECODE_OP_LOAD_FIELD_S8
] = "LOAD_FIELD_S8",
145 [ BYTECODE_OP_LOAD_FIELD_S16
] = "LOAD_FIELD_S16",
146 [ BYTECODE_OP_LOAD_FIELD_S32
] = "LOAD_FIELD_S32",
147 [ BYTECODE_OP_LOAD_FIELD_S64
] = "LOAD_FIELD_S64",
148 [ BYTECODE_OP_LOAD_FIELD_U8
] = "LOAD_FIELD_U8",
149 [ BYTECODE_OP_LOAD_FIELD_U16
] = "LOAD_FIELD_U16",
150 [ BYTECODE_OP_LOAD_FIELD_U32
] = "LOAD_FIELD_U32",
151 [ BYTECODE_OP_LOAD_FIELD_U64
] = "LOAD_FIELD_U64",
152 [ BYTECODE_OP_LOAD_FIELD_STRING
] = "LOAD_FIELD_STRING",
153 [ BYTECODE_OP_LOAD_FIELD_SEQUENCE
] = "LOAD_FIELD_SEQUENCE",
154 [ BYTECODE_OP_LOAD_FIELD_DOUBLE
] = "LOAD_FIELD_DOUBLE",
156 [ BYTECODE_OP_UNARY_BIT_NOT
] = "UNARY_BIT_NOT",
158 [ BYTECODE_OP_RETURN_S64
] = "RETURN_S64",
161 const char *lttng_bytecode_print_op(enum bytecode_op op
)
163 if (op
>= NR_BYTECODE_OPS
)
170 int apply_field_reloc(const struct lttng_event_desc
*event_desc
,
171 struct bytecode_runtime
*runtime
,
172 uint32_t runtime_len
,
173 uint32_t reloc_offset
,
174 const char *field_name
,
175 enum bytecode_op bytecode_op
)
177 const struct lttng_event_field
*fields
, *field
= NULL
;
178 unsigned int nr_fields
, i
;
180 uint32_t field_offset
= 0;
182 dbg_printk("Apply field reloc: %u %s\n", reloc_offset
, field_name
);
184 /* Lookup event by name */
187 fields
= event_desc
->fields
;
190 nr_fields
= event_desc
->nr_fields
;
191 for (i
= 0; i
< nr_fields
; i
++) {
192 if (fields
[i
].nofilter
)
194 if (!strcmp(fields
[i
].name
, field_name
)) {
198 /* compute field offset */
199 switch (fields
[i
].type
.atype
) {
201 case atype_enum_nestable
:
202 field_offset
+= sizeof(int64_t);
204 case atype_array_nestable
:
205 if (!lttng_is_bytewise_integer(fields
[i
].type
.u
.array_nestable
.elem_type
))
207 field_offset
+= sizeof(unsigned long);
208 field_offset
+= sizeof(void *);
210 case atype_sequence_nestable
:
211 if (!lttng_is_bytewise_integer(fields
[i
].type
.u
.sequence_nestable
.elem_type
))
213 field_offset
+= sizeof(unsigned long);
214 field_offset
+= sizeof(void *);
217 field_offset
+= sizeof(void *);
219 case atype_struct_nestable
: /* Unsupported. */
220 case atype_variant_nestable
: /* Unsupported. */
228 /* Check if field offset is too large for 16-bit offset */
229 if (field_offset
> LTTNG_KERNEL_FILTER_BYTECODE_MAX_LEN
- 1)
233 op
= (struct load_op
*) &runtime
->code
[reloc_offset
];
235 switch (bytecode_op
) {
236 case BYTECODE_OP_LOAD_FIELD_REF
:
238 struct field_ref
*field_ref
;
240 field_ref
= (struct field_ref
*) op
->data
;
241 switch (field
->type
.atype
) {
243 case atype_enum_nestable
:
244 op
->op
= BYTECODE_OP_LOAD_FIELD_REF_S64
;
246 case atype_array_nestable
:
247 case atype_sequence_nestable
:
249 op
->op
= BYTECODE_OP_LOAD_FIELD_REF_USER_SEQUENCE
;
251 op
->op
= BYTECODE_OP_LOAD_FIELD_REF_SEQUENCE
;
255 op
->op
= BYTECODE_OP_LOAD_FIELD_REF_USER_STRING
;
257 op
->op
= BYTECODE_OP_LOAD_FIELD_REF_STRING
;
259 case atype_struct_nestable
: /* Unsupported. */
260 case atype_variant_nestable
: /* Unsupported. */
265 field_ref
->offset
= (uint16_t) field_offset
;
275 int apply_context_reloc(struct bytecode_runtime
*runtime
,
276 uint32_t runtime_len
,
277 uint32_t reloc_offset
,
278 const char *context_name
,
279 enum bytecode_op bytecode_op
)
282 struct lttng_ctx_field
*ctx_field
;
285 dbg_printk("Apply context reloc: %u %s\n", reloc_offset
, context_name
);
287 /* Get context index */
288 idx
= lttng_get_context_index(lttng_static_ctx
, context_name
);
292 /* Check if idx is too large for 16-bit offset */
293 if (idx
> LTTNG_KERNEL_FILTER_BYTECODE_MAX_LEN
- 1)
296 /* Get context return type */
297 ctx_field
= <tng_static_ctx
->fields
[idx
];
298 op
= (struct load_op
*) &runtime
->code
[reloc_offset
];
300 switch (bytecode_op
) {
301 case BYTECODE_OP_GET_CONTEXT_REF
:
303 struct field_ref
*field_ref
;
305 field_ref
= (struct field_ref
*) op
->data
;
306 switch (ctx_field
->event_field
.type
.atype
) {
308 case atype_enum_nestable
:
309 op
->op
= BYTECODE_OP_GET_CONTEXT_REF_S64
;
311 /* Sequence and array supported as string */
313 BUG_ON(ctx_field
->event_field
.user
);
314 op
->op
= BYTECODE_OP_GET_CONTEXT_REF_STRING
;
316 case atype_array_nestable
:
317 if (!lttng_is_bytewise_integer(ctx_field
->event_field
.type
.u
.array_nestable
.elem_type
))
319 BUG_ON(ctx_field
->event_field
.user
);
320 op
->op
= BYTECODE_OP_GET_CONTEXT_REF_STRING
;
322 case atype_sequence_nestable
:
323 if (!lttng_is_bytewise_integer(ctx_field
->event_field
.type
.u
.sequence_nestable
.elem_type
))
325 BUG_ON(ctx_field
->event_field
.user
);
326 op
->op
= BYTECODE_OP_GET_CONTEXT_REF_STRING
;
328 case atype_struct_nestable
: /* Unsupported. */
329 case atype_variant_nestable
: /* Unsupported. */
333 /* set offset to context index within channel contexts */
334 field_ref
->offset
= (uint16_t) idx
;
344 int apply_reloc(const struct lttng_event_desc
*event_desc
,
345 struct bytecode_runtime
*runtime
,
346 uint32_t runtime_len
,
347 uint32_t reloc_offset
,
352 dbg_printk("Apply reloc: %u %s\n", reloc_offset
, name
);
354 /* Ensure that the reloc is within the code */
355 if (runtime_len
- reloc_offset
< sizeof(uint16_t))
358 op
= (struct load_op
*) &runtime
->code
[reloc_offset
];
360 case BYTECODE_OP_LOAD_FIELD_REF
:
361 return apply_field_reloc(event_desc
, runtime
, runtime_len
,
362 reloc_offset
, name
, op
->op
);
363 case BYTECODE_OP_GET_CONTEXT_REF
:
364 return apply_context_reloc(runtime
, runtime_len
,
365 reloc_offset
, name
, op
->op
);
366 case BYTECODE_OP_GET_SYMBOL
:
367 case BYTECODE_OP_GET_SYMBOL_FIELD
:
369 * Will be handled by load specialize phase or
370 * dynamically by interpreter.
374 printk(KERN_WARNING
"LTTng: filter: Unknown reloc op type %u\n", op
->op
);
381 int bytecode_is_linked(struct lttng_bytecode_node
*bytecode
,
382 struct list_head
*bytecode_runtime_head
)
384 struct lttng_bytecode_runtime
*bc_runtime
;
386 list_for_each_entry(bc_runtime
, bytecode_runtime_head
, node
) {
387 if (bc_runtime
->bc
== bytecode
)
394 * Take a bytecode with reloc table and link it to an event to create a
398 int link_bytecode(const struct lttng_event_desc
*event_desc
,
399 struct lttng_ctx
*ctx
,
400 struct lttng_bytecode_node
*bytecode
,
401 struct list_head
*insert_loc
)
403 int ret
, offset
, next_offset
;
404 struct bytecode_runtime
*runtime
= NULL
;
405 size_t runtime_alloc_len
;
409 /* Bytecode already linked */
410 if (bytecode_is_linked(bytecode
, insert_loc
))
413 dbg_printk("Linking...\n");
415 /* We don't need the reloc table in the runtime */
416 runtime_alloc_len
= sizeof(*runtime
) + bytecode
->bc
.reloc_offset
;
417 runtime
= kzalloc(runtime_alloc_len
, GFP_KERNEL
);
422 runtime
->p
.bc
= bytecode
;
423 runtime
->p
.ctx
= ctx
;
424 runtime
->len
= bytecode
->bc
.reloc_offset
;
425 /* copy original bytecode */
426 memcpy(runtime
->code
, bytecode
->bc
.data
, runtime
->len
);
428 * apply relocs. Those are a uint16_t (offset in bytecode)
429 * followed by a string (field name).
431 for (offset
= bytecode
->bc
.reloc_offset
;
432 offset
< bytecode
->bc
.len
;
433 offset
= next_offset
) {
434 uint16_t reloc_offset
=
435 *(uint16_t *) &bytecode
->bc
.data
[offset
];
437 (const char *) &bytecode
->bc
.data
[offset
+ sizeof(uint16_t)];
439 ret
= apply_reloc(event_desc
, runtime
, runtime
->len
, reloc_offset
, name
);
443 next_offset
= offset
+ sizeof(uint16_t) + strlen(name
) + 1;
445 /* Validate bytecode */
446 ret
= lttng_bytecode_validate(runtime
);
450 /* Specialize bytecode */
451 ret
= lttng_bytecode_specialize(event_desc
, runtime
);
456 switch (bytecode
->type
) {
457 case LTTNG_BYTECODE_NODE_TYPE_FILTER
:
458 runtime
->p
.interpreter_funcs
.filter
= lttng_bytecode_filter_interpret
;
464 runtime
->p
.link_failed
= 0;
465 list_add_rcu(&runtime
->p
.node
, insert_loc
);
466 dbg_printk("Linking successful.\n");
471 switch (bytecode
->type
) {
472 case LTTNG_BYTECODE_NODE_TYPE_FILTER
:
473 runtime
->p
.interpreter_funcs
.filter
= lttng_bytecode_filter_interpret_false
;
478 runtime
->p
.link_failed
= 1;
479 list_add_rcu(&runtime
->p
.node
, insert_loc
);
481 dbg_printk("Linking failed.\n");
485 void lttng_bytecode_filter_sync_state(struct lttng_bytecode_runtime
*runtime
)
487 struct lttng_bytecode_node
*bc
= runtime
->bc
;
489 if (!bc
->enabler
->enabled
|| runtime
->link_failed
)
490 runtime
->interpreter_funcs
.filter
= lttng_bytecode_filter_interpret_false
;
492 runtime
->interpreter_funcs
.filter
= lttng_bytecode_filter_interpret
;
496 * Link bytecode for all enablers referenced by an event.
498 void lttng_enabler_link_bytecode(const struct lttng_event_desc
*event_desc
,
499 struct lttng_ctx
*ctx
,
500 struct list_head
*bytecode_runtime_head
,
501 struct lttng_enabler
*enabler
)
503 struct lttng_bytecode_node
*bc
;
504 struct lttng_bytecode_runtime
*runtime
;
506 /* Can only be called for events with desc attached */
507 WARN_ON_ONCE(!event_desc
);
509 /* Link each bytecode. */
510 list_for_each_entry(bc
, &enabler
->filter_bytecode_head
, node
) {
512 struct list_head
*insert_loc
;
514 list_for_each_entry(runtime
,
515 bytecode_runtime_head
, node
) {
516 if (runtime
->bc
== bc
) {
521 /* Skip bytecode already linked */
526 * Insert at specified priority (seqnum) in increasing
527 * order. If there already is a bytecode of the same priority,
528 * insert the new bytecode right after it.
530 list_for_each_entry_reverse(runtime
,
531 bytecode_runtime_head
, node
) {
532 if (runtime
->bc
->bc
.seqnum
<= bc
->bc
.seqnum
) {
534 insert_loc
= &runtime
->node
;
538 /* Add to head to list */
539 insert_loc
= bytecode_runtime_head
;
541 dbg_printk("linking bytecode\n");
542 ret
= link_bytecode(event_desc
, ctx
, bc
, insert_loc
);
544 dbg_printk("[lttng filter] warning: cannot link event bytecode\n");
550 * We own the filter_bytecode if we return success.
552 int lttng_filter_enabler_attach_bytecode(struct lttng_enabler
*enabler
,
553 struct lttng_bytecode_node
*filter_bytecode
)
555 list_add(&filter_bytecode
->node
, &enabler
->filter_bytecode_head
);
559 void lttng_free_enabler_filter_bytecode(struct lttng_enabler
*enabler
)
561 struct lttng_bytecode_node
*filter_bytecode
, *tmp
;
563 list_for_each_entry_safe(filter_bytecode
, tmp
,
564 &enabler
->filter_bytecode_head
, node
) {
565 kfree(filter_bytecode
);
569 void lttng_free_event_filter_runtime(struct lttng_event
*event
)
571 struct bytecode_runtime
*runtime
, *tmp
;
573 list_for_each_entry_safe(runtime
, tmp
,
574 &event
->filter_bytecode_runtime_head
, p
.node
) {
575 kfree(runtime
->data
);