Filter: ensure logical operator merge is always s64
[lttng-ust.git] / liblttng-ust / lttng-filter.c
index 7fe2a7b5d5642cb3ac92f0de76589448956ea2c6..db4d107a63df629f07af9c3b2cf6f82858cd92bc 100644 (file)
@@ -163,6 +163,11 @@ static const char *opnames[] = {
        [ FILTER_OP_LOAD_STRING ] = "LOAD_STRING",
        [ FILTER_OP_LOAD_S64 ] = "LOAD_S64",
        [ FILTER_OP_LOAD_DOUBLE ] = "LOAD_DOUBLE",
+
+       /* cast */
+       [ FILTER_OP_CAST_TO_S64 ] = "CAST_TO_S64",
+       [ FILTER_OP_CAST_DOUBLE_TO_S64 ] = "CAST_DOUBLE_TO_S64",
+       [ FILTER_OP_CAST_NOP ] = "CAST_NOP",
 };
 
 static
@@ -264,10 +269,12 @@ int lttng_filter_false(void *filter_data,
        return 0;
 }
 
-#define INTERPRETER_USE_SWITCH
-
 #ifdef INTERPRETER_USE_SWITCH
 
+/*
+ * Fallback for compilers that do not support taking address of labels.
+ */
+
 #define START_OP       \
        start_pc = &bytecode->data[0]; \
        for (pc = next_pc = start_pc; pc - start_pc < bytecode->len; \
@@ -286,7 +293,25 @@ int lttng_filter_false(void *filter_data,
 
 #else
 
-#define OP(name)       
+/*
+ * Dispatch-table based interpreter.
+ */
+
+#define START_OP                                       \
+       start_pc = &bytecode->data[0];                  \
+       pc = next_pc = start_pc;                        \
+       if (unlikely(pc - start_pc >= bytecode->len))   \
+               goto end;                               \
+       goto *dispatch[*(filter_opcode_t *) pc];
+
+#define OP(name)                                       \
+LABEL_##name
+
+#define PO                                             \
+               pc = next_pc;                           \
+               goto *dispatch[*(filter_opcode_t *) pc];
+
+#define END_OP
 
 #endif
 
@@ -301,7 +326,7 @@ int lttng_filter_interpret_bytecode(void *filter_data,
        struct reg reg[NR_REG];
 #ifndef INTERPRETER_USE_SWITCH
        static void *dispatch[NR_FILTER_OPS] = {
-               [ FILTER_OP_UNKNOWN ] = &&LABEL_FILTER_OP_UNKNOWN = 0,
+               [ FILTER_OP_UNKNOWN ] = &&LABEL_FILTER_OP_UNKNOWN,
 
                [ FILTER_OP_RETURN ] = &&LABEL_FILTER_OP_RETURN,
 
@@ -374,6 +399,11 @@ int lttng_filter_interpret_bytecode(void *filter_data,
                [ FILTER_OP_LOAD_STRING ] = &&LABEL_FILTER_OP_LOAD_STRING,
                [ FILTER_OP_LOAD_S64 ] = &&LABEL_FILTER_OP_LOAD_S64,
                [ FILTER_OP_LOAD_DOUBLE ] = &&LABEL_FILTER_OP_LOAD_DOUBLE,
+
+               /* cast */
+               [ FILTER_OP_CAST_TO_S64 ] = &&LABEL_FILTER_OP_CAST_TO_S64,
+               [ FILTER_OP_CAST_DOUBLE_TO_S64 ] = &&LABEL_FILTER_OP_CAST_DOUBLE_TO_S64,
+               [ FILTER_OP_CAST_NOP ] = &&LABEL_FILTER_OP_CAST_NOP,
        };
 #endif /* #ifndef INTERPRETER_USE_SWITCH */
 
@@ -629,8 +659,7 @@ int lttng_filter_interpret_bytecode(void *filter_data,
                        struct logical_op *insn = (struct logical_op *) pc;
 
                        /* If REG_R0 is 0, skip and evaluate to 0 */
-                       if ((reg[REG_R0].type == REG_S64 && reg[REG_R0].v == 0)
-                                       || unlikely(reg[REG_R0].type == REG_DOUBLE && reg[REG_R0].d == 0.0)) {
+                       if (unlikely(reg[REG_R0].v == 0)) {
                                dbg_printf("Jumping to bytecode offset %u\n",
                                        (unsigned int) insn->skip_offset);
                                next_pc = start_pc + insn->skip_offset;
@@ -645,8 +674,7 @@ int lttng_filter_interpret_bytecode(void *filter_data,
 
                        /* If REG_R0 is nonzero, skip and evaluate to 1 */
 
-                       if ((reg[REG_R0].type == REG_S64 && reg[REG_R0].v != 0)
-                                       || unlikely(reg[REG_R0].type == REG_DOUBLE && reg[REG_R0].d != 0.0)) {
+                       if (unlikely(reg[REG_R0].v != 0)) {
                                reg[REG_R0].v = 1;
                                dbg_printf("Jumping to bytecode offset %u\n",
                                        (unsigned int) insn->skip_offset);
@@ -657,6 +685,7 @@ int lttng_filter_interpret_bytecode(void *filter_data,
                        PO;
                }
 
+
                /* load */
                OP(FILTER_OP_LOAD_FIELD_REF_STRING):
                {
@@ -703,7 +732,6 @@ int lttng_filter_interpret_bytecode(void *filter_data,
                        memcpy(&reg[insn->reg].v, &filter_stack_data[ref->offset],
                                sizeof(struct literal_numeric));
                        reg[insn->reg].type = REG_S64;
-                       reg[insn->reg].literal = 0;
                        dbg_printf("ref load s64 %" PRIi64 "\n", reg[insn->reg].v);
                        next_pc += sizeof(struct load_op) + sizeof(struct field_ref);
                        PO;
@@ -719,7 +747,6 @@ int lttng_filter_interpret_bytecode(void *filter_data,
                        memcpy(&reg[insn->reg].d, &filter_stack_data[ref->offset],
                                sizeof(struct literal_double));
                        reg[insn->reg].type = REG_DOUBLE;
-                       reg[insn->reg].literal = 0;
                        dbg_printf("ref load double %g\n", reg[insn->reg].d);
                        next_pc += sizeof(struct load_op) + sizeof(struct field_ref);
                        PO;
@@ -746,7 +773,6 @@ int lttng_filter_interpret_bytecode(void *filter_data,
                                sizeof(struct literal_numeric));
                        dbg_printf("load s64 %" PRIi64 "\n", reg[insn->reg].v);
                        reg[insn->reg].type = REG_S64;
-                       reg[insn->reg].literal = 1;
                        next_pc += sizeof(struct load_op)
                                        + sizeof(struct literal_numeric);
                        PO;
@@ -760,12 +786,34 @@ int lttng_filter_interpret_bytecode(void *filter_data,
                                sizeof(struct literal_double));
                        dbg_printf("load s64 %g\n", reg[insn->reg].d);
                        reg[insn->reg].type = REG_DOUBLE;
-                       reg[insn->reg].literal = 1;
                        next_pc += sizeof(struct load_op)
                                        + sizeof(struct literal_double);
                        PO;
                }
 
+               /* cast */
+               OP(FILTER_OP_CAST_TO_S64):
+                       ERR("unsupported non-specialized bytecode op %u\n",
+                               (unsigned int) *(filter_opcode_t *) pc);
+                       ret = -EINVAL;
+                       goto end;
+
+               OP(FILTER_OP_CAST_DOUBLE_TO_S64):
+               {
+                       struct cast_op *insn = (struct cast_op *) pc;
+
+                       reg[insn->reg].v = (int64_t) reg[insn->reg].d;
+                       reg[insn->reg].type = REG_S64;
+                       next_pc += sizeof(struct cast_op);
+                       PO;
+               }
+
+               OP(FILTER_OP_CAST_NOP):
+               {
+                       next_pc += sizeof(struct cast_op);
+                       PO;
+               }
+
        END_OP
 end:
        /* return 0 (discard) on error */
@@ -774,6 +822,11 @@ end:
        return retval;
 }
 
+#undef START_OP
+#undef OP
+#undef PO
+#undef END_OP
+
 static
 int bin_op_compare_check(struct vreg reg[NR_REG], const char *str)
 {
@@ -974,6 +1027,11 @@ int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode)
                                ret = -EINVAL;
                                goto end;
                        }
+                       if (reg[REG_R0].type != REG_DOUBLE && reg[REG_R1].type != REG_DOUBLE) {
+                               ERR("Double operator should have at least one double register\n");
+                               ret = -EINVAL;
+                               goto end;
+                       }
                        reg[REG_R0].type = REG_DOUBLE;
                        next_pc += sizeof(struct binary_op);
                        break;
@@ -1059,11 +1117,8 @@ int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode)
                {
                        struct logical_op *insn = (struct logical_op *) pc;
 
-                       if (unlikely(reg[REG_R0].type == REG_TYPE_UNKNOWN
-                                       || reg[REG_R1].type == REG_TYPE_UNKNOWN
-                                       || reg[REG_R0].type == REG_STRING
-                                       || reg[REG_R1].type == REG_STRING)) {
-                               ERR("Logical comparator can only be applied to numeric and floating point registers\n");
+                       if (reg[REG_R0].type != REG_S64) {
+                               ERR("Logical comparator expects S64 register\n");
                                ret = -EINVAL;
                                goto end;
                        }
@@ -1191,6 +1246,50 @@ int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode)
                                        + sizeof(struct literal_double);
                        break;
                }
+
+               case FILTER_OP_CAST_TO_S64:
+               case FILTER_OP_CAST_DOUBLE_TO_S64:
+               {
+                       struct cast_op *insn = (struct cast_op *) pc;
+
+                       if (unlikely(insn->reg >= REG_ERROR)) {
+                               ERR("invalid register %u\n",
+                                       (unsigned int) insn->reg);
+                               ret = -EINVAL;
+                               goto end;
+                       }
+                       switch (reg[insn->reg].type) {
+                       default:
+                               ERR("unknown register type\n");
+                               ret = -EINVAL;
+                               goto end;
+
+                       case REG_STRING:
+                               ERR("Cast op can only be applied to numeric or floating point registers\n");
+                               ret = -EINVAL;
+                               goto end;
+                       case REG_S64:
+                               break;
+                       case REG_DOUBLE:
+                               break;
+                       }
+                       if (insn->op == FILTER_OP_CAST_DOUBLE_TO_S64) {
+                               if (reg[insn->reg].type != REG_DOUBLE) {
+                                       ERR("Cast expects double\n");
+                                       ret = -EINVAL;
+                                       goto end;
+                               }
+                       }
+                       reg[insn->reg].type = REG_S64;
+                       next_pc += sizeof(struct cast_op);
+                       break;
+               }
+               case FILTER_OP_CAST_NOP:
+               {
+                       next_pc += sizeof(struct cast_op);
+                       break;
+               }
+
                }
        }
 end:
@@ -1450,6 +1549,7 @@ int lttng_filter_specialize_bytecode(struct bytecode_runtime *bytecode)
                                insn->op = FILTER_OP_UNARY_PLUS_DOUBLE;
                                break;
                        }
+                       next_pc += sizeof(struct unary_op);
                        break;
                }
 
@@ -1470,6 +1570,7 @@ int lttng_filter_specialize_bytecode(struct bytecode_runtime *bytecode)
                                insn->op = FILTER_OP_UNARY_MINUS_DOUBLE;
                                break;
                        }
+                       next_pc += sizeof(struct unary_op);
                        break;
                }
 
@@ -1490,6 +1591,7 @@ int lttng_filter_specialize_bytecode(struct bytecode_runtime *bytecode)
                                insn->op = FILTER_OP_UNARY_NOT_DOUBLE;
                                break;
                        }
+                       next_pc += sizeof(struct unary_op);
                        break;
                }
 
@@ -1579,14 +1681,54 @@ int lttng_filter_specialize_bytecode(struct bytecode_runtime *bytecode)
                                        + sizeof(struct literal_double);
                        break;
                }
+
+               /* cast */
+               case FILTER_OP_CAST_TO_S64:
+               {
+                       struct cast_op *insn = (struct cast_op *) pc;
+
+                       switch (reg[insn->reg].type) {
+                       default:
+                               ERR("unknown register type\n");
+                               ret = -EINVAL;
+                               goto end;
+
+                       case REG_STRING:
+                               ERR("Cast op can only be applied to numeric or floating point registers\n");
+                               ret = -EINVAL;
+                               goto end;
+                       case REG_S64:
+                               insn->op = FILTER_OP_CAST_NOP;
+                               break;
+                       case REG_DOUBLE:
+                               insn->op = FILTER_OP_CAST_DOUBLE_TO_S64;
+                               break;
+                       }
+                       reg[insn->reg].type = REG_S64;
+                       next_pc += sizeof(struct cast_op);
+                       break;
+               }
+               case FILTER_OP_CAST_DOUBLE_TO_S64:
+               {
+                       struct cast_op *insn = (struct cast_op *) pc;
+
+                       reg[insn->reg].type = REG_S64;
+                       next_pc += sizeof(struct cast_op);
+                       break;
+               }
+               case FILTER_OP_CAST_NOP:
+               {
+                       next_pc += sizeof(struct cast_op);
+                       break;
+               }
+
+
                }
        }
 end:
        return ret;
 }
 
-
-
 static
 int apply_field_reloc(struct ltt_event *event,
                struct bytecode_runtime *runtime,
This page took 0.039352 seconds and 4 git commands to generate.