bytecode: allow interpreter to return any type
[lttng-ust.git] / liblttng-ust / lttng-filter-validator.c
index 6e52065c2d81b40dcc417cc754b9bdfa7e93f789..e6982369bdc1d8c8e90c4c9d8ff29c6f9b20c16a 100644 (file)
  */
 
 #define _LGPL_SOURCE
-#include <urcu-bp.h>
+#include <stddef.h>
+#include <stdint.h>
 #include <time.h>
-#include "lttng-filter.h"
 
+#include <urcu-bp.h>
 #include <urcu/rculfhash.h>
+
+#include "lttng-filter.h"
 #include "lttng-hash-helper.h"
 #include "string-utils.h"
+#include "ust-events-internal.h"
 
 /*
  * Number of merge points for hash table size. Hash table initialized to
@@ -156,6 +160,7 @@ int bin_op_compare_check(struct vstack *stack, filter_opcode_t opcode,
                        }
                        break;
                case REG_S64:
+               case REG_U64:
                case REG_DOUBLE:
                        goto error_mismatch;
                }
@@ -174,11 +179,13 @@ int bin_op_compare_check(struct vstack *stack, filter_opcode_t opcode,
                        break;
                case REG_STAR_GLOB_STRING:
                case REG_S64:
+               case REG_U64:
                case REG_DOUBLE:
                        goto error_mismatch;
                }
                break;
        case REG_S64:
+       case REG_U64:
        case REG_DOUBLE:
                switch (vstack_bx(stack)->type) {
                default:
@@ -190,6 +197,7 @@ int bin_op_compare_check(struct vstack *stack, filter_opcode_t opcode,
                case REG_STAR_GLOB_STRING:
                        goto error_mismatch;
                case REG_S64:
+               case REG_U64:
                case REG_DOUBLE:
                        break;
                }
@@ -232,6 +240,7 @@ int bin_op_bitwise_check(struct vstack *stack, filter_opcode_t opcode,
        case REG_UNKNOWN:
                goto unknown;
        case REG_S64:
+       case REG_U64:
                switch (vstack_bx(stack)->type) {
                default:
                        goto error_type;
@@ -239,6 +248,7 @@ int bin_op_bitwise_check(struct vstack *stack, filter_opcode_t opcode,
                case REG_UNKNOWN:
                        goto unknown;
                case REG_S64:
+               case REG_U64:
                        break;
                }
                break;
@@ -296,6 +306,7 @@ int bytecode_validate_overflow(struct bytecode_runtime *bytecode,
        }
 
        case FILTER_OP_RETURN:
+       case FILTER_OP_RETURN_S64:
        {
                if (unlikely(pc + sizeof(struct return_op)
                                > start_pc + bytecode->len)) {
@@ -310,8 +321,6 @@ int bytecode_validate_overflow(struct bytecode_runtime *bytecode,
        case FILTER_OP_MOD:
        case FILTER_OP_PLUS:
        case FILTER_OP_MINUS:
-       case FILTER_OP_RSHIFT:
-       case FILTER_OP_LSHIFT:
        {
                ERR("unsupported bytecode op %u\n",
                        (unsigned int) *(filter_opcode_t *) pc);
@@ -357,6 +366,8 @@ int bytecode_validate_overflow(struct bytecode_runtime *bytecode,
        case FILTER_OP_LT_S64_DOUBLE:
        case FILTER_OP_GE_S64_DOUBLE:
        case FILTER_OP_LE_S64_DOUBLE:
+       case FILTER_OP_BIT_RSHIFT:
+       case FILTER_OP_BIT_LSHIFT:
        case FILTER_OP_BIT_AND:
        case FILTER_OP_BIT_OR:
        case FILTER_OP_BIT_XOR:
@@ -378,6 +389,7 @@ int bytecode_validate_overflow(struct bytecode_runtime *bytecode,
        case FILTER_OP_UNARY_PLUS_DOUBLE:
        case FILTER_OP_UNARY_MINUS_DOUBLE:
        case FILTER_OP_UNARY_NOT_DOUBLE:
+       case FILTER_OP_UNARY_BIT_NOT:
        {
                if (unlikely(pc + sizeof(struct unary_op)
                                > start_pc + bytecode->len)) {
@@ -505,6 +517,7 @@ int bytecode_validate_overflow(struct bytecode_runtime *bytecode,
                if (unlikely(pc + sizeof(struct load_op) + sizeof(struct get_symbol)
                                > start_pc + bytecode->len)) {
                        ret = -ERANGE;
+                       break;
                }
                ret = validate_get_symbol(bytecode, sym);
                break;
@@ -577,6 +590,7 @@ int validate_instruction_context(struct bytecode_runtime *bytecode,
        }
 
        case FILTER_OP_RETURN:
+       case FILTER_OP_RETURN_S64:
        {
                goto end;
        }
@@ -587,8 +601,6 @@ int validate_instruction_context(struct bytecode_runtime *bytecode,
        case FILTER_OP_MOD:
        case FILTER_OP_PLUS:
        case FILTER_OP_MINUS:
-       case FILTER_OP_RSHIFT:
-       case FILTER_OP_LSHIFT:
        {
                ERR("unsupported bytecode op %u\n",
                        (unsigned int) opcode);
@@ -689,8 +701,20 @@ int validate_instruction_context(struct bytecode_runtime *bytecode,
                        ret = -EINVAL;
                        goto end;
                }
-               if (vstack_ax(stack)->type != REG_S64
-                               || vstack_bx(stack)->type != REG_S64) {
+               switch (vstack_ax(stack)->type) {
+               case REG_S64:
+               case REG_U64:
+                       break;
+               default:
+                       ERR("Unexpected register type for s64 comparator\n");
+                       ret = -EINVAL;
+                       goto end;
+               }
+               switch (vstack_bx(stack)->type) {
+               case REG_S64:
+               case REG_U64:
+                       break;
+               default:
                        ERR("Unexpected register type for s64 comparator\n");
                        ret = -EINVAL;
                        goto end;
@@ -730,7 +754,19 @@ int validate_instruction_context(struct bytecode_runtime *bytecode,
                        ret = -EINVAL;
                        goto end;
                }
-               if (vstack_ax(stack)->type != REG_S64 && vstack_bx(stack)->type != REG_DOUBLE) {
+               switch (vstack_ax(stack)->type) {
+               case REG_S64:
+               case REG_U64:
+                       break;
+               default:
+                       ERR("Double-S64 operator has unexpected register types\n");
+                       ret = -EINVAL;
+                       goto end;
+               }
+               switch (vstack_bx(stack)->type) {
+               case REG_DOUBLE:
+                       break;
+               default:
                        ERR("Double-S64 operator has unexpected register types\n");
                        ret = -EINVAL;
                        goto end;
@@ -750,7 +786,19 @@ int validate_instruction_context(struct bytecode_runtime *bytecode,
                        ret = -EINVAL;
                        goto end;
                }
-               if (vstack_ax(stack)->type != REG_DOUBLE && vstack_bx(stack)->type != REG_S64) {
+               switch (vstack_ax(stack)->type) {
+               case REG_DOUBLE:
+                       break;
+               default:
+                       ERR("S64-Double operator has unexpected register types\n");
+                       ret = -EINVAL;
+                       goto end;
+               }
+               switch (vstack_bx(stack)->type) {
+               case REG_S64:
+               case REG_U64:
+                       break;
+               default:
                        ERR("S64-Double operator has unexpected register types\n");
                        ret = -EINVAL;
                        goto end;
@@ -758,6 +806,16 @@ int validate_instruction_context(struct bytecode_runtime *bytecode,
                break;
        }
 
+       case FILTER_OP_BIT_RSHIFT:
+               ret = bin_op_bitwise_check(stack, opcode, ">>");
+               if (ret < 0)
+                       goto end;
+               break;
+       case FILTER_OP_BIT_LSHIFT:
+               ret = bin_op_bitwise_check(stack, opcode, "<<");
+               if (ret < 0)
+                       goto end;
+               break;
        case FILTER_OP_BIT_AND:
                ret = bin_op_bitwise_check(stack, opcode, "&");
                if (ret < 0)
@@ -797,6 +855,8 @@ int validate_instruction_context(struct bytecode_runtime *bytecode,
                        goto end;
                case REG_S64:
                        break;
+               case REG_U64:
+                       break;
                case REG_DOUBLE:
                        break;
                case REG_UNKNOWN:
@@ -804,6 +864,34 @@ int validate_instruction_context(struct bytecode_runtime *bytecode,
                }
                break;
        }
+       case FILTER_OP_UNARY_BIT_NOT:
+       {
+               if (!vstack_ax(stack)) {
+                       ERR("Empty stack\n");
+                       ret = -EINVAL;
+                       goto end;
+               }
+               switch (vstack_ax(stack)->type) {
+               default:
+                       ERR("unknown register type\n");
+                       ret = -EINVAL;
+                       goto end;
+
+               case REG_STRING:
+               case REG_STAR_GLOB_STRING:
+               case REG_DOUBLE:
+                       ERR("Unary bitwise op can only be applied to numeric registers\n");
+                       ret = -EINVAL;
+                       goto end;
+               case REG_S64:
+                       break;
+               case REG_U64:
+                       break;
+               case REG_UNKNOWN:
+                       break;
+               }
+               break;
+       }
 
        case FILTER_OP_UNARY_PLUS_S64:
        case FILTER_OP_UNARY_MINUS_S64:
@@ -814,7 +902,8 @@ int validate_instruction_context(struct bytecode_runtime *bytecode,
                        ret = -EINVAL;
                        goto end;
                }
-               if (vstack_ax(stack)->type != REG_S64) {
+               if (vstack_ax(stack)->type != REG_S64 &&
+                               vstack_ax(stack)->type != REG_U64) {
                        ERR("Invalid register type\n");
                        ret = -EINVAL;
                        goto end;
@@ -851,8 +940,9 @@ int validate_instruction_context(struct bytecode_runtime *bytecode,
                        goto end;
                }
                if (vstack_ax(stack)->type != REG_S64
+                               && vstack_ax(stack)->type != REG_U64
                                && vstack_ax(stack)->type != REG_UNKNOWN) {
-                       ERR("Logical comparator expects S64 or dynamic register\n");
+                       ERR("Logical comparator expects S64, U64 or dynamic register\n");
                        ret = -EINVAL;
                        goto end;
                }
@@ -943,6 +1033,8 @@ int validate_instruction_context(struct bytecode_runtime *bytecode,
                        goto end;
                case REG_S64:
                        break;
+               case REG_U64:
+                       break;
                case REG_DOUBLE:
                        break;
                case REG_UNKNOWN:
@@ -1208,6 +1300,10 @@ int exec_insn(struct bytecode_runtime *bytecode,
                }
                switch (vstack_ax(stack)->type) {
                case REG_S64:
+               case REG_U64:
+               case REG_DOUBLE:
+               case REG_STRING:
+               case REG_PTR:
                case REG_UNKNOWN:
                        break;
                default:
@@ -1220,6 +1316,28 @@ int exec_insn(struct bytecode_runtime *bytecode,
                ret = 0;
                goto end;
        }
+       case FILTER_OP_RETURN_S64:
+       {
+               if (!vstack_ax(stack)) {
+                       ERR("Empty stack\n");
+                       ret = -EINVAL;
+                       goto end;
+               }
+               switch (vstack_ax(stack)->type) {
+               case REG_S64:
+               case REG_U64:
+                       break;
+               default:
+               case REG_UNKNOWN:
+                       ERR("Unexpected register type %d at end of bytecode\n",
+                               (int) vstack_ax(stack)->type);
+                       ret = -EINVAL;
+                       goto end;
+               }
+
+               ret = 0;
+               goto end;
+       }
 
        /* binary */
        case FILTER_OP_MUL:
@@ -1227,8 +1345,6 @@ int exec_insn(struct bytecode_runtime *bytecode,
        case FILTER_OP_MOD:
        case FILTER_OP_PLUS:
        case FILTER_OP_MINUS:
-       case FILTER_OP_RSHIFT:
-       case FILTER_OP_LSHIFT:
        {
                ERR("unsupported bytecode op %u\n",
                        (unsigned int) *(filter_opcode_t *) pc);
@@ -1274,6 +1390,39 @@ int exec_insn(struct bytecode_runtime *bytecode,
        case FILTER_OP_LT_S64_DOUBLE:
        case FILTER_OP_GE_S64_DOUBLE:
        case FILTER_OP_LE_S64_DOUBLE:
+       {
+               /* Pop 2, push 1 */
+               if (vstack_pop(stack)) {
+                       ret = -EINVAL;
+                       goto end;
+               }
+               if (!vstack_ax(stack)) {
+                       ERR("Empty stack\n");
+                       ret = -EINVAL;
+                       goto end;
+               }
+               switch (vstack_ax(stack)->type) {
+               case REG_S64:
+               case REG_U64:
+               case REG_DOUBLE:
+               case REG_STRING:
+               case REG_STAR_GLOB_STRING:
+               case REG_UNKNOWN:
+                       break;
+               default:
+                       ERR("Unexpected register type %d for operation\n",
+                               (int) vstack_ax(stack)->type);
+                       ret = -EINVAL;
+                       goto end;
+               }
+
+               vstack_ax(stack)->type = REG_S64;
+               next_pc += sizeof(struct binary_op);
+               break;
+       }
+
+       case FILTER_OP_BIT_RSHIFT:
+       case FILTER_OP_BIT_LSHIFT:
        case FILTER_OP_BIT_AND:
        case FILTER_OP_BIT_OR:
        case FILTER_OP_BIT_XOR:
@@ -1290,6 +1439,7 @@ int exec_insn(struct bytecode_runtime *bytecode,
                }
                switch (vstack_ax(stack)->type) {
                case REG_S64:
+               case REG_U64:
                case REG_DOUBLE:
                case REG_STRING:
                case REG_STAR_GLOB_STRING:
@@ -1302,7 +1452,7 @@ int exec_insn(struct bytecode_runtime *bytecode,
                        goto end;
                }
 
-               vstack_ax(stack)->type = REG_S64;
+               vstack_ax(stack)->type = REG_U64;
                next_pc += sizeof(struct binary_op);
                break;
        }
@@ -1321,6 +1471,7 @@ int exec_insn(struct bytecode_runtime *bytecode,
                case REG_UNKNOWN:
                case REG_DOUBLE:
                case REG_S64:
+               case REG_U64:
                        break;
                default:
                        ERR("Unexpected register type %d for operation\n",
@@ -1345,6 +1496,7 @@ int exec_insn(struct bytecode_runtime *bytecode,
                }
                switch (vstack_ax(stack)->type) {
                case REG_S64:
+               case REG_U64:
                        break;
                default:
                        ERR("Unexpected register type %d for operation\n",
@@ -1353,7 +1505,6 @@ int exec_insn(struct bytecode_runtime *bytecode,
                        goto end;
                }
 
-               vstack_ax(stack)->type = REG_S64;
                next_pc += sizeof(struct unary_op);
                break;
        }
@@ -1370,6 +1521,7 @@ int exec_insn(struct bytecode_runtime *bytecode,
                case REG_UNKNOWN:
                case REG_DOUBLE:
                case REG_S64:
+               case REG_U64:
                        break;
                default:
                        ERR("Unexpected register type %d for operation\n",
@@ -1378,7 +1530,32 @@ int exec_insn(struct bytecode_runtime *bytecode,
                        goto end;
                }
 
-               vstack_ax(stack)->type = REG_S64;
+               next_pc += sizeof(struct unary_op);
+               break;
+       }
+
+       case FILTER_OP_UNARY_BIT_NOT:
+       {
+               /* Pop 1, push 1 */
+               if (!vstack_ax(stack)) {
+                       ERR("Empty stack\n");
+                       ret = -EINVAL;
+                       goto end;
+               }
+               switch (vstack_ax(stack)->type) {
+               case REG_UNKNOWN:
+               case REG_S64:
+               case REG_U64:
+                       break;
+               case REG_DOUBLE:
+               default:
+                       ERR("Unexpected register type %d for operation\n",
+                               (int) vstack_ax(stack)->type);
+                       ret = -EINVAL;
+                       goto end;
+               }
+
+               vstack_ax(stack)->type = REG_U64;
                next_pc += sizeof(struct unary_op);
                break;
        }
@@ -1453,6 +1630,7 @@ int exec_insn(struct bytecode_runtime *bytecode,
                /* There is always a cast-to-s64 operation before a or/and op. */
                switch (vstack_ax(stack)->type) {
                case REG_S64:
+               case REG_U64:
                        break;
                default:
                        ERR("Incorrect register type %d for operation\n",
@@ -1586,6 +1764,7 @@ int exec_insn(struct bytecode_runtime *bytecode,
                }
                switch (vstack_ax(stack)->type) {
                case REG_S64:
+               case REG_U64:
                case REG_DOUBLE:
                case REG_UNKNOWN:
                        break;
@@ -1643,6 +1822,23 @@ int exec_insn(struct bytecode_runtime *bytecode,
        case FILTER_OP_LOAD_FIELD_S16:
        case FILTER_OP_LOAD_FIELD_S32:
        case FILTER_OP_LOAD_FIELD_S64:
+       {
+               /* Pop 1, push 1 */
+               if (!vstack_ax(stack)) {
+                       ERR("Empty stack\n");
+                       ret = -EINVAL;
+                       goto end;
+               }
+               if (vstack_ax(stack)->type != REG_PTR) {
+                       ERR("Expecting pointer on top of stack\n");
+                       ret = -EINVAL;
+                       goto end;
+               }
+               vstack_ax(stack)->type = REG_S64;
+               next_pc += sizeof(struct load_op);
+               break;
+       }
+
        case FILTER_OP_LOAD_FIELD_U8:
        case FILTER_OP_LOAD_FIELD_U16:
        case FILTER_OP_LOAD_FIELD_U32:
@@ -1659,7 +1855,7 @@ int exec_insn(struct bytecode_runtime *bytecode,
                        ret = -EINVAL;
                        goto end;
                }
-               vstack_ax(stack)->type = REG_S64;
+               vstack_ax(stack)->type = REG_U64;
                next_pc += sizeof(struct load_op);
                break;
        }
This page took 0.035033 seconds and 4 git commands to generate.