Fix: Generation of bytecode longer than 32768 bytes fails
[lttng-tools.git] / src / lib / lttng-ctl / filter / filter-visitor-generate-bytecode.c
1 /*
2 * filter-visitor-generate-bytecode.c
3 *
4 * LTTng filter bytecode generation
5 *
6 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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.
11 *
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.
16 *
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
20 */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include "align.h"
26 #include "filter-bytecode.h"
27 #include "filter-ir.h"
28 #include "filter-ast.h"
29
30 #ifndef max_t
31 #define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b)))
32 #endif
33
34 //#define INIT_ALLOC_SIZE PAGE_SIZE
35 #define INIT_ALLOC_SIZE 4
36
37 static
38 int recursive_visit_gen_bytecode(struct filter_parser_ctx *ctx,
39 struct ir_op *node);
40
41 static inline int fls(unsigned int x)
42 {
43 int r = 32;
44
45 if (!x)
46 return 0;
47 if (!(x & 0xFFFF0000U)) {
48 x <<= 16;
49 r -= 16;
50 }
51 if (!(x & 0xFF000000U)) {
52 x <<= 8;
53 r -= 8;
54 }
55 if (!(x & 0xF0000000U)) {
56 x <<= 4;
57 r -= 4;
58 }
59 if (!(x & 0xC0000000U)) {
60 x <<= 2;
61 r -= 2;
62 }
63 if (!(x & 0x80000000U)) {
64 x <<= 1;
65 r -= 1;
66 }
67 return r;
68 }
69
70 static inline int get_count_order(unsigned int count)
71 {
72 int order;
73
74 order = fls(count) - 1;
75 if (count & (count - 1))
76 order++;
77 return order;
78 }
79
80 static
81 int bytecode_init(struct lttng_filter_bytecode_alloc **fb)
82 {
83 *fb = calloc(sizeof(struct lttng_filter_bytecode_alloc) + INIT_ALLOC_SIZE, 1);
84 if (!*fb) {
85 return -ENOMEM;
86 } else {
87 (*fb)->alloc_len = INIT_ALLOC_SIZE;
88 return 0;
89 }
90 }
91
92 static
93 int32_t bytecode_reserve(struct lttng_filter_bytecode_alloc **fb, uint32_t align, uint32_t len)
94 {
95 int32_t ret;
96 uint32_t padding = offset_align((*fb)->b.len, align);
97
98 if ((*fb)->b.len + padding + len > LTTNG_FILTER_MAX_LEN)
99 return -EINVAL;
100
101 if ((*fb)->b.len + padding + len > (*fb)->alloc_len) {
102 uint32_t new_len =
103 max_t(uint32_t, 1U << get_count_order((*fb)->b.len + padding + len),
104 (*fb)->alloc_len << 1);
105 uint32_t old_len = (*fb)->alloc_len;
106
107 *fb = realloc(*fb, sizeof(struct lttng_filter_bytecode_alloc) + new_len);
108 if (!*fb)
109 return -ENOMEM;
110 memset(&(*fb)->b.data[old_len], 0, new_len - old_len);
111 (*fb)->alloc_len = new_len;
112 }
113 (*fb)->b.len += padding;
114 ret = (*fb)->b.len;
115 (*fb)->b.len += len;
116 return ret;
117 }
118
119 static
120 int bytecode_push(struct lttng_filter_bytecode_alloc **fb, const void *data,
121 uint32_t align, uint32_t len)
122 {
123 int32_t offset;
124
125 offset = bytecode_reserve(fb, align, len);
126 if (offset < 0)
127 return offset;
128 memcpy(&(*fb)->b.data[offset], data, len);
129 return 0;
130 }
131
132 static
133 int bytecode_push_logical(struct lttng_filter_bytecode_alloc **fb,
134 struct logical_op *data,
135 uint32_t align, uint32_t len,
136 uint16_t *skip_offset)
137 {
138 int32_t offset;
139
140 offset = bytecode_reserve(fb, align, len);
141 if (offset < 0)
142 return offset;
143 memcpy(&(*fb)->b.data[offset], data, len);
144 *skip_offset =
145 (void *) &((struct logical_op *) &(*fb)->b.data[offset])->skip_offset
146 - (void *) &(*fb)->b.data[0];
147 return 0;
148 }
149
150 static
151 int bytecode_patch(struct lttng_filter_bytecode_alloc **fb,
152 const void *data,
153 uint16_t offset,
154 uint32_t len)
155 {
156 if (offset >= (*fb)->b.len) {
157 return -EINVAL;
158 }
159 memcpy(&(*fb)->b.data[offset], data, len);
160 return 0;
161 }
162
163 static
164 int visit_node_root(struct filter_parser_ctx *ctx, struct ir_op *node)
165 {
166 int ret;
167 struct return_op insn;
168
169 /* Visit child */
170 ret = recursive_visit_gen_bytecode(ctx, node->u.root.child);
171 if (ret)
172 return ret;
173
174 /* Generate end of bytecode instruction */
175 insn.op = FILTER_OP_RETURN;
176 return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn));
177 }
178
179 static
180 int visit_node_load(struct filter_parser_ctx *ctx, struct ir_op *node)
181 {
182 int ret;
183
184 switch (node->data_type) {
185 case IR_DATA_UNKNOWN:
186 default:
187 fprintf(stderr, "[error] Unknown data type in %s\n",
188 __func__);
189 return -EINVAL;
190
191 case IR_DATA_STRING:
192 {
193 struct load_op *insn;
194 uint32_t insn_len = sizeof(struct load_op)
195 + strlen(node->u.load.u.string) + 1;
196
197 insn = calloc(insn_len, 1);
198 if (!insn)
199 return -ENOMEM;
200 insn->op = FILTER_OP_LOAD_STRING;
201 strcpy(insn->data, node->u.load.u.string);
202 ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len);
203 free(insn);
204 return ret;
205 }
206 case IR_DATA_NUMERIC:
207 {
208 struct load_op *insn;
209 uint32_t insn_len = sizeof(struct load_op)
210 + sizeof(struct literal_numeric);
211
212 insn = calloc(insn_len, 1);
213 if (!insn)
214 return -ENOMEM;
215 insn->op = FILTER_OP_LOAD_S64;
216 *(int64_t *) insn->data = node->u.load.u.num;
217 ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len);
218 free(insn);
219 return ret;
220 }
221 case IR_DATA_FLOAT:
222 {
223 struct load_op *insn;
224 uint32_t insn_len = sizeof(struct load_op)
225 + sizeof(struct literal_double);
226
227 insn = calloc(insn_len, 1);
228 if (!insn)
229 return -ENOMEM;
230 insn->op = FILTER_OP_LOAD_DOUBLE;
231 *(double *) insn->data = node->u.load.u.flt;
232 ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len);
233 free(insn);
234 return ret;
235 }
236 case IR_DATA_FIELD_REF:
237 {
238 struct load_op *insn;
239 uint32_t insn_len = sizeof(struct load_op)
240 + sizeof(struct field_ref);
241 struct field_ref ref_offset;
242 uint16_t reloc_offset;
243
244 insn = calloc(insn_len, 1);
245 if (!insn)
246 return -ENOMEM;
247 insn->op = FILTER_OP_LOAD_FIELD_REF;
248 ref_offset.offset = (uint16_t) -1U;
249 memcpy(insn->data, &ref_offset, sizeof(ref_offset));
250 /* reloc_offset points to struct load_op */
251 reloc_offset = bytecode_get_len(&ctx->bytecode->b);
252 ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len);
253 if (ret) {
254 free(insn);
255 return ret;
256 }
257 /* append reloc */
258 ret = bytecode_push(&ctx->bytecode_reloc, &reloc_offset,
259 1, sizeof(reloc_offset));
260 if (ret) {
261 free(insn);
262 return ret;
263 }
264 ret = bytecode_push(&ctx->bytecode_reloc, node->u.load.u.ref,
265 1, strlen(node->u.load.u.ref) + 1);
266 free(insn);
267 return ret;
268 }
269 }
270 }
271
272 static
273 int visit_node_unary(struct filter_parser_ctx *ctx, struct ir_op *node)
274 {
275 int ret;
276 struct unary_op insn;
277
278 /* Visit child */
279 ret = recursive_visit_gen_bytecode(ctx, node->u.unary.child);
280 if (ret)
281 return ret;
282
283 /* Generate end of bytecode instruction */
284 switch (node->u.unary.type) {
285 case AST_UNARY_UNKNOWN:
286 default:
287 fprintf(stderr, "[error] Unknown unary node type in %s\n",
288 __func__);
289 return -EINVAL;
290 case AST_UNARY_PLUS:
291 /* Nothing to do. */
292 return 0;
293 case AST_UNARY_MINUS:
294 insn.op = FILTER_OP_UNARY_MINUS;
295 return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn));
296 case AST_UNARY_NOT:
297 insn.op = FILTER_OP_UNARY_NOT;
298 return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn));
299 }
300 }
301
302 /*
303 * Binary comparator nesting is disallowed. This allows fitting into
304 * only 2 registers.
305 */
306 static
307 int visit_node_binary(struct filter_parser_ctx *ctx, struct ir_op *node)
308 {
309 int ret;
310 struct binary_op insn;
311
312 /* Visit child */
313 ret = recursive_visit_gen_bytecode(ctx, node->u.binary.left);
314 if (ret)
315 return ret;
316 ret = recursive_visit_gen_bytecode(ctx, node->u.binary.right);
317 if (ret)
318 return ret;
319
320 switch (node->u.binary.type) {
321 case AST_OP_UNKNOWN:
322 default:
323 fprintf(stderr, "[error] Unknown unary node type in %s\n",
324 __func__);
325 return -EINVAL;
326
327 case AST_OP_AND:
328 case AST_OP_OR:
329 fprintf(stderr, "[error] Unexpected logical node type in %s\n",
330 __func__);
331 return -EINVAL;
332
333 case AST_OP_MUL:
334 insn.op = FILTER_OP_MUL;
335 break;
336 case AST_OP_DIV:
337 insn.op = FILTER_OP_DIV;
338 break;
339 case AST_OP_MOD:
340 insn.op = FILTER_OP_MOD;
341 break;
342 case AST_OP_PLUS:
343 insn.op = FILTER_OP_PLUS;
344 break;
345 case AST_OP_MINUS:
346 insn.op = FILTER_OP_MINUS;
347 break;
348 case AST_OP_RSHIFT:
349 insn.op = FILTER_OP_RSHIFT;
350 break;
351 case AST_OP_LSHIFT:
352 insn.op = FILTER_OP_LSHIFT;
353 break;
354 case AST_OP_BIN_AND:
355 insn.op = FILTER_OP_BIN_AND;
356 break;
357 case AST_OP_BIN_OR:
358 insn.op = FILTER_OP_BIN_OR;
359 break;
360 case AST_OP_BIN_XOR:
361 insn.op = FILTER_OP_BIN_XOR;
362 break;
363
364 case AST_OP_EQ:
365 insn.op = FILTER_OP_EQ;
366 break;
367 case AST_OP_NE:
368 insn.op = FILTER_OP_NE;
369 break;
370 case AST_OP_GT:
371 insn.op = FILTER_OP_GT;
372 break;
373 case AST_OP_LT:
374 insn.op = FILTER_OP_LT;
375 break;
376 case AST_OP_GE:
377 insn.op = FILTER_OP_GE;
378 break;
379 case AST_OP_LE:
380 insn.op = FILTER_OP_LE;
381 break;
382 }
383 return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn));
384 }
385
386 /*
387 * A logical op always return a s64 (1 or 0).
388 */
389 static
390 int visit_node_logical(struct filter_parser_ctx *ctx, struct ir_op *node)
391 {
392 int ret;
393 struct logical_op insn;
394 uint16_t skip_offset_loc;
395 uint16_t target_loc;
396
397 /* Visit left child */
398 ret = recursive_visit_gen_bytecode(ctx, node->u.binary.left);
399 if (ret)
400 return ret;
401 /* Cast to s64 if float or field ref */
402 if (node->u.binary.left->data_type == IR_DATA_FIELD_REF
403 || node->u.binary.left->data_type == IR_DATA_FLOAT) {
404 struct cast_op cast_insn;
405
406 if (node->u.binary.left->data_type == IR_DATA_FIELD_REF) {
407 cast_insn.op = FILTER_OP_CAST_TO_S64;
408 } else {
409 cast_insn.op = FILTER_OP_CAST_DOUBLE_TO_S64;
410 }
411 ret = bytecode_push(&ctx->bytecode, &cast_insn,
412 1, sizeof(cast_insn));
413 if (ret)
414 return ret;
415 }
416 switch (node->u.logical.type) {
417 default:
418 fprintf(stderr, "[error] Unknown node type in %s\n",
419 __func__);
420 return -EINVAL;
421
422 case AST_OP_AND:
423 insn.op = FILTER_OP_AND;
424 break;
425 case AST_OP_OR:
426 insn.op = FILTER_OP_OR;
427 break;
428 }
429 insn.skip_offset = (uint16_t) -1UL; /* Temporary */
430 ret = bytecode_push_logical(&ctx->bytecode, &insn, 1, sizeof(insn),
431 &skip_offset_loc);
432 if (ret)
433 return ret;
434 /* Visit right child */
435 ret = recursive_visit_gen_bytecode(ctx, node->u.binary.right);
436 if (ret)
437 return ret;
438 /* Cast to s64 if float or field ref */
439 if (node->u.binary.right->data_type == IR_DATA_FIELD_REF
440 || node->u.binary.right->data_type == IR_DATA_FLOAT) {
441 struct cast_op cast_insn;
442
443 if (node->u.binary.right->data_type == IR_DATA_FIELD_REF) {
444 cast_insn.op = FILTER_OP_CAST_TO_S64;
445 } else {
446 cast_insn.op = FILTER_OP_CAST_DOUBLE_TO_S64;
447 }
448 ret = bytecode_push(&ctx->bytecode, &cast_insn,
449 1, sizeof(cast_insn));
450 if (ret)
451 return ret;
452 }
453 /* We now know where the logical op can skip. */
454 target_loc = (uint16_t) bytecode_get_len(&ctx->bytecode->b);
455 ret = bytecode_patch(&ctx->bytecode,
456 &target_loc, /* Offset to jump to */
457 skip_offset_loc, /* Where to patch */
458 sizeof(uint16_t));
459 return ret;
460 }
461
462 /*
463 * Postorder traversal of the tree. We need the children result before
464 * we can evaluate the parent.
465 */
466 static
467 int recursive_visit_gen_bytecode(struct filter_parser_ctx *ctx,
468 struct ir_op *node)
469 {
470 switch (node->op) {
471 case IR_OP_UNKNOWN:
472 default:
473 fprintf(stderr, "[error] Unknown node type in %s\n",
474 __func__);
475 return -EINVAL;
476
477 case IR_OP_ROOT:
478 return visit_node_root(ctx, node);
479 case IR_OP_LOAD:
480 return visit_node_load(ctx, node);
481 case IR_OP_UNARY:
482 return visit_node_unary(ctx, node);
483 case IR_OP_BINARY:
484 return visit_node_binary(ctx, node);
485 case IR_OP_LOGICAL:
486 return visit_node_logical(ctx, node);
487 }
488 }
489
490 __attribute__((visibility("hidden")))
491 void filter_bytecode_free(struct filter_parser_ctx *ctx)
492 {
493 free(ctx->bytecode);
494 ctx->bytecode = NULL;
495 free(ctx->bytecode_reloc);
496 ctx->bytecode_reloc = NULL;
497 }
498
499 __attribute__((visibility("hidden")))
500 int filter_visitor_bytecode_generate(struct filter_parser_ctx *ctx)
501 {
502 int ret;
503
504 ret = bytecode_init(&ctx->bytecode);
505 if (ret)
506 return ret;
507 ret = bytecode_init(&ctx->bytecode_reloc);
508 if (ret)
509 goto error;
510 ret = recursive_visit_gen_bytecode(ctx, ctx->ir_root);
511 if (ret)
512 goto error;
513
514 /* Finally, append symbol table to bytecode */
515 ctx->bytecode->b.reloc_table_offset = bytecode_get_len(&ctx->bytecode->b);
516 return bytecode_push(&ctx->bytecode, ctx->bytecode_reloc->b.data,
517 1, bytecode_get_len(&ctx->bytecode_reloc->b));
518
519 error:
520 filter_bytecode_free(ctx);
521 return ret;
522 }
This page took 0.06153 seconds and 5 git commands to generate.