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
:
314 ERR("unsupported bytecode op %u\n",
315 (unsigned int) *(filter_opcode_t
*) pc
);
326 case FILTER_OP_EQ_STRING
:
327 case FILTER_OP_NE_STRING
:
328 case FILTER_OP_GT_STRING
:
329 case FILTER_OP_LT_STRING
:
330 case FILTER_OP_GE_STRING
:
331 case FILTER_OP_LE_STRING
:
332 case FILTER_OP_EQ_STAR_GLOB_STRING
:
333 case FILTER_OP_NE_STAR_GLOB_STRING
:
334 case FILTER_OP_EQ_S64
:
335 case FILTER_OP_NE_S64
:
336 case FILTER_OP_GT_S64
:
337 case FILTER_OP_LT_S64
:
338 case FILTER_OP_GE_S64
:
339 case FILTER_OP_LE_S64
:
340 case FILTER_OP_EQ_DOUBLE
:
341 case FILTER_OP_NE_DOUBLE
:
342 case FILTER_OP_GT_DOUBLE
:
343 case FILTER_OP_LT_DOUBLE
:
344 case FILTER_OP_GE_DOUBLE
:
345 case FILTER_OP_LE_DOUBLE
:
346 case FILTER_OP_EQ_DOUBLE_S64
:
347 case FILTER_OP_NE_DOUBLE_S64
:
348 case FILTER_OP_GT_DOUBLE_S64
:
349 case FILTER_OP_LT_DOUBLE_S64
:
350 case FILTER_OP_GE_DOUBLE_S64
:
351 case FILTER_OP_LE_DOUBLE_S64
:
352 case FILTER_OP_EQ_S64_DOUBLE
:
353 case FILTER_OP_NE_S64_DOUBLE
:
354 case FILTER_OP_GT_S64_DOUBLE
:
355 case FILTER_OP_LT_S64_DOUBLE
:
356 case FILTER_OP_GE_S64_DOUBLE
:
357 case FILTER_OP_LE_S64_DOUBLE
:
358 case FILTER_OP_BIT_RSHIFT
:
359 case FILTER_OP_BIT_LSHIFT
:
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
:
381 case FILTER_OP_UNARY_BIT_NOT
:
383 if (unlikely(pc
+ sizeof(struct unary_op
)
384 > start_pc
+ bytecode
->len
)) {
394 if (unlikely(pc
+ sizeof(struct logical_op
)
395 > start_pc
+ bytecode
->len
)) {
402 case FILTER_OP_LOAD_FIELD_REF
:
404 ERR("Unknown field ref type\n");
409 /* get context ref */
410 case FILTER_OP_GET_CONTEXT_REF
:
411 case FILTER_OP_LOAD_FIELD_REF_STRING
:
412 case FILTER_OP_LOAD_FIELD_REF_SEQUENCE
:
413 case FILTER_OP_LOAD_FIELD_REF_S64
:
414 case FILTER_OP_LOAD_FIELD_REF_DOUBLE
:
415 case FILTER_OP_GET_CONTEXT_REF_STRING
:
416 case FILTER_OP_GET_CONTEXT_REF_S64
:
417 case FILTER_OP_GET_CONTEXT_REF_DOUBLE
:
419 if (unlikely(pc
+ sizeof(struct load_op
) + sizeof(struct field_ref
)
420 > start_pc
+ bytecode
->len
)) {
426 /* load from immediate operand */
427 case FILTER_OP_LOAD_STRING
:
428 case FILTER_OP_LOAD_STAR_GLOB_STRING
:
430 struct load_op
*insn
= (struct load_op
*) pc
;
431 uint32_t str_len
, maxlen
;
433 if (unlikely(pc
+ sizeof(struct load_op
)
434 > start_pc
+ bytecode
->len
)) {
439 maxlen
= start_pc
+ bytecode
->len
- pc
- sizeof(struct load_op
);
440 str_len
= strnlen(insn
->data
, maxlen
);
441 if (unlikely(str_len
>= maxlen
)) {
442 /* Final '\0' not found within range */
448 case FILTER_OP_LOAD_S64
:
450 if (unlikely(pc
+ sizeof(struct load_op
) + sizeof(struct literal_numeric
)
451 > start_pc
+ bytecode
->len
)) {
457 case FILTER_OP_LOAD_DOUBLE
:
459 if (unlikely(pc
+ sizeof(struct load_op
) + sizeof(struct literal_double
)
460 > start_pc
+ bytecode
->len
)) {
466 case FILTER_OP_CAST_TO_S64
:
467 case FILTER_OP_CAST_DOUBLE_TO_S64
:
468 case FILTER_OP_CAST_NOP
:
470 if (unlikely(pc
+ sizeof(struct cast_op
)
471 > start_pc
+ bytecode
->len
)) {
478 * Instructions for recursive traversal through composed types.
480 case FILTER_OP_GET_CONTEXT_ROOT
:
481 case FILTER_OP_GET_APP_CONTEXT_ROOT
:
482 case FILTER_OP_GET_PAYLOAD_ROOT
:
483 case FILTER_OP_LOAD_FIELD
:
484 case FILTER_OP_LOAD_FIELD_S8
:
485 case FILTER_OP_LOAD_FIELD_S16
:
486 case FILTER_OP_LOAD_FIELD_S32
:
487 case FILTER_OP_LOAD_FIELD_S64
:
488 case FILTER_OP_LOAD_FIELD_U8
:
489 case FILTER_OP_LOAD_FIELD_U16
:
490 case FILTER_OP_LOAD_FIELD_U32
:
491 case FILTER_OP_LOAD_FIELD_U64
:
492 case FILTER_OP_LOAD_FIELD_STRING
:
493 case FILTER_OP_LOAD_FIELD_SEQUENCE
:
494 case FILTER_OP_LOAD_FIELD_DOUBLE
:
495 if (unlikely(pc
+ sizeof(struct load_op
)
496 > start_pc
+ bytecode
->len
)) {
501 case FILTER_OP_GET_SYMBOL
:
503 struct load_op
*insn
= (struct load_op
*) pc
;
504 struct get_symbol
*sym
= (struct get_symbol
*) insn
->data
;
506 if (unlikely(pc
+ sizeof(struct load_op
) + sizeof(struct get_symbol
)
507 > start_pc
+ bytecode
->len
)) {
510 ret
= validate_get_symbol(bytecode
, sym
);
514 case FILTER_OP_GET_SYMBOL_FIELD
:
515 ERR("Unexpected get symbol field");
519 case FILTER_OP_GET_INDEX_U16
:
520 if (unlikely(pc
+ sizeof(struct load_op
) + sizeof(struct get_index_u16
)
521 > start_pc
+ bytecode
->len
)) {
526 case FILTER_OP_GET_INDEX_U64
:
527 if (unlikely(pc
+ sizeof(struct load_op
) + sizeof(struct get_index_u64
)
528 > start_pc
+ bytecode
->len
)) {
538 unsigned long delete_all_nodes(struct cds_lfht
*ht
)
540 struct cds_lfht_iter iter
;
541 struct lfht_mp_node
*node
;
542 unsigned long nr_nodes
= 0;
544 cds_lfht_for_each_entry(ht
, &iter
, node
, node
) {
547 ret
= cds_lfht_del(ht
, cds_lfht_iter_get_node(&iter
));
549 /* note: this hash table is never used concurrently */
562 int validate_instruction_context(struct bytecode_runtime
*bytecode
,
563 struct vstack
*stack
,
568 const filter_opcode_t opcode
= *(filter_opcode_t
*) pc
;
571 case FILTER_OP_UNKNOWN
:
574 ERR("unknown bytecode op %u\n",
575 (unsigned int) *(filter_opcode_t
*) pc
);
580 case FILTER_OP_RETURN
:
590 case FILTER_OP_MINUS
:
592 ERR("unsupported bytecode op %u\n",
593 (unsigned int) opcode
);
600 ret
= bin_op_compare_check(stack
, opcode
, "==");
607 ret
= bin_op_compare_check(stack
, opcode
, "!=");
614 ret
= bin_op_compare_check(stack
, opcode
, ">");
621 ret
= bin_op_compare_check(stack
, opcode
, "<");
628 ret
= bin_op_compare_check(stack
, opcode
, ">=");
635 ret
= bin_op_compare_check(stack
, opcode
, "<=");
641 case FILTER_OP_EQ_STRING
:
642 case FILTER_OP_NE_STRING
:
643 case FILTER_OP_GT_STRING
:
644 case FILTER_OP_LT_STRING
:
645 case FILTER_OP_GE_STRING
:
646 case FILTER_OP_LE_STRING
:
648 if (!vstack_ax(stack
) || !vstack_bx(stack
)) {
649 ERR("Empty stack\n");
653 if (vstack_ax(stack
)->type
!= REG_STRING
654 || vstack_bx(stack
)->type
!= REG_STRING
) {
655 ERR("Unexpected register type for string comparator\n");
662 case FILTER_OP_EQ_STAR_GLOB_STRING
:
663 case FILTER_OP_NE_STAR_GLOB_STRING
:
665 if (!vstack_ax(stack
) || !vstack_bx(stack
)) {
666 ERR("Empty stack\n");
670 if (vstack_ax(stack
)->type
!= REG_STAR_GLOB_STRING
671 && vstack_bx(stack
)->type
!= REG_STAR_GLOB_STRING
) {
672 ERR("Unexpected register type for globbing pattern comparator\n");
679 case FILTER_OP_EQ_S64
:
680 case FILTER_OP_NE_S64
:
681 case FILTER_OP_GT_S64
:
682 case FILTER_OP_LT_S64
:
683 case FILTER_OP_GE_S64
:
684 case FILTER_OP_LE_S64
:
686 if (!vstack_ax(stack
) || !vstack_bx(stack
)) {
687 ERR("Empty stack\n");
691 if (vstack_ax(stack
)->type
!= REG_S64
692 || vstack_bx(stack
)->type
!= REG_S64
) {
693 ERR("Unexpected register type for s64 comparator\n");
700 case FILTER_OP_EQ_DOUBLE
:
701 case FILTER_OP_NE_DOUBLE
:
702 case FILTER_OP_GT_DOUBLE
:
703 case FILTER_OP_LT_DOUBLE
:
704 case FILTER_OP_GE_DOUBLE
:
705 case FILTER_OP_LE_DOUBLE
:
707 if (!vstack_ax(stack
) || !vstack_bx(stack
)) {
708 ERR("Empty stack\n");
712 if (vstack_ax(stack
)->type
!= REG_DOUBLE
&& vstack_bx(stack
)->type
!= REG_DOUBLE
) {
713 ERR("Double operator should have two double registers\n");
720 case FILTER_OP_EQ_DOUBLE_S64
:
721 case FILTER_OP_NE_DOUBLE_S64
:
722 case FILTER_OP_GT_DOUBLE_S64
:
723 case FILTER_OP_LT_DOUBLE_S64
:
724 case FILTER_OP_GE_DOUBLE_S64
:
725 case FILTER_OP_LE_DOUBLE_S64
:
727 if (!vstack_ax(stack
) || !vstack_bx(stack
)) {
728 ERR("Empty stack\n");
732 if (vstack_ax(stack
)->type
!= REG_S64
&& vstack_bx(stack
)->type
!= REG_DOUBLE
) {
733 ERR("Double-S64 operator has unexpected register types\n");
740 case FILTER_OP_EQ_S64_DOUBLE
:
741 case FILTER_OP_NE_S64_DOUBLE
:
742 case FILTER_OP_GT_S64_DOUBLE
:
743 case FILTER_OP_LT_S64_DOUBLE
:
744 case FILTER_OP_GE_S64_DOUBLE
:
745 case FILTER_OP_LE_S64_DOUBLE
:
747 if (!vstack_ax(stack
) || !vstack_bx(stack
)) {
748 ERR("Empty stack\n");
752 if (vstack_ax(stack
)->type
!= REG_DOUBLE
&& vstack_bx(stack
)->type
!= REG_S64
) {
753 ERR("S64-Double operator has unexpected register types\n");
760 case FILTER_OP_BIT_RSHIFT
:
761 ret
= bin_op_bitwise_check(stack
, opcode
, ">>");
765 case FILTER_OP_BIT_LSHIFT
:
766 ret
= bin_op_bitwise_check(stack
, opcode
, "<<");
770 case FILTER_OP_BIT_AND
:
771 ret
= bin_op_bitwise_check(stack
, opcode
, "&");
775 case FILTER_OP_BIT_OR
:
776 ret
= bin_op_bitwise_check(stack
, opcode
, "|");
780 case FILTER_OP_BIT_XOR
:
781 ret
= bin_op_bitwise_check(stack
, opcode
, "^");
787 case FILTER_OP_UNARY_PLUS
:
788 case FILTER_OP_UNARY_MINUS
:
789 case FILTER_OP_UNARY_NOT
:
791 if (!vstack_ax(stack
)) {
792 ERR("Empty stack\n");
796 switch (vstack_ax(stack
)->type
) {
798 ERR("unknown register type\n");
803 case REG_STAR_GLOB_STRING
:
804 ERR("Unary op can only be applied to numeric or floating point registers\n");
816 case FILTER_OP_UNARY_BIT_NOT
:
818 if (!vstack_ax(stack
)) {
819 ERR("Empty stack\n");
823 switch (vstack_ax(stack
)->type
) {
825 ERR("unknown register type\n");
830 case REG_STAR_GLOB_STRING
:
832 ERR("Unary bitwise op can only be applied to numeric registers\n");
843 case FILTER_OP_UNARY_PLUS_S64
:
844 case FILTER_OP_UNARY_MINUS_S64
:
845 case FILTER_OP_UNARY_NOT_S64
:
847 if (!vstack_ax(stack
)) {
848 ERR("Empty stack\n");
852 if (vstack_ax(stack
)->type
!= REG_S64
) {
853 ERR("Invalid register type\n");
860 case FILTER_OP_UNARY_PLUS_DOUBLE
:
861 case FILTER_OP_UNARY_MINUS_DOUBLE
:
862 case FILTER_OP_UNARY_NOT_DOUBLE
:
864 if (!vstack_ax(stack
)) {
865 ERR("Empty stack\n");
869 if (vstack_ax(stack
)->type
!= REG_DOUBLE
) {
870 ERR("Invalid register type\n");
881 struct logical_op
*insn
= (struct logical_op
*) pc
;
883 if (!vstack_ax(stack
)) {
884 ERR("Empty stack\n");
888 if (vstack_ax(stack
)->type
!= REG_S64
889 && vstack_ax(stack
)->type
!= REG_UNKNOWN
) {
890 ERR("Logical comparator expects S64 or dynamic register\n");
895 dbg_printf("Validate jumping to bytecode offset %u\n",
896 (unsigned int) insn
->skip_offset
);
897 if (unlikely(start_pc
+ insn
->skip_offset
<= pc
)) {
898 ERR("Loops are not allowed in bytecode\n");
906 case FILTER_OP_LOAD_FIELD_REF
:
908 ERR("Unknown field ref type\n");
912 case FILTER_OP_LOAD_FIELD_REF_STRING
:
913 case FILTER_OP_LOAD_FIELD_REF_SEQUENCE
:
915 struct load_op
*insn
= (struct load_op
*) pc
;
916 struct field_ref
*ref
= (struct field_ref
*) insn
->data
;
918 dbg_printf("Validate load field ref offset %u type string\n",
922 case FILTER_OP_LOAD_FIELD_REF_S64
:
924 struct load_op
*insn
= (struct load_op
*) pc
;
925 struct field_ref
*ref
= (struct field_ref
*) insn
->data
;
927 dbg_printf("Validate load field ref offset %u type s64\n",
931 case FILTER_OP_LOAD_FIELD_REF_DOUBLE
:
933 struct load_op
*insn
= (struct load_op
*) pc
;
934 struct field_ref
*ref
= (struct field_ref
*) insn
->data
;
936 dbg_printf("Validate load field ref offset %u type double\n",
941 /* load from immediate operand */
942 case FILTER_OP_LOAD_STRING
:
943 case FILTER_OP_LOAD_STAR_GLOB_STRING
:
948 case FILTER_OP_LOAD_S64
:
953 case FILTER_OP_LOAD_DOUBLE
:
958 case FILTER_OP_CAST_TO_S64
:
959 case FILTER_OP_CAST_DOUBLE_TO_S64
:
961 struct cast_op
*insn
= (struct cast_op
*) pc
;
963 if (!vstack_ax(stack
)) {
964 ERR("Empty stack\n");
968 switch (vstack_ax(stack
)->type
) {
970 ERR("unknown register type\n");
975 case REG_STAR_GLOB_STRING
:
976 ERR("Cast op can only be applied to numeric or floating point registers\n");
986 if (insn
->op
== FILTER_OP_CAST_DOUBLE_TO_S64
) {
987 if (vstack_ax(stack
)->type
!= REG_DOUBLE
) {
988 ERR("Cast expects double\n");
995 case FILTER_OP_CAST_NOP
:
1000 /* get context ref */
1001 case FILTER_OP_GET_CONTEXT_REF
:
1003 struct load_op
*insn
= (struct load_op
*) pc
;
1004 struct field_ref
*ref
= (struct field_ref
*) insn
->data
;
1006 dbg_printf("Validate get context ref offset %u type dynamic\n",
1010 case FILTER_OP_GET_CONTEXT_REF_STRING
:
1012 struct load_op
*insn
= (struct load_op
*) pc
;
1013 struct field_ref
*ref
= (struct field_ref
*) insn
->data
;
1015 dbg_printf("Validate get context ref offset %u type string\n",
1019 case FILTER_OP_GET_CONTEXT_REF_S64
:
1021 struct load_op
*insn
= (struct load_op
*) pc
;
1022 struct field_ref
*ref
= (struct field_ref
*) insn
->data
;
1024 dbg_printf("Validate get context ref offset %u type s64\n",
1028 case FILTER_OP_GET_CONTEXT_REF_DOUBLE
:
1030 struct load_op
*insn
= (struct load_op
*) pc
;
1031 struct field_ref
*ref
= (struct field_ref
*) insn
->data
;
1033 dbg_printf("Validate get context ref offset %u type double\n",
1039 * Instructions for recursive traversal through composed types.
1041 case FILTER_OP_GET_CONTEXT_ROOT
:
1043 dbg_printf("Validate get context root\n");
1046 case FILTER_OP_GET_APP_CONTEXT_ROOT
:
1048 dbg_printf("Validate get app context root\n");
1051 case FILTER_OP_GET_PAYLOAD_ROOT
:
1053 dbg_printf("Validate get payload root\n");
1056 case FILTER_OP_LOAD_FIELD
:
1059 * We tolerate that field type is unknown at validation,
1060 * because we are performing the load specialization in
1061 * a phase after validation.
1063 dbg_printf("Validate load field\n");
1066 case FILTER_OP_LOAD_FIELD_S8
:
1068 dbg_printf("Validate load field s8\n");
1071 case FILTER_OP_LOAD_FIELD_S16
:
1073 dbg_printf("Validate load field s16\n");
1076 case FILTER_OP_LOAD_FIELD_S32
:
1078 dbg_printf("Validate load field s32\n");
1081 case FILTER_OP_LOAD_FIELD_S64
:
1083 dbg_printf("Validate load field s64\n");
1086 case FILTER_OP_LOAD_FIELD_U8
:
1088 dbg_printf("Validate load field u8\n");
1091 case FILTER_OP_LOAD_FIELD_U16
:
1093 dbg_printf("Validate load field u16\n");
1096 case FILTER_OP_LOAD_FIELD_U32
:
1098 dbg_printf("Validate load field u32\n");
1101 case FILTER_OP_LOAD_FIELD_U64
:
1103 dbg_printf("Validate load field u64\n");
1106 case FILTER_OP_LOAD_FIELD_STRING
:
1108 dbg_printf("Validate load field string\n");
1111 case FILTER_OP_LOAD_FIELD_SEQUENCE
:
1113 dbg_printf("Validate load field sequence\n");
1116 case FILTER_OP_LOAD_FIELD_DOUBLE
:
1118 dbg_printf("Validate load field double\n");
1122 case FILTER_OP_GET_SYMBOL
:
1124 struct load_op
*insn
= (struct load_op
*) pc
;
1125 struct get_symbol
*sym
= (struct get_symbol
*) insn
->data
;
1127 dbg_printf("Validate get symbol offset %u\n", sym
->offset
);
1131 case FILTER_OP_GET_SYMBOL_FIELD
:
1133 struct load_op
*insn
= (struct load_op
*) pc
;
1134 struct get_symbol
*sym
= (struct get_symbol
*) insn
->data
;
1136 dbg_printf("Validate get symbol field offset %u\n", sym
->offset
);
1140 case FILTER_OP_GET_INDEX_U16
:
1142 struct load_op
*insn
= (struct load_op
*) pc
;
1143 struct get_index_u16
*get_index
= (struct get_index_u16
*) insn
->data
;
1145 dbg_printf("Validate get index u16 index %u\n", get_index
->index
);
1149 case FILTER_OP_GET_INDEX_U64
:
1151 struct load_op
*insn
= (struct load_op
*) pc
;
1152 struct get_index_u64
*get_index
= (struct get_index_u64
*) insn
->data
;
1154 dbg_printf("Validate get index u64 index %" PRIu64
"\n", get_index
->index
);
1168 int validate_instruction_all_contexts(struct bytecode_runtime
*bytecode
,
1169 struct cds_lfht
*merge_points
,
1170 struct vstack
*stack
,
1175 unsigned long target_pc
= pc
- start_pc
;
1176 struct cds_lfht_iter iter
;
1177 struct cds_lfht_node
*node
;
1178 struct lfht_mp_node
*mp_node
;
1181 /* Validate the context resulting from the previous instruction */
1182 ret
= validate_instruction_context(bytecode
, stack
, start_pc
, pc
);
1186 /* Validate merge points */
1187 hash
= lttng_hash_mix((const char *) target_pc
, sizeof(target_pc
),
1189 cds_lfht_lookup(merge_points
, hash
, lttng_hash_match
,
1190 (const char *) target_pc
, &iter
);
1191 node
= cds_lfht_iter_get_node(&iter
);
1193 mp_node
= caa_container_of(node
, struct lfht_mp_node
, node
);
1195 dbg_printf("Filter: validate merge point at offset %lu\n",
1197 if (merge_points_compare(stack
, &mp_node
->stack
)) {
1198 ERR("Merge points differ for offset %lu\n",
1202 /* Once validated, we can remove the merge point */
1203 dbg_printf("Filter: remove merge point at offset %lu\n",
1205 ret
= cds_lfht_del(merge_points
, node
);
1213 * >0: going to next insn.
1214 * 0: success, stop iteration.
1218 int exec_insn(struct bytecode_runtime
*bytecode
,
1219 struct cds_lfht
*merge_points
,
1220 struct vstack
*stack
,
1225 char *next_pc
= *_next_pc
;
1227 switch (*(filter_opcode_t
*) pc
) {
1228 case FILTER_OP_UNKNOWN
:
1231 ERR("unknown bytecode op %u\n",
1232 (unsigned int) *(filter_opcode_t
*) pc
);
1237 case FILTER_OP_RETURN
:
1239 if (!vstack_ax(stack
)) {
1240 ERR("Empty stack\n");
1244 switch (vstack_ax(stack
)->type
) {
1249 ERR("Unexpected register type %d at end of bytecode\n",
1250 (int) vstack_ax(stack
)->type
);
1263 case FILTER_OP_PLUS
:
1264 case FILTER_OP_MINUS
:
1266 ERR("unsupported bytecode op %u\n",
1267 (unsigned int) *(filter_opcode_t
*) pc
);
1278 case FILTER_OP_EQ_STRING
:
1279 case FILTER_OP_NE_STRING
:
1280 case FILTER_OP_GT_STRING
:
1281 case FILTER_OP_LT_STRING
:
1282 case FILTER_OP_GE_STRING
:
1283 case FILTER_OP_LE_STRING
:
1284 case FILTER_OP_EQ_STAR_GLOB_STRING
:
1285 case FILTER_OP_NE_STAR_GLOB_STRING
:
1286 case FILTER_OP_EQ_S64
:
1287 case FILTER_OP_NE_S64
:
1288 case FILTER_OP_GT_S64
:
1289 case FILTER_OP_LT_S64
:
1290 case FILTER_OP_GE_S64
:
1291 case FILTER_OP_LE_S64
:
1292 case FILTER_OP_EQ_DOUBLE
:
1293 case FILTER_OP_NE_DOUBLE
:
1294 case FILTER_OP_GT_DOUBLE
:
1295 case FILTER_OP_LT_DOUBLE
:
1296 case FILTER_OP_GE_DOUBLE
:
1297 case FILTER_OP_LE_DOUBLE
:
1298 case FILTER_OP_EQ_DOUBLE_S64
:
1299 case FILTER_OP_NE_DOUBLE_S64
:
1300 case FILTER_OP_GT_DOUBLE_S64
:
1301 case FILTER_OP_LT_DOUBLE_S64
:
1302 case FILTER_OP_GE_DOUBLE_S64
:
1303 case FILTER_OP_LE_DOUBLE_S64
:
1304 case FILTER_OP_EQ_S64_DOUBLE
:
1305 case FILTER_OP_NE_S64_DOUBLE
:
1306 case FILTER_OP_GT_S64_DOUBLE
:
1307 case FILTER_OP_LT_S64_DOUBLE
:
1308 case FILTER_OP_GE_S64_DOUBLE
:
1309 case FILTER_OP_LE_S64_DOUBLE
:
1310 case FILTER_OP_BIT_RSHIFT
:
1311 case FILTER_OP_BIT_LSHIFT
:
1312 case FILTER_OP_BIT_AND
:
1313 case FILTER_OP_BIT_OR
:
1314 case FILTER_OP_BIT_XOR
:
1317 if (vstack_pop(stack
)) {
1321 if (!vstack_ax(stack
)) {
1322 ERR("Empty stack\n");
1326 switch (vstack_ax(stack
)->type
) {
1330 case REG_STAR_GLOB_STRING
:
1334 ERR("Unexpected register type %d for operation\n",
1335 (int) vstack_ax(stack
)->type
);
1340 vstack_ax(stack
)->type
= REG_S64
;
1341 next_pc
+= sizeof(struct binary_op
);
1346 case FILTER_OP_UNARY_PLUS
:
1347 case FILTER_OP_UNARY_MINUS
:
1350 if (!vstack_ax(stack
)) {
1351 ERR("Empty stack\n");
1355 switch (vstack_ax(stack
)->type
) {
1361 ERR("Unexpected register type %d for operation\n",
1362 (int) vstack_ax(stack
)->type
);
1366 vstack_ax(stack
)->type
= REG_UNKNOWN
;
1367 next_pc
+= sizeof(struct unary_op
);
1371 case FILTER_OP_UNARY_PLUS_S64
:
1372 case FILTER_OP_UNARY_MINUS_S64
:
1373 case FILTER_OP_UNARY_NOT_S64
:
1376 if (!vstack_ax(stack
)) {
1377 ERR("Empty stack\n");
1381 switch (vstack_ax(stack
)->type
) {
1385 ERR("Unexpected register type %d for operation\n",
1386 (int) vstack_ax(stack
)->type
);
1391 vstack_ax(stack
)->type
= REG_S64
;
1392 next_pc
+= sizeof(struct unary_op
);
1396 case FILTER_OP_UNARY_NOT
:
1399 if (!vstack_ax(stack
)) {
1400 ERR("Empty stack\n");
1404 switch (vstack_ax(stack
)->type
) {
1410 ERR("Unexpected register type %d for operation\n",
1411 (int) vstack_ax(stack
)->type
);
1416 vstack_ax(stack
)->type
= REG_S64
;
1417 next_pc
+= sizeof(struct unary_op
);
1421 case FILTER_OP_UNARY_BIT_NOT
:
1424 if (!vstack_ax(stack
)) {
1425 ERR("Empty stack\n");
1429 switch (vstack_ax(stack
)->type
) {
1435 ERR("Unexpected register type %d for operation\n",
1436 (int) vstack_ax(stack
)->type
);
1441 vstack_ax(stack
)->type
= REG_S64
;
1442 next_pc
+= sizeof(struct unary_op
);
1446 case FILTER_OP_UNARY_NOT_DOUBLE
:
1449 if (!vstack_ax(stack
)) {
1450 ERR("Empty stack\n");
1454 switch (vstack_ax(stack
)->type
) {
1458 ERR("Incorrect register type %d for operation\n",
1459 (int) vstack_ax(stack
)->type
);
1464 vstack_ax(stack
)->type
= REG_S64
;
1465 next_pc
+= sizeof(struct unary_op
);
1469 case FILTER_OP_UNARY_PLUS_DOUBLE
:
1470 case FILTER_OP_UNARY_MINUS_DOUBLE
:
1473 if (!vstack_ax(stack
)) {
1474 ERR("Empty stack\n");
1478 switch (vstack_ax(stack
)->type
) {
1482 ERR("Incorrect register type %d for operation\n",
1483 (int) vstack_ax(stack
)->type
);
1488 vstack_ax(stack
)->type
= REG_DOUBLE
;
1489 next_pc
+= sizeof(struct unary_op
);
1497 struct logical_op
*insn
= (struct logical_op
*) pc
;
1500 /* Add merge point to table */
1501 merge_ret
= merge_point_add_check(merge_points
,
1502 insn
->skip_offset
, stack
);
1508 if (!vstack_ax(stack
)) {
1509 ERR("Empty stack\n");
1513 /* There is always a cast-to-s64 operation before a or/and op. */
1514 switch (vstack_ax(stack
)->type
) {
1518 ERR("Incorrect register type %d for operation\n",
1519 (int) vstack_ax(stack
)->type
);
1524 /* Continue to next instruction */
1525 /* Pop 1 when jump not taken */
1526 if (vstack_pop(stack
)) {
1530 next_pc
+= sizeof(struct logical_op
);
1534 /* load field ref */
1535 case FILTER_OP_LOAD_FIELD_REF
:
1537 ERR("Unknown field ref type\n");
1541 /* get context ref */
1542 case FILTER_OP_GET_CONTEXT_REF
:
1544 if (vstack_push(stack
)) {
1548 vstack_ax(stack
)->type
= REG_UNKNOWN
;
1549 next_pc
+= sizeof(struct load_op
) + sizeof(struct field_ref
);
1552 case FILTER_OP_LOAD_FIELD_REF_STRING
:
1553 case FILTER_OP_LOAD_FIELD_REF_SEQUENCE
:
1554 case FILTER_OP_GET_CONTEXT_REF_STRING
:
1556 if (vstack_push(stack
)) {
1560 vstack_ax(stack
)->type
= REG_STRING
;
1561 next_pc
+= sizeof(struct load_op
) + sizeof(struct field_ref
);
1564 case FILTER_OP_LOAD_FIELD_REF_S64
:
1565 case FILTER_OP_GET_CONTEXT_REF_S64
:
1567 if (vstack_push(stack
)) {
1571 vstack_ax(stack
)->type
= REG_S64
;
1572 next_pc
+= sizeof(struct load_op
) + sizeof(struct field_ref
);
1575 case FILTER_OP_LOAD_FIELD_REF_DOUBLE
:
1576 case FILTER_OP_GET_CONTEXT_REF_DOUBLE
:
1578 if (vstack_push(stack
)) {
1582 vstack_ax(stack
)->type
= REG_DOUBLE
;
1583 next_pc
+= sizeof(struct load_op
) + sizeof(struct field_ref
);
1587 /* load from immediate operand */
1588 case FILTER_OP_LOAD_STRING
:
1590 struct load_op
*insn
= (struct load_op
*) pc
;
1592 if (vstack_push(stack
)) {
1596 vstack_ax(stack
)->type
= REG_STRING
;
1597 next_pc
+= sizeof(struct load_op
) + strlen(insn
->data
) + 1;
1601 case FILTER_OP_LOAD_STAR_GLOB_STRING
:
1603 struct load_op
*insn
= (struct load_op
*) pc
;
1605 if (vstack_push(stack
)) {
1609 vstack_ax(stack
)->type
= REG_STAR_GLOB_STRING
;
1610 next_pc
+= sizeof(struct load_op
) + strlen(insn
->data
) + 1;
1614 case FILTER_OP_LOAD_S64
:
1616 if (vstack_push(stack
)) {
1620 vstack_ax(stack
)->type
= REG_S64
;
1621 next_pc
+= sizeof(struct load_op
)
1622 + sizeof(struct literal_numeric
);
1626 case FILTER_OP_LOAD_DOUBLE
:
1628 if (vstack_push(stack
)) {
1632 vstack_ax(stack
)->type
= REG_DOUBLE
;
1633 next_pc
+= sizeof(struct load_op
)
1634 + sizeof(struct literal_double
);
1638 case FILTER_OP_CAST_TO_S64
:
1639 case FILTER_OP_CAST_DOUBLE_TO_S64
:
1642 if (!vstack_ax(stack
)) {
1643 ERR("Empty stack\n");
1647 switch (vstack_ax(stack
)->type
) {
1653 ERR("Incorrect register type %d for cast\n",
1654 (int) vstack_ax(stack
)->type
);
1658 vstack_ax(stack
)->type
= REG_S64
;
1659 next_pc
+= sizeof(struct cast_op
);
1662 case FILTER_OP_CAST_NOP
:
1664 next_pc
+= sizeof(struct cast_op
);
1669 * Instructions for recursive traversal through composed types.
1671 case FILTER_OP_GET_CONTEXT_ROOT
:
1672 case FILTER_OP_GET_APP_CONTEXT_ROOT
:
1673 case FILTER_OP_GET_PAYLOAD_ROOT
:
1675 if (vstack_push(stack
)) {
1679 vstack_ax(stack
)->type
= REG_PTR
;
1680 next_pc
+= sizeof(struct load_op
);
1684 case FILTER_OP_LOAD_FIELD
:
1687 if (!vstack_ax(stack
)) {
1688 ERR("Empty stack\n");
1692 if (vstack_ax(stack
)->type
!= REG_PTR
) {
1693 ERR("Expecting pointer on top of stack\n");
1697 vstack_ax(stack
)->type
= REG_UNKNOWN
;
1698 next_pc
+= sizeof(struct load_op
);
1702 case FILTER_OP_LOAD_FIELD_S8
:
1703 case FILTER_OP_LOAD_FIELD_S16
:
1704 case FILTER_OP_LOAD_FIELD_S32
:
1705 case FILTER_OP_LOAD_FIELD_S64
:
1706 case FILTER_OP_LOAD_FIELD_U8
:
1707 case FILTER_OP_LOAD_FIELD_U16
:
1708 case FILTER_OP_LOAD_FIELD_U32
:
1709 case FILTER_OP_LOAD_FIELD_U64
:
1712 if (!vstack_ax(stack
)) {
1713 ERR("Empty stack\n");
1717 if (vstack_ax(stack
)->type
!= REG_PTR
) {
1718 ERR("Expecting pointer on top of stack\n");
1722 vstack_ax(stack
)->type
= REG_S64
;
1723 next_pc
+= sizeof(struct load_op
);
1727 case FILTER_OP_LOAD_FIELD_STRING
:
1728 case FILTER_OP_LOAD_FIELD_SEQUENCE
:
1731 if (!vstack_ax(stack
)) {
1732 ERR("Empty stack\n");
1736 if (vstack_ax(stack
)->type
!= REG_PTR
) {
1737 ERR("Expecting pointer on top of stack\n");
1741 vstack_ax(stack
)->type
= REG_STRING
;
1742 next_pc
+= sizeof(struct load_op
);
1746 case FILTER_OP_LOAD_FIELD_DOUBLE
:
1749 if (!vstack_ax(stack
)) {
1750 ERR("Empty stack\n");
1754 if (vstack_ax(stack
)->type
!= REG_PTR
) {
1755 ERR("Expecting pointer on top of stack\n");
1759 vstack_ax(stack
)->type
= REG_DOUBLE
;
1760 next_pc
+= sizeof(struct load_op
);
1764 case FILTER_OP_GET_SYMBOL
:
1765 case FILTER_OP_GET_SYMBOL_FIELD
:
1768 if (!vstack_ax(stack
)) {
1769 ERR("Empty stack\n");
1773 if (vstack_ax(stack
)->type
!= REG_PTR
) {
1774 ERR("Expecting pointer on top of stack\n");
1778 next_pc
+= sizeof(struct load_op
) + sizeof(struct get_symbol
);
1782 case FILTER_OP_GET_INDEX_U16
:
1785 if (!vstack_ax(stack
)) {
1786 ERR("Empty stack\n");
1790 if (vstack_ax(stack
)->type
!= REG_PTR
) {
1791 ERR("Expecting pointer on top of stack\n");
1795 next_pc
+= sizeof(struct load_op
) + sizeof(struct get_index_u16
);
1799 case FILTER_OP_GET_INDEX_U64
:
1802 if (!vstack_ax(stack
)) {
1803 ERR("Empty stack\n");
1807 if (vstack_ax(stack
)->type
!= REG_PTR
) {
1808 ERR("Expecting pointer on top of stack\n");
1812 next_pc
+= sizeof(struct load_op
) + sizeof(struct get_index_u64
);
1818 *_next_pc
= next_pc
;
1823 * Never called concurrently (hash seed is shared).
1825 int lttng_filter_validate_bytecode(struct bytecode_runtime
*bytecode
)
1827 struct cds_lfht
*merge_points
;
1828 char *pc
, *next_pc
, *start_pc
;
1830 struct vstack stack
;
1832 vstack_init(&stack
);
1834 if (!lttng_hash_seed_ready
) {
1835 lttng_hash_seed
= time(NULL
);
1836 lttng_hash_seed_ready
= 1;
1839 * Note: merge_points hash table used by single thread, and
1840 * never concurrently resized. Therefore, we can use it without
1841 * holding RCU read-side lock and free nodes without using
1844 merge_points
= cds_lfht_new(DEFAULT_NR_MERGE_POINTS
,
1845 MIN_NR_BUCKETS
, MAX_NR_BUCKETS
,
1847 if (!merge_points
) {
1848 ERR("Error allocating hash table for bytecode validation\n");
1851 start_pc
= &bytecode
->code
[0];
1852 for (pc
= next_pc
= start_pc
; pc
- start_pc
< bytecode
->len
;
1854 ret
= bytecode_validate_overflow(bytecode
, start_pc
, pc
);
1857 ERR("filter bytecode overflow\n");
1860 dbg_printf("Validating op %s (%u)\n",
1861 print_op((unsigned int) *(filter_opcode_t
*) pc
),
1862 (unsigned int) *(filter_opcode_t
*) pc
);
1865 * For each instruction, validate the current context
1866 * (traversal of entire execution flow), and validate
1867 * all merge points targeting this instruction.
1869 ret
= validate_instruction_all_contexts(bytecode
, merge_points
,
1870 &stack
, start_pc
, pc
);
1873 ret
= exec_insn(bytecode
, merge_points
, &stack
, &next_pc
, pc
);
1878 if (delete_all_nodes(merge_points
)) {
1880 ERR("Unexpected merge points\n");
1884 if (cds_lfht_destroy(merge_points
, NULL
)) {
1885 ERR("Error destroying hash table\n");