2 * lttng-filter-validator.c
4 * LTTng UST filter bytecode validator.
6 * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include "lttng-filter.h"
32 #include <urcu/rculfhash.h>
33 #include "lttng-hash-helper.h"
34 #include "string-utils.h"
37 * Number of merge points for hash table size. Hash table initialized to
38 * that size, and we do not resize, because we do not want to trigger
39 * RCU worker thread execution: fall-back on linear traversal if number
40 * of merge points exceeds this value.
42 #define DEFAULT_NR_MERGE_POINTS 128
43 #define MIN_NR_BUCKETS 128
44 #define MAX_NR_BUCKETS 128
46 /* merge point table node */
48 struct cds_lfht_node node
;
50 /* Context at merge point */
52 unsigned long target_pc
;
55 static unsigned long lttng_hash_seed
;
56 static unsigned int lttng_hash_seed_ready
;
59 int lttng_hash_match(struct cds_lfht_node
*node
, const void *key
)
61 struct lfht_mp_node
*mp_node
=
62 caa_container_of(node
, struct lfht_mp_node
, node
);
63 unsigned long key_pc
= (unsigned long) key
;
65 if (mp_node
->target_pc
== key_pc
)
72 int merge_points_compare(const struct vstack
*stacka
,
73 const struct vstack
*stackb
)
77 if (stacka
->top
!= stackb
->top
)
79 len
= stacka
->top
+ 1;
81 for (i
= 0; i
< len
; i
++) {
82 if (stacka
->e
[i
].type
!= REG_UNKNOWN
83 && stackb
->e
[i
].type
!= REG_UNKNOWN
84 && stacka
->e
[i
].type
!= stackb
->e
[i
].type
)
91 int merge_point_add_check(struct cds_lfht
*ht
, unsigned long target_pc
,
92 const struct vstack
*stack
)
94 struct lfht_mp_node
*node
;
95 unsigned long hash
= lttng_hash_mix((const char *) target_pc
,
98 struct cds_lfht_node
*ret
;
100 dbg_printf("Filter: adding merge point at offset %lu, hash %lu\n",
102 node
= zmalloc(sizeof(struct lfht_mp_node
));
105 node
->target_pc
= target_pc
;
106 memcpy(&node
->stack
, stack
, sizeof(node
->stack
));
107 ret
= cds_lfht_add_unique(ht
, hash
, lttng_hash_match
,
108 (const char *) target_pc
, &node
->node
);
109 if (ret
!= &node
->node
) {
110 struct lfht_mp_node
*ret_mp
=
111 caa_container_of(ret
, struct lfht_mp_node
, node
);
113 /* Key already present */
114 dbg_printf("Filter: compare merge points for offset %lu, hash %lu\n",
117 if (merge_points_compare(stack
, &ret_mp
->stack
)) {
118 ERR("Merge points differ for offset %lu\n",
127 * Binary comparators use top of stack and top of stack -1.
128 * Return 0 if typing is known to match, 1 if typing is dynamic
129 * (unknown), negative error value on error.
132 int bin_op_compare_check(struct vstack
*stack
, filter_opcode_t opcode
,
135 if (unlikely(!vstack_ax(stack
) || !vstack_bx(stack
)))
138 switch (vstack_ax(stack
)->type
) {
145 switch (vstack_bx(stack
)->type
) {
153 case REG_STAR_GLOB_STRING
:
154 if (opcode
!= FILTER_OP_EQ
&& opcode
!= FILTER_OP_NE
) {
163 case REG_STAR_GLOB_STRING
:
164 switch (vstack_bx(stack
)->type
) {
171 if (opcode
!= FILTER_OP_EQ
&& opcode
!= FILTER_OP_NE
) {
175 case REG_STAR_GLOB_STRING
:
183 switch (vstack_bx(stack
)->type
) {
190 case REG_STAR_GLOB_STRING
:
204 ERR("type mismatch for '%s' binary operator\n", str
);
208 ERR("empty stack for '%s' binary operator\n", str
);
212 ERR("unknown type for '%s' binary operator\n", str
);
217 * Binary bitwise operators use top of stack and top of stack -1.
218 * Return 0 if typing is known to match, 1 if typing is dynamic
219 * (unknown), negative error value on error.
222 int bin_op_bitwise_check(struct vstack
*stack
, filter_opcode_t opcode
,
225 if (unlikely(!vstack_ax(stack
) || !vstack_bx(stack
)))
228 switch (vstack_ax(stack
)->type
) {
235 switch (vstack_bx(stack
)->type
) {
252 ERR("empty stack for '%s' binary operator\n", str
);
256 ERR("unknown type for '%s' binary operator\n", str
);
261 int validate_get_symbol(struct bytecode_runtime
*bytecode
,
262 const struct get_symbol
*sym
)
264 const char *str
, *str_limit
;
267 if (sym
->offset
>= bytecode
->p
.bc
->bc
.len
- bytecode
->p
.bc
->bc
.reloc_offset
)
270 str
= bytecode
->p
.bc
->bc
.data
+ bytecode
->p
.bc
->bc
.reloc_offset
+ sym
->offset
;
271 str_limit
= bytecode
->p
.bc
->bc
.data
+ bytecode
->p
.bc
->bc
.len
;
272 len_limit
= str_limit
- str
;
273 if (strnlen(str
, len_limit
) == len_limit
)
279 * Validate bytecode range overflow within the validation pass.
280 * Called for each instruction encountered.
283 int bytecode_validate_overflow(struct bytecode_runtime
*bytecode
,
284 char *start_pc
, char *pc
)
288 switch (*(filter_opcode_t
*) pc
) {
289 case FILTER_OP_UNKNOWN
:
292 ERR("unknown bytecode op %u\n",
293 (unsigned int) *(filter_opcode_t
*) pc
);
298 case FILTER_OP_RETURN
:
300 if (unlikely(pc
+ sizeof(struct return_op
)
301 > start_pc
+ bytecode
->len
)) {
312 case FILTER_OP_MINUS
:
313 case FILTER_OP_RSHIFT
:
314 case FILTER_OP_LSHIFT
:
316 ERR("unsupported bytecode op %u\n",
317 (unsigned int) *(filter_opcode_t
*) pc
);
328 case FILTER_OP_EQ_STRING
:
329 case FILTER_OP_NE_STRING
:
330 case FILTER_OP_GT_STRING
:
331 case FILTER_OP_LT_STRING
:
332 case FILTER_OP_GE_STRING
:
333 case FILTER_OP_LE_STRING
:
334 case FILTER_OP_EQ_STAR_GLOB_STRING
:
335 case FILTER_OP_NE_STAR_GLOB_STRING
:
336 case FILTER_OP_EQ_S64
:
337 case FILTER_OP_NE_S64
:
338 case FILTER_OP_GT_S64
:
339 case FILTER_OP_LT_S64
:
340 case FILTER_OP_GE_S64
:
341 case FILTER_OP_LE_S64
:
342 case FILTER_OP_EQ_DOUBLE
:
343 case FILTER_OP_NE_DOUBLE
:
344 case FILTER_OP_GT_DOUBLE
:
345 case FILTER_OP_LT_DOUBLE
:
346 case FILTER_OP_GE_DOUBLE
:
347 case FILTER_OP_LE_DOUBLE
:
348 case FILTER_OP_EQ_DOUBLE_S64
:
349 case FILTER_OP_NE_DOUBLE_S64
:
350 case FILTER_OP_GT_DOUBLE_S64
:
351 case FILTER_OP_LT_DOUBLE_S64
:
352 case FILTER_OP_GE_DOUBLE_S64
:
353 case FILTER_OP_LE_DOUBLE_S64
:
354 case FILTER_OP_EQ_S64_DOUBLE
:
355 case FILTER_OP_NE_S64_DOUBLE
:
356 case FILTER_OP_GT_S64_DOUBLE
:
357 case FILTER_OP_LT_S64_DOUBLE
:
358 case FILTER_OP_GE_S64_DOUBLE
:
359 case FILTER_OP_LE_S64_DOUBLE
:
360 case FILTER_OP_BIT_AND
:
361 case FILTER_OP_BIT_OR
:
362 case FILTER_OP_BIT_XOR
:
364 if (unlikely(pc
+ sizeof(struct binary_op
)
365 > start_pc
+ bytecode
->len
)) {
372 case FILTER_OP_UNARY_PLUS
:
373 case FILTER_OP_UNARY_MINUS
:
374 case FILTER_OP_UNARY_NOT
:
375 case FILTER_OP_UNARY_PLUS_S64
:
376 case FILTER_OP_UNARY_MINUS_S64
:
377 case FILTER_OP_UNARY_NOT_S64
:
378 case FILTER_OP_UNARY_PLUS_DOUBLE
:
379 case FILTER_OP_UNARY_MINUS_DOUBLE
:
380 case FILTER_OP_UNARY_NOT_DOUBLE
:
382 if (unlikely(pc
+ sizeof(struct unary_op
)
383 > start_pc
+ bytecode
->len
)) {
393 if (unlikely(pc
+ sizeof(struct logical_op
)
394 > start_pc
+ bytecode
->len
)) {
401 case FILTER_OP_LOAD_FIELD_REF
:
403 ERR("Unknown field ref type\n");
408 /* get context ref */
409 case FILTER_OP_GET_CONTEXT_REF
:
410 case FILTER_OP_LOAD_FIELD_REF_STRING
:
411 case FILTER_OP_LOAD_FIELD_REF_SEQUENCE
:
412 case FILTER_OP_LOAD_FIELD_REF_S64
:
413 case FILTER_OP_LOAD_FIELD_REF_DOUBLE
:
414 case FILTER_OP_GET_CONTEXT_REF_STRING
:
415 case FILTER_OP_GET_CONTEXT_REF_S64
:
416 case FILTER_OP_GET_CONTEXT_REF_DOUBLE
:
418 if (unlikely(pc
+ sizeof(struct load_op
) + sizeof(struct field_ref
)
419 > start_pc
+ bytecode
->len
)) {
425 /* load from immediate operand */
426 case FILTER_OP_LOAD_STRING
:
427 case FILTER_OP_LOAD_STAR_GLOB_STRING
:
429 struct load_op
*insn
= (struct load_op
*) pc
;
430 uint32_t str_len
, maxlen
;
432 if (unlikely(pc
+ sizeof(struct load_op
)
433 > start_pc
+ bytecode
->len
)) {
438 maxlen
= start_pc
+ bytecode
->len
- pc
- sizeof(struct load_op
);
439 str_len
= strnlen(insn
->data
, maxlen
);
440 if (unlikely(str_len
>= maxlen
)) {
441 /* Final '\0' not found within range */
447 case FILTER_OP_LOAD_S64
:
449 if (unlikely(pc
+ sizeof(struct load_op
) + sizeof(struct literal_numeric
)
450 > start_pc
+ bytecode
->len
)) {
456 case FILTER_OP_LOAD_DOUBLE
:
458 if (unlikely(pc
+ sizeof(struct load_op
) + sizeof(struct literal_double
)
459 > start_pc
+ bytecode
->len
)) {
465 case FILTER_OP_CAST_TO_S64
:
466 case FILTER_OP_CAST_DOUBLE_TO_S64
:
467 case FILTER_OP_CAST_NOP
:
469 if (unlikely(pc
+ sizeof(struct cast_op
)
470 > start_pc
+ bytecode
->len
)) {
477 * Instructions for recursive traversal through composed types.
479 case FILTER_OP_GET_CONTEXT_ROOT
:
480 case FILTER_OP_GET_APP_CONTEXT_ROOT
:
481 case FILTER_OP_GET_PAYLOAD_ROOT
:
482 case FILTER_OP_LOAD_FIELD
:
483 case FILTER_OP_LOAD_FIELD_S8
:
484 case FILTER_OP_LOAD_FIELD_S16
:
485 case FILTER_OP_LOAD_FIELD_S32
:
486 case FILTER_OP_LOAD_FIELD_S64
:
487 case FILTER_OP_LOAD_FIELD_U8
:
488 case FILTER_OP_LOAD_FIELD_U16
:
489 case FILTER_OP_LOAD_FIELD_U32
:
490 case FILTER_OP_LOAD_FIELD_U64
:
491 case FILTER_OP_LOAD_FIELD_STRING
:
492 case FILTER_OP_LOAD_FIELD_SEQUENCE
:
493 case FILTER_OP_LOAD_FIELD_DOUBLE
:
494 if (unlikely(pc
+ sizeof(struct load_op
)
495 > start_pc
+ bytecode
->len
)) {
500 case FILTER_OP_GET_SYMBOL
:
502 struct load_op
*insn
= (struct load_op
*) pc
;
503 struct get_symbol
*sym
= (struct get_symbol
*) insn
->data
;
505 if (unlikely(pc
+ sizeof(struct load_op
) + sizeof(struct get_symbol
)
506 > start_pc
+ bytecode
->len
)) {
509 ret
= validate_get_symbol(bytecode
, sym
);
513 case FILTER_OP_GET_SYMBOL_FIELD
:
514 ERR("Unexpected get symbol field");
518 case FILTER_OP_GET_INDEX_U16
:
519 if (unlikely(pc
+ sizeof(struct load_op
) + sizeof(struct get_index_u16
)
520 > start_pc
+ bytecode
->len
)) {
525 case FILTER_OP_GET_INDEX_U64
:
526 if (unlikely(pc
+ sizeof(struct load_op
) + sizeof(struct get_index_u64
)
527 > start_pc
+ bytecode
->len
)) {
537 unsigned long delete_all_nodes(struct cds_lfht
*ht
)
539 struct cds_lfht_iter iter
;
540 struct lfht_mp_node
*node
;
541 unsigned long nr_nodes
= 0;
543 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
546 ret
= cds_lfht_del(ht
, cds_lfht_iter_get_node(&iter
));
548 /* note: this hash table is never used concurrently */
561 int validate_instruction_context(struct bytecode_runtime
*bytecode
,
562 struct vstack
*stack
,
567 const filter_opcode_t opcode
= *(filter_opcode_t
*) pc
;
570 case FILTER_OP_UNKNOWN
:
573 ERR("unknown bytecode op %u\n",
574 (unsigned int) *(filter_opcode_t
*) pc
);
579 case FILTER_OP_RETURN
:
589 case FILTER_OP_MINUS
:
590 case FILTER_OP_RSHIFT
:
591 case FILTER_OP_LSHIFT
:
593 ERR("unsupported bytecode op %u\n",
594 (unsigned int) opcode
);
601 ret
= bin_op_compare_check(stack
, opcode
, "==");
608 ret
= bin_op_compare_check(stack
, opcode
, "!=");
615 ret
= bin_op_compare_check(stack
, opcode
, ">");
622 ret
= bin_op_compare_check(stack
, opcode
, "<");
629 ret
= bin_op_compare_check(stack
, opcode
, ">=");
636 ret
= bin_op_compare_check(stack
, opcode
, "<=");
642 case FILTER_OP_EQ_STRING
:
643 case FILTER_OP_NE_STRING
:
644 case FILTER_OP_GT_STRING
:
645 case FILTER_OP_LT_STRING
:
646 case FILTER_OP_GE_STRING
:
647 case FILTER_OP_LE_STRING
:
649 if (!vstack_ax(stack
) || !vstack_bx(stack
)) {
650 ERR("Empty stack\n");
654 if (vstack_ax(stack
)->type
!= REG_STRING
655 || vstack_bx(stack
)->type
!= REG_STRING
) {
656 ERR("Unexpected register type for string comparator\n");
663 case FILTER_OP_EQ_STAR_GLOB_STRING
:
664 case FILTER_OP_NE_STAR_GLOB_STRING
:
666 if (!vstack_ax(stack
) || !vstack_bx(stack
)) {
667 ERR("Empty stack\n");
671 if (vstack_ax(stack
)->type
!= REG_STAR_GLOB_STRING
672 && vstack_bx(stack
)->type
!= REG_STAR_GLOB_STRING
) {
673 ERR("Unexpected register type for globbing pattern comparator\n");
680 case FILTER_OP_EQ_S64
:
681 case FILTER_OP_NE_S64
:
682 case FILTER_OP_GT_S64
:
683 case FILTER_OP_LT_S64
:
684 case FILTER_OP_GE_S64
:
685 case FILTER_OP_LE_S64
:
687 if (!vstack_ax(stack
) || !vstack_bx(stack
)) {
688 ERR("Empty stack\n");
692 if (vstack_ax(stack
)->type
!= REG_S64
693 || vstack_bx(stack
)->type
!= REG_S64
) {
694 ERR("Unexpected register type for s64 comparator\n");
701 case FILTER_OP_EQ_DOUBLE
:
702 case FILTER_OP_NE_DOUBLE
:
703 case FILTER_OP_GT_DOUBLE
:
704 case FILTER_OP_LT_DOUBLE
:
705 case FILTER_OP_GE_DOUBLE
:
706 case FILTER_OP_LE_DOUBLE
:
708 if (!vstack_ax(stack
) || !vstack_bx(stack
)) {
709 ERR("Empty stack\n");
713 if (vstack_ax(stack
)->type
!= REG_DOUBLE
&& vstack_bx(stack
)->type
!= REG_DOUBLE
) {
714 ERR("Double operator should have two double registers\n");
721 case FILTER_OP_EQ_DOUBLE_S64
:
722 case FILTER_OP_NE_DOUBLE_S64
:
723 case FILTER_OP_GT_DOUBLE_S64
:
724 case FILTER_OP_LT_DOUBLE_S64
:
725 case FILTER_OP_GE_DOUBLE_S64
:
726 case FILTER_OP_LE_DOUBLE_S64
:
728 if (!vstack_ax(stack
) || !vstack_bx(stack
)) {
729 ERR("Empty stack\n");
733 if (vstack_ax(stack
)->type
!= REG_S64
&& vstack_bx(stack
)->type
!= REG_DOUBLE
) {
734 ERR("Double-S64 operator has unexpected register types\n");
741 case FILTER_OP_EQ_S64_DOUBLE
:
742 case FILTER_OP_NE_S64_DOUBLE
:
743 case FILTER_OP_GT_S64_DOUBLE
:
744 case FILTER_OP_LT_S64_DOUBLE
:
745 case FILTER_OP_GE_S64_DOUBLE
:
746 case FILTER_OP_LE_S64_DOUBLE
:
748 if (!vstack_ax(stack
) || !vstack_bx(stack
)) {
749 ERR("Empty stack\n");
753 if (vstack_ax(stack
)->type
!= REG_DOUBLE
&& vstack_bx(stack
)->type
!= REG_S64
) {
754 ERR("S64-Double operator has unexpected register types\n");
761 case FILTER_OP_BIT_AND
:
762 ret
= bin_op_bitwise_check(stack
, opcode
, "&");
766 case FILTER_OP_BIT_OR
:
767 ret
= bin_op_bitwise_check(stack
, opcode
, "|");
771 case FILTER_OP_BIT_XOR
:
772 ret
= bin_op_bitwise_check(stack
, opcode
, "^");
778 case FILTER_OP_UNARY_PLUS
:
779 case FILTER_OP_UNARY_MINUS
:
780 case FILTER_OP_UNARY_NOT
:
782 if (!vstack_ax(stack
)) {
783 ERR("Empty stack\n");
787 switch (vstack_ax(stack
)->type
) {
789 ERR("unknown register type\n");
794 case REG_STAR_GLOB_STRING
:
795 ERR("Unary op can only be applied to numeric or floating point registers\n");
808 case FILTER_OP_UNARY_PLUS_S64
:
809 case FILTER_OP_UNARY_MINUS_S64
:
810 case FILTER_OP_UNARY_NOT_S64
:
812 if (!vstack_ax(stack
)) {
813 ERR("Empty stack\n");
817 if (vstack_ax(stack
)->type
!= REG_S64
) {
818 ERR("Invalid register type\n");
825 case FILTER_OP_UNARY_PLUS_DOUBLE
:
826 case FILTER_OP_UNARY_MINUS_DOUBLE
:
827 case FILTER_OP_UNARY_NOT_DOUBLE
:
829 if (!vstack_ax(stack
)) {
830 ERR("Empty stack\n");
834 if (vstack_ax(stack
)->type
!= REG_DOUBLE
) {
835 ERR("Invalid register type\n");
846 struct logical_op
*insn
= (struct logical_op
*) pc
;
848 if (!vstack_ax(stack
)) {
849 ERR("Empty stack\n");
853 if (vstack_ax(stack
)->type
!= REG_S64
854 && vstack_ax(stack
)->type
!= REG_UNKNOWN
) {
855 ERR("Logical comparator expects S64 or dynamic register\n");
860 dbg_printf("Validate jumping to bytecode offset %u\n",
861 (unsigned int) insn
->skip_offset
);
862 if (unlikely(start_pc
+ insn
->skip_offset
<= pc
)) {
863 ERR("Loops are not allowed in bytecode\n");
871 case FILTER_OP_LOAD_FIELD_REF
:
873 ERR("Unknown field ref type\n");
877 case FILTER_OP_LOAD_FIELD_REF_STRING
:
878 case FILTER_OP_LOAD_FIELD_REF_SEQUENCE
:
880 struct load_op
*insn
= (struct load_op
*) pc
;
881 struct field_ref
*ref
= (struct field_ref
*) insn
->data
;
883 dbg_printf("Validate load field ref offset %u type string\n",
887 case FILTER_OP_LOAD_FIELD_REF_S64
:
889 struct load_op
*insn
= (struct load_op
*) pc
;
890 struct field_ref
*ref
= (struct field_ref
*) insn
->data
;
892 dbg_printf("Validate load field ref offset %u type s64\n",
896 case FILTER_OP_LOAD_FIELD_REF_DOUBLE
:
898 struct load_op
*insn
= (struct load_op
*) pc
;
899 struct field_ref
*ref
= (struct field_ref
*) insn
->data
;
901 dbg_printf("Validate load field ref offset %u type double\n",
906 /* load from immediate operand */
907 case FILTER_OP_LOAD_STRING
:
908 case FILTER_OP_LOAD_STAR_GLOB_STRING
:
913 case FILTER_OP_LOAD_S64
:
918 case FILTER_OP_LOAD_DOUBLE
:
923 case FILTER_OP_CAST_TO_S64
:
924 case FILTER_OP_CAST_DOUBLE_TO_S64
:
926 struct cast_op
*insn
= (struct cast_op
*) pc
;
928 if (!vstack_ax(stack
)) {
929 ERR("Empty stack\n");
933 switch (vstack_ax(stack
)->type
) {
935 ERR("unknown register type\n");
940 case REG_STAR_GLOB_STRING
:
941 ERR("Cast op can only be applied to numeric or floating point registers\n");
951 if (insn
->op
== FILTER_OP_CAST_DOUBLE_TO_S64
) {
952 if (vstack_ax(stack
)->type
!= REG_DOUBLE
) {
953 ERR("Cast expects double\n");
960 case FILTER_OP_CAST_NOP
:
965 /* get context ref */
966 case FILTER_OP_GET_CONTEXT_REF
:
968 struct load_op
*insn
= (struct load_op
*) pc
;
969 struct field_ref
*ref
= (struct field_ref
*) insn
->data
;
971 dbg_printf("Validate get context ref offset %u type dynamic\n",
975 case FILTER_OP_GET_CONTEXT_REF_STRING
:
977 struct load_op
*insn
= (struct load_op
*) pc
;
978 struct field_ref
*ref
= (struct field_ref
*) insn
->data
;
980 dbg_printf("Validate get context ref offset %u type string\n",
984 case FILTER_OP_GET_CONTEXT_REF_S64
:
986 struct load_op
*insn
= (struct load_op
*) pc
;
987 struct field_ref
*ref
= (struct field_ref
*) insn
->data
;
989 dbg_printf("Validate get context ref offset %u type s64\n",
993 case FILTER_OP_GET_CONTEXT_REF_DOUBLE
:
995 struct load_op
*insn
= (struct load_op
*) pc
;
996 struct field_ref
*ref
= (struct field_ref
*) insn
->data
;
998 dbg_printf("Validate get context ref offset %u type double\n",
1004 * Instructions for recursive traversal through composed types.
1006 case FILTER_OP_GET_CONTEXT_ROOT
:
1008 dbg_printf("Validate get context root\n");
1011 case FILTER_OP_GET_APP_CONTEXT_ROOT
:
1013 dbg_printf("Validate get app context root\n");
1016 case FILTER_OP_GET_PAYLOAD_ROOT
:
1018 dbg_printf("Validate get payload root\n");
1021 case FILTER_OP_LOAD_FIELD
:
1024 * We tolerate that field type is unknown at validation,
1025 * because we are performing the load specialization in
1026 * a phase after validation.
1028 dbg_printf("Validate load field\n");
1031 case FILTER_OP_LOAD_FIELD_S8
:
1033 dbg_printf("Validate load field s8\n");
1036 case FILTER_OP_LOAD_FIELD_S16
:
1038 dbg_printf("Validate load field s16\n");
1041 case FILTER_OP_LOAD_FIELD_S32
:
1043 dbg_printf("Validate load field s32\n");
1046 case FILTER_OP_LOAD_FIELD_S64
:
1048 dbg_printf("Validate load field s64\n");
1051 case FILTER_OP_LOAD_FIELD_U8
:
1053 dbg_printf("Validate load field u8\n");
1056 case FILTER_OP_LOAD_FIELD_U16
:
1058 dbg_printf("Validate load field u16\n");
1061 case FILTER_OP_LOAD_FIELD_U32
:
1063 dbg_printf("Validate load field u32\n");
1066 case FILTER_OP_LOAD_FIELD_U64
:
1068 dbg_printf("Validate load field u64\n");
1071 case FILTER_OP_LOAD_FIELD_STRING
:
1073 dbg_printf("Validate load field string\n");
1076 case FILTER_OP_LOAD_FIELD_SEQUENCE
:
1078 dbg_printf("Validate load field sequence\n");
1081 case FILTER_OP_LOAD_FIELD_DOUBLE
:
1083 dbg_printf("Validate load field double\n");
1087 case FILTER_OP_GET_SYMBOL
:
1089 struct load_op
*insn
= (struct load_op
*) pc
;
1090 struct get_symbol
*sym
= (struct get_symbol
*) insn
->data
;
1092 dbg_printf("Validate get symbol offset %u\n", sym
->offset
);
1096 case FILTER_OP_GET_SYMBOL_FIELD
:
1098 struct load_op
*insn
= (struct load_op
*) pc
;
1099 struct get_symbol
*sym
= (struct get_symbol
*) insn
->data
;
1101 dbg_printf("Validate get symbol field offset %u\n", sym
->offset
);
1105 case FILTER_OP_GET_INDEX_U16
:
1107 struct load_op
*insn
= (struct load_op
*) pc
;
1108 struct get_index_u16
*get_index
= (struct get_index_u16
*) insn
->data
;
1110 dbg_printf("Validate get index u16 index %u\n", get_index
->index
);
1114 case FILTER_OP_GET_INDEX_U64
:
1116 struct load_op
*insn
= (struct load_op
*) pc
;
1117 struct get_index_u64
*get_index
= (struct get_index_u64
*) insn
->data
;
1119 dbg_printf("Validate get index u64 index %" PRIu64
"\n", get_index
->index
);
1133 int validate_instruction_all_contexts(struct bytecode_runtime
*bytecode
,
1134 struct cds_lfht
*merge_points
,
1135 struct vstack
*stack
,
1140 unsigned long target_pc
= pc
- start_pc
;
1141 struct cds_lfht_iter iter
;
1142 struct cds_lfht_node
*node
;
1143 struct lfht_mp_node
*mp_node
;
1146 /* Validate the context resulting from the previous instruction */
1147 ret
= validate_instruction_context(bytecode
, stack
, start_pc
, pc
);
1151 /* Validate merge points */
1152 hash
= lttng_hash_mix((const char *) target_pc
, sizeof(target_pc
),
1154 cds_lfht_lookup(merge_points
, hash
, lttng_hash_match
,
1155 (const char *) target_pc
, &iter
);
1156 node
= cds_lfht_iter_get_node(&iter
);
1158 mp_node
= caa_container_of(node
, struct lfht_mp_node
, node
);
1160 dbg_printf("Filter: validate merge point at offset %lu\n",
1162 if (merge_points_compare(stack
, &mp_node
->stack
)) {
1163 ERR("Merge points differ for offset %lu\n",
1167 /* Once validated, we can remove the merge point */
1168 dbg_printf("Filter: remove merge point at offset %lu\n",
1170 ret
= cds_lfht_del(merge_points
, node
);
1178 * >0: going to next insn.
1179 * 0: success, stop iteration.
1183 int exec_insn(struct bytecode_runtime
*bytecode
,
1184 struct cds_lfht
*merge_points
,
1185 struct vstack
*stack
,
1190 char *next_pc
= *_next_pc
;
1192 switch (*(filter_opcode_t
*) pc
) {
1193 case FILTER_OP_UNKNOWN
:
1196 ERR("unknown bytecode op %u\n",
1197 (unsigned int) *(filter_opcode_t
*) pc
);
1202 case FILTER_OP_RETURN
:
1204 if (!vstack_ax(stack
)) {
1205 ERR("Empty stack\n");
1209 switch (vstack_ax(stack
)->type
) {
1214 ERR("Unexpected register type %d at end of bytecode\n",
1215 (int) vstack_ax(stack
)->type
);
1228 case FILTER_OP_PLUS
:
1229 case FILTER_OP_MINUS
:
1230 case FILTER_OP_RSHIFT
:
1231 case FILTER_OP_LSHIFT
:
1233 ERR("unsupported bytecode op %u\n",
1234 (unsigned int) *(filter_opcode_t
*) pc
);
1245 case FILTER_OP_EQ_STRING
:
1246 case FILTER_OP_NE_STRING
:
1247 case FILTER_OP_GT_STRING
:
1248 case FILTER_OP_LT_STRING
:
1249 case FILTER_OP_GE_STRING
:
1250 case FILTER_OP_LE_STRING
:
1251 case FILTER_OP_EQ_STAR_GLOB_STRING
:
1252 case FILTER_OP_NE_STAR_GLOB_STRING
:
1253 case FILTER_OP_EQ_S64
:
1254 case FILTER_OP_NE_S64
:
1255 case FILTER_OP_GT_S64
:
1256 case FILTER_OP_LT_S64
:
1257 case FILTER_OP_GE_S64
:
1258 case FILTER_OP_LE_S64
:
1259 case FILTER_OP_EQ_DOUBLE
:
1260 case FILTER_OP_NE_DOUBLE
:
1261 case FILTER_OP_GT_DOUBLE
:
1262 case FILTER_OP_LT_DOUBLE
:
1263 case FILTER_OP_GE_DOUBLE
:
1264 case FILTER_OP_LE_DOUBLE
:
1265 case FILTER_OP_EQ_DOUBLE_S64
:
1266 case FILTER_OP_NE_DOUBLE_S64
:
1267 case FILTER_OP_GT_DOUBLE_S64
:
1268 case FILTER_OP_LT_DOUBLE_S64
:
1269 case FILTER_OP_GE_DOUBLE_S64
:
1270 case FILTER_OP_LE_DOUBLE_S64
:
1271 case FILTER_OP_EQ_S64_DOUBLE
:
1272 case FILTER_OP_NE_S64_DOUBLE
:
1273 case FILTER_OP_GT_S64_DOUBLE
:
1274 case FILTER_OP_LT_S64_DOUBLE
:
1275 case FILTER_OP_GE_S64_DOUBLE
:
1276 case FILTER_OP_LE_S64_DOUBLE
:
1277 case FILTER_OP_BIT_AND
:
1278 case FILTER_OP_BIT_OR
:
1279 case FILTER_OP_BIT_XOR
:
1282 if (vstack_pop(stack
)) {
1286 if (!vstack_ax(stack
)) {
1287 ERR("Empty stack\n");
1291 switch (vstack_ax(stack
)->type
) {
1295 case REG_STAR_GLOB_STRING
:
1299 ERR("Unexpected register type %d for operation\n",
1300 (int) vstack_ax(stack
)->type
);
1305 vstack_ax(stack
)->type
= REG_S64
;
1306 next_pc
+= sizeof(struct binary_op
);
1311 case FILTER_OP_UNARY_PLUS
:
1312 case FILTER_OP_UNARY_MINUS
:
1315 if (!vstack_ax(stack
)) {
1316 ERR("Empty stack\n");
1320 switch (vstack_ax(stack
)->type
) {
1326 ERR("Unexpected register type %d for operation\n",
1327 (int) vstack_ax(stack
)->type
);
1331 vstack_ax(stack
)->type
= REG_UNKNOWN
;
1332 next_pc
+= sizeof(struct unary_op
);
1336 case FILTER_OP_UNARY_PLUS_S64
:
1337 case FILTER_OP_UNARY_MINUS_S64
:
1338 case FILTER_OP_UNARY_NOT_S64
:
1341 if (!vstack_ax(stack
)) {
1342 ERR("Empty stack\n");
1346 switch (vstack_ax(stack
)->type
) {
1350 ERR("Unexpected register type %d for operation\n",
1351 (int) vstack_ax(stack
)->type
);
1356 vstack_ax(stack
)->type
= REG_S64
;
1357 next_pc
+= sizeof(struct unary_op
);
1361 case FILTER_OP_UNARY_NOT
:
1364 if (!vstack_ax(stack
)) {
1365 ERR("Empty stack\n");
1369 switch (vstack_ax(stack
)->type
) {
1375 ERR("Unexpected register type %d for operation\n",
1376 (int) vstack_ax(stack
)->type
);
1381 vstack_ax(stack
)->type
= REG_S64
;
1382 next_pc
+= sizeof(struct unary_op
);
1386 case FILTER_OP_UNARY_NOT_DOUBLE
:
1389 if (!vstack_ax(stack
)) {
1390 ERR("Empty stack\n");
1394 switch (vstack_ax(stack
)->type
) {
1398 ERR("Incorrect register type %d for operation\n",
1399 (int) vstack_ax(stack
)->type
);
1404 vstack_ax(stack
)->type
= REG_S64
;
1405 next_pc
+= sizeof(struct unary_op
);
1409 case FILTER_OP_UNARY_PLUS_DOUBLE
:
1410 case FILTER_OP_UNARY_MINUS_DOUBLE
:
1413 if (!vstack_ax(stack
)) {
1414 ERR("Empty stack\n");
1418 switch (vstack_ax(stack
)->type
) {
1422 ERR("Incorrect register type %d for operation\n",
1423 (int) vstack_ax(stack
)->type
);
1428 vstack_ax(stack
)->type
= REG_DOUBLE
;
1429 next_pc
+= sizeof(struct unary_op
);
1437 struct logical_op
*insn
= (struct logical_op
*) pc
;
1440 /* Add merge point to table */
1441 merge_ret
= merge_point_add_check(merge_points
,
1442 insn
->skip_offset
, stack
);
1448 if (!vstack_ax(stack
)) {
1449 ERR("Empty stack\n");
1453 /* There is always a cast-to-s64 operation before a or/and op. */
1454 switch (vstack_ax(stack
)->type
) {
1458 ERR("Incorrect register type %d for operation\n",
1459 (int) vstack_ax(stack
)->type
);
1464 /* Continue to next instruction */
1465 /* Pop 1 when jump not taken */
1466 if (vstack_pop(stack
)) {
1470 next_pc
+= sizeof(struct logical_op
);
1474 /* load field ref */
1475 case FILTER_OP_LOAD_FIELD_REF
:
1477 ERR("Unknown field ref type\n");
1481 /* get context ref */
1482 case FILTER_OP_GET_CONTEXT_REF
:
1484 if (vstack_push(stack
)) {
1488 vstack_ax(stack
)->type
= REG_UNKNOWN
;
1489 next_pc
+= sizeof(struct load_op
) + sizeof(struct field_ref
);
1492 case FILTER_OP_LOAD_FIELD_REF_STRING
:
1493 case FILTER_OP_LOAD_FIELD_REF_SEQUENCE
:
1494 case FILTER_OP_GET_CONTEXT_REF_STRING
:
1496 if (vstack_push(stack
)) {
1500 vstack_ax(stack
)->type
= REG_STRING
;
1501 next_pc
+= sizeof(struct load_op
) + sizeof(struct field_ref
);
1504 case FILTER_OP_LOAD_FIELD_REF_S64
:
1505 case FILTER_OP_GET_CONTEXT_REF_S64
:
1507 if (vstack_push(stack
)) {
1511 vstack_ax(stack
)->type
= REG_S64
;
1512 next_pc
+= sizeof(struct load_op
) + sizeof(struct field_ref
);
1515 case FILTER_OP_LOAD_FIELD_REF_DOUBLE
:
1516 case FILTER_OP_GET_CONTEXT_REF_DOUBLE
:
1518 if (vstack_push(stack
)) {
1522 vstack_ax(stack
)->type
= REG_DOUBLE
;
1523 next_pc
+= sizeof(struct load_op
) + sizeof(struct field_ref
);
1527 /* load from immediate operand */
1528 case FILTER_OP_LOAD_STRING
:
1530 struct load_op
*insn
= (struct load_op
*) pc
;
1532 if (vstack_push(stack
)) {
1536 vstack_ax(stack
)->type
= REG_STRING
;
1537 next_pc
+= sizeof(struct load_op
) + strlen(insn
->data
) + 1;
1541 case FILTER_OP_LOAD_STAR_GLOB_STRING
:
1543 struct load_op
*insn
= (struct load_op
*) pc
;
1545 if (vstack_push(stack
)) {
1549 vstack_ax(stack
)->type
= REG_STAR_GLOB_STRING
;
1550 next_pc
+= sizeof(struct load_op
) + strlen(insn
->data
) + 1;
1554 case FILTER_OP_LOAD_S64
:
1556 if (vstack_push(stack
)) {
1560 vstack_ax(stack
)->type
= REG_S64
;
1561 next_pc
+= sizeof(struct load_op
)
1562 + sizeof(struct literal_numeric
);
1566 case FILTER_OP_LOAD_DOUBLE
:
1568 if (vstack_push(stack
)) {
1572 vstack_ax(stack
)->type
= REG_DOUBLE
;
1573 next_pc
+= sizeof(struct load_op
)
1574 + sizeof(struct literal_double
);
1578 case FILTER_OP_CAST_TO_S64
:
1579 case FILTER_OP_CAST_DOUBLE_TO_S64
:
1582 if (!vstack_ax(stack
)) {
1583 ERR("Empty stack\n");
1587 switch (vstack_ax(stack
)->type
) {
1593 ERR("Incorrect register type %d for cast\n",
1594 (int) vstack_ax(stack
)->type
);
1598 vstack_ax(stack
)->type
= REG_S64
;
1599 next_pc
+= sizeof(struct cast_op
);
1602 case FILTER_OP_CAST_NOP
:
1604 next_pc
+= sizeof(struct cast_op
);
1609 * Instructions for recursive traversal through composed types.
1611 case FILTER_OP_GET_CONTEXT_ROOT
:
1612 case FILTER_OP_GET_APP_CONTEXT_ROOT
:
1613 case FILTER_OP_GET_PAYLOAD_ROOT
:
1615 if (vstack_push(stack
)) {
1619 vstack_ax(stack
)->type
= REG_PTR
;
1620 next_pc
+= sizeof(struct load_op
);
1624 case FILTER_OP_LOAD_FIELD
:
1627 if (!vstack_ax(stack
)) {
1628 ERR("Empty stack\n");
1632 if (vstack_ax(stack
)->type
!= REG_PTR
) {
1633 ERR("Expecting pointer on top of stack\n");
1637 vstack_ax(stack
)->type
= REG_UNKNOWN
;
1638 next_pc
+= sizeof(struct load_op
);
1642 case FILTER_OP_LOAD_FIELD_S8
:
1643 case FILTER_OP_LOAD_FIELD_S16
:
1644 case FILTER_OP_LOAD_FIELD_S32
:
1645 case FILTER_OP_LOAD_FIELD_S64
:
1646 case FILTER_OP_LOAD_FIELD_U8
:
1647 case FILTER_OP_LOAD_FIELD_U16
:
1648 case FILTER_OP_LOAD_FIELD_U32
:
1649 case FILTER_OP_LOAD_FIELD_U64
:
1652 if (!vstack_ax(stack
)) {
1653 ERR("Empty stack\n");
1657 if (vstack_ax(stack
)->type
!= REG_PTR
) {
1658 ERR("Expecting pointer on top of stack\n");
1662 vstack_ax(stack
)->type
= REG_S64
;
1663 next_pc
+= sizeof(struct load_op
);
1667 case FILTER_OP_LOAD_FIELD_STRING
:
1668 case FILTER_OP_LOAD_FIELD_SEQUENCE
:
1671 if (!vstack_ax(stack
)) {
1672 ERR("Empty stack\n");
1676 if (vstack_ax(stack
)->type
!= REG_PTR
) {
1677 ERR("Expecting pointer on top of stack\n");
1681 vstack_ax(stack
)->type
= REG_STRING
;
1682 next_pc
+= sizeof(struct load_op
);
1686 case FILTER_OP_LOAD_FIELD_DOUBLE
:
1689 if (!vstack_ax(stack
)) {
1690 ERR("Empty stack\n");
1694 if (vstack_ax(stack
)->type
!= REG_PTR
) {
1695 ERR("Expecting pointer on top of stack\n");
1699 vstack_ax(stack
)->type
= REG_DOUBLE
;
1700 next_pc
+= sizeof(struct load_op
);
1704 case FILTER_OP_GET_SYMBOL
:
1705 case FILTER_OP_GET_SYMBOL_FIELD
:
1708 if (!vstack_ax(stack
)) {
1709 ERR("Empty stack\n");
1713 if (vstack_ax(stack
)->type
!= REG_PTR
) {
1714 ERR("Expecting pointer on top of stack\n");
1718 next_pc
+= sizeof(struct load_op
) + sizeof(struct get_symbol
);
1722 case FILTER_OP_GET_INDEX_U16
:
1725 if (!vstack_ax(stack
)) {
1726 ERR("Empty stack\n");
1730 if (vstack_ax(stack
)->type
!= REG_PTR
) {
1731 ERR("Expecting pointer on top of stack\n");
1735 next_pc
+= sizeof(struct load_op
) + sizeof(struct get_index_u16
);
1739 case FILTER_OP_GET_INDEX_U64
:
1742 if (!vstack_ax(stack
)) {
1743 ERR("Empty stack\n");
1747 if (vstack_ax(stack
)->type
!= REG_PTR
) {
1748 ERR("Expecting pointer on top of stack\n");
1752 next_pc
+= sizeof(struct load_op
) + sizeof(struct get_index_u64
);
1758 *_next_pc
= next_pc
;
1763 * Never called concurrently (hash seed is shared).
1765 int lttng_filter_validate_bytecode(struct bytecode_runtime
*bytecode
)
1767 struct cds_lfht
*merge_points
;
1768 char *pc
, *next_pc
, *start_pc
;
1770 struct vstack stack
;
1772 vstack_init(&stack
);
1774 if (!lttng_hash_seed_ready
) {
1775 lttng_hash_seed
= time(NULL
);
1776 lttng_hash_seed_ready
= 1;
1779 * Note: merge_points hash table used by single thread, and
1780 * never concurrently resized. Therefore, we can use it without
1781 * holding RCU read-side lock and free nodes without using
1784 merge_points
= cds_lfht_new(DEFAULT_NR_MERGE_POINTS
,
1785 MIN_NR_BUCKETS
, MAX_NR_BUCKETS
,
1787 if (!merge_points
) {
1788 ERR("Error allocating hash table for bytecode validation\n");
1791 start_pc
= &bytecode
->code
[0];
1792 for (pc
= next_pc
= start_pc
; pc
- start_pc
< bytecode
->len
;
1794 ret
= bytecode_validate_overflow(bytecode
, start_pc
, pc
);
1797 ERR("filter bytecode overflow\n");
1800 dbg_printf("Validating op %s (%u)\n",
1801 print_op((unsigned int) *(filter_opcode_t
*) pc
),
1802 (unsigned int) *(filter_opcode_t
*) pc
);
1805 * For each instruction, validate the current context
1806 * (traversal of entire execution flow), and validate
1807 * all merge points targeting this instruction.
1809 ret
= validate_instruction_all_contexts(bytecode
, merge_points
,
1810 &stack
, start_pc
, pc
);
1813 ret
= exec_insn(bytecode
, merge_points
, &stack
, &next_pc
, pc
);
1818 if (delete_all_nodes(merge_points
)) {
1820 ERR("Unexpected merge points\n");
1824 if (cds_lfht_destroy(merge_points
, NULL
)) {
1825 ERR("Error destroying hash table\n");