| 1 | #ifndef _LTTNG_FILTER_H |
| 2 | #define _LTTNG_FILTER_H |
| 3 | |
| 4 | /* |
| 5 | * lttng-filter.h |
| 6 | * |
| 7 | * LTTng modules filter header. |
| 8 | * |
| 9 | * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 10 | * |
| 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 27 | * SOFTWARE. |
| 28 | */ |
| 29 | |
| 30 | #include <linux/kernel.h> |
| 31 | |
| 32 | #include <lttng-events.h> |
| 33 | #include <filter-bytecode.h> |
| 34 | |
| 35 | /* Filter stack length, in number of entries */ |
| 36 | #define FILTER_STACK_LEN 10 /* includes 2 dummy */ |
| 37 | #define FILTER_STACK_EMPTY 1 |
| 38 | |
| 39 | #define FILTER_MAX_DATA_LEN 65536 |
| 40 | |
| 41 | #ifdef DEBUG |
| 42 | #define dbg_printk(fmt, args...) \ |
| 43 | printk(KERN_DEBUG "[debug bytecode in %s:%s@%u] " fmt, \ |
| 44 | __FILE__, __func__, __LINE__, ## args) |
| 45 | #else |
| 46 | #define dbg_printk(fmt, args...) \ |
| 47 | do { \ |
| 48 | /* do nothing but check printf format */ \ |
| 49 | if (0) \ |
| 50 | printk(KERN_DEBUG "[debug bytecode in %s:%s@%u] " fmt, \ |
| 51 | __FILE__, __func__, __LINE__, ## args); \ |
| 52 | } while (0) |
| 53 | #endif |
| 54 | |
| 55 | /* Linked bytecode. Child of struct lttng_bytecode_runtime. */ |
| 56 | struct bytecode_runtime { |
| 57 | struct lttng_bytecode_runtime p; |
| 58 | size_t data_len; |
| 59 | size_t data_alloc_len; |
| 60 | char *data; |
| 61 | uint16_t len; |
| 62 | char code[0]; |
| 63 | }; |
| 64 | |
| 65 | enum entry_type { |
| 66 | REG_S64, |
| 67 | REG_DOUBLE, |
| 68 | REG_STRING, |
| 69 | REG_STAR_GLOB_STRING, |
| 70 | REG_TYPE_UNKNOWN, |
| 71 | REG_PTR, |
| 72 | }; |
| 73 | |
| 74 | enum load_type { |
| 75 | LOAD_ROOT_CONTEXT, |
| 76 | LOAD_ROOT_APP_CONTEXT, |
| 77 | LOAD_ROOT_PAYLOAD, |
| 78 | LOAD_OBJECT, |
| 79 | }; |
| 80 | |
| 81 | enum object_type { |
| 82 | OBJECT_TYPE_S8, |
| 83 | OBJECT_TYPE_S16, |
| 84 | OBJECT_TYPE_S32, |
| 85 | OBJECT_TYPE_S64, |
| 86 | OBJECT_TYPE_U8, |
| 87 | OBJECT_TYPE_U16, |
| 88 | OBJECT_TYPE_U32, |
| 89 | OBJECT_TYPE_U64, |
| 90 | |
| 91 | OBJECT_TYPE_DOUBLE, |
| 92 | OBJECT_TYPE_STRING, |
| 93 | OBJECT_TYPE_STRING_SEQUENCE, |
| 94 | |
| 95 | OBJECT_TYPE_SEQUENCE, |
| 96 | OBJECT_TYPE_ARRAY, |
| 97 | OBJECT_TYPE_STRUCT, |
| 98 | OBJECT_TYPE_VARIANT, |
| 99 | |
| 100 | OBJECT_TYPE_DYNAMIC, |
| 101 | }; |
| 102 | |
| 103 | struct filter_get_index_data { |
| 104 | uint64_t offset; /* in bytes */ |
| 105 | size_t ctx_index; |
| 106 | size_t array_len; |
| 107 | struct { |
| 108 | size_t len; |
| 109 | enum object_type type; |
| 110 | bool rev_bo; /* reverse byte order */ |
| 111 | } elem; |
| 112 | }; |
| 113 | |
| 114 | /* Validation stack */ |
| 115 | struct vstack_load { |
| 116 | enum load_type type; |
| 117 | enum object_type object_type; |
| 118 | const struct lttng_event_field *field; |
| 119 | bool rev_bo; /* reverse byte order */ |
| 120 | }; |
| 121 | |
| 122 | struct vstack_entry { |
| 123 | enum entry_type type; |
| 124 | struct vstack_load load; |
| 125 | }; |
| 126 | |
| 127 | struct vstack { |
| 128 | int top; /* top of stack */ |
| 129 | struct vstack_entry e[FILTER_STACK_LEN]; |
| 130 | }; |
| 131 | |
| 132 | static inline |
| 133 | void vstack_init(struct vstack *stack) |
| 134 | { |
| 135 | stack->top = -1; |
| 136 | } |
| 137 | |
| 138 | static inline |
| 139 | struct vstack_entry *vstack_ax(struct vstack *stack) |
| 140 | { |
| 141 | if (unlikely(stack->top < 0)) |
| 142 | return NULL; |
| 143 | return &stack->e[stack->top]; |
| 144 | } |
| 145 | |
| 146 | static inline |
| 147 | struct vstack_entry *vstack_bx(struct vstack *stack) |
| 148 | { |
| 149 | if (unlikely(stack->top < 1)) |
| 150 | return NULL; |
| 151 | return &stack->e[stack->top - 1]; |
| 152 | } |
| 153 | |
| 154 | static inline |
| 155 | int vstack_push(struct vstack *stack) |
| 156 | { |
| 157 | if (stack->top >= FILTER_STACK_LEN - 1) { |
| 158 | printk(KERN_WARNING "Stack full\n"); |
| 159 | return -EINVAL; |
| 160 | } |
| 161 | ++stack->top; |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static inline |
| 166 | int vstack_pop(struct vstack *stack) |
| 167 | { |
| 168 | if (unlikely(stack->top < 0)) { |
| 169 | printk(KERN_WARNING "Stack empty\n"); |
| 170 | return -EINVAL; |
| 171 | } |
| 172 | stack->top--; |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | /* Execution stack */ |
| 177 | enum estack_string_literal_type { |
| 178 | ESTACK_STRING_LITERAL_TYPE_NONE, |
| 179 | ESTACK_STRING_LITERAL_TYPE_PLAIN, |
| 180 | ESTACK_STRING_LITERAL_TYPE_STAR_GLOB, |
| 181 | }; |
| 182 | |
| 183 | struct load_ptr { |
| 184 | enum load_type type; |
| 185 | enum object_type object_type; |
| 186 | const void *ptr; |
| 187 | bool rev_bo; |
| 188 | /* Temporary place-holders for contexts. */ |
| 189 | union { |
| 190 | int64_t s64; |
| 191 | uint64_t u64; |
| 192 | double d; |
| 193 | } u; |
| 194 | /* |
| 195 | * "field" is only needed when nested under a variant, in which |
| 196 | * case we cannot specialize the nested operations. |
| 197 | */ |
| 198 | const struct lttng_event_field *field; |
| 199 | }; |
| 200 | |
| 201 | struct estack_entry { |
| 202 | union { |
| 203 | int64_t v; |
| 204 | |
| 205 | struct { |
| 206 | const char *str; |
| 207 | const char __user *user_str; |
| 208 | size_t seq_len; |
| 209 | enum estack_string_literal_type literal_type; |
| 210 | int user; /* is string from userspace ? */ |
| 211 | } s; |
| 212 | struct load_ptr ptr; |
| 213 | } u; |
| 214 | }; |
| 215 | |
| 216 | struct estack { |
| 217 | int top; /* top of stack */ |
| 218 | struct estack_entry e[FILTER_STACK_LEN]; |
| 219 | }; |
| 220 | |
| 221 | #define estack_ax_v ax |
| 222 | #define estack_bx_v bx |
| 223 | |
| 224 | #define estack_ax(stack, top) \ |
| 225 | ({ \ |
| 226 | BUG_ON((top) <= FILTER_STACK_EMPTY); \ |
| 227 | &(stack)->e[top]; \ |
| 228 | }) |
| 229 | |
| 230 | #define estack_bx(stack, top) \ |
| 231 | ({ \ |
| 232 | BUG_ON((top) <= FILTER_STACK_EMPTY + 1); \ |
| 233 | &(stack)->e[(top) - 1]; \ |
| 234 | }) |
| 235 | |
| 236 | #define estack_push(stack, top, ax, bx) \ |
| 237 | do { \ |
| 238 | BUG_ON((top) >= FILTER_STACK_LEN - 1); \ |
| 239 | (stack)->e[(top) - 1].u.v = (bx); \ |
| 240 | (bx) = (ax); \ |
| 241 | ++(top); \ |
| 242 | } while (0) |
| 243 | |
| 244 | #define estack_pop(stack, top, ax, bx) \ |
| 245 | do { \ |
| 246 | BUG_ON((top) <= FILTER_STACK_EMPTY); \ |
| 247 | (ax) = (bx); \ |
| 248 | (bx) = (stack)->e[(top) - 2].u.v; \ |
| 249 | (top)--; \ |
| 250 | } while (0) |
| 251 | |
| 252 | const char *lttng_filter_print_op(enum filter_op op); |
| 253 | |
| 254 | int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode); |
| 255 | int lttng_filter_specialize_bytecode(struct lttng_event *event, |
| 256 | struct bytecode_runtime *bytecode); |
| 257 | |
| 258 | uint64_t lttng_filter_false(void *filter_data, |
| 259 | struct lttng_probe_ctx *lttng_probe_ctx, |
| 260 | const char *filter_stack_data); |
| 261 | uint64_t lttng_filter_interpret_bytecode(void *filter_data, |
| 262 | struct lttng_probe_ctx *lttng_probe_ctx, |
| 263 | const char *filter_stack_data); |
| 264 | |
| 265 | #endif /* _LTTNG_FILTER_H */ |