LCOV - code coverage report
Current view: top level - lib/bpf - bpf_validate.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 784 874 89.7 %
Date: 2026-11-01 19:11:22 Functions: 55 59 93.2 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 575 777 74.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2018 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <stdio.h>
       6                 :            : #include <stdlib.h>
       7                 :            : #include <string.h>
       8                 :            : #include <errno.h>
       9                 :            : #include <stdint.h>
      10                 :            : #include <inttypes.h>
      11                 :            : 
      12                 :            : #include <rte_bpf_validate_debug.h>
      13                 :            : #include <rte_common.h>
      14                 :            : 
      15                 :            : #include "bpf_impl.h"
      16                 :            : #include "bpf_validate.h"
      17                 :            : #include "bpf_validate_debug.h"
      18                 :            : #include "bpf_value_set.h"
      19                 :            : 
      20                 :            : #define BPF_ARG_PTR_STACK RTE_BPF_ARG_RESERVED
      21                 :            : 
      22                 :            : /* type containing no values (AKA "bottom", "never" etc)  */
      23                 :            : #define BPF_ARG_UNINHABITED ((enum rte_bpf_arg_type)(RTE_BPF_ARG_UNDEF - 1))
      24                 :            : 
      25                 :            : struct bpf_reg_val {
      26                 :            :         struct rte_bpf_arg v;
      27                 :            :         uint64_t mask;
      28                 :            :         struct {
      29                 :            :                 int64_t min;
      30                 :            :                 int64_t max;
      31                 :            :         } s;
      32                 :            :         struct {
      33                 :            :                 uint64_t min;
      34                 :            :                 uint64_t max;
      35                 :            :         } u;
      36                 :            : };
      37                 :            : 
      38                 :            : struct bpf_eval_state {
      39                 :            :         SLIST_ENTRY(bpf_eval_state) next; /* for @safe list traversal */
      40                 :            :         struct bpf_reg_val rv[EBPF_REG_NUM];
      41                 :            :         struct bpf_reg_val sv[MAX_BPF_STACK_SIZE / sizeof(uint64_t)];
      42                 :            :         /* flag set for branches determined to be dynamically unreachable */
      43                 :            :         bool unreachable;
      44                 :            : };
      45                 :            : 
      46                 :            : SLIST_HEAD(bpf_evst_head, bpf_eval_state);
      47                 :            : 
      48                 :            : /* possible instruction node colour */
      49                 :            : enum {
      50                 :            :         WHITE,
      51                 :            :         GREY,
      52                 :            :         BLACK,
      53                 :            :         MAX_NODE_COLOUR
      54                 :            : };
      55                 :            : 
      56                 :            : /* possible edge types */
      57                 :            : enum {
      58                 :            :         UNKNOWN_EDGE,
      59                 :            :         TREE_EDGE,
      60                 :            :         BACK_EDGE,
      61                 :            :         CROSS_EDGE,
      62                 :            :         MAX_EDGE_TYPE
      63                 :            : };
      64                 :            : 
      65                 :            : #define MAX_EDGES       2
      66                 :            : 
      67                 :            : /* max number of 'safe' evaluated states to track per node */
      68                 :            : #define NODE_EVST_MAX   32
      69                 :            : 
      70                 :            : struct inst_node {
      71                 :            :         uint8_t colour;
      72                 :            :         uint8_t nb_edge:4;
      73                 :            :         uint8_t cur_edge:4;
      74                 :            :         uint8_t edge_type[MAX_EDGES];
      75                 :            :         uint32_t edge_dest[MAX_EDGES];
      76                 :            :         struct inst_node *prev_node;
      77                 :            :         struct {
      78                 :            :                 struct bpf_eval_state *cur;   /* save/restore for jcc targets */
      79                 :            :                 struct bpf_eval_state *start;
      80                 :            :                 struct bpf_evst_head safe;    /* safe states for track/prune */
      81                 :            :                 uint32_t nb_safe;
      82                 :            :         } evst;
      83                 :            : };
      84                 :            : 
      85                 :            : struct evst_pool {
      86                 :            :         uint32_t num;
      87                 :            :         uint32_t cur;
      88                 :            :         struct bpf_eval_state *ent;
      89                 :            : };
      90                 :            : 
      91                 :            : struct bpf_verifier {
      92                 :            :         const struct rte_bpf_prm_ex *prm;
      93                 :            :         struct inst_node *in;
      94                 :            :         uint64_t stack_sz;
      95                 :            :         uint32_t nb_nodes;
      96                 :            :         uint32_t nb_jcc_nodes;
      97                 :            :         uint32_t nb_ldmb_nodes;
      98                 :            :         uint32_t node_colour[MAX_NODE_COLOUR];
      99                 :            :         uint32_t edge_type[MAX_EDGE_TYPE];
     100                 :            :         struct bpf_eval_state *evst;
     101                 :            :         struct inst_node *evin;
     102                 :            :         struct evst_pool evst_sr_pool; /* for evst save/restore */
     103                 :            :         struct evst_pool evst_tp_pool; /* for evst track/prune */
     104                 :            :         struct rte_bpf_validate_debug *debug;
     105                 :            : };
     106                 :            : 
     107                 :            : struct bpf_ins_check {
     108                 :            :         struct {
     109                 :            :                 uint16_t dreg;
     110                 :            :                 uint16_t sreg;
     111                 :            :         } mask;
     112                 :            :         struct {
     113                 :            :                 uint16_t min;
     114                 :            :                 uint16_t max;
     115                 :            :         } off;
     116                 :            :         struct {
     117                 :            :                 uint32_t min;
     118                 :            :                 uint32_t max;
     119                 :            :         } imm;
     120                 :            :         const char * (*check)(const struct rte_ebpf_insn *ins);
     121                 :            :         const char * (*eval)(struct bpf_verifier *bpf, const struct rte_ebpf_insn *ins);
     122                 :            : };
     123                 :            : 
     124                 :            : #define ALL_REGS        RTE_LEN2MASK(EBPF_REG_NUM, uint16_t)
     125                 :            : #define WRT_REGS        RTE_LEN2MASK(EBPF_REG_10, uint16_t)
     126                 :            : #define ZERO_REG        RTE_LEN2MASK(EBPF_REG_1, uint16_t)
     127                 :            : 
     128                 :            : /* For LD_IND R6 is an implicit CTX register. */
     129                 :            : #define IND_SRC_REGS    (WRT_REGS ^ 1 << EBPF_REG_6)
     130                 :            : 
     131                 :            : /*
     132                 :            :  * Debugging internal interface and helpers.
     133                 :            :  */
     134                 :            : 
     135                 :            : static bool
     136                 :     486730 : reg_val_range_is_valid(const struct bpf_reg_val *rv)
     137                 :            : {
     138         [ +  + ]:     486730 :         if (rv->v.type == RTE_BPF_ARG_UNDEF)
     139                 :            :                 return true;
     140                 :            : 
     141         [ +  - ]:      49013 :         if (rv->s.min > rv->s.max)
     142                 :            :                 return false;
     143                 :            : 
     144         [ +  - ]:      49013 :         if (rv->u.min > rv->u.max)
     145                 :            :                 return false;
     146                 :            : 
     147                 :            :         /* If one of the ranges does not change sign, the other should match. */
     148   [ +  +  +  + ]:      49013 :         if (rv->s.min >= 0 || rv->s.max < 0 ||
     149   [ +  +  +  + ]:       5168 :                         rv->u.min > INT64_MAX || rv->u.max <= INT64_MAX)
     150         [ +  + ]:      93026 :                 return rv->u.min == (uint64_t)rv->s.min &&
     151         [ +  + ]:      41843 :                         rv->u.max == (uint64_t)rv->s.max;
     152                 :            : 
     153                 :            :         return true;
     154                 :            : }
     155                 :            : 
     156                 :            : int
     157                 :      12033 : __rte_bpf_validate_state_is_valid(const struct bpf_verifier *verifier)
     158                 :            : {
     159                 :      12033 :         const struct bpf_eval_state *const st = verifier->evst;
     160                 :            : 
     161         [ +  + ]:      94229 :         for (int reg = 0; reg != RTE_DIM(st->rv); ++reg)
     162         [ +  + ]:      88028 :                 if (!reg_val_range_is_valid(st->rv + reg))
     163                 :            :                         return false;
     164                 :            : 
     165         [ +  + ]:     403065 :         for (int var = 0; var != RTE_DIM(st->sv); ++var)
     166         [ +  - ]:     396864 :                 if (!reg_val_range_is_valid(st->sv + var))
     167                 :            :                         return false;
     168                 :            : 
     169                 :            :         return true;
     170                 :            : }
     171                 :            : 
     172                 :            : int
     173                 :         96 : __rte_bpf_validate_can_access(const struct bpf_verifier *verifier,
     174                 :            :         const struct rte_ebpf_insn *access, uint64_t off64)
     175                 :            : {
     176                 :         96 :         const struct bpf_eval_state *const st = verifier->evst;
     177                 :            :         const struct bpf_reg_val *rv;
     178                 :            :         /* Set of accessed byte offsets relative to memory area base. */
     179                 :            :         struct value_set access_set;
     180                 :            :         uint32_t opsz;
     181                 :            : 
     182         [ +  - ]:         96 :         if (st->unreachable)
     183                 :            :                 return -ENOENT;
     184                 :            : 
     185   [ +  -  -  - ]:         96 :         switch (BPF_CLASS(access->code)) {
     186                 :         96 :         case BPF_LDX:
     187                 :         96 :                 rv = &st->rv[access->src_reg];
     188         [ +  - ]:         96 :                 if (rv->v.type == BPF_ARG_PTR_STACK)
     189                 :            :                         /* Not supporting stack access queries yet. */
     190                 :            :                         return -ENOTSUP;
     191                 :            :                 break;
     192                 :          0 :         case BPF_ST:
     193                 :          0 :                 rv = &st->rv[access->dst_reg];
     194                 :          0 :                 break;
     195                 :          0 :         case BPF_STX:
     196                 :          0 :                 rv = &st->rv[access->dst_reg];
     197         [ #  # ]:          0 :                 if (st->rv[access->src_reg].v.type == RTE_BPF_ARG_UNDEF)
     198                 :            :                         return false;
     199                 :            :                 break;
     200                 :            :         default:
     201                 :            :                 return -ENOTSUP;
     202                 :            :         }
     203                 :            : 
     204   [ +  -  +  - ]:         96 :         if (!RTE_BPF_ARG_PTR_TYPE(rv->v.type) || rv->v.size == 0)
     205                 :            :                 return false;
     206                 :            : 
     207                 :         96 :         access_set = value_set_from_pair(rv->s.min, rv->s.max, rv->u.min, rv->u.max);
     208                 :         96 :         value_set_translate(&access_set, off64);
     209         [ -  + ]:         96 :         opsz = bpf_size(BPF_SIZE(access->code));
     210                 :         96 :         value_set_add_contiguous(&access_set, 0, opsz - 1);
     211                 :            : 
     212                 :         96 :         return value_set_is_covered_by_contiguous(&access_set, 0, rv->v.size - 1);
     213                 :            : }
     214                 :            : 
     215                 :            : /* Return true if instruction `code` is supported by `may_jump`. */
     216                 :            : static bool
     217                 :      14656 : may_jump_code_is_supported(uint8_t code)
     218                 :            : {
     219         [ +  - ]:      14656 :         if (BPF_CLASS(code) != BPF_JMP)
     220                 :            :                 return false;
     221                 :            : 
     222         [ -  + ]:      14656 :         switch (BPF_OP(code)) {
     223                 :            :         case BPF_JEQ:
     224                 :            :         case BPF_JGT:
     225                 :            :         case BPF_JGE:
     226                 :            :         case EBPF_JNE:
     227                 :            :         case EBPF_JSGT:
     228                 :            :         case EBPF_JSGE:
     229                 :            :         case EBPF_JLT:
     230                 :            :         case EBPF_JLE:
     231                 :            :         case EBPF_JSLT:
     232                 :            :         case EBPF_JSLE:
     233                 :            :                 return true;
     234                 :          0 :         default:
     235                 :          0 :                 return false;
     236                 :            :         }
     237                 :            : }
     238                 :            : 
     239                 :            : /* Return true if instruction `code` corresponds to a signed comparison. */
     240                 :            : static bool
     241                 :            : may_jump_code_is_signed(uint8_t code)
     242                 :            : {
     243                 :      14656 :         switch (BPF_OP(code)) {
     244                 :            :         case EBPF_JSGT:
     245                 :            :         case EBPF_JSGE:
     246                 :            :         case EBPF_JSLT:
     247                 :            :         case EBPF_JSLE:
     248                 :            :                 return true;
     249                 :            :         default:
     250                 :            :                 return false;
     251                 :            :         }
     252                 :            : }
     253                 :            : 
     254                 :            : /* Return true the specified jump condition _may_ be true. */
     255                 :            : static bool
     256                 :      29312 : may_jump(uint8_t code, const struct value_set *origin,
     257                 :            :         const struct value_set *dst_set, const struct value_set *src_set)
     258                 :            : {
     259   [ +  +  +  +  :      29312 :         switch (BPF_OP(code)) {
                +  +  - ]
     260                 :       7328 :         case BPF_JEQ:
     261                 :       7328 :                 return value_sets_intersect(dst_set, src_set);
     262                 :       7328 :         case EBPF_JNE:
     263   [ +  +  -  + ]:       9300 :                 return !(value_set_is_singleton(dst_set) &&
     264                 :       1972 :                         value_sets_equal(dst_set, src_set));
     265                 :       3664 :         case BPF_JGT:
     266                 :            :         case EBPF_JSGT:
     267                 :       3664 :                 return !value_sets_based_less_or_equal(origin, dst_set, src_set);
     268                 :       3664 :         case BPF_JGE:
     269                 :            :         case EBPF_JSGE:
     270                 :       3664 :                 return !value_sets_based_less(origin, dst_set, src_set);
     271                 :       3664 :         case EBPF_JLT:
     272                 :            :         case EBPF_JSLT:
     273                 :       3664 :                 return !value_sets_based_less_or_equal(origin, src_set, dst_set);
     274                 :       3664 :         case EBPF_JSLE:
     275                 :            :         case EBPF_JLE:
     276                 :       3664 :                 return !value_sets_based_less(origin, src_set, dst_set);
     277                 :            :         }
     278                 :            :         /* may_jump_code_is_supported should have caught this */
     279                 :            :         RTE_ASSERT(false);
     280                 :            :         return false;
     281                 :            : }
     282                 :            : 
     283                 :            : /* Return instruction code for jump condition complement (negated result). */
     284                 :            : static uint8_t
     285                 :      14656 : may_jump_code_complement(uint8_t code)
     286                 :            : {
     287   [ +  +  +  +  :      14656 :         switch (BPF_OP(code)) {
                   +  - ]
     288                 :       7328 :         case BPF_JEQ:
     289                 :            :         case EBPF_JNE:
     290                 :       7328 :                 return code ^ BPF_JEQ ^ EBPF_JNE;
     291                 :       1838 :         case BPF_JGT:
     292                 :            :         case EBPF_JLE:
     293                 :       1838 :                 return code ^ BPF_JGT ^ EBPF_JLE;
     294                 :       1838 :         case BPF_JGE:
     295                 :            :         case EBPF_JLT:
     296                 :       1838 :                 return code ^ BPF_JGE ^ EBPF_JLT;
     297                 :       1826 :         case EBPF_JSGT:
     298                 :            :         case EBPF_JSLE:
     299                 :       1826 :                 return code ^ EBPF_JSGT ^ EBPF_JSLE;
     300                 :       1826 :         case EBPF_JSGE:
     301                 :            :         case EBPF_JSLT:
     302                 :       1826 :                 return code ^ EBPF_JSGE ^ EBPF_JSLT;
     303                 :            :         }
     304                 :            :         /* may_jump_code_is_supported should have caught this */
     305                 :            :         RTE_ASSERT(false);
     306                 :            :         return 0;
     307                 :            : }
     308                 :            : 
     309                 :            : int
     310                 :      14656 : __rte_bpf_validate_may_jump(const struct bpf_verifier *verifier,
     311                 :            :         const struct rte_ebpf_insn *jump, uint64_t imm64)
     312                 :            : {
     313                 :      14656 :         const struct bpf_eval_state *const st = verifier->evst;
     314                 :            :         const struct bpf_reg_val *rd, *rs;
     315                 :            :         struct value_set dst_set, src_set, origin;
     316                 :            :         int result;
     317                 :            : 
     318         [ +  - ]:      14656 :         if (!may_jump_code_is_supported(jump->code))
     319                 :            :                 return -ENOTSUP;
     320                 :            : 
     321         [ +  - ]:      14656 :         if (st->unreachable)
     322                 :            :                 /* Set no bits since neither false nor true is possible. */
     323                 :            :                 return 0;
     324                 :            : 
     325                 :      14656 :         rd = &st->rv[jump->dst_reg];
     326         [ +  - ]:      14656 :         dst_set = (rd->v.type == RTE_BPF_ARG_UNDEF) ? value_set_full :
     327                 :      14656 :                 value_set_from_pair(rd->s.min, rd->s.max, rd->u.min, rd->u.max);
     328                 :            : 
     329         [ +  + ]:      14656 :         rs = BPF_SRC(jump->code) == BPF_X ? &st->rv[jump->src_reg] : NULL;
     330                 :      14608 :         src_set = rs == NULL ? value_set_singleton((int64_t)jump->imm) :
     331         [ +  - ]:         48 :                 rs->v.type == RTE_BPF_ARG_UNDEF ? value_set_full :
     332                 :         48 :                 value_set_from_pair(rs->s.min, rs->s.max, rs->u.min, rs->u.max);
     333                 :            : 
     334                 :      14656 :         value_set_translate(&src_set, imm64);
     335                 :            : 
     336   [ +  +  +  - ]:      14656 :         if (RTE_BPF_ARG_PTR_TYPE(rd->v.type) &&
     337         [ +  - ]:         48 :                         (rs != NULL && RTE_BPF_ARG_PTR_TYPE(rs->v.type)) &&
     338         [ +  - ]:         48 :                         rd->v.size == rs->v.size) {
     339                 :            :                 /*
     340                 :            :                  * Both sides are pointers with the same memory area size.
     341                 :            :                  * Until tracking of memory areas is implemented we will consider them
     342                 :            :                  * pointing to the same memory area just because of this.
     343                 :            :                  * In this case our value sets represent offsets from the memory area base,
     344                 :            :                  * which is some unknown distance from the scalar zero (NULL).
     345                 :            :                  * We know however that the memory area cannot cross zero address.
     346                 :            :                  * Thus range of origin relative to memory base starts with 1 byte gap
     347                 :            :                  * after the memory area and ends just before it.
     348                 :            :                  */
     349                 :         48 :                 origin = value_set_contiguous(rd->v.size + 1, -1);
     350                 :            :         } else {
     351                 :            :                 /* Scalar value of a pointer depends on the memory area base address. */
     352         [ -  + ]:      14608 :                 if (RTE_BPF_ARG_PTR_TYPE(rd->v.type))
     353                 :          0 :                         value_set_add_contiguous(&dst_set, 1, UINT64_MAX - rd->v.size);
     354   [ -  +  -  - ]:      14608 :                 if (rs != NULL && RTE_BPF_ARG_PTR_TYPE(rs->v.type))
     355                 :          0 :                         value_set_add_contiguous(&dst_set, 1, UINT64_MAX - rs->v.size);
     356                 :      14608 :                 origin = value_set_singleton(0);
     357                 :            :         }
     358                 :            : 
     359         [ +  + ]:      14656 :         if (may_jump_code_is_signed(jump->code))
     360                 :            :                 /* Shift origin to the minimal value for signed comparisons. */
     361                 :       3652 :                 value_set_translate(&origin, INT64_MIN);
     362                 :            : 
     363                 :            :         result = 0;
     364                 :            : 
     365         [ +  + ]:      14656 :         if (may_jump(jump->code, &origin, &dst_set, &src_set))
     366                 :            :                 result |= RTE_BPF_VALIDATE_DEBUG_MAY_BE_TRUE;
     367                 :            : 
     368         [ +  + ]:      14656 :         if (may_jump(may_jump_code_complement(jump->code), &origin, &dst_set, &src_set))
     369                 :      12684 :                 result |= RTE_BPF_VALIDATE_DEBUG_MAY_BE_FALSE;
     370                 :            : 
     371                 :            :         return result;
     372                 :            : }
     373                 :            : 
     374                 :            : /* Like snprintf, but advances (except for overflow) ptr and reduces szleft. */
     375                 :            : __rte_format_printf(3, 4)
     376                 :            : static int
     377                 :        680 : buf_printf(char **ptr, ssize_t *szleft, const char *format, ...)
     378                 :            : {
     379                 :            :         va_list args;
     380                 :            :         int rc;
     381                 :            : 
     382                 :        680 :         va_start(args, format);
     383         [ +  - ]:        680 :         rc = vsnprintf(*ptr, RTE_MAX(0, *szleft), format, args);
     384                 :        680 :         va_end(args);
     385                 :            : 
     386         [ +  - ]:        680 :         if (rc > 0) {
     387                 :        680 :                 *szleft -= rc;
     388         [ +  - ]:        680 :                 if (*szleft > 0)
     389                 :        680 :                         *ptr += rc;
     390                 :            :         }
     391                 :            : 
     392                 :        680 :         return rc;
     393                 :            : }
     394                 :            : 
     395                 :            : static int
     396                 :       1838 : format_memory_area(char **ptr, ssize_t *szleft, const struct bpf_reg_val *rv)
     397                 :            : {
     398   [ +  -  -  -  :       1838 :         switch (rv->v.type) {
                      + ]
     399                 :            :         case RTE_BPF_ARG_RAW:
     400                 :            :                 return 0;
     401                 :         12 :         case RTE_BPF_ARG_PTR:
     402                 :         12 :                 return buf_printf(ptr, szleft, "%%buffer<%zu> + ",
     403                 :         12 :                         (size_t)rv->v.size);
     404                 :          0 :         case RTE_BPF_ARG_PTR_MBUF:
     405                 :          0 :                 return buf_printf(ptr, szleft, "%%mbuf<%zu, %zu> + ",
     406                 :          0 :                         (size_t)rv->v.size, (size_t)rv->v.buf_size);
     407                 :          0 :         case BPF_ARG_PTR_STACK:
     408                 :          0 :                 return buf_printf(ptr, szleft, "%%stack + ");
     409                 :          0 :         default:
     410                 :          0 :                 return -ENOTSUP;
     411                 :            :         }
     412                 :            : }
     413                 :            : 
     414                 :            : /* Format min..max interval using validate-debug API and updating ptr and szleft. */
     415                 :            : static int
     416                 :       2172 : buf_print_interval(char **ptr, ssize_t *szleft, char format, uint64_t min, uint64_t max)
     417                 :            : {
     418                 :            :         int rc;
     419                 :            : 
     420                 :       2172 :         rc = rte_bpf_validate_debug_format_interval(*ptr, RTE_MAX(0, *szleft),
     421                 :            :                 format, min, max);
     422                 :            : 
     423         [ +  - ]:       2172 :         if (rc > 0) {
     424                 :       2172 :                 *szleft -= rc;
     425         [ +  - ]:       2172 :                 if (*szleft > 0)
     426                 :       2172 :                         *ptr += rc;
     427                 :            :         }
     428                 :            : 
     429                 :       2172 :         return rc;
     430                 :            : }
     431                 :            : 
     432                 :            : /* Format rv roughly as "<signed-range> INTERSECT <unsigned-hex-range>" */
     433                 :            : static int
     434                 :       1838 : format_register_range(char **ptr, ssize_t *szleft, const struct bpf_reg_val *rv)
     435                 :            : {
     436                 :            :         int rc;
     437                 :            :         uint64_t expected_unsigned_min, expected_unsigned_max;
     438                 :       1838 :         const bool valid = reg_val_range_is_valid(rv);
     439                 :            : 
     440                 :            :         /* Print signed unless trivial. */
     441   [ +  +  +  +  :       1838 :         if (!valid || rv->s.min != INT64_MIN || rv->s.max != INT64_MAX) {
                   +  + ]
     442                 :       1776 :                 rc = buf_print_interval(ptr, szleft, 'd', rv->s.min, rv->s.max);
     443         [ +  - ]:       1776 :                 if (rc < 0)
     444                 :            :                         return rc;
     445                 :            : 
     446         [ +  + ]:       1776 :                 if (valid) {
     447                 :            :                         /* Skip printing unsigned if it has expected values. */
     448   [ +  +  +  + ]:       1442 :                         if (rv->s.min >= 0 || rv->s.max < 0) {
     449                 :       1420 :                                 expected_unsigned_min = (uint64_t)rv->s.min;
     450                 :       1420 :                                 expected_unsigned_max = (uint64_t)rv->s.max;
     451                 :            :                         } else {
     452                 :            :                                 expected_unsigned_min = 0;
     453                 :            :                                 expected_unsigned_max = UINT64_MAX;
     454                 :            :                         }
     455                 :            : 
     456         [ +  - ]:       1442 :                         if (rv->u.min == expected_unsigned_min &&
     457         [ -  + ]:       1442 :                                         rv->u.max == expected_unsigned_max)
     458                 :            :                                 return 0;
     459                 :            :                 }
     460                 :            : 
     461                 :        334 :                 rc = buf_printf(ptr, szleft, " INTERSECT ");
     462         [ +  - ]:        334 :                 if (rc < 0)
     463                 :            :                         return rc;
     464                 :            :         }
     465                 :            : 
     466                 :        396 :         rc = buf_print_interval(ptr, szleft, 'x', rv->u.min, rv->u.max);
     467         [ +  - ]:        396 :         if (rc < 0)
     468                 :            :                 return rc;
     469                 :            : 
     470         [ +  + ]:        396 :         if (!valid) {
     471                 :        334 :                 rc = buf_printf(ptr, szleft, " (!)");
     472                 :            :                 if (rc < 0)
     473                 :            :                         return rc;
     474                 :            :         }
     475                 :            : 
     476                 :            :         return 0;
     477                 :            : }
     478                 :            : 
     479                 :            : /* Format rv roughly as "<memory-object> + <offsets-range>" */
     480                 :            : static int
     481                 :       1839 : format_reg_val(char *buffer, size_t bufsz, const struct bpf_reg_val *rv)
     482                 :            : {
     483                 :       1839 :         char *ptr = buffer;
     484                 :       1839 :         ssize_t szleft = bufsz;
     485                 :            :         int rc;
     486                 :            : 
     487         [ +  + ]:       1839 :         if (rv->v.type == RTE_BPF_ARG_UNDEF)
     488                 :          1 :                 return snprintf(buffer, bufsz, "%%undefined");
     489                 :            : 
     490                 :            :         /* Print data area info, if any. */
     491                 :       1838 :         rc = format_memory_area(&ptr, &szleft, rv);
     492         [ +  - ]:       1838 :         if (rc < 0)
     493                 :            :                 return rc;
     494                 :            : 
     495                 :       1838 :         rc = format_register_range(&ptr, &szleft, rv);
     496         [ +  - ]:       1838 :         if (rc < 0)
     497                 :            :                 return rc;
     498                 :            : 
     499                 :            :         /* At least one snprintf was called and added terminating zero. */
     500                 :            :         RTE_ASSERT(szleft < (ssize_t)bufsz);
     501                 :       1838 :         --szleft;
     502                 :            : 
     503                 :       1838 :         return bufsz - szleft;
     504                 :            : }
     505                 :            : 
     506                 :            : int
     507                 :       1839 : __rte_bpf_validate_format_register_info(const struct bpf_verifier *verifier,
     508                 :            :         char *buffer, size_t bufsz, uint8_t reg)
     509                 :            : {
     510         [ +  - ]:       1839 :         if (reg >= EBPF_REG_NUM)
     511                 :            :                 return -EINVAL;
     512                 :            : 
     513                 :       1839 :         return format_reg_val(buffer, bufsz, &verifier->evst->rv[reg]);
     514                 :            : }
     515                 :            : 
     516                 :            : int
     517                 :          0 : __rte_bpf_validate_format_frame_info(const struct bpf_verifier *verifier,
     518                 :            :         char *buffer, size_t bufsz, int32_t offset)
     519                 :            : {
     520         [ #  # ]:          0 :         if (offset % sizeof(uint64_t) != 0)
     521                 :            :                 return -EINVAL;
     522                 :            : 
     523         [ #  # ]:          0 :         if (offset >= 0 || offset < -MAX_BPF_STACK_SIZE)
     524                 :            :                 return -ERANGE;
     525                 :            : 
     526                 :          0 :         offset = (MAX_BPF_STACK_SIZE + offset) / sizeof(uint64_t);
     527                 :            : 
     528                 :          0 :         return format_reg_val(buffer, bufsz, &verifier->evst->sv[offset]);
     529                 :            : }
     530                 :            : 
     531                 :            : int32_t
     532                 :          0 : __rte_bpf_validate_get_frame_size(const struct bpf_verifier *verifier)
     533                 :            : {
     534         [ #  # ]:          0 :         if (verifier->stack_sz > INT32_MAX)
     535                 :            :                 return -ERANGE;
     536                 :            : 
     537                 :          0 :         return verifier->stack_sz;
     538                 :            : }
     539                 :            : 
     540                 :            : 
     541                 :            : /*
     542                 :            :  * check and evaluate functions for particular instruction types.
     543                 :            :  */
     544                 :            : 
     545                 :            : static const char *
     546                 :          8 : check_alu_bele(const struct rte_ebpf_insn *ins)
     547                 :            : {
     548   [ +  +  -  + ]:          8 :         if (ins->imm != 16 && ins->imm != 32 && ins->imm != 64)
     549                 :          0 :                 return "invalid imm field";
     550                 :            :         return NULL;
     551                 :            : }
     552                 :            : 
     553                 :            : static const char *
     554                 :       3275 : eval_exit(struct bpf_verifier *bvf, const struct rte_ebpf_insn *ins)
     555                 :            : {
     556                 :            :         RTE_SET_USED(ins);
     557         [ +  + ]:       3275 :         if (bvf->evst->rv[EBPF_REG_0].v.type == RTE_BPF_ARG_UNDEF)
     558                 :          1 :                 return "undefined return value";
     559                 :            :         return NULL;
     560                 :            : }
     561                 :            : 
     562                 :            : /* setup max possible with this mask bounds */
     563                 :            : static void
     564                 :            : eval_umax_bound(struct bpf_reg_val *rv, uint64_t mask)
     565                 :            : {
     566                 :       1383 :         rv->u.max = mask;
     567                 :       1382 :         rv->u.min = 0;
     568                 :         53 : }
     569                 :            : 
     570                 :            : static void
     571                 :            : eval_smax_bound(struct bpf_reg_val *rv, uint64_t mask)
     572                 :            : {
     573                 :       4214 :         rv->s.max = mask >> 1;
     574                 :       4214 :         rv->s.min = rv->s.max ^ UINT64_MAX;
     575                 :        654 : }
     576                 :            : 
     577                 :            : static void
     578                 :            : eval_max_bound(struct bpf_reg_val *rv, uint64_t mask)
     579                 :            : {
     580                 :            :         eval_umax_bound(rv, mask);
     581                 :            :         eval_smax_bound(rv, mask);
     582                 :        117 : }
     583                 :            : 
     584                 :            : static void
     585                 :            : eval_fill_max_bound(struct bpf_reg_val *rv, uint64_t mask)
     586                 :            : {
     587                 :            :         eval_max_bound(rv, mask);
     588                 :        619 :         rv->v.type = RTE_BPF_ARG_RAW;
     589                 :        620 :         rv->mask = mask;
     590                 :        104 : }
     591                 :            : 
     592                 :            : static void
     593                 :            : eval_fill_imm64(struct bpf_reg_val *rv, uint64_t mask, uint64_t val)
     594                 :            : {
     595                 :       5781 :         rv->mask = mask;
     596                 :       5791 :         rv->s.min = val;
     597                 :       5781 :         rv->s.max = val;
     598                 :       5781 :         rv->u.min = val;
     599                 :       5781 :         rv->u.max = val;
     600                 :          1 : }
     601                 :            : 
     602                 :            : static void
     603                 :            : eval_fill_imm(struct bpf_reg_val *rv, uint64_t mask, int32_t imm)
     604                 :            : {
     605                 :            :         uint64_t v;
     606                 :            : 
     607                 :       5314 :         v = (uint64_t)imm & mask;
     608                 :            : 
     609                 :          0 :         rv->v.type = RTE_BPF_ARG_RAW;
     610                 :            :         eval_fill_imm64(rv, mask, v);
     611                 :        111 : }
     612                 :            : 
     613                 :            : static const char *
     614                 :        365 : eval_ld_imm64(struct bpf_verifier *bvf, const struct rte_ebpf_insn *ins)
     615                 :            : {
     616                 :            :         uint32_t i;
     617                 :            :         uint64_t val;
     618                 :            :         struct bpf_reg_val *rd;
     619                 :            : 
     620                 :        365 :         val = (uint32_t)ins[0].imm | (uint64_t)(uint32_t)ins[1].imm << 32;
     621                 :            : 
     622                 :        365 :         rd = bvf->evst->rv + ins->dst_reg;
     623                 :        365 :         rd->v.type = RTE_BPF_ARG_RAW;
     624                 :            :         eval_fill_imm64(rd, UINT64_MAX, val);
     625                 :            : 
     626         [ -  + ]:        365 :         for (i = 0; i != bvf->prm->nb_xsym; i++) {
     627                 :            : 
     628                 :            :                 /* load of external variable */
     629         [ #  # ]:          0 :                 if (bvf->prm->xsym[i].type == RTE_BPF_XTYPE_VAR &&
     630         [ #  # ]:          0 :                                 (uintptr_t)bvf->prm->xsym[i].var.val == val) {
     631                 :          0 :                         rd->v = bvf->prm->xsym[i].var.desc;
     632                 :            :                         eval_fill_imm64(rd, UINT64_MAX, 0);
     633                 :            :                         break;
     634                 :            :                 }
     635                 :            :         }
     636                 :            : 
     637                 :        365 :         return NULL;
     638                 :            : }
     639                 :            : 
     640                 :            : static void
     641                 :            : eval_apply_mask(struct bpf_reg_val *rv, uint64_t mask)
     642                 :            : {
     643                 :            :         struct bpf_reg_val rt;
     644                 :            : 
     645                 :       2807 :         rt.u.min = rv->u.min & mask;
     646                 :       2807 :         rt.u.max = rv->u.max & mask;
     647   [ +  +  -  +  :       2806 :         if (rt.u.min != rv->u.min || rt.u.max != rv->u.max) {
          +  -  -  +  +  
             +  +  -  -  
                      + ]
     648                 :         13 :                 rv->u.max = RTE_MAX(rt.u.max, mask);
     649                 :          2 :                 rv->u.min = 0;
     650                 :            :         }
     651                 :            : 
     652                 :            :         eval_smax_bound(&rt, mask);
     653                 :       2807 :         rv->s.max = RTE_MIN(rt.s.max, rv->s.max);
     654                 :       2807 :         rv->s.min = RTE_MAX(rt.s.min, rv->s.min);
     655                 :            : 
     656                 :       2728 :         rv->mask = mask;
     657                 :        484 : }
     658                 :            : 
     659                 :            : static void
     660                 :        854 : eval_add(struct bpf_reg_val *rd, const struct bpf_reg_val *rs, uint64_t msk)
     661                 :            : {
     662                 :        854 :         struct bpf_reg_val rs_buf = { .v.type = RTE_BPF_ARG_RAW };
     663                 :            :         struct bpf_reg_val rv;
     664                 :            : 
     665         [ +  + ]:        854 :         if (RTE_BPF_ARG_PTR_TYPE(rs->v.type) != 0) {
     666         [ +  + ]:          8 :                 if (RTE_BPF_ARG_PTR_TYPE(rd->v.type) != 0) {
     667                 :            :                         /* treat sum of pointers as sum of two unknown scalars */
     668                 :            :                         eval_fill_max_bound(&rs_buf, msk);
     669                 :          1 :                         *rd = rs_buf;
     670                 :            :                         rs = &rs_buf;
     671                 :            :                 } else
     672                 :            :                         /* scalar + pointer is a pointer of the same type */
     673                 :          7 :                         rd->v = rs->v;
     674                 :            :         }
     675                 :            : 
     676                 :        854 :         rv.u.min = (rd->u.min + rs->u.min) & msk;
     677                 :        854 :         rv.u.max = (rd->u.max + rs->u.max) & msk;
     678                 :        854 :         rv.s.min = ((uint64_t)rd->s.min + (uint64_t)rs->s.min) & msk;
     679                 :        854 :         rv.s.max = ((uint64_t)rd->s.max + (uint64_t)rs->s.max) & msk;
     680                 :            : 
     681                 :            :         /*
     682                 :            :          * if at least one of the operands is not constant,
     683                 :            :          * then check for overflow
     684                 :            :          */
     685   [ +  +  +  +  :        854 :         if ((rd->u.min != rd->u.max || rs->u.min != rs->u.max) &&
                   +  - ]
     686         [ +  + ]:         98 :                         (rv.u.min < rd->u.min || rv.u.max < rd->u.max))
     687                 :            :                 eval_umax_bound(&rv, msk);
     688                 :            : 
     689   [ +  +  +  +  :        854 :         if ((rd->s.min != rd->s.max || rs->s.min != rs->s.max) &&
                   +  + ]
     690   [ +  +  +  + ]:         98 :                         (((rs->s.min < 0 && rv.s.min > rd->s.min) ||
     691         [ -  + ]:         75 :                         rv.s.min < rd->s.min) ||
     692   [ -  -  +  + ]:         75 :                         ((rs->s.max < 0 && rv.s.max > rd->s.max) ||
     693                 :            :                                 rv.s.max < rd->s.max)))
     694                 :            :                 eval_smax_bound(&rv, msk);
     695                 :            : 
     696                 :        854 :         rd->s = rv.s;
     697                 :        854 :         rd->u = rv.u;
     698                 :        854 : }
     699                 :            : 
     700                 :            : static void
     701                 :         10 : eval_sub(struct bpf_reg_val *rd, const struct bpf_reg_val *rs, uint64_t msk)
     702                 :            : {
     703                 :            :         struct bpf_reg_val rv;
     704                 :            : 
     705                 :         10 :         rv.u.min = (rd->u.min - rs->u.max) & msk;
     706                 :         10 :         rv.u.max = (rd->u.max - rs->u.min) & msk;
     707                 :         10 :         rv.s.min = ((uint64_t)rd->s.min - (uint64_t)rs->s.max) & msk;
     708                 :         10 :         rv.s.max = ((uint64_t)rd->s.max - (uint64_t)rs->s.min) & msk;
     709                 :            : 
     710                 :            :         /*
     711                 :            :          * if at least one of the operands is not constant,
     712                 :            :          * then check for overflow
     713                 :            :          */
     714   [ +  +  -  +  :         10 :         if ((rd->u.min != rd->u.max || rs->u.min != rs->u.max) &&
                   -  + ]
     715         [ #  # ]:          0 :                         (rv.u.min > rd->u.min || rv.u.max > rd->u.max))
     716                 :            :                 eval_umax_bound(&rv, msk);
     717                 :            : 
     718   [ +  +  -  +  :         10 :         if ((rd->s.min != rd->s.max || rs->s.min != rs->s.max) &&
                   -  + ]
     719   [ -  -  +  + ]:          7 :                         (((rs->s.max < 0 && rv.s.min < rd->s.min) ||
     720         [ +  - ]:          1 :                         rv.s.min > rd->s.min) ||
     721   [ -  +  -  - ]:          1 :                         ((rs->s.min < 0 && rv.s.max < rd->s.max) ||
     722                 :            :                         rv.s.max > rd->s.max)))
     723                 :            :                 eval_smax_bound(&rv, msk);
     724                 :            : 
     725                 :         10 :         rd->s = rv.s;
     726                 :         10 :         rd->u = rv.u;
     727                 :         10 : }
     728                 :            : 
     729                 :            : static void
     730                 :         36 : eval_lsh(struct bpf_reg_val *rd, const struct bpf_reg_val *rs, size_t opsz,
     731                 :            :         uint64_t msk)
     732                 :            : {
     733                 :            :         /* check if shift value is less then max result bits */
     734         [ +  + ]:         36 :         if (rs->u.max >= opsz) {
     735                 :            :                 eval_max_bound(rd, msk);
     736                 :          1 :                 return;
     737                 :            :         }
     738                 :            : 
     739                 :            :         /* check for overflow */
     740         [ +  + ]:         35 :         if (rd->u.max > RTE_LEN2MASK(opsz - rs->u.max, uint64_t))
     741                 :            :                 eval_umax_bound(rd, msk);
     742                 :            :         else {
     743                 :         31 :                 rd->u.max <<= rs->u.max;
     744                 :         31 :                 rd->u.min <<= rs->u.min;
     745                 :            :         }
     746                 :            : 
     747                 :            :         /* check that dreg values are and would remain always positive */
     748   [ +  +  +  + ]:         67 :         if ((uint64_t)rd->s.min >> (opsz - 1) != 0 || rd->s.max >=
     749         [ +  + ]:         32 :                         (rs->u.max == opsz - 1 ? 0 :
     750                 :         30 :                                  RTE_LEN2MASK(opsz - rs->u.max - 1, int64_t)))
     751                 :            :                 eval_smax_bound(rd, msk);
     752                 :            :         else {
     753                 :         29 :                 rd->s.max <<= rs->u.max;
     754                 :         29 :                 rd->s.min <<= rs->u.min;
     755                 :            :         }
     756                 :            : }
     757                 :            : 
     758                 :            : static void
     759                 :         17 : eval_rsh(struct bpf_reg_val *rd, const struct bpf_reg_val *rs, size_t opsz,
     760                 :            :         uint64_t msk)
     761                 :            : {
     762                 :            :         /* check if shift value is less then max result bits */
     763         [ +  + ]:         17 :         if (rs->u.max >= opsz) {
     764                 :            :                 eval_max_bound(rd, msk);
     765                 :          1 :                 return;
     766                 :            :         }
     767                 :            : 
     768                 :         16 :         rd->u.max >>= rs->u.min;
     769                 :         16 :         rd->u.min >>= rs->u.max;
     770                 :            : 
     771                 :            :         /* check that dreg values are always positive */
     772         [ +  + ]:         16 :         if ((uint64_t)rd->s.min >> (opsz - 1) != 0)
     773                 :            :                 eval_smax_bound(rd, msk);
     774                 :            :         else {
     775                 :         10 :                 rd->s.max >>= rs->u.min;
     776                 :         10 :                 rd->s.min >>= rs->u.max;
     777                 :            :         }
     778                 :            : }
     779                 :            : 
     780                 :            : static void
     781                 :          3 : eval_arsh(struct bpf_reg_val *rd, const struct bpf_reg_val *rs, size_t opsz,
     782                 :            :         uint64_t msk)
     783                 :            : {
     784                 :            :         uint32_t shv;
     785                 :            : 
     786                 :            :         /* check if shift value is less then max result bits */
     787         [ +  + ]:          3 :         if (rs->u.max >= opsz) {
     788                 :            :                 eval_max_bound(rd, msk);
     789                 :          1 :                 return;
     790                 :            :         }
     791                 :            : 
     792                 :          2 :         rd->u.max = (int64_t)rd->u.max >> rs->u.min;
     793                 :          2 :         rd->u.min = (int64_t)rd->u.min >> rs->u.max;
     794                 :            : 
     795                 :            :         /* if we have 32-bit values - extend them to 64-bit */
     796         [ -  + ]:          2 :         if (opsz == sizeof(uint32_t) * CHAR_BIT) {
     797                 :          0 :                 rd->s.min <<= opsz;
     798                 :          0 :                 rd->s.max <<= opsz;
     799                 :            :                 shv = opsz;
     800                 :            :         } else
     801                 :            :                 shv = 0;
     802                 :            : 
     803         [ +  - ]:          2 :         if (rd->s.min < 0)
     804                 :          2 :                 rd->s.min = (rd->s.min >> (rs->u.min + shv)) & msk;
     805                 :            :         else
     806                 :          0 :                 rd->s.min = (rd->s.min >> (rs->u.max + shv)) & msk;
     807                 :            : 
     808         [ -  + ]:          2 :         if (rd->s.max < 0)
     809                 :          0 :                 rd->s.max = (rd->s.max >> (rs->u.max + shv)) & msk;
     810                 :            :         else
     811                 :          2 :                 rd->s.max = (rd->s.max >> (rs->u.min + shv)) & msk;
     812                 :            : }
     813                 :            : 
     814                 :            : static uint64_t
     815                 :            : eval_umax_bits(uint64_t v)
     816                 :            : {
     817   [ +  +  +  +  :        200 :         if (v == 0)
          +  +  +  +  +  
                -  +  - ]
     818                 :            :                 return 0;
     819                 :            : 
     820                 :            :         v = rte_clz64(v);
     821                 :        395 :         return RTE_LEN2MASK(64 - v, uint64_t);
     822                 :            : }
     823                 :            : 
     824                 :            : /* estimate max possible value for (v1 & v2) */
     825                 :            : static uint64_t
     826                 :            : eval_uand_max(uint64_t v1, uint64_t v2)
     827                 :            : {
     828                 :            :         v1 = eval_umax_bits(v1);
     829                 :            :         v2 = eval_umax_bits(v2);
     830                 :        183 :         return (v1 & v2);
     831                 :            : }
     832                 :            : 
     833                 :            : /* estimate max possible value for (v1 | v2) */
     834                 :            : static uint64_t
     835                 :            : eval_uor_max(uint64_t v1, uint64_t v2)
     836                 :            : {
     837                 :            :         v1 = eval_umax_bits(v1);
     838                 :            :         v2 = eval_umax_bits(v2);
     839                 :         17 :         return (v1 | v2);
     840                 :            : }
     841                 :            : 
     842                 :            : static void
     843                 :         92 : eval_and(struct bpf_reg_val *rd, const struct bpf_reg_val *rs, uint64_t msk)
     844                 :            : {
     845                 :            :         /* both operands are constants */
     846   [ -  +  -  - ]:         92 :         if (rd->u.min == rd->u.max && rs->u.min == rs->u.max) {
     847                 :          0 :                 rd->u.min &= rs->u.min;
     848                 :          0 :                 rd->u.max &= rs->u.max;
     849                 :            :         } else {
     850         [ +  - ]:         92 :                 rd->u.max = eval_uand_max(rd->u.max, rs->u.max);
     851                 :         92 :                 rd->u.min = 0;
     852                 :            :         }
     853                 :            : 
     854                 :            :         /* both operands are constants */
     855   [ -  +  -  - ]:         92 :         if (rd->s.min == rd->s.max && rs->s.min == rs->s.max) {
     856                 :          0 :                 rd->s.min &= rs->s.min;
     857                 :          0 :                 rd->s.max &= rs->s.max;
     858                 :            :         /* at least one of operand is non-negative */
     859   [ +  +  +  + ]:         92 :         } else if (rd->s.min >= 0 || rs->s.min >= 0) {
     860                 :        182 :                 rd->s.max = eval_uand_max(rd->s.max & (msk >> 1),
     861         [ +  - ]:         91 :                         rs->s.max & (msk >> 1));
     862                 :         91 :                 rd->s.min = 0;
     863                 :            :         } else
     864                 :            :                 eval_smax_bound(rd, msk);
     865                 :         92 : }
     866                 :            : 
     867                 :            : static void
     868                 :        169 : eval_or(struct bpf_reg_val *rd, const struct bpf_reg_val *rs, uint64_t msk)
     869                 :            : {
     870                 :            :         /* both operands are constants */
     871   [ +  +  +  - ]:        169 :         if (rd->u.min == rd->u.max && rs->u.min == rs->u.max) {
     872                 :        162 :                 rd->u.min |= rs->u.min;
     873                 :        162 :                 rd->u.max |= rs->u.max;
     874                 :            :         } else {
     875         [ +  - ]:          7 :                 rd->u.max = eval_uor_max(rd->u.max, rs->u.max);
     876                 :          7 :                 rd->u.min = RTE_MAX(rd->u.min, rs->u.min);
     877                 :            :         }
     878                 :            : 
     879                 :            :         /* both operands are constants */
     880   [ +  +  +  - ]:        169 :         if (rd->s.min == rd->s.max && rs->s.min == rs->s.max) {
     881                 :        162 :                 rd->s.min |= rs->s.min;
     882                 :        162 :                 rd->s.max |= rs->s.max;
     883                 :            : 
     884                 :            :         /* both operands are non-negative */
     885   [ +  +  +  + ]:          7 :         } else if (rd->s.min >= 0 && rs->s.min >= 0) {
     886         [ +  - ]:          3 :                 rd->s.max = eval_uor_max(rd->s.max, rs->s.max);
     887                 :          3 :                 rd->s.min = RTE_MAX(rd->s.min, rs->s.min);
     888                 :            :         } else
     889                 :            :                 eval_smax_bound(rd, msk);
     890                 :        169 : }
     891                 :            : 
     892                 :            : static void
     893                 :        116 : eval_xor(struct bpf_reg_val *rd, const struct bpf_reg_val *rs, uint64_t msk)
     894                 :            : {
     895                 :            :         /* both operands are constants */
     896   [ +  +  +  - ]:        116 :         if (rd->u.min == rd->u.max && rs->u.min == rs->u.max) {
     897                 :        111 :                 rd->u.min ^= rs->u.min;
     898                 :        111 :                 rd->u.max ^= rs->u.max;
     899                 :            :         } else {
     900         [ +  - ]:          5 :                 rd->u.max = eval_uor_max(rd->u.max, rs->u.max);
     901                 :          5 :                 rd->u.min = 0;
     902                 :            :         }
     903                 :            : 
     904                 :            :         /* both operands are constants */
     905   [ +  +  +  - ]:        116 :         if (rd->s.min == rd->s.max && rs->s.min == rs->s.max) {
     906                 :        111 :                 rd->s.min ^= rs->s.min;
     907                 :        111 :                 rd->s.max ^= rs->s.max;
     908                 :            : 
     909                 :            :         /* both operands are non-negative */
     910   [ +  +  +  - ]:          5 :         } else if (rd->s.min >= 0 && rs->s.min >= 0) {
     911         [ +  - ]:          2 :                 rd->s.max = eval_uor_max(rd->s.max, rs->s.max);
     912                 :          2 :                 rd->s.min = 0;
     913                 :            :         } else
     914                 :            :                 eval_smax_bound(rd, msk);
     915                 :        116 : }
     916                 :            : 
     917                 :            : static void
     918                 :         16 : eval_mul(struct bpf_reg_val *rd, const struct bpf_reg_val *rs, size_t opsz,
     919                 :            :         uint64_t msk)
     920                 :            : {
     921                 :            :         /* both operands are constants */
     922   [ +  +  +  + ]:         16 :         if (rd->u.min == rd->u.max && rs->u.min == rs->u.max) {
     923                 :          1 :                 rd->u.min = (rd->u.min * rs->u.min) & msk;
     924                 :          1 :                 rd->u.max = (rd->u.max * rs->u.max) & msk;
     925                 :            :         /* check for overflow */
     926   [ +  +  +  + ]:         15 :         } else if (rd->u.max <= msk >> opsz / 2 && rs->u.max <= msk >> opsz) {
     927                 :          2 :                 rd->u.max *= rs->u.max;
     928                 :          2 :                 rd->u.min *= rs->u.min;
     929                 :            :         } else
     930                 :            :                 eval_umax_bound(rd, msk);
     931                 :            : 
     932                 :            :         /* both operands are constants */
     933   [ +  +  +  + ]:         16 :         if (rd->s.min == rd->s.max && rs->s.min == rs->s.max) {
     934                 :          1 :                 rd->s.min = ((uint64_t)rd->s.min * (uint64_t)rs->s.min) & msk;
     935                 :          1 :                 rd->s.max = ((uint64_t)rd->s.max * (uint64_t)rs->s.max) & msk;
     936                 :            :         /* check that both operands are positive and no overflow */
     937   [ +  +  +  + ]:         15 :         } else if (rd->s.min >= 0 && rs->s.min >= 0) {
     938                 :         13 :                 rd->s.max *= rs->s.max;
     939                 :         13 :                 rd->s.min *= rs->s.min;
     940                 :            :         } else
     941                 :            :                 eval_smax_bound(rd, msk);
     942                 :         16 : }
     943                 :            : 
     944                 :            : static const char *
     945                 :         47 : eval_divmod(uint32_t op, struct bpf_reg_val *rd, struct bpf_reg_val *rs, uint64_t msk)
     946                 :            : {
     947                 :            :         /* both operands are constants */
     948   [ +  +  +  - ]:         47 :         if (rd->u.min == rd->u.max && rs->u.min == rs->u.max) {
     949         [ +  - ]:         24 :                 if (rs->u.max == 0)
     950                 :            :                         return "division by 0";
     951         [ +  + ]:         24 :                 if (op == BPF_DIV) {
     952                 :         12 :                         rd->u.min /= rs->u.min;
     953                 :         12 :                         rd->u.max /= rs->u.max;
     954                 :            :                 } else {
     955                 :         12 :                         rd->u.min %= rs->u.min;
     956                 :         12 :                         rd->u.max %= rs->u.max;
     957                 :            :                 }
     958                 :            :         } else {
     959         [ +  + ]:         23 :                 if (op == BPF_MOD)
     960                 :         11 :                         rd->u.max = RTE_MIN(rd->u.max, rs->u.max - 1);
     961                 :            :                 else
     962                 :            :                         rd->u.max = rd->u.max;
     963                 :         23 :                 rd->u.min = 0;
     964                 :            :         }
     965                 :            : 
     966   [ +  +  +  + ]:         47 :         if (rd->u.min >= (uint64_t)INT64_MIN || rd->u.max <= (uint64_t)INT64_MAX) {
     967                 :            :                 /*
     968                 :            :                  * All values have the same sign bit, which means range
     969                 :            :                  * contiguous as unsigned is also contiguous as signed,
     970                 :            :                  * so we can just reuse it without any changes.
     971                 :            :                  */
     972                 :         34 :                 rd->s.min = rd->u.min;
     973                 :         34 :                 rd->s.max = rd->u.max;
     974                 :            :         } else
     975                 :            :                 eval_smax_bound(rd, msk);
     976                 :            : 
     977                 :            :         return NULL;
     978                 :            : }
     979                 :            : 
     980                 :            : static void
     981                 :         41 : eval_neg(struct bpf_reg_val *rd, size_t opsz, uint64_t msk)
     982                 :            : {
     983                 :            :         uint64_t ux, uy;
     984                 :            :         int64_t sx, sy;
     985                 :            :         /* additional limits imposed by signed on unsigned and back */
     986                 :            :         struct bpf_reg_val cross_limits = {
     987                 :            :                 .s = { INT64_MIN, INT64_MAX },
     988                 :            :                 .u = { 0, UINT64_MAX },
     989                 :            :         };
     990                 :            : 
     991                 :            :         /* if we have 32-bit values - extend them to 64-bit */
     992         [ +  + ]:         41 :         if (opsz == sizeof(uint32_t) * CHAR_BIT) {
     993                 :          1 :                 rd->u.min = (int32_t)rd->u.min;
     994                 :          1 :                 rd->u.max = (int32_t)rd->u.max;
     995                 :            :         }
     996                 :            : 
     997         [ +  + ]:         41 :         if (rd->u.min == 0) {
     998                 :            :                 /* special case: ranges that include 0 and, possibly, 1 */
     999                 :            : 
    1000                 :            :                 /*
    1001                 :            :                  * Calculate requirements on the signed range of negation.
    1002                 :            :                  * It is only possible when negated range does not cross from
    1003                 :            :                  * INT64_MIN to INT64_MAX, which means our original range does
    1004                 :            :                  * not reach (uint64_t)-INT64_MAX.
    1005                 :            :                  */
    1006         [ +  + ]:         26 :                 if (rd->u.max < (uint64_t)-INT64_MAX) {
    1007                 :          9 :                         cross_limits.s.min = -rd->u.max;
    1008                 :            :                         cross_limits.s.max = -rd->u.min;
    1009                 :            :                 }
    1010                 :            : 
    1011         [ +  + ]:         26 :                 if (rd->u.max != 0)
    1012                 :         24 :                         rd->u.max = UINT64_MAX;
    1013                 :            :         } else {
    1014                 :         15 :                 ux = -rd->u.min & msk;
    1015                 :         15 :                 uy = -rd->u.max & msk;
    1016                 :            : 
    1017                 :         15 :                 rd->u.max = RTE_MAX(ux, uy);
    1018                 :         15 :                 rd->u.min = RTE_MIN(ux, uy);
    1019                 :            :         }
    1020                 :            : 
    1021                 :            :         /* if we have 32-bit values - extend them to 64-bit */
    1022         [ +  + ]:         41 :         if (opsz == sizeof(uint32_t) * CHAR_BIT) {
    1023                 :          1 :                 rd->s.min = (int32_t)rd->s.min;
    1024                 :          1 :                 rd->s.max = (int32_t)rd->s.max;
    1025                 :            :         }
    1026                 :            : 
    1027         [ +  + ]:         41 :         if (rd->s.min == INT64_MIN) {
    1028                 :            :                 /* special case: negation of INT64_MIN is INT64_MIN */
    1029         [ +  + ]:         25 :                 if (rd->s.max <= 0) {
    1030                 :          8 :                         cross_limits.u.min = -(uint64_t)rd->s.max;
    1031                 :            :                         cross_limits.u.max = -(uint64_t)rd->s.min;
    1032                 :            :                 }
    1033         [ +  + ]:         25 :                 if (rd->s.max != INT64_MIN)
    1034                 :         23 :                         rd->s.max = INT64_MAX;
    1035                 :            :         } else {
    1036                 :            :                 /* since max >= min, neither can be INT64_MIN here */
    1037                 :         16 :                 sx = -rd->s.min & msk;
    1038                 :         16 :                 sy = -rd->s.max & msk;
    1039                 :            : 
    1040                 :         16 :                 rd->s.max = RTE_MAX(sx, sy);
    1041                 :         16 :                 rd->s.min = RTE_MIN(sx, sy);
    1042                 :            :         }
    1043                 :            : 
    1044                 :         41 :         rd->s.min = RTE_MAX(rd->s.min, cross_limits.s.min) & msk;
    1045                 :         41 :         rd->s.max = RTE_MIN(rd->s.max, cross_limits.s.max) & msk;
    1046                 :         41 :         rd->u.min = RTE_MAX(rd->u.min, cross_limits.u.min) & msk;
    1047                 :         41 :         rd->u.max = RTE_MIN(rd->u.max, cross_limits.u.max) & msk;
    1048                 :         41 : }
    1049                 :            : 
    1050                 :            : static const char *
    1051                 :        510 : eval_ld_mbuf(struct bpf_verifier *bvf, const struct rte_ebpf_insn *ins)
    1052                 :            : {
    1053                 :            :         uint32_t i, mode;
    1054                 :            :         struct bpf_reg_val *rv, ri, rs;
    1055                 :            : 
    1056                 :        510 :         mode = BPF_MODE(ins->code);
    1057                 :            : 
    1058                 :            :         /* R6 is an implicit input that must contain pointer to mbuf */
    1059         [ +  - ]:        510 :         if (bvf->evst->rv[EBPF_REG_6].v.type != RTE_BPF_ARG_PTR_MBUF)
    1060                 :            :                 return "invalid type for implicit ctx register";
    1061                 :            : 
    1062         [ +  + ]:        510 :         if (mode == BPF_IND) {
    1063                 :         65 :                 rs = bvf->evst->rv[ins->src_reg];
    1064         [ +  - ]:         65 :                 if (rs.v.type != RTE_BPF_ARG_RAW)
    1065                 :            :                         return "unexpected type for src register";
    1066                 :            : 
    1067                 :         65 :                 eval_fill_imm(&ri, UINT64_MAX, ins->imm);
    1068                 :         65 :                 eval_add(&rs, &ri, UINT64_MAX);
    1069                 :            : 
    1070   [ +  -  +  - ]:         65 :                 if (rs.s.max < 0 || rs.u.min > UINT32_MAX)
    1071                 :            :                         return "mbuf boundary violation";
    1072                 :            :         }
    1073                 :            : 
    1074                 :            :         /* R1-R5 scratch registers */
    1075         [ +  + ]:       3060 :         for (i = EBPF_REG_1; i != EBPF_REG_6; i++)
    1076                 :       2550 :                 bvf->evst->rv[i].v.type = RTE_BPF_ARG_UNDEF;
    1077                 :            : 
    1078                 :            :         /* R0 is an implicit output, contains data fetched from the packet */
    1079                 :            :         rv = bvf->evst->rv + EBPF_REG_0;
    1080         [ +  + ]:        510 :         rv->v.size = bpf_size(BPF_SIZE(ins->code));
    1081                 :        510 :         eval_fill_max_bound(rv, RTE_LEN2MASK(rv->v.size * CHAR_BIT, uint64_t));
    1082                 :            : 
    1083                 :        510 :         return NULL;
    1084                 :            : }
    1085                 :            : 
    1086                 :            : /*
    1087                 :            :  * check that destination and source operand are in defined state.
    1088                 :            :  */
    1089                 :            : static const char *
    1090                 :            : eval_defined(const struct bpf_reg_val *dst, const struct bpf_reg_val *src)
    1091                 :            : {
    1092   [ +  -  +  - ]:       3819 :         if (dst != NULL && dst->v.type == RTE_BPF_ARG_UNDEF)
    1093                 :            :                 return "dest reg value is undefined";
    1094   [ -  +  +  -  :       5625 :         if (src != NULL && src->v.type == RTE_BPF_ARG_UNDEF)
             +  +  -  + ]
    1095                 :            :                 return "src reg value is undefined";
    1096                 :            :         return NULL;
    1097                 :            : }
    1098                 :            : 
    1099                 :            : static const char *
    1100                 :       2323 : eval_alu(struct bpf_verifier *bvf, const struct rte_ebpf_insn *ins)
    1101                 :            : {
    1102                 :            :         uint64_t msk;
    1103                 :            :         uint32_t op;
    1104                 :            :         size_t opsz, sz;
    1105                 :            :         const char *err;
    1106                 :            :         struct bpf_eval_state *st;
    1107                 :            :         struct bpf_reg_val *rd, rs;
    1108                 :            : 
    1109                 :       2323 :         sz = (BPF_CLASS(ins->code) == BPF_ALU) ?
    1110         [ +  + ]:       2323 :                 sizeof(uint32_t) : sizeof(uint64_t);
    1111                 :       2323 :         opsz = sz * CHAR_BIT;
    1112                 :       2323 :         msk = RTE_LEN2MASK(opsz, uint64_t);
    1113                 :            : 
    1114                 :       2323 :         st = bvf->evst;
    1115                 :       2323 :         rd = st->rv + ins->dst_reg;
    1116                 :            : 
    1117         [ +  + ]:       2323 :         if (BPF_SRC(ins->code) == BPF_X) {
    1118         [ +  + ]:        402 :                 rs = st->rv[ins->src_reg];
    1119                 :            :                 eval_apply_mask(&rs, msk);
    1120                 :            :         } else {
    1121                 :       1921 :                 rs = (struct bpf_reg_val){.v = {.size = sz,},};
    1122                 :       1921 :                 eval_fill_imm(&rs, msk, ins->imm);
    1123                 :            :         }
    1124                 :            : 
    1125                 :            :         eval_apply_mask(rd, msk);
    1126                 :            : 
    1127                 :       2323 :         op = BPF_OP(ins->code);
    1128                 :            : 
    1129                 :            :         /* Allow self-xor as way to zero register */
    1130   [ +  +  +  + ]:       2323 :         if (op == BPF_XOR && BPF_SRC(ins->code) == BPF_X &&
    1131         [ +  + ]:        112 :             ins->src_reg == ins->dst_reg) {
    1132                 :            :                 eval_fill_imm(&rs, UINT64_MAX, 0);
    1133                 :            :                 eval_fill_imm(rd, UINT64_MAX, 0);
    1134                 :            :         }
    1135                 :            : 
    1136   [ +  +  +  + ]:       2364 :         err = eval_defined((op != EBPF_MOV) ? rd : NULL,
    1137                 :            :                            (op != BPF_NEG) ? &rs : NULL);
    1138                 :            :         if (err != NULL)
    1139                 :          0 :                 return err;
    1140                 :            : 
    1141         [ +  + ]:       2323 :         if (op == BPF_ADD)
    1142                 :         59 :                 eval_add(rd, &rs, msk);
    1143                 :            :         else if (op == BPF_SUB)
    1144                 :         10 :                 eval_sub(rd, &rs, msk);
    1145                 :            :         else if (op == BPF_LSH)
    1146                 :         36 :                 eval_lsh(rd, &rs, opsz, msk);
    1147                 :            :         else if (op == BPF_RSH)
    1148                 :         17 :                 eval_rsh(rd, &rs, opsz, msk);
    1149                 :            :         else if (op == EBPF_ARSH)
    1150                 :          3 :                 eval_arsh(rd, &rs, opsz, msk);
    1151                 :            :         else if (op == BPF_AND)
    1152                 :         92 :                 eval_and(rd, &rs, msk);
    1153                 :            :         else if (op == BPF_OR)
    1154                 :        169 :                 eval_or(rd, &rs, msk);
    1155                 :            :         else if (op == BPF_XOR)
    1156                 :        116 :                 eval_xor(rd, &rs, msk);
    1157                 :            :         else if (op == BPF_MUL)
    1158                 :         16 :                 eval_mul(rd, &rs, opsz, msk);
    1159                 :            :         else if (op == BPF_DIV || op == BPF_MOD)
    1160                 :         47 :                 err = eval_divmod(op, rd, &rs, msk);
    1161                 :            :         else if (op == BPF_NEG)
    1162                 :         41 :                 eval_neg(rd, opsz, msk);
    1163                 :            :         else if (op == EBPF_MOV)
    1164                 :       1717 :                 *rd = rs;
    1165                 :            :         else
    1166                 :            :                 eval_max_bound(rd, msk);
    1167                 :            : 
    1168                 :            :         return err;
    1169                 :            : }
    1170                 :            : 
    1171                 :            : static const char *
    1172                 :         10 : eval_bele(struct bpf_verifier *bvf, const struct rte_ebpf_insn *ins)
    1173                 :            : {
    1174                 :            :         uint64_t msk;
    1175                 :            :         struct bpf_eval_state *st;
    1176                 :            :         struct bpf_reg_val *rd;
    1177                 :            :         const char *err;
    1178                 :            : 
    1179                 :         10 :         msk = RTE_LEN2MASK(ins->imm, uint64_t);
    1180                 :            : 
    1181                 :         10 :         st = bvf->evst;
    1182         [ +  - ]:         10 :         rd = st->rv + ins->dst_reg;
    1183                 :            : 
    1184                 :            :         err = eval_defined(rd, NULL);
    1185                 :            :         if (err != NULL)
    1186                 :            :                 return err;
    1187                 :            : 
    1188                 :            : #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
    1189         [ +  + ]:         10 :         if (ins->code == (BPF_ALU | EBPF_END | EBPF_TO_BE))
    1190                 :            :                 eval_max_bound(rd, msk);
    1191                 :            :         else
    1192                 :            :                 eval_apply_mask(rd, msk);
    1193                 :            : #else
    1194                 :            :         if (ins->code == (BPF_ALU | EBPF_END | EBPF_TO_LE))
    1195                 :            :                 eval_max_bound(rd, msk);
    1196                 :            :         else
    1197                 :            :                 eval_apply_mask(rd, msk);
    1198                 :            : #endif
    1199                 :            : 
    1200                 :            :         return NULL;
    1201                 :            : }
    1202                 :            : 
    1203                 :            : static const char *
    1204                 :        730 : eval_ptr(struct bpf_verifier *bvf, struct bpf_reg_val *rm, uint32_t opsz,
    1205                 :            :         uint32_t align, int16_t off)
    1206                 :            : {
    1207                 :            :         struct bpf_reg_val rv;
    1208                 :            : 
    1209                 :            :         /* calculate reg + offset */
    1210                 :        730 :         eval_fill_imm(&rv, rm->mask, off);
    1211                 :        730 :         eval_add(rm, &rv, rm->mask);
    1212                 :            : 
    1213         [ +  + ]:        730 :         if (RTE_BPF_ARG_PTR_TYPE(rm->v.type) == 0)
    1214                 :            :                 return "destination is not a pointer";
    1215                 :            : 
    1216         [ +  - ]:        728 :         if (rm->mask != UINT64_MAX)
    1217                 :            :                 return "pointer truncation";
    1218                 :            : 
    1219         [ +  - ]:        728 :         if (rm->u.max + opsz > rm->v.size ||
    1220         [ +  - ]:        728 :                         (uint64_t)rm->s.max + opsz > rm->v.size ||
    1221         [ +  - ]:        728 :                         rm->s.min < 0)
    1222                 :            :                 return "memory boundary violation";
    1223                 :            : 
    1224         [ +  - ]:        728 :         if (rm->u.max % align !=  0)
    1225                 :            :                 return "unaligned memory access";
    1226                 :            : 
    1227         [ +  + ]:        728 :         if (rm->v.type == BPF_ARG_PTR_STACK) {
    1228                 :            : 
    1229   [ +  -  +  -  :         43 :                 if (rm->u.max != rm->u.min || rm->s.max != rm->s.min ||
                   +  - ]
    1230                 :            :                                 rm->u.max != (uint64_t)rm->s.max)
    1231                 :            :                         return "stack access with variable offset";
    1232                 :            : 
    1233                 :         43 :                 bvf->stack_sz = RTE_MAX(bvf->stack_sz, rm->v.size - rm->u.max);
    1234                 :            : 
    1235                 :            :         /* pointer to mbuf */
    1236         [ +  + ]:        685 :         } else if (rm->v.type == RTE_BPF_ARG_PTR_MBUF) {
    1237                 :            : 
    1238   [ +  -  +  -  :          2 :                 if (rm->u.max != rm->u.min || rm->s.max != rm->s.min ||
                   -  + ]
    1239                 :            :                                 rm->u.max != (uint64_t)rm->s.max)
    1240                 :          0 :                         return "mbuf access with variable offset";
    1241                 :            :         }
    1242                 :            : 
    1243                 :            :         return NULL;
    1244                 :            : }
    1245                 :            : 
    1246                 :            : static void
    1247                 :            : eval_max_load(struct bpf_reg_val *rv, uint64_t mask)
    1248                 :            : {
    1249                 :            :         eval_umax_bound(rv, mask);
    1250                 :            : 
    1251                 :            :         /* full 64-bit load */
    1252   [ -  +  +  + ]:        613 :         if (mask == UINT64_MAX)
    1253                 :            :                 eval_smax_bound(rv, mask);
    1254                 :            :         else {
    1255                 :            :                 /* zero-extend load */
    1256                 :         31 :                 rv->s.min = rv->u.min;
    1257                 :         31 :                 rv->s.max = rv->u.max;
    1258                 :            :         }
    1259                 :            : }
    1260                 :            : 
    1261                 :            : 
    1262                 :            : static const char *
    1263                 :        630 : eval_load(struct bpf_verifier *bvf, const struct rte_ebpf_insn *ins)
    1264                 :            : {
    1265                 :            :         uint32_t opsz;
    1266                 :            :         uint64_t msk;
    1267                 :            :         const char *err;
    1268                 :            :         struct bpf_eval_state *st;
    1269                 :            :         struct bpf_reg_val *rd, rs;
    1270                 :            :         const struct bpf_reg_val *sv;
    1271                 :            : 
    1272                 :        630 :         st = bvf->evst;
    1273                 :        630 :         rd = st->rv + ins->dst_reg;
    1274                 :        630 :         rs = st->rv[ins->src_reg];
    1275         [ +  + ]:        630 :         opsz = bpf_size(BPF_SIZE(ins->code));
    1276                 :        630 :         msk = RTE_LEN2MASK(opsz * CHAR_BIT, uint64_t);
    1277                 :            : 
    1278                 :        630 :         err = eval_ptr(bvf, &rs, opsz, 1, ins->off);
    1279         [ +  - ]:        630 :         if (err != NULL)
    1280                 :            :                 return err;
    1281                 :            : 
    1282         [ +  + ]:        630 :         if (rs.v.type == BPF_ARG_PTR_STACK) {
    1283                 :            : 
    1284                 :         17 :                 sv = st->sv + rs.u.max / sizeof(uint64_t);
    1285   [ +  -  +  - ]:         17 :                 if (sv->v.type == RTE_BPF_ARG_UNDEF || sv->mask < msk)
    1286                 :            :                         return "undefined value on the stack";
    1287                 :            : 
    1288                 :         17 :                 *rd = *sv;
    1289                 :            : 
    1290                 :            :         /* pointer to mbuf */
    1291         [ +  + ]:        613 :         } else if (rs.v.type == RTE_BPF_ARG_PTR_MBUF) {
    1292                 :            : 
    1293         [ -  + ]:          2 :                 if (rs.u.max == offsetof(struct rte_mbuf, next)) {
    1294                 :            :                         eval_fill_imm(rd, msk, 0);
    1295                 :          0 :                         rd->v = rs.v;
    1296         [ -  + ]:          2 :                 } else if (rs.u.max == offsetof(struct rte_mbuf, buf_addr)) {
    1297                 :            :                         eval_fill_imm(rd, msk, 0);
    1298                 :          0 :                         rd->v.type = RTE_BPF_ARG_PTR;
    1299                 :          0 :                         rd->v.size = rs.v.buf_size;
    1300         [ -  + ]:          2 :                 } else if (rs.u.max == offsetof(struct rte_mbuf, data_off)) {
    1301                 :            :                         eval_fill_imm(rd, msk, RTE_PKTMBUF_HEADROOM);
    1302                 :            :                         rd->v.type = RTE_BPF_ARG_RAW;
    1303                 :            :                 } else {
    1304                 :            :                         eval_max_load(rd, msk);
    1305                 :          2 :                         rd->v.type = RTE_BPF_ARG_RAW;
    1306                 :            :                 }
    1307                 :            : 
    1308                 :            :         /* pointer to raw data */
    1309                 :            :         } else {
    1310                 :            :                 eval_max_load(rd, msk);
    1311                 :        611 :                 rd->v.type = RTE_BPF_ARG_RAW;
    1312                 :            :         }
    1313                 :            : 
    1314                 :            :         return NULL;
    1315                 :            : }
    1316                 :            : 
    1317                 :            : static const char *
    1318                 :            : eval_mbuf_store(const struct bpf_reg_val *rv, uint32_t opsz)
    1319                 :            : {
    1320                 :            :         uint32_t i;
    1321                 :            : 
    1322                 :            :         static const struct {
    1323                 :            :                 size_t off;
    1324                 :            :                 size_t sz;
    1325                 :            :         } mbuf_ro_fileds[] = {
    1326                 :            :                 { .off = offsetof(struct rte_mbuf, buf_addr), },
    1327                 :            :                 { .off = offsetof(struct rte_mbuf, refcnt), },
    1328                 :            :                 { .off = offsetof(struct rte_mbuf, nb_segs), },
    1329                 :            :                 { .off = offsetof(struct rte_mbuf, buf_len), },
    1330                 :            :                 { .off = offsetof(struct rte_mbuf, pool), },
    1331                 :            :                 { .off = offsetof(struct rte_mbuf, next), },
    1332                 :            :                 { .off = offsetof(struct rte_mbuf, priv_size), },
    1333                 :            :         };
    1334                 :            : 
    1335         [ #  # ]:          0 :         for (i = 0; i != RTE_DIM(mbuf_ro_fileds) &&
    1336                 :          0 :                         (mbuf_ro_fileds[i].off + mbuf_ro_fileds[i].sz <=
    1337   [ #  #  #  # ]:          0 :                         rv->u.max || rv->u.max + opsz <= mbuf_ro_fileds[i].off);
    1338                 :          0 :                         i++)
    1339                 :            :                 ;
    1340                 :            : 
    1341         [ #  # ]:          0 :         if (i != RTE_DIM(mbuf_ro_fileds))
    1342                 :            :                 return "store to the read-only mbuf field";
    1343                 :            : 
    1344                 :            :         return NULL;
    1345                 :            : 
    1346                 :            : }
    1347                 :            : 
    1348                 :            : static const char *
    1349                 :        313 : eval_store(struct bpf_verifier *bvf, const struct rte_ebpf_insn *ins)
    1350                 :            : {
    1351                 :            :         uint32_t opsz;
    1352                 :            :         uint64_t msk;
    1353                 :            :         const char *err;
    1354                 :            :         struct bpf_eval_state *st;
    1355                 :            :         struct bpf_reg_val rd, rs, *sv;
    1356                 :            : 
    1357         [ +  + ]:        313 :         opsz = bpf_size(BPF_SIZE(ins->code));
    1358                 :        313 :         msk = RTE_LEN2MASK(opsz * CHAR_BIT, uint64_t);
    1359                 :            : 
    1360                 :        313 :         st = bvf->evst;
    1361                 :        313 :         rd = st->rv[ins->dst_reg];
    1362                 :            : 
    1363         [ +  + ]:        313 :         if (BPF_CLASS(ins->code) == BPF_STX) {
    1364                 :        303 :                 rs = st->rv[ins->src_reg];
    1365         [ +  + ]:        303 :                 if (BPF_MODE(ins->code) == EBPF_ATOMIC)
    1366      [ +  +  + ]:        260 :                         switch (ins->imm) {
    1367                 :            :                         case BPF_ATOMIC_ADD:
    1368                 :            :                                 break;
    1369                 :         13 :                         case BPF_ATOMIC_XCHG:
    1370                 :            :                                 eval_max_bound(&st->rv[ins->src_reg], msk);
    1371                 :            :                                 break;
    1372                 :            :                         default:
    1373                 :            :                                 return "unsupported atomic operation";
    1374                 :            :                         }
    1375                 :            :                 eval_apply_mask(&rs, msk);
    1376                 :            :         } else
    1377                 :         10 :                 eval_fill_imm(&rs, msk, ins->imm);
    1378                 :            : 
    1379                 :            :         err = eval_defined(NULL, &rs);
    1380                 :            :         if (err != NULL)
    1381                 :            :                 return err;
    1382                 :            : 
    1383                 :         89 :         err = eval_ptr(bvf, &rd, opsz, 1, ins->off);
    1384         [ +  + ]:         89 :         if (err != NULL)
    1385                 :            :                 return err;
    1386                 :            : 
    1387         [ +  + ]:         87 :         if (rd.v.type == BPF_ARG_PTR_STACK) {
    1388                 :            : 
    1389                 :         17 :                 sv = st->sv + rd.u.max / sizeof(uint64_t);
    1390         [ -  + ]:         17 :                 if (BPF_CLASS(ins->code) == BPF_STX &&
    1391                 :            :                                 BPF_MODE(ins->code) == EBPF_ATOMIC)
    1392                 :            :                         eval_max_bound(sv, msk);
    1393                 :            :                 else
    1394                 :         17 :                         *sv = rs;
    1395                 :            : 
    1396                 :            :         /* pointer to mbuf */
    1397         [ -  + ]:         70 :         } else if (rd.v.type == RTE_BPF_ARG_PTR_MBUF) {
    1398                 :            :                 err = eval_mbuf_store(&rd, opsz);
    1399                 :            :                 if (err != NULL)
    1400                 :          0 :                         return err;
    1401                 :            :         }
    1402                 :            : 
    1403                 :            :         return NULL;
    1404                 :            : }
    1405                 :            : 
    1406                 :            : static const char *
    1407                 :        199 : eval_ja(struct bpf_verifier *bvf, const struct rte_ebpf_insn *ins)
    1408                 :            : {
    1409                 :            :         RTE_SET_USED(bvf);
    1410                 :            :         RTE_SET_USED(ins);
    1411                 :        199 :         return NULL;
    1412                 :            : }
    1413                 :            : 
    1414                 :            : static const char *
    1415                 :        116 : eval_func_arg(struct bpf_verifier *bvf, const struct rte_bpf_arg *arg,
    1416                 :            :         struct bpf_reg_val *rv)
    1417                 :            : {
    1418                 :            :         uint32_t i, n;
    1419                 :            :         struct bpf_eval_state *st;
    1420                 :            :         const char *err;
    1421                 :            : 
    1422                 :        116 :         st = bvf->evst;
    1423                 :            : 
    1424         [ +  - ]:        116 :         if (rv->v.type == RTE_BPF_ARG_UNDEF)
    1425                 :            :                 return "Undefined argument type";
    1426                 :            : 
    1427   [ +  +  +  - ]:        116 :         if (arg->type != rv->v.type &&
    1428         [ +  - ]:          9 :                         arg->type != RTE_BPF_ARG_RAW &&
    1429                 :          9 :                         (arg->type != RTE_BPF_ARG_PTR ||
    1430         [ +  - ]:          9 :                         RTE_BPF_ARG_PTR_TYPE(rv->v.type) == 0))
    1431                 :            :                 return "Invalid argument type";
    1432                 :            : 
    1433                 :            :         err = NULL;
    1434                 :            : 
    1435                 :            :         /* argument is a pointer */
    1436         [ +  + ]:        116 :         if (RTE_BPF_ARG_PTR_TYPE(arg->type) != 0) {
    1437                 :            : 
    1438                 :         11 :                 err = eval_ptr(bvf, rv, arg->size, 1, 0);
    1439                 :            : 
    1440                 :            :                 /*
    1441                 :            :                  * pointer to the variable on the stack is passed
    1442                 :            :                  * as an argument, mark stack space it occupies as initialized.
    1443                 :            :                  */
    1444   [ +  -  +  + ]:         11 :                 if (err == NULL && rv->v.type == BPF_ARG_PTR_STACK) {
    1445                 :            : 
    1446                 :          9 :                         i = rv->u.max / sizeof(uint64_t);
    1447                 :          9 :                         n = i + arg->size / sizeof(uint64_t);
    1448         [ +  + ]:         14 :                         while (i != n) {
    1449                 :          5 :                                 eval_fill_max_bound(st->sv + i, UINT64_MAX);
    1450                 :          5 :                                 i++;
    1451                 :            :                         };
    1452                 :            :                 }
    1453                 :            :         }
    1454                 :            : 
    1455                 :            :         return err;
    1456                 :            : }
    1457                 :            : 
    1458                 :            : static const char *
    1459                 :        107 : eval_call(struct bpf_verifier *bvf, const struct rte_ebpf_insn *ins)
    1460                 :            : {
    1461                 :            :         uint32_t i, idx;
    1462                 :            :         struct bpf_reg_val *rv;
    1463                 :            :         const struct rte_bpf_xsym *xsym;
    1464                 :            :         const char *err;
    1465                 :            : 
    1466                 :        107 :         idx = ins->imm;
    1467                 :            : 
    1468         [ +  - ]:        107 :         if (idx >= bvf->prm->nb_xsym ||
    1469         [ +  - ]:        107 :                         bvf->prm->xsym[idx].type != RTE_BPF_XTYPE_FUNC)
    1470                 :            :                 return "invalid external function index";
    1471                 :            : 
    1472                 :            :         /* for now don't support function calls on 32 bit platform */
    1473                 :            :         if (sizeof(uint64_t) != sizeof(uintptr_t))
    1474                 :            :                 return "function calls are supported only for 64 bit apps";
    1475                 :            : 
    1476                 :            :         xsym = bvf->prm->xsym + idx;
    1477                 :            : 
    1478                 :            :         /* evaluate function arguments */
    1479                 :            :         err = NULL;
    1480   [ +  +  +  - ]:        223 :         for (i = 0; i != xsym->func.nb_args && err == NULL; i++) {
    1481                 :        116 :                 err = eval_func_arg(bvf, xsym->func.args + i,
    1482                 :        116 :                         bvf->evst->rv + EBPF_REG_1 + i);
    1483                 :            :         }
    1484                 :            : 
    1485                 :            :         /* R1-R5 argument/scratch registers */
    1486         [ +  + ]:        642 :         for (i = EBPF_REG_1; i != EBPF_REG_6; i++)
    1487                 :        535 :                 bvf->evst->rv[i].v.type = RTE_BPF_ARG_UNDEF;
    1488                 :            : 
    1489                 :            :         /* update return value */
    1490                 :            : 
    1491                 :        107 :         rv = bvf->evst->rv + EBPF_REG_0;
    1492                 :        107 :         rv->v = xsym->func.ret;
    1493         [ +  + ]:        107 :         if (rv->v.type == RTE_BPF_ARG_RAW)
    1494                 :        104 :                 eval_fill_max_bound(rv,
    1495                 :        104 :                         RTE_LEN2MASK(rv->v.size * CHAR_BIT, uint64_t));
    1496         [ +  + ]:          3 :         else if (RTE_BPF_ARG_PTR_TYPE(rv->v.type) != 0)
    1497                 :            :                 eval_fill_imm64(rv, UINTPTR_MAX, 0);
    1498                 :            : 
    1499                 :            :         return err;
    1500                 :            : }
    1501                 :            : 
    1502                 :            : static void
    1503                 :        525 : eval_jeq_jne(struct bpf_reg_val *trd, struct bpf_reg_val *trs)
    1504                 :            : {
    1505                 :            :         /* sreg is constant */
    1506         [ +  + ]:        525 :         if (trs->u.min == trs->u.max) {
    1507                 :        507 :                 trd->u = trs->u;
    1508                 :            :         /* dreg is constant */
    1509         [ +  + ]:         18 :         } else if (trd->u.min == trd->u.max) {
    1510                 :          8 :                 trs->u = trd->u;
    1511                 :            :         } else {
    1512                 :         10 :                 trd->u.max = RTE_MIN(trd->u.max, trs->u.max);
    1513                 :         10 :                 trd->u.min = RTE_MAX(trd->u.min, trs->u.min);
    1514                 :         10 :                 trs->u = trd->u;
    1515                 :            :         }
    1516                 :            : 
    1517                 :            :         /* sreg is constant */
    1518         [ +  + ]:        525 :         if (trs->s.min == trs->s.max) {
    1519                 :        507 :                 trd->s = trs->s;
    1520                 :            :         /* dreg is constant */
    1521         [ +  + ]:         18 :         } else if (trd->s.min == trd->s.max) {
    1522                 :          8 :                 trs->s = trd->s;
    1523                 :            :         } else {
    1524                 :         10 :                 trd->s.max = RTE_MIN(trd->s.max, trs->s.max);
    1525                 :         10 :                 trd->s.min = RTE_MAX(trd->s.min, trs->s.min);
    1526                 :         10 :                 trs->s = trd->s;
    1527                 :            :         }
    1528                 :        525 : }
    1529                 :            : 
    1530                 :            : static void
    1531                 :            : eval_jgt_jle(struct bpf_reg_val *trd, struct bpf_reg_val *trs,
    1532                 :            :         struct bpf_reg_val *frd, struct bpf_reg_val *frs)
    1533                 :            : {
    1534   [ +  +  +  + ]:        691 :         if (frd->u.min <= frs->u.max) {
    1535                 :        674 :                 frd->u.max = RTE_MIN(frd->u.max, frs->u.max);
    1536                 :        674 :                 frs->u.min = RTE_MAX(frs->u.min, frd->u.min);
    1537                 :            :         } else
    1538                 :         17 :                 frd->v.type = frs->v.type = BPF_ARG_UNINHABITED;
    1539                 :            : 
    1540   [ +  +  +  + ]:        691 :         if (trs->u.min < trd->u.max) {
    1541                 :        650 :                 trd->u.min = RTE_MAX(trd->u.min, trs->u.min + 1);
    1542                 :        650 :                 trs->u.max = RTE_MIN(trs->u.max, trd->u.max - 1);
    1543                 :            :         } else
    1544                 :         41 :                 trd->v.type = trs->v.type = BPF_ARG_UNINHABITED;
    1545                 :            : }
    1546                 :            : 
    1547                 :            : static void
    1548                 :            : eval_jlt_jge(struct bpf_reg_val *trd, struct bpf_reg_val *trs,
    1549                 :            :         struct bpf_reg_val *frd, struct bpf_reg_val *frs)
    1550                 :            : {
    1551   [ +  +  +  + ]:        613 :         if (frs->u.min <= frd->u.max) {
    1552                 :        596 :                 frd->u.min = RTE_MAX(frd->u.min, frs->u.min);
    1553                 :        596 :                 frs->u.max = RTE_MIN(frs->u.max, frd->u.max);
    1554                 :            :         } else
    1555                 :         17 :                 frd->v.type = frs->v.type = BPF_ARG_UNINHABITED;
    1556                 :            : 
    1557   [ +  +  +  + ]:        613 :         if (trd->u.min < trs->u.max) {
    1558                 :        587 :                 trd->u.max = RTE_MIN(trd->u.max, trs->u.max - 1);
    1559                 :        587 :                 trs->u.min = RTE_MAX(trs->u.min, trd->u.min + 1);
    1560                 :            :         } else
    1561                 :         26 :                 trd->v.type = trs->v.type = BPF_ARG_UNINHABITED;
    1562                 :            : }
    1563                 :            : 
    1564                 :            : static void
    1565                 :            : eval_jsgt_jsle(struct bpf_reg_val *trd, struct bpf_reg_val *trs,
    1566                 :            :         struct bpf_reg_val *frd, struct bpf_reg_val *frs)
    1567                 :            : {
    1568   [ +  +  +  + ]:        651 :         if (frd->s.min <= frs->s.max) {
    1569                 :        635 :                 frd->s.max = RTE_MIN(frd->s.max, frs->s.max);
    1570                 :        635 :                 frs->s.min = RTE_MAX(frs->s.min, frd->s.min);
    1571                 :            :         } else
    1572                 :         16 :                 frd->v.type = frs->v.type = BPF_ARG_UNINHABITED;
    1573                 :            : 
    1574   [ +  +  +  + ]:        651 :         if (trs->s.min < trd->s.max) {
    1575                 :        607 :                 trd->s.min = RTE_MAX(trd->s.min, trs->s.min + 1);
    1576                 :        607 :                 trs->s.max = RTE_MIN(trs->s.max, trd->s.max - 1);
    1577                 :            :         } else
    1578                 :         44 :                 trd->v.type = trs->v.type = BPF_ARG_UNINHABITED;
    1579                 :            : }
    1580                 :            : 
    1581                 :            : static void
    1582                 :            : eval_jslt_jsge(struct bpf_reg_val *trd, struct bpf_reg_val *trs,
    1583                 :            :         struct bpf_reg_val *frd, struct bpf_reg_val *frs)
    1584                 :            : {
    1585   [ +  +  +  + ]:        606 :         if (frs->s.min <= frd->s.max) {
    1586                 :        590 :                 frd->s.min = RTE_MAX(frd->s.min, frs->s.min);
    1587                 :        590 :                 frs->s.max = RTE_MIN(frs->s.max, frd->s.max);
    1588                 :            :         } else
    1589                 :         16 :                 frd->v.type = frs->v.type = BPF_ARG_UNINHABITED;
    1590                 :            : 
    1591   [ +  +  +  + ]:        606 :         if (trd->s.min < trs->s.max) {
    1592                 :        582 :                 trd->s.max = RTE_MIN(trd->s.max, trs->s.max - 1);
    1593                 :        582 :                 trs->s.min = RTE_MAX(trs->s.min, trd->s.min + 1);
    1594                 :            :         } else
    1595                 :         24 :                 trd->v.type = trs->v.type = BPF_ARG_UNINHABITED;
    1596                 :            : }
    1597                 :            : 
    1598                 :            : static const char *
    1599                 :       3213 : eval_jcc(struct bpf_verifier *bvf, const struct rte_ebpf_insn *ins)
    1600                 :            : {
    1601                 :            :         uint32_t op;
    1602                 :            :         const char *err;
    1603                 :            :         struct bpf_eval_state *fst, *tst;
    1604                 :            :         struct bpf_reg_val *frd, *frs, *trd, *trs;
    1605                 :            :         struct bpf_reg_val rvf, rvt;
    1606                 :            : 
    1607                 :       3213 :         tst = bvf->evst;
    1608                 :       3213 :         fst = bvf->evin->evst.cur;
    1609                 :            : 
    1610                 :       3213 :         frd = fst->rv + ins->dst_reg;
    1611                 :       3213 :         trd = tst->rv + ins->dst_reg;
    1612                 :            : 
    1613         [ +  + ]:       3213 :         if (BPF_SRC(ins->code) == BPF_X) {
    1614                 :        625 :                 frs = fst->rv + ins->src_reg;
    1615                 :        625 :                 trs = tst->rv + ins->src_reg;
    1616                 :            :         } else {
    1617                 :            :                 frs = &rvf;
    1618                 :            :                 trs = &rvt;
    1619                 :       2588 :                 eval_fill_imm(frs, UINT64_MAX, ins->imm);
    1620                 :            :                 eval_fill_imm(trs, UINT64_MAX, ins->imm);
    1621                 :            :         }
    1622                 :            : 
    1623                 :            :         err = eval_defined(trd, trs);
    1624                 :            :         if (err != NULL)
    1625                 :          0 :                 return err;
    1626                 :            : 
    1627                 :       3213 :         op = BPF_OP(ins->code);
    1628                 :            : 
    1629         [ +  + ]:       3213 :         if (op == BPF_JEQ)
    1630                 :        195 :                 eval_jeq_jne(trd, trs);
    1631         [ +  + ]:       3018 :         else if (op == EBPF_JNE)
    1632                 :        330 :                 eval_jeq_jne(frd, frs);
    1633         [ +  + ]:       2688 :         else if (op == BPF_JGT)
    1634                 :            :                 eval_jgt_jle(trd, trs, frd, frs);
    1635         [ +  + ]:       2081 :         else if (op == EBPF_JLE)
    1636                 :            :                 eval_jgt_jle(frd, frs, trd, trs);
    1637         [ +  + ]:       1997 :         else if (op == EBPF_JLT)
    1638                 :            :                 eval_jlt_jge(trd, trs, frd, frs);
    1639         [ +  + ]:       1433 :         else if (op == BPF_JGE)
    1640                 :            :                 eval_jlt_jge(frd, frs, trd, trs);
    1641         [ +  + ]:       1384 :         else if (op == EBPF_JSGT)
    1642                 :            :                 eval_jsgt_jsle(trd, trs, frd, frs);
    1643         [ +  + ]:        775 :         else if (op == EBPF_JSLE)
    1644                 :            :                 eval_jsgt_jsle(frd, frs, trd, trs);
    1645         [ +  + ]:        733 :         else if (op == EBPF_JSLT)
    1646                 :            :                 eval_jslt_jsge(trd, trs, frd, frs);
    1647         [ +  + ]:        167 :         else if (op == EBPF_JSGE)
    1648                 :            :                 eval_jslt_jsge(frd, frs, trd, trs);
    1649                 :            : 
    1650         [ +  + ]:       3213 :         if (trd->v.type == BPF_ARG_UNINHABITED ||
    1651         [ -  + ]:       3111 :                         trs->v.type == BPF_ARG_UNINHABITED)
    1652                 :        102 :                 tst->unreachable = true;
    1653                 :            : 
    1654         [ +  + ]:       3213 :         if (frd->v.type == BPF_ARG_UNINHABITED ||
    1655         [ -  + ]:       3114 :                         frs->v.type == BPF_ARG_UNINHABITED)
    1656                 :         99 :                 fst->unreachable = true;
    1657                 :            : 
    1658                 :            :         return NULL;
    1659                 :            : }
    1660                 :            : 
    1661                 :            : /*
    1662                 :            :  * validate parameters for each instruction type.
    1663                 :            :  */
    1664                 :            : static const struct bpf_ins_check ins_chk[UINT8_MAX + 1] = {
    1665                 :            :         /* ALU IMM 32-bit instructions */
    1666                 :            :         [(BPF_ALU | BPF_ADD | BPF_K)] = {
    1667                 :            :                 .mask = {.dreg = WRT_REGS, .sreg = ZERO_REG},
    1668                 :            :                 .off = { .min = 0, .max = 0},
    1669                 :            :                 .imm = { .min = 0, .max = UINT32_MAX,},
    1670                 :            :                 .eval = eval_alu,
    1671                 :            :         },
    1672                 :            :         [(BPF_ALU | BPF_SUB | BPF_K)] = {
    1673                 :            :                 .mask = {.dreg = WRT_REGS, .sreg = ZERO_REG},
    1674                 :            :                 .off = { .min = 0, .max = 0},
    1675                 :            :                 .imm = { .min = 0, .max = UINT32_MAX,},
    1676                 :            :                 .eval = eval_alu,
    1677                 :            :         },
    1678                 :            :         [(BPF_ALU | BPF_AND | BPF_K)] = {
    1679                 :            :                 .mask = {.dreg = WRT_REGS, .sreg = ZERO_REG},
    1680                 :            :                 .off = { .min = 0, .max = 0},
    1681                 :            :                 .imm = { .min = 0, .max = UINT32_MAX,},
    1682                 :            :                 .eval = eval_alu,
    1683                 :            :         },
    1684                 :            :         [(BPF_ALU | BPF_OR | BPF_K)] = {
    1685                 :            :                 .mask = {.dreg = WRT_REGS, .sreg = ZERO_REG},
    1686                 :            :                 .off = { .min = 0, .max = 0},
    1687                 :            :                 .imm = { .min = 0, .max = UINT32_MAX,},
    1688                 :            :                 .eval = eval_alu,
    1689                 :            :         },
    1690                 :            :         [(BPF_ALU | BPF_LSH | BPF_K)] = {
    1691                 :            :                 .mask = {.dreg = WRT_REGS, .sreg = ZERO_REG},
    1692                 :            :                 .off = { .min = 0, .max = 0},
    1693                 :            :                 .imm = { .min = 0, .max = UINT32_MAX,},
    1694                 :            :                 .eval = eval_alu,
    1695                 :            :         },
    1696                 :            :         [(BPF_ALU | BPF_RSH | BPF_K)] = {
    1697                 :            :                 .mask = {.dreg = WRT_REGS, .sreg = ZERO_REG},
    1698                 :            :                 .off = { .min = 0, .max = 0},
    1699                 :            :                 .imm = { .min = 0, .max = UINT32_MAX,},
    1700                 :            :                 .eval = eval_alu,
    1701                 :            :         },
    1702                 :            :         [(BPF_ALU | BPF_XOR | BPF_K)] = {
    1703                 :            :                 .mask = {.dreg = WRT_REGS, .sreg = ZERO_REG},
    1704                 :            :                 .off = { .min = 0, .max = 0},
    1705                 :            :                 .imm = { .min = 0, .max = UINT32_MAX,},
    1706                 :            :                 .eval = eval_alu,
    1707                 :            :         },
    1708                 :            :         [(BPF_ALU | BPF_MUL | BPF_K)] = {
    1709                 :            :                 .mask = {.dreg = WRT_REGS, .sreg = ZERO_REG},
    1710                 :            :                 .off = { .min = 0, .max = 0},
    1711                 :            :                 .imm = { .min = 0, .max = UINT32_MAX,},
    1712                 :            :                 .eval = eval_alu,
    1713                 :            :         },
    1714                 :            :         [(BPF_ALU | EBPF_MOV | BPF_K)] = {
    1715                 :            :                 .mask = {.dreg = WRT_REGS, .sreg = ZERO_REG},
    1716                 :            :                 .off = { .min = 0, .max = 0},
    1717                 :            :                 .imm = { .min = 0, .max = UINT32_MAX,},
    1718                 :            :                 .eval = eval_alu,
    1719                 :            :         },
    1720                 :            :         [(BPF_ALU | BPF_DIV | BPF_K)] = {
    1721                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ZERO_REG},
    1722                 :            :                 .off = { .min = 0, .max = 0},
    1723                 :            :                 .imm = { .min = 1, .max = UINT32_MAX},
    1724                 :            :                 .eval = eval_alu,
    1725                 :            :         },
    1726                 :            :         [(BPF_ALU | BPF_MOD | BPF_K)] = {
    1727                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ZERO_REG},
    1728                 :            :                 .off = { .min = 0, .max = 0},
    1729                 :            :                 .imm = { .min = 1, .max = UINT32_MAX},
    1730                 :            :                 .eval = eval_alu,
    1731                 :            :         },
    1732                 :            :         /* ALU IMM 64-bit instructions */
    1733                 :            :         [(EBPF_ALU64 | BPF_ADD | BPF_K)] = {
    1734                 :            :                 .mask = {.dreg = WRT_REGS, .sreg = ZERO_REG},
    1735                 :            :                 .off = { .min = 0, .max = 0},
    1736                 :            :                 .imm = { .min = 0, .max = UINT32_MAX,},
    1737                 :            :                 .eval = eval_alu,
    1738                 :            :         },
    1739                 :            :         [(EBPF_ALU64 | BPF_SUB | BPF_K)] = {
    1740                 :            :                 .mask = {.dreg = WRT_REGS, .sreg = ZERO_REG},
    1741                 :            :                 .off = { .min = 0, .max = 0},
    1742                 :            :                 .imm = { .min = 0, .max = UINT32_MAX,},
    1743                 :            :                 .eval = eval_alu,
    1744                 :            :         },
    1745                 :            :         [(EBPF_ALU64 | BPF_AND | BPF_K)] = {
    1746                 :            :                 .mask = {.dreg = WRT_REGS, .sreg = ZERO_REG},
    1747                 :            :                 .off = { .min = 0, .max = 0},
    1748                 :            :                 .imm = { .min = 0, .max = UINT32_MAX,},
    1749                 :            :                 .eval = eval_alu,
    1750                 :            :         },
    1751                 :            :         [(EBPF_ALU64 | BPF_OR | BPF_K)] = {
    1752                 :            :                 .mask = {.dreg = WRT_REGS, .sreg = ZERO_REG},
    1753                 :            :                 .off = { .min = 0, .max = 0},
    1754                 :            :                 .imm = { .min = 0, .max = UINT32_MAX,},
    1755                 :            :                 .eval = eval_alu,
    1756                 :            :         },
    1757                 :            :         [(EBPF_ALU64 | BPF_LSH | BPF_K)] = {
    1758                 :            :                 .mask = {.dreg = WRT_REGS, .sreg = ZERO_REG},
    1759                 :            :                 .off = { .min = 0, .max = 0},
    1760                 :            :                 .imm = { .min = 0, .max = UINT32_MAX,},
    1761                 :            :                 .eval = eval_alu,
    1762                 :            :         },
    1763                 :            :         [(EBPF_ALU64 | BPF_RSH | BPF_K)] = {
    1764                 :            :                 .mask = {.dreg = WRT_REGS, .sreg = ZERO_REG},
    1765                 :            :                 .off = { .min = 0, .max = 0},
    1766                 :            :                 .imm = { .min = 0, .max = UINT32_MAX,},
    1767                 :            :                 .eval = eval_alu,
    1768                 :            :         },
    1769                 :            :         [(EBPF_ALU64 | EBPF_ARSH | BPF_K)] = {
    1770                 :            :                 .mask = {.dreg = WRT_REGS, .sreg = ZERO_REG},
    1771                 :            :                 .off = { .min = 0, .max = 0},
    1772                 :            :                 .imm = { .min = 0, .max = UINT32_MAX,},
    1773                 :            :                 .eval = eval_alu,
    1774                 :            :         },
    1775                 :            :         [(EBPF_ALU64 | BPF_XOR | BPF_K)] = {
    1776                 :            :                 .mask = {.dreg = WRT_REGS, .sreg = ZERO_REG},
    1777                 :            :                 .off = { .min = 0, .max = 0},
    1778                 :            :                 .imm = { .min = 0, .max = UINT32_MAX,},
    1779                 :            :                 .eval = eval_alu,
    1780                 :            :         },
    1781                 :            :         [(EBPF_ALU64 | BPF_MUL | BPF_K)] = {
    1782                 :            :                 .mask = {.dreg = WRT_REGS, .sreg = ZERO_REG},
    1783                 :            :                 .off = { .min = 0, .max = 0},
    1784                 :            :                 .imm = { .min = 0, .max = UINT32_MAX,},
    1785                 :            :                 .eval = eval_alu,
    1786                 :            :         },
    1787                 :            :         [(EBPF_ALU64 | EBPF_MOV | BPF_K)] = {
    1788                 :            :                 .mask = {.dreg = WRT_REGS, .sreg = ZERO_REG},
    1789                 :            :                 .off = { .min = 0, .max = 0},
    1790                 :            :                 .imm = { .min = 0, .max = UINT32_MAX,},
    1791                 :            :                 .eval = eval_alu,
    1792                 :            :         },
    1793                 :            :         [(EBPF_ALU64 | BPF_DIV | BPF_K)] = {
    1794                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ZERO_REG},
    1795                 :            :                 .off = { .min = 0, .max = 0},
    1796                 :            :                 .imm = { .min = 1, .max = UINT32_MAX},
    1797                 :            :                 .eval = eval_alu,
    1798                 :            :         },
    1799                 :            :         [(EBPF_ALU64 | BPF_MOD | BPF_K)] = {
    1800                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ZERO_REG},
    1801                 :            :                 .off = { .min = 0, .max = 0},
    1802                 :            :                 .imm = { .min = 1, .max = UINT32_MAX},
    1803                 :            :                 .eval = eval_alu,
    1804                 :            :         },
    1805                 :            :         /* ALU REG 32-bit instructions */
    1806                 :            :         [(BPF_ALU | BPF_ADD | BPF_X)] = {
    1807                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1808                 :            :                 .off = { .min = 0, .max = 0},
    1809                 :            :                 .imm = { .min = 0, .max = 0},
    1810                 :            :                 .eval = eval_alu,
    1811                 :            :         },
    1812                 :            :         [(BPF_ALU | BPF_SUB | BPF_X)] = {
    1813                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1814                 :            :                 .off = { .min = 0, .max = 0},
    1815                 :            :                 .imm = { .min = 0, .max = 0},
    1816                 :            :                 .eval = eval_alu,
    1817                 :            :         },
    1818                 :            :         [(BPF_ALU | BPF_AND | BPF_X)] = {
    1819                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1820                 :            :                 .off = { .min = 0, .max = 0},
    1821                 :            :                 .imm = { .min = 0, .max = 0},
    1822                 :            :                 .eval = eval_alu,
    1823                 :            :         },
    1824                 :            :         [(BPF_ALU | BPF_OR | BPF_X)] = {
    1825                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1826                 :            :                 .off = { .min = 0, .max = 0},
    1827                 :            :                 .imm = { .min = 0, .max = 0},
    1828                 :            :                 .eval = eval_alu,
    1829                 :            :         },
    1830                 :            :         [(BPF_ALU | BPF_LSH | BPF_X)] = {
    1831                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1832                 :            :                 .off = { .min = 0, .max = 0},
    1833                 :            :                 .imm = { .min = 0, .max = 0},
    1834                 :            :                 .eval = eval_alu,
    1835                 :            :         },
    1836                 :            :         [(BPF_ALU | BPF_RSH | BPF_X)] = {
    1837                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1838                 :            :                 .off = { .min = 0, .max = 0},
    1839                 :            :                 .imm = { .min = 0, .max = 0},
    1840                 :            :                 .eval = eval_alu,
    1841                 :            :         },
    1842                 :            :         [(BPF_ALU | BPF_XOR | BPF_X)] = {
    1843                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1844                 :            :                 .off = { .min = 0, .max = 0},
    1845                 :            :                 .imm = { .min = 0, .max = 0},
    1846                 :            :                 .eval = eval_alu,
    1847                 :            :         },
    1848                 :            :         [(BPF_ALU | BPF_MUL | BPF_X)] = {
    1849                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1850                 :            :                 .off = { .min = 0, .max = 0},
    1851                 :            :                 .imm = { .min = 0, .max = 0},
    1852                 :            :                 .eval = eval_alu,
    1853                 :            :         },
    1854                 :            :         [(BPF_ALU | BPF_DIV | BPF_X)] = {
    1855                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1856                 :            :                 .off = { .min = 0, .max = 0},
    1857                 :            :                 .imm = { .min = 0, .max = 0},
    1858                 :            :                 .eval = eval_alu,
    1859                 :            :         },
    1860                 :            :         [(BPF_ALU | BPF_MOD | BPF_X)] = {
    1861                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1862                 :            :                 .off = { .min = 0, .max = 0},
    1863                 :            :                 .imm = { .min = 0, .max = 0},
    1864                 :            :                 .eval = eval_alu,
    1865                 :            :         },
    1866                 :            :         [(BPF_ALU | EBPF_MOV | BPF_X)] = {
    1867                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1868                 :            :                 .off = { .min = 0, .max = 0},
    1869                 :            :                 .imm = { .min = 0, .max = 0},
    1870                 :            :                 .eval = eval_alu,
    1871                 :            :         },
    1872                 :            :         [(BPF_ALU | BPF_NEG)] = {
    1873                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ZERO_REG},
    1874                 :            :                 .off = { .min = 0, .max = 0},
    1875                 :            :                 .imm = { .min = 0, .max = 0},
    1876                 :            :                 .eval = eval_alu,
    1877                 :            :         },
    1878                 :            :         [(BPF_ALU | EBPF_END | EBPF_TO_BE)] = {
    1879                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ZERO_REG},
    1880                 :            :                 .off = { .min = 0, .max = 0},
    1881                 :            :                 .imm = { .min = 16, .max = 64},
    1882                 :            :                 .check = check_alu_bele,
    1883                 :            :                 .eval = eval_bele,
    1884                 :            :         },
    1885                 :            :         [(BPF_ALU | EBPF_END | EBPF_TO_LE)] = {
    1886                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ZERO_REG},
    1887                 :            :                 .off = { .min = 0, .max = 0},
    1888                 :            :                 .imm = { .min = 16, .max = 64},
    1889                 :            :                 .check = check_alu_bele,
    1890                 :            :                 .eval = eval_bele,
    1891                 :            :         },
    1892                 :            :         /* ALU REG 64-bit instructions */
    1893                 :            :         [(EBPF_ALU64 | BPF_ADD | BPF_X)] = {
    1894                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1895                 :            :                 .off = { .min = 0, .max = 0},
    1896                 :            :                 .imm = { .min = 0, .max = 0},
    1897                 :            :                 .eval = eval_alu,
    1898                 :            :         },
    1899                 :            :         [(EBPF_ALU64 | BPF_SUB | BPF_X)] = {
    1900                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1901                 :            :                 .off = { .min = 0, .max = 0},
    1902                 :            :                 .imm = { .min = 0, .max = 0},
    1903                 :            :                 .eval = eval_alu,
    1904                 :            :         },
    1905                 :            :         [(EBPF_ALU64 | BPF_AND | BPF_X)] = {
    1906                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1907                 :            :                 .off = { .min = 0, .max = 0},
    1908                 :            :                 .imm = { .min = 0, .max = 0},
    1909                 :            :                 .eval = eval_alu,
    1910                 :            :         },
    1911                 :            :         [(EBPF_ALU64 | BPF_OR | BPF_X)] = {
    1912                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1913                 :            :                 .off = { .min = 0, .max = 0},
    1914                 :            :                 .imm = { .min = 0, .max = 0},
    1915                 :            :                 .eval = eval_alu,
    1916                 :            :         },
    1917                 :            :         [(EBPF_ALU64 | BPF_LSH | BPF_X)] = {
    1918                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1919                 :            :                 .off = { .min = 0, .max = 0},
    1920                 :            :                 .imm = { .min = 0, .max = 0},
    1921                 :            :                 .eval = eval_alu,
    1922                 :            :         },
    1923                 :            :         [(EBPF_ALU64 | BPF_RSH | BPF_X)] = {
    1924                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1925                 :            :                 .off = { .min = 0, .max = 0},
    1926                 :            :                 .imm = { .min = 0, .max = 0},
    1927                 :            :                 .eval = eval_alu,
    1928                 :            :         },
    1929                 :            :         [(EBPF_ALU64 | EBPF_ARSH | BPF_X)] = {
    1930                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1931                 :            :                 .off = { .min = 0, .max = 0},
    1932                 :            :                 .imm = { .min = 0, .max = 0},
    1933                 :            :                 .eval = eval_alu,
    1934                 :            :         },
    1935                 :            :         [(EBPF_ALU64 | BPF_XOR | BPF_X)] = {
    1936                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1937                 :            :                 .off = { .min = 0, .max = 0},
    1938                 :            :                 .imm = { .min = 0, .max = 0},
    1939                 :            :                 .eval = eval_alu,
    1940                 :            :         },
    1941                 :            :         [(EBPF_ALU64 | BPF_MUL | BPF_X)] = {
    1942                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1943                 :            :                 .off = { .min = 0, .max = 0},
    1944                 :            :                 .imm = { .min = 0, .max = 0},
    1945                 :            :                 .eval = eval_alu,
    1946                 :            :         },
    1947                 :            :         [(EBPF_ALU64 | BPF_DIV | BPF_X)] = {
    1948                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1949                 :            :                 .off = { .min = 0, .max = 0},
    1950                 :            :                 .imm = { .min = 0, .max = 0},
    1951                 :            :                 .eval = eval_alu,
    1952                 :            :         },
    1953                 :            :         [(EBPF_ALU64 | BPF_MOD | BPF_X)] = {
    1954                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1955                 :            :                 .off = { .min = 0, .max = 0},
    1956                 :            :                 .imm = { .min = 0, .max = 0},
    1957                 :            :                 .eval = eval_alu,
    1958                 :            :         },
    1959                 :            :         [(EBPF_ALU64 | EBPF_MOV | BPF_X)] = {
    1960                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ALL_REGS},
    1961                 :            :                 .off = { .min = 0, .max = 0},
    1962                 :            :                 .imm = { .min = 0, .max = 0},
    1963                 :            :                 .eval = eval_alu,
    1964                 :            :         },
    1965                 :            :         [(EBPF_ALU64 | BPF_NEG)] = {
    1966                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ZERO_REG},
    1967                 :            :                 .off = { .min = 0, .max = 0},
    1968                 :            :                 .imm = { .min = 0, .max = 0},
    1969                 :            :                 .eval = eval_alu,
    1970                 :            :         },
    1971                 :            :         /* load instructions */
    1972                 :            :         [(BPF_LDX | BPF_MEM | BPF_B)] = {
    1973                 :            :                 .mask = {. dreg = WRT_REGS, .sreg = ALL_REGS},
    1974                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    1975                 :            :                 .imm = { .min = 0, .max = 0},
    1976                 :            :                 .eval = eval_load,
    1977                 :            :         },
    1978                 :            :         [(BPF_LDX | BPF_MEM | BPF_H)] = {
    1979                 :            :                 .mask = {. dreg = WRT_REGS, .sreg = ALL_REGS},
    1980                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    1981                 :            :                 .imm = { .min = 0, .max = 0},
    1982                 :            :                 .eval = eval_load,
    1983                 :            :         },
    1984                 :            :         [(BPF_LDX | BPF_MEM | BPF_W)] = {
    1985                 :            :                 .mask = {. dreg = WRT_REGS, .sreg = ALL_REGS},
    1986                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    1987                 :            :                 .imm = { .min = 0, .max = 0},
    1988                 :            :                 .eval = eval_load,
    1989                 :            :         },
    1990                 :            :         [(BPF_LDX | BPF_MEM | EBPF_DW)] = {
    1991                 :            :                 .mask = {. dreg = WRT_REGS, .sreg = ALL_REGS},
    1992                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    1993                 :            :                 .imm = { .min = 0, .max = 0},
    1994                 :            :                 .eval = eval_load,
    1995                 :            :         },
    1996                 :            :         /* load 64 bit immediate value */
    1997                 :            :         [(BPF_LD | BPF_IMM | EBPF_DW)] = {
    1998                 :            :                 .mask = { .dreg = WRT_REGS, .sreg = ZERO_REG},
    1999                 :            :                 .off = { .min = 0, .max = 0},
    2000                 :            :                 .imm = { .min = 0, .max = UINT32_MAX},
    2001                 :            :                 .eval = eval_ld_imm64,
    2002                 :            :         },
    2003                 :            :         /* load absolute instructions */
    2004                 :            :         [(BPF_LD | BPF_ABS | BPF_B)] = {
    2005                 :            :                 .mask = {. dreg = ZERO_REG, .sreg = ZERO_REG},
    2006                 :            :                 .off = { .min = 0, .max = 0},
    2007                 :            :                 .imm = { .min = 0, .max = INT32_MAX},
    2008                 :            :                 .eval = eval_ld_mbuf,
    2009                 :            :         },
    2010                 :            :         [(BPF_LD | BPF_ABS | BPF_H)] = {
    2011                 :            :                 .mask = {. dreg = ZERO_REG, .sreg = ZERO_REG},
    2012                 :            :                 .off = { .min = 0, .max = 0},
    2013                 :            :                 .imm = { .min = 0, .max = INT32_MAX},
    2014                 :            :                 .eval = eval_ld_mbuf,
    2015                 :            :         },
    2016                 :            :         [(BPF_LD | BPF_ABS | BPF_W)] = {
    2017                 :            :                 .mask = {. dreg = ZERO_REG, .sreg = ZERO_REG},
    2018                 :            :                 .off = { .min = 0, .max = 0},
    2019                 :            :                 .imm = { .min = 0, .max = INT32_MAX},
    2020                 :            :                 .eval = eval_ld_mbuf,
    2021                 :            :         },
    2022                 :            :         /* load indirect instructions */
    2023                 :            :         [(BPF_LD | BPF_IND | BPF_B)] = {
    2024                 :            :                 .mask = {. dreg = ZERO_REG, .sreg = IND_SRC_REGS},
    2025                 :            :                 .off = { .min = 0, .max = 0},
    2026                 :            :                 .imm = { .min = 0, .max = UINT32_MAX},
    2027                 :            :                 .eval = eval_ld_mbuf,
    2028                 :            :         },
    2029                 :            :         [(BPF_LD | BPF_IND | BPF_H)] = {
    2030                 :            :                 .mask = {. dreg = ZERO_REG, .sreg = IND_SRC_REGS},
    2031                 :            :                 .off = { .min = 0, .max = 0},
    2032                 :            :                 .imm = { .min = 0, .max = UINT32_MAX},
    2033                 :            :                 .eval = eval_ld_mbuf,
    2034                 :            :         },
    2035                 :            :         [(BPF_LD | BPF_IND | BPF_W)] = {
    2036                 :            :                 .mask = {. dreg = ZERO_REG, .sreg = IND_SRC_REGS},
    2037                 :            :                 .off = { .min = 0, .max = 0},
    2038                 :            :                 .imm = { .min = 0, .max = UINT32_MAX},
    2039                 :            :                 .eval = eval_ld_mbuf,
    2040                 :            :         },
    2041                 :            :         /* store REG instructions */
    2042                 :            :         [(BPF_STX | BPF_MEM | BPF_B)] = {
    2043                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ALL_REGS},
    2044                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2045                 :            :                 .imm = { .min = 0, .max = 0},
    2046                 :            :                 .eval = eval_store,
    2047                 :            :         },
    2048                 :            :         [(BPF_STX | BPF_MEM | BPF_H)] = {
    2049                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ALL_REGS},
    2050                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2051                 :            :                 .imm = { .min = 0, .max = 0},
    2052                 :            :                 .eval = eval_store,
    2053                 :            :         },
    2054                 :            :         [(BPF_STX | BPF_MEM | BPF_W)] = {
    2055                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ALL_REGS},
    2056                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2057                 :            :                 .imm = { .min = 0, .max = 0},
    2058                 :            :                 .eval = eval_store,
    2059                 :            :         },
    2060                 :            :         [(BPF_STX | BPF_MEM | EBPF_DW)] = {
    2061                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ALL_REGS},
    2062                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2063                 :            :                 .imm = { .min = 0, .max = 0},
    2064                 :            :                 .eval = eval_store,
    2065                 :            :         },
    2066                 :            :         /* atomic instructions */
    2067                 :            :         [(BPF_STX | EBPF_ATOMIC | BPF_W)] = {
    2068                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ALL_REGS},
    2069                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2070                 :            :                 .imm = { .min = BPF_ATOMIC_ADD, .max = BPF_ATOMIC_XCHG},
    2071                 :            :                 .eval = eval_store,
    2072                 :            :         },
    2073                 :            :         [(BPF_STX | EBPF_ATOMIC | EBPF_DW)] = {
    2074                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ALL_REGS},
    2075                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2076                 :            :                 .imm = { .min = BPF_ATOMIC_ADD, .max = BPF_ATOMIC_XCHG},
    2077                 :            :                 .eval = eval_store,
    2078                 :            :         },
    2079                 :            :         /* store IMM instructions */
    2080                 :            :         [(BPF_ST | BPF_MEM | BPF_B)] = {
    2081                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ZERO_REG},
    2082                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2083                 :            :                 .imm = { .min = 0, .max = UINT32_MAX},
    2084                 :            :                 .eval = eval_store,
    2085                 :            :         },
    2086                 :            :         [(BPF_ST | BPF_MEM | BPF_H)] = {
    2087                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ZERO_REG},
    2088                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2089                 :            :                 .imm = { .min = 0, .max = UINT32_MAX},
    2090                 :            :                 .eval = eval_store,
    2091                 :            :         },
    2092                 :            :         [(BPF_ST | BPF_MEM | BPF_W)] = {
    2093                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ZERO_REG},
    2094                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2095                 :            :                 .imm = { .min = 0, .max = UINT32_MAX},
    2096                 :            :                 .eval = eval_store,
    2097                 :            :         },
    2098                 :            :         [(BPF_ST | BPF_MEM | EBPF_DW)] = {
    2099                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ZERO_REG},
    2100                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2101                 :            :                 .imm = { .min = 0, .max = UINT32_MAX},
    2102                 :            :                 .eval = eval_store,
    2103                 :            :         },
    2104                 :            :         /* jump instruction */
    2105                 :            :         [(BPF_JMP | BPF_JA)] = {
    2106                 :            :                 .mask = { .dreg = ZERO_REG, .sreg = ZERO_REG},
    2107                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2108                 :            :                 .imm = { .min = 0, .max = 0},
    2109                 :            :                 .eval = eval_ja,
    2110                 :            :         },
    2111                 :            :         /* jcc IMM instructions */
    2112                 :            :         [(BPF_JMP | BPF_JEQ | BPF_K)] = {
    2113                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ZERO_REG},
    2114                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2115                 :            :                 .imm = { .min = 0, .max = UINT32_MAX},
    2116                 :            :                 .eval = eval_jcc,
    2117                 :            :         },
    2118                 :            :         [(BPF_JMP | EBPF_JNE | BPF_K)] = {
    2119                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ZERO_REG},
    2120                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2121                 :            :                 .imm = { .min = 0, .max = UINT32_MAX},
    2122                 :            :                 .eval = eval_jcc,
    2123                 :            :         },
    2124                 :            :         [(BPF_JMP | BPF_JGT | BPF_K)] = {
    2125                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ZERO_REG},
    2126                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2127                 :            :                 .imm = { .min = 0, .max = UINT32_MAX},
    2128                 :            :                 .eval = eval_jcc,
    2129                 :            :         },
    2130                 :            :         [(BPF_JMP | EBPF_JLT | BPF_K)] = {
    2131                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ZERO_REG},
    2132                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2133                 :            :                 .imm = { .min = 0, .max = UINT32_MAX},
    2134                 :            :                 .eval = eval_jcc,
    2135                 :            :         },
    2136                 :            :         [(BPF_JMP | BPF_JGE | BPF_K)] = {
    2137                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ZERO_REG},
    2138                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2139                 :            :                 .imm = { .min = 0, .max = UINT32_MAX},
    2140                 :            :                 .eval = eval_jcc,
    2141                 :            :         },
    2142                 :            :         [(BPF_JMP | EBPF_JLE | BPF_K)] = {
    2143                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ZERO_REG},
    2144                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2145                 :            :                 .imm = { .min = 0, .max = UINT32_MAX},
    2146                 :            :                 .eval = eval_jcc,
    2147                 :            :         },
    2148                 :            :         [(BPF_JMP | EBPF_JSGT | BPF_K)] = {
    2149                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ZERO_REG},
    2150                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2151                 :            :                 .imm = { .min = 0, .max = UINT32_MAX},
    2152                 :            :                 .eval = eval_jcc,
    2153                 :            :         },
    2154                 :            :         [(BPF_JMP | EBPF_JSLT | BPF_K)] = {
    2155                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ZERO_REG},
    2156                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2157                 :            :                 .imm = { .min = 0, .max = UINT32_MAX},
    2158                 :            :                 .eval = eval_jcc,
    2159                 :            :         },
    2160                 :            :         [(BPF_JMP | EBPF_JSGE | BPF_K)] = {
    2161                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ZERO_REG},
    2162                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2163                 :            :                 .imm = { .min = 0, .max = UINT32_MAX},
    2164                 :            :                 .eval = eval_jcc,
    2165                 :            :         },
    2166                 :            :         [(BPF_JMP | EBPF_JSLE | BPF_K)] = {
    2167                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ZERO_REG},
    2168                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2169                 :            :                 .imm = { .min = 0, .max = UINT32_MAX},
    2170                 :            :                 .eval = eval_jcc,
    2171                 :            :         },
    2172                 :            :         [(BPF_JMP | BPF_JSET | BPF_K)] = {
    2173                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ZERO_REG},
    2174                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2175                 :            :                 .imm = { .min = 0, .max = UINT32_MAX},
    2176                 :            :                 .eval = eval_jcc,
    2177                 :            :         },
    2178                 :            :         /* jcc REG instructions */
    2179                 :            :         [(BPF_JMP | BPF_JEQ | BPF_X)] = {
    2180                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ALL_REGS},
    2181                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2182                 :            :                 .imm = { .min = 0, .max = 0},
    2183                 :            :                 .eval = eval_jcc,
    2184                 :            :         },
    2185                 :            :         [(BPF_JMP | EBPF_JNE | BPF_X)] = {
    2186                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ALL_REGS},
    2187                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2188                 :            :                 .imm = { .min = 0, .max = 0},
    2189                 :            :                 .eval = eval_jcc,
    2190                 :            :         },
    2191                 :            :         [(BPF_JMP | BPF_JGT | BPF_X)] = {
    2192                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ALL_REGS},
    2193                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2194                 :            :                 .imm = { .min = 0, .max = 0},
    2195                 :            :                 .eval = eval_jcc,
    2196                 :            :         },
    2197                 :            :         [(BPF_JMP | EBPF_JLT | BPF_X)] = {
    2198                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ALL_REGS},
    2199                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2200                 :            :                 .imm = { .min = 0, .max = 0},
    2201                 :            :                 .eval = eval_jcc,
    2202                 :            :         },
    2203                 :            :         [(BPF_JMP | BPF_JGE | BPF_X)] = {
    2204                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ALL_REGS},
    2205                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2206                 :            :                 .imm = { .min = 0, .max = 0},
    2207                 :            :                 .eval = eval_jcc,
    2208                 :            :         },
    2209                 :            :         [(BPF_JMP | EBPF_JLE | BPF_X)] = {
    2210                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ALL_REGS},
    2211                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2212                 :            :                 .imm = { .min = 0, .max = 0},
    2213                 :            :                 .eval = eval_jcc,
    2214                 :            :         },
    2215                 :            :         [(BPF_JMP | EBPF_JSGT | BPF_X)] = {
    2216                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ALL_REGS},
    2217                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2218                 :            :                 .imm = { .min = 0, .max = 0},
    2219                 :            :                 .eval = eval_jcc,
    2220                 :            :         },
    2221                 :            :         [(BPF_JMP | EBPF_JSLT | BPF_X)] = {
    2222                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ALL_REGS},
    2223                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2224                 :            :                 .imm = { .min = 0, .max = 0},
    2225                 :            :                 .eval = eval_jcc,
    2226                 :            :         },
    2227                 :            :         [(BPF_JMP | EBPF_JSGE | BPF_X)] = {
    2228                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ALL_REGS},
    2229                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2230                 :            :                 .imm = { .min = 0, .max = 0},
    2231                 :            :                 .eval = eval_jcc,
    2232                 :            :         },
    2233                 :            :         [(BPF_JMP | EBPF_JSLE | BPF_X)] = {
    2234                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ALL_REGS},
    2235                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2236                 :            :                 .imm = { .min = 0, .max = 0},
    2237                 :            :                 .eval = eval_jcc,
    2238                 :            :         },
    2239                 :            :         [(BPF_JMP | BPF_JSET | BPF_X)] = {
    2240                 :            :                 .mask = { .dreg = ALL_REGS, .sreg = ALL_REGS},
    2241                 :            :                 .off = { .min = 0, .max = UINT16_MAX},
    2242                 :            :                 .imm = { .min = 0, .max = 0},
    2243                 :            :                 .eval = eval_jcc,
    2244                 :            :         },
    2245                 :            :         /* call instruction */
    2246                 :            :         [(BPF_JMP | EBPF_CALL)] = {
    2247                 :            :                 .mask = { .dreg = ZERO_REG, .sreg = ZERO_REG},
    2248                 :            :                 .off = { .min = 0, .max = 0},
    2249                 :            :                 .imm = { .min = 0, .max = UINT32_MAX},
    2250                 :            :                 .eval = eval_call,
    2251                 :            :         },
    2252                 :            :         /* ret instruction */
    2253                 :            :         [(BPF_JMP | EBPF_EXIT)] = {
    2254                 :            :                 .mask = { .dreg = ZERO_REG, .sreg = ZERO_REG},
    2255                 :            :                 .off = { .min = 0, .max = 0},
    2256                 :            :                 .imm = { .min = 0, .max = 0},
    2257                 :            :                 .eval = eval_exit,
    2258                 :            :         },
    2259                 :            : };
    2260                 :            : 
    2261                 :            : /*
    2262                 :            :  * make sure that instruction syntax is valid,
    2263                 :            :  * and its fields don't violate particular instruction type restrictions.
    2264                 :            :  */
    2265                 :            : static const char *
    2266                 :       8540 : check_syntax(const struct rte_ebpf_insn *ins)
    2267                 :            : {
    2268                 :            : 
    2269                 :            :         uint8_t op;
    2270                 :            :         uint16_t off;
    2271                 :            :         uint32_t imm;
    2272                 :            : 
    2273                 :       8540 :         op = ins->code;
    2274                 :            : 
    2275         [ +  - ]:       8540 :         if (ins_chk[op].mask.dreg == 0)
    2276                 :            :                 return "invalid opcode";
    2277                 :            : 
    2278         [ +  - ]:       8540 :         if ((ins_chk[op].mask.dreg & 1 << ins->dst_reg) == 0)
    2279                 :            :                 return "invalid dst-reg field";
    2280                 :            : 
    2281         [ +  - ]:       8540 :         if ((ins_chk[op].mask.sreg & 1 << ins->src_reg) == 0)
    2282                 :            :                 return "invalid src-reg field";
    2283                 :            : 
    2284                 :       8540 :         off = ins->off;
    2285   [ +  -  +  - ]:       8540 :         if (ins_chk[op].off.min > off || ins_chk[op].off.max < off)
    2286                 :            :                 return "invalid off field";
    2287                 :            : 
    2288                 :       8540 :         imm = ins->imm;
    2289   [ +  -  +  + ]:       8540 :         if (ins_chk[op].imm.min > imm || ins_chk[op].imm.max < imm)
    2290                 :            :                 return "invalid imm field";
    2291                 :            : 
    2292         [ +  + ]:       8536 :         if (ins_chk[op].check != NULL)
    2293                 :          8 :                 return ins_chk[op].check(ins);
    2294                 :            : 
    2295                 :            :         return NULL;
    2296                 :            : }
    2297                 :            : 
    2298                 :            : /*
    2299                 :            :  * helper function, return instruction index for the given node.
    2300                 :            :  */
    2301                 :            : static uint32_t
    2302                 :            : get_node_idx(const struct bpf_verifier *bvf, const struct inst_node *node)
    2303                 :            : {
    2304                 :      35589 :         return node - bvf->in;
    2305                 :            : }
    2306                 :            : 
    2307                 :            : /*
    2308                 :            :  * helper function, used to walk through constructed CFG.
    2309                 :            :  */
    2310                 :            : static struct inst_node *
    2311                 :            : get_next_node(struct bpf_verifier *bvf, struct inst_node *node)
    2312                 :            : {
    2313                 :            :         uint32_t ce, ne, dst;
    2314                 :            : 
    2315                 :      39972 :         ne = node->nb_edge;
    2316                 :      39972 :         ce = node->cur_edge;
    2317   [ +  +  +  + ]:      39972 :         if (ce == ne)
    2318                 :            :                 return NULL;
    2319                 :            : 
    2320                 :      20958 :         node->cur_edge++;
    2321                 :      20958 :         dst = node->edge_dest[ce];
    2322                 :      20958 :         return bvf->in + dst;
    2323                 :            : }
    2324                 :            : 
    2325                 :            : static void
    2326                 :            : set_node_colour(struct bpf_verifier *bvf, struct inst_node *node,
    2327                 :            :         uint32_t new)
    2328                 :            : {
    2329                 :            :         uint32_t prev;
    2330                 :            : 
    2331                 :            :         prev = node->colour;
    2332                 :      17046 :         node->colour = new;
    2333                 :            : 
    2334                 :      17046 :         bvf->node_colour[prev]--;
    2335                 :      17046 :         bvf->node_colour[new]++;
    2336                 :       8523 : }
    2337                 :            : 
    2338                 :            : /*
    2339                 :            :  * helper function, add new edge between two nodes.
    2340                 :            :  */
    2341                 :            : static int
    2342                 :      10314 : add_edge(struct bpf_verifier *bvf, struct inst_node *node, uint32_t nidx)
    2343                 :            : {
    2344                 :            :         uint32_t ne;
    2345                 :            : 
    2346         [ +  + ]:      10314 :         if (nidx >= bvf->prm->raw.nb_ins) {
    2347                 :          1 :                 RTE_BPF_LOG_FUNC_LINE(ERR,
    2348                 :            :                         "program boundary violation at pc: %u, next pc: %u",
    2349                 :            :                         get_node_idx(bvf, node), nidx);
    2350                 :          1 :                 return -EINVAL;
    2351                 :            :         }
    2352                 :            : 
    2353                 :      10313 :         ne = node->nb_edge;
    2354         [ -  + ]:      10313 :         if (ne >= RTE_DIM(node->edge_dest)) {
    2355                 :          0 :                 RTE_BPF_LOG_FUNC_LINE(ERR, "internal error at pc: %u",
    2356                 :            :                         get_node_idx(bvf, node));
    2357                 :          0 :                 return -EINVAL;
    2358                 :            :         }
    2359                 :            : 
    2360                 :      10313 :         node->edge_dest[ne] = nidx;
    2361                 :      10313 :         node->nb_edge = ne + 1;
    2362                 :      10313 :         return 0;
    2363                 :            : }
    2364                 :            : 
    2365                 :            : /*
    2366                 :            :  * helper function, determine type of edge between two nodes.
    2367                 :            :  */
    2368                 :            : static void
    2369                 :            : set_edge_type(struct bpf_verifier *bvf, struct inst_node *node,
    2370                 :            :         const struct inst_node *next)
    2371                 :            : {
    2372                 :            :         uint32_t ce, clr, type;
    2373                 :            : 
    2374                 :      10301 :         ce = node->cur_edge - 1;
    2375                 :      10301 :         clr = next->colour;
    2376                 :            : 
    2377                 :            :         type = UNKNOWN_EDGE;
    2378                 :            : 
    2379         [ +  + ]:      10301 :         if (clr == WHITE)
    2380                 :            :                 type = TREE_EDGE;
    2381         [ +  - ]:       2616 :         else if (clr == GREY)
    2382                 :            :                 type = BACK_EDGE;
    2383         [ +  - ]:       2616 :         else if (clr == BLACK)
    2384                 :            :                 /*
    2385                 :            :                  * in fact it could be either direct or cross edge,
    2386                 :            :                  * but for now, we don't need to distinguish between them.
    2387                 :            :                  */
    2388                 :            :                 type = CROSS_EDGE;
    2389                 :            : 
    2390                 :      10301 :         node->edge_type[ce] = type;
    2391                 :      10301 :         bvf->edge_type[type]++;
    2392                 :            : }
    2393                 :            : 
    2394                 :            : /*
    2395                 :            :  * Depth-First Search (DFS) through previously constructed
    2396                 :            :  * Control Flow Graph (CFG).
    2397                 :            :  * Information collected at this path would be used later
    2398                 :            :  * to determine is there any loops, and/or statically unreachable instructions.
    2399                 :            :  * PREREQUISITE: there is at least one node.
    2400                 :            :  */
    2401                 :            : static void
    2402                 :        838 : dfs(struct bpf_verifier *bvf)
    2403                 :            : {
    2404                 :            :         struct inst_node *next, *node;
    2405                 :            : 
    2406                 :            :         RTE_ASSERT(bvf->nb_nodes != 0);
    2407                 :            :         /*
    2408                 :            :          * Since there is at least one node, node with index 0 always exists;
    2409                 :            :          * it is our program entry point.
    2410                 :            :          */
    2411                 :        838 :         node = &bvf->in[0];
    2412                 :            :         do {
    2413                 :            : 
    2414         [ +  + ]:      16208 :                 if (node->colour == WHITE)
    2415                 :            :                         set_node_colour(bvf, node, GREY);
    2416                 :            : 
    2417         [ +  - ]:      16208 :                 if (node->colour == GREY) {
    2418                 :            : 
    2419                 :            :                         /* find next unprocessed child node */
    2420                 :            :                         do {
    2421                 :            :                                 next = get_next_node(bvf, node);
    2422         [ +  - ]:      10301 :                                 if (next == NULL)
    2423                 :            :                                         break;
    2424                 :            :                                 set_edge_type(bvf, node, next);
    2425         [ +  + ]:      10301 :                         } while (next->colour != WHITE);
    2426                 :            : 
    2427         [ +  + ]:      16208 :                         if (next != NULL) {
    2428                 :            :                                 /* proceed with next child */
    2429                 :       7685 :                                 next->prev_node = node;
    2430                 :            :                                 node = next;
    2431                 :            :                         } else {
    2432                 :            :                                 /*
    2433                 :            :                                  * finished with current node and all it's kids,
    2434                 :            :                                  * proceed with parent
    2435                 :            :                                  */
    2436                 :            :                                 set_node_colour(bvf, node, BLACK);
    2437                 :       8523 :                                 node->cur_edge = 0;
    2438                 :       8523 :                                 node = node->prev_node;
    2439                 :            :                         }
    2440                 :            :                 } else
    2441                 :            :                         node = NULL;
    2442         [ +  + ]:      16208 :         } while (node != NULL);
    2443                 :        838 : }
    2444                 :            : 
    2445                 :            : /*
    2446                 :            :  * report statically unreachable instructions.
    2447                 :            :  */
    2448                 :            : static void
    2449                 :          0 : log_unreachable(const struct bpf_verifier *bvf)
    2450                 :            : {
    2451                 :            :         uint32_t i;
    2452                 :            :         struct inst_node *node;
    2453                 :            :         const struct rte_ebpf_insn *ins;
    2454                 :            : 
    2455         [ #  # ]:          0 :         for (i = 0; i != bvf->prm->raw.nb_ins; i++) {
    2456                 :            : 
    2457                 :          0 :                 node = bvf->in + i;
    2458                 :          0 :                 ins = bvf->prm->raw.ins + i;
    2459                 :            : 
    2460         [ #  # ]:          0 :                 if (node->colour == WHITE &&
    2461         [ #  # ]:          0 :                                 ins->code != (BPF_LD | BPF_IMM | EBPF_DW))
    2462                 :          0 :                         RTE_BPF_LOG_LINE(ERR, "unreachable code at pc: %u;", i);
    2463                 :            :         }
    2464                 :          0 : }
    2465                 :            : 
    2466                 :            : /*
    2467                 :            :  * report loops detected.
    2468                 :            :  */
    2469                 :            : static void
    2470                 :          0 : log_loop(const struct bpf_verifier *bvf)
    2471                 :            : {
    2472                 :            :         uint32_t i, j;
    2473                 :            :         struct inst_node *node;
    2474                 :            : 
    2475         [ #  # ]:          0 :         for (i = 0; i != bvf->prm->raw.nb_ins; i++) {
    2476                 :            : 
    2477                 :          0 :                 node = bvf->in + i;
    2478         [ #  # ]:          0 :                 if (node->colour != BLACK)
    2479                 :          0 :                         continue;
    2480                 :            : 
    2481         [ #  # ]:          0 :                 for (j = 0; j != node->nb_edge; j++) {
    2482         [ #  # ]:          0 :                         if (node->edge_type[j] == BACK_EDGE)
    2483                 :          0 :                                 RTE_BPF_LOG_LINE(ERR,
    2484                 :            :                                         "loop at pc:%u --> pc:%u;",
    2485                 :            :                                         i, node->edge_dest[j]);
    2486                 :            :                 }
    2487                 :            :         }
    2488                 :          0 : }
    2489                 :            : 
    2490                 :            : /*
    2491                 :            :  * First pass goes though all instructions in the set, checks that each
    2492                 :            :  * instruction is a valid one (correct syntax, valid field values, etc.)
    2493                 :            :  * and constructs control flow graph (CFG).
    2494                 :            :  * Then depth-first search is performed over the constructed graph.
    2495                 :            :  * Programs with unreachable instructions and/or loops will be rejected.
    2496                 :            :  */
    2497                 :            : static int
    2498                 :        843 : validate(struct bpf_verifier *bvf)
    2499                 :            : {
    2500                 :            :         int32_t rc;
    2501                 :            :         uint32_t i;
    2502                 :            :         struct inst_node *node;
    2503                 :            :         const struct rte_ebpf_insn *ins;
    2504                 :            :         const char *err;
    2505                 :            : 
    2506                 :            :         rc = 0;
    2507         [ +  + ]:       9383 :         for (i = 0; i < bvf->prm->raw.nb_ins; i++) {
    2508                 :            : 
    2509                 :       8540 :                 ins = bvf->prm->raw.ins + i;
    2510                 :       8540 :                 node = bvf->in + i;
    2511                 :            : 
    2512                 :       8540 :                 err = check_syntax(ins);
    2513         [ +  + ]:       8540 :                 if (err != 0) {
    2514                 :          4 :                         RTE_BPF_LOG_FUNC_LINE(ERR, "%s at pc: %u", err, i);
    2515                 :          4 :                         rc |= -EINVAL;
    2516                 :            :                 }
    2517                 :            : 
    2518                 :            :                 /*
    2519                 :            :                  * construct CFG, jcc nodes have to outgoing edges,
    2520                 :            :                  * 'exit' nodes - none, all other nodes have exactly one
    2521                 :            :                  * outgoing edge.
    2522                 :            :                  */
    2523   [ +  +  +  +  :       8540 :                 switch (ins->code) {
                   +  + ]
    2524                 :            :                 case (BPF_JMP | EBPF_EXIT):
    2525                 :            :                         break;
    2526                 :       2997 :                 case (BPF_JMP | BPF_JEQ | BPF_K):
    2527                 :            :                 case (BPF_JMP | EBPF_JNE | BPF_K):
    2528                 :            :                 case (BPF_JMP | BPF_JGT | BPF_K):
    2529                 :            :                 case (BPF_JMP | EBPF_JLT | BPF_K):
    2530                 :            :                 case (BPF_JMP | BPF_JGE | BPF_K):
    2531                 :            :                 case (BPF_JMP | EBPF_JLE | BPF_K):
    2532                 :            :                 case (BPF_JMP | EBPF_JSGT | BPF_K):
    2533                 :            :                 case (BPF_JMP | EBPF_JSLT | BPF_K):
    2534                 :            :                 case (BPF_JMP | EBPF_JSGE | BPF_K):
    2535                 :            :                 case (BPF_JMP | EBPF_JSLE | BPF_K):
    2536                 :            :                 case (BPF_JMP | BPF_JSET | BPF_K):
    2537                 :            :                 case (BPF_JMP | BPF_JEQ | BPF_X):
    2538                 :            :                 case (BPF_JMP | EBPF_JNE | BPF_X):
    2539                 :            :                 case (BPF_JMP | BPF_JGT | BPF_X):
    2540                 :            :                 case (BPF_JMP | EBPF_JLT | BPF_X):
    2541                 :            :                 case (BPF_JMP | BPF_JGE | BPF_X):
    2542                 :            :                 case (BPF_JMP | EBPF_JLE | BPF_X):
    2543                 :            :                 case (BPF_JMP | EBPF_JSGT | BPF_X):
    2544                 :            :                 case (BPF_JMP | EBPF_JSLT | BPF_X):
    2545                 :            :                 case (BPF_JMP | EBPF_JSGE | BPF_X):
    2546                 :            :                 case (BPF_JMP | EBPF_JSLE | BPF_X):
    2547                 :            :                 case (BPF_JMP | BPF_JSET | BPF_X):
    2548                 :       2997 :                         rc |= add_edge(bvf, node, i + ins->off + 1);
    2549                 :       2997 :                         rc |= add_edge(bvf, node, i + 1);
    2550                 :       2997 :                         bvf->nb_jcc_nodes++;
    2551                 :       2997 :                         break;
    2552                 :         48 :                 case (BPF_JMP | BPF_JA):
    2553                 :         48 :                         rc |= add_edge(bvf, node, i + ins->off + 1);
    2554                 :         48 :                         break;
    2555                 :            :                 /* load 64 bit immediate value */
    2556                 :        369 :                 case (BPF_LD | BPF_IMM | EBPF_DW):
    2557                 :        369 :                         rc |= add_edge(bvf, node, i + 2);
    2558                 :        369 :                         i++;
    2559                 :        369 :                         break;
    2560                 :        470 :                 case (BPF_LD | BPF_ABS | BPF_B):
    2561                 :            :                 case (BPF_LD | BPF_ABS | BPF_H):
    2562                 :            :                 case (BPF_LD | BPF_ABS | BPF_W):
    2563                 :            :                 case (BPF_LD | BPF_IND | BPF_B):
    2564                 :            :                 case (BPF_LD | BPF_IND | BPF_H):
    2565                 :            :                 case (BPF_LD | BPF_IND | BPF_W):
    2566                 :        470 :                         bvf->nb_ldmb_nodes++;
    2567                 :            :                         /* fallthrough */
    2568                 :       3903 :                 default:
    2569                 :       3903 :                         rc |= add_edge(bvf, node, i + 1);
    2570                 :       3903 :                         break;
    2571                 :            :                 }
    2572                 :            : 
    2573                 :       8540 :                 bvf->nb_nodes++;
    2574                 :       8540 :                 bvf->node_colour[WHITE]++;
    2575                 :            :         }
    2576                 :            : 
    2577         [ +  + ]:        843 :         if (rc != 0)
    2578                 :            :                 return rc;
    2579                 :            : 
    2580         [ -  + ]:        838 :         if (bvf->nb_nodes == 0) {
    2581                 :          0 :                 RTE_BPF_LOG_LINE(ERR, "%s(%p) the program is empty",
    2582                 :            :                         __func__, bvf);
    2583                 :          0 :                 return -EINVAL;
    2584                 :            :         }
    2585                 :            : 
    2586                 :        838 :         dfs(bvf);
    2587                 :            : 
    2588                 :        838 :         RTE_LOG(DEBUG, BPF, "%s(%p) stats:\n"
    2589                 :            :                 "nb_nodes=%u;\n"
    2590                 :            :                 "nb_jcc_nodes=%u;\n"
    2591                 :            :                 "node_color={[WHITE]=%u, [GREY]=%u,, [BLACK]=%u};\n"
    2592                 :            :                 "edge_type={[UNKNOWN]=%u, [TREE]=%u, [BACK]=%u, [CROSS]=%u};\n",
    2593                 :            :                 __func__, bvf,
    2594                 :            :                 bvf->nb_nodes,
    2595                 :            :                 bvf->nb_jcc_nodes,
    2596                 :            :                 bvf->node_colour[WHITE], bvf->node_colour[GREY],
    2597                 :            :                         bvf->node_colour[BLACK],
    2598                 :            :                 bvf->edge_type[UNKNOWN_EDGE], bvf->edge_type[TREE_EDGE],
    2599                 :            :                 bvf->edge_type[BACK_EDGE], bvf->edge_type[CROSS_EDGE]);
    2600                 :            : 
    2601         [ -  + ]:        838 :         if (bvf->node_colour[BLACK] != bvf->nb_nodes) {
    2602                 :          0 :                 RTE_BPF_LOG_LINE(ERR, "%s(%p) unreachable instructions;",
    2603                 :            :                         __func__, bvf);
    2604                 :          0 :                 log_unreachable(bvf);
    2605                 :          0 :                 return -EINVAL;
    2606                 :            :         }
    2607                 :            : 
    2608   [ +  -  +  - ]:        838 :         if (bvf->node_colour[GREY] != 0 || bvf->node_colour[WHITE] != 0 ||
    2609         [ -  + ]:        838 :                         bvf->edge_type[UNKNOWN_EDGE] != 0) {
    2610                 :          0 :                 RTE_BPF_LOG_LINE(ERR, "%s(%p) DFS internal error;",
    2611                 :            :                         __func__, bvf);
    2612                 :          0 :                 return -EINVAL;
    2613                 :            :         }
    2614                 :            : 
    2615         [ -  + ]:        838 :         if (bvf->edge_type[BACK_EDGE] != 0) {
    2616                 :          0 :                 RTE_BPF_LOG_LINE(ERR, "%s(%p) loops detected;",
    2617                 :            :                         __func__, bvf);
    2618                 :          0 :                 log_loop(bvf);
    2619                 :          0 :                 return -EINVAL;
    2620                 :            :         }
    2621                 :            : 
    2622                 :            :         return 0;
    2623                 :            : }
    2624                 :            : 
    2625                 :            : /*
    2626                 :            :  * helper functions get/free eval states.
    2627                 :            :  */
    2628                 :            : static struct bpf_eval_state *
    2629                 :            : pull_eval_state(struct evst_pool *pool)
    2630                 :            : {
    2631                 :            :         uint32_t n;
    2632                 :            : 
    2633                 :       9089 :         n = pool->cur;
    2634         [ +  + ]:       5876 :         if (n == pool->num)
    2635                 :            :                 return NULL;
    2636                 :            : 
    2637                 :       9621 :         pool->cur = n + 1;
    2638                 :       5570 :         return pool->ent + n;
    2639                 :            : }
    2640                 :            : 
    2641                 :            : static void
    2642                 :            : push_eval_state(struct evst_pool *pool)
    2643                 :            : {
    2644                 :            :         RTE_ASSERT(pool->cur != 0);
    2645                 :       3213 :         pool->cur--;
    2646                 :            : }
    2647                 :            : 
    2648                 :            : static void
    2649                 :        838 : evst_pool_fini(struct bpf_verifier *bvf)
    2650                 :            : {
    2651                 :        838 :         bvf->evst = NULL;
    2652                 :        838 :         free(bvf->evst_sr_pool.ent);
    2653                 :        838 :         memset(&bvf->evst_sr_pool, 0, sizeof(bvf->evst_sr_pool));
    2654                 :        838 :         memset(&bvf->evst_tp_pool, 0, sizeof(bvf->evst_tp_pool));
    2655                 :        838 : }
    2656                 :            : 
    2657                 :            : static int
    2658                 :        838 : evst_pool_init(struct bpf_verifier *bvf)
    2659                 :            : {
    2660                 :            :         uint32_t k, n;
    2661                 :            : 
    2662         [ +  - ]:        838 :         if (bvf->nb_jcc_nodes > UINT32_MAX / 4)
    2663                 :            :                 /* Calculations that follow may overflow. */
    2664                 :            :                 return -E2BIG;
    2665                 :            : 
    2666                 :            :         /*
    2667                 :            :          * We need nb_jcc_nodes + 1 for save_cur/restore_cur
    2668                 :            :          * remaining ones will be used for state tracking/pruning.
    2669                 :            :          */
    2670                 :        838 :         k = bvf->nb_jcc_nodes + 1;
    2671                 :        838 :         n = k * 3;
    2672                 :            : 
    2673                 :        838 :         bvf->evst_sr_pool.ent = calloc(n, sizeof(bvf->evst_sr_pool.ent[0]));
    2674         [ +  - ]:        838 :         if (bvf->evst_sr_pool.ent == NULL)
    2675                 :            :                 return -ENOMEM;
    2676                 :            : 
    2677                 :        838 :         bvf->evst_sr_pool.num = k;
    2678                 :            :         bvf->evst_sr_pool.cur = 0;
    2679                 :            : 
    2680                 :        838 :         bvf->evst_tp_pool.ent = bvf->evst_sr_pool.ent + k;
    2681                 :        838 :         bvf->evst_tp_pool.num = n - k;
    2682                 :        838 :         bvf->evst_tp_pool.cur = 0;
    2683                 :            : 
    2684                 :        838 :         bvf->evst = pull_eval_state(&bvf->evst_sr_pool);
    2685                 :        838 :         return 0;
    2686                 :            : }
    2687                 :            : 
    2688                 :            : /*
    2689                 :            :  * try to allocate and initialise new eval state for given node.
    2690                 :            :  * later if no errors will be encountered, this state will be accepted as
    2691                 :            :  * one of the possible 'safe' states for that node.
    2692                 :            :  */
    2693                 :            : static void
    2694                 :       5876 : save_start_eval_state(struct bpf_verifier *bvf, struct inst_node *node)
    2695                 :            : {
    2696                 :            :         RTE_ASSERT(node->evst.start == NULL);
    2697                 :            : 
    2698                 :            :         /* limit number of states for one node with some reasonable value */
    2699         [ +  - ]:       5876 :         if (node->evst.nb_safe >= NODE_EVST_MAX)
    2700                 :            :                 return;
    2701                 :            : 
    2702                 :            :         /* try to get new eval_state */
    2703                 :       5876 :         node->evst.start = pull_eval_state(&bvf->evst_tp_pool);
    2704                 :            : 
    2705                 :            :         /* make a copy of current state */
    2706         [ +  + ]:       5876 :         if (node->evst.start != NULL) {
    2707                 :       5570 :                 memcpy(node->evst.start, bvf->evst, sizeof(*node->evst.start));
    2708                 :       5570 :                 SLIST_NEXT(node->evst.start, next) = NULL;
    2709                 :            :         }
    2710                 :            : }
    2711                 :            : 
    2712                 :            : /*
    2713                 :            :  * add @start state to the list of @safe states.
    2714                 :            :  */
    2715                 :            : static void
    2716                 :      10491 : save_safe_eval_state(struct bpf_verifier *bvf, struct inst_node *node)
    2717                 :            : {
    2718         [ +  + ]:      10491 :         if (node->evst.start == NULL)
    2719                 :            :                 return;
    2720                 :            : 
    2721                 :       5568 :         SLIST_INSERT_HEAD(&node->evst.safe, node->evst.start, next);
    2722                 :       5568 :         node->evst.nb_safe++;
    2723                 :            : 
    2724                 :       5568 :         RTE_BPF_LOG_LINE(DEBUG, "%s(bvf=%p,node=%u,state=%p): nb_safe=%u;",
    2725                 :            :                 __func__, bvf, get_node_idx(bvf, node), node->evst.start,
    2726                 :            :                 node->evst.nb_safe);
    2727                 :            : 
    2728                 :       5568 :         node->evst.start = NULL;
    2729                 :            : }
    2730                 :            : 
    2731                 :            : /*
    2732                 :            :  * Save current eval state.
    2733                 :            :  */
    2734                 :            : static int
    2735         [ +  - ]:       3213 : save_cur_eval_state(struct bpf_verifier *bvf, struct inst_node *node)
    2736                 :            : {
    2737                 :            :         struct bpf_eval_state *st;
    2738                 :            : 
    2739                 :            :         /* get new eval_state for this node */
    2740                 :            :         st = pull_eval_state(&bvf->evst_sr_pool);
    2741         [ -  + ]:       3213 :         if (st == NULL) {
    2742                 :          0 :                 RTE_BPF_LOG_FUNC_LINE(ERR,
    2743                 :            :                         "internal error (out of space) at pc: %u",
    2744                 :            :                         get_node_idx(bvf, node));
    2745                 :          0 :                 return -ENOMEM;
    2746                 :            :         }
    2747                 :            : 
    2748                 :            :         /* make a copy of current state */
    2749                 :       3213 :         memcpy(st, bvf->evst, sizeof(*st));
    2750                 :            : 
    2751                 :            :         /* swap current state with new one */
    2752                 :            :         RTE_ASSERT(node->evst.cur == NULL);
    2753                 :       3213 :         node->evst.cur = bvf->evst;
    2754                 :       3213 :         bvf->evst = st;
    2755                 :            : 
    2756                 :       3213 :         RTE_BPF_LOG_LINE(DEBUG, "%s(bvf=%p,node=%u) old/new states: %p/%p;",
    2757                 :            :                 __func__, bvf, get_node_idx(bvf, node), node->evst.cur,
    2758                 :            :                 bvf->evst);
    2759                 :            : 
    2760                 :       3213 :         return 0;
    2761                 :            : }
    2762                 :            : 
    2763                 :            : /*
    2764                 :            :  * Restore previous eval state and mark current eval state as free.
    2765                 :            :  */
    2766                 :            : static void
    2767                 :       3213 : restore_cur_eval_state(struct bpf_verifier *bvf, struct inst_node *node)
    2768                 :            : {
    2769                 :       3213 :         RTE_BPF_LOG_LINE(DEBUG, "%s(bvf=%p,node=%u) old/new states: %p/%p;",
    2770                 :            :                 __func__, bvf, get_node_idx(bvf, node), bvf->evst,
    2771                 :            :                 node->evst.cur);
    2772                 :            : 
    2773                 :       3213 :         bvf->evst = node->evst.cur;
    2774                 :       3213 :         node->evst.cur = NULL;
    2775                 :            :         push_eval_state(&bvf->evst_sr_pool);
    2776                 :       3213 : }
    2777                 :            : 
    2778                 :            : static void
    2779                 :      10718 : log_dbg_eval_state(const struct bpf_verifier *bvf, const struct rte_ebpf_insn *ins,
    2780                 :            :         uint32_t pc)
    2781                 :            : {
    2782                 :            :         const struct bpf_eval_state *st;
    2783                 :            :         const struct bpf_reg_val *rv;
    2784                 :            : 
    2785                 :      10718 :         RTE_BPF_LOG_LINE(DEBUG, "%s(pc=%u):", __func__, pc);
    2786                 :            : 
    2787                 :      10718 :         st = bvf->evst;
    2788                 :      10718 :         rv = st->rv + ins->dst_reg;
    2789                 :            : 
    2790                 :      10718 :         RTE_LOG(DEBUG, BPF,
    2791                 :            :                 "r%u={\n"
    2792                 :            :                 "\tv={type=%u, size=%zu, buf_size=%zu},\n"
    2793                 :            :                 "\tmask=0x%" PRIx64 ",\n"
    2794                 :            :                 "\tu={min=0x%" PRIx64 ", max=0x%" PRIx64 "},\n"
    2795                 :            :                 "\ts={min=%" PRId64 ", max=%" PRId64 "},\n"
    2796                 :            :                 "};\n",
    2797                 :            :                 ins->dst_reg,
    2798                 :            :                 rv->v.type, rv->v.size, rv->v.buf_size,
    2799                 :            :                 rv->mask,
    2800                 :            :                 rv->u.min, rv->u.max,
    2801                 :            :                 rv->s.min, rv->s.max);
    2802                 :      10718 : }
    2803                 :            : 
    2804                 :            : /*
    2805                 :            :  * compare two evaluation states.
    2806                 :            :  * returns zero if @lv is more conservative (safer) then @rv.
    2807                 :            :  * returns non-zero value otherwise.
    2808                 :            :  */
    2809                 :            : static int
    2810                 :       7175 : cmp_reg_val_within(const struct bpf_reg_val *lv, const struct bpf_reg_val *rv)
    2811                 :            : {
    2812                 :            :         /* expect @v and @mask to be identical */
    2813   [ +  +  +  + ]:       7175 :         if (memcmp(&lv->v, &rv->v, sizeof(lv->v)) != 0 || lv->mask != rv->mask)
    2814                 :            :                 return -1;
    2815                 :            : 
    2816                 :            :         /* exact match only for mbuf and stack pointers */
    2817         [ +  - ]:       6857 :         if (lv->v.type == RTE_BPF_ARG_PTR_MBUF ||
    2818                 :            :                         lv->v.type == BPF_ARG_PTR_STACK)
    2819                 :            :                 return -1;
    2820                 :            : 
    2821   [ +  +  +  + ]:       6857 :         if (lv->u.min <= rv->u.min && lv->u.max >= rv->u.max &&
    2822   [ +  +  +  + ]:       2095 :                         lv->s.min <= rv->s.min && lv->s.max >= rv->s.max)
    2823                 :         28 :                 return 0;
    2824                 :            : 
    2825                 :            :         return -1;
    2826                 :            : }
    2827                 :            : 
    2828                 :            : /*
    2829                 :            :  * compare two evaluation states.
    2830                 :            :  * returns zero if they are identical.
    2831                 :            :  * returns positive value if @lv is more conservative (safer) then @rv.
    2832                 :            :  * returns negative value otherwise.
    2833                 :            :  */
    2834                 :            : static int
    2835                 :       7496 : cmp_eval_state(const struct bpf_eval_state *lv, const struct bpf_eval_state *rv)
    2836                 :            : {
    2837                 :            :         int32_t rc;
    2838                 :            :         uint32_t i, k;
    2839                 :            : 
    2840                 :            :         /* for stack expect identical values */
    2841                 :       7496 :         rc = memcmp(lv->sv, rv->sv, sizeof(lv->sv));
    2842         [ +  - ]:       7496 :         if (rc != 0)
    2843                 :            :                 return -(2 * EBPF_REG_NUM);
    2844                 :            : 
    2845                 :            :         k = 0;
    2846                 :            :         /* check register values */
    2847         [ +  + ]:      25415 :         for (i = 0; i != RTE_DIM(lv->rv); i++) {
    2848                 :      25066 :                 rc = memcmp(&lv->rv[i], &rv->rv[i], sizeof(lv->rv[i]));
    2849   [ +  +  +  + ]:      25066 :                 if (rc != 0 && cmp_reg_val_within(&lv->rv[i], &rv->rv[i]) != 0)
    2850                 :       7147 :                         return -(i + 1);
    2851                 :      17919 :                 k += (rc != 0);
    2852                 :            :         }
    2853                 :            : 
    2854                 :        349 :         return k;
    2855                 :            : }
    2856                 :            : 
    2857                 :            : /*
    2858                 :            :  * check did we already evaluated that path and can it be pruned that time.
    2859                 :            :  */
    2860                 :            : static int
    2861                 :       6225 : prune_eval_state(struct bpf_verifier *bvf, const struct inst_node *node,
    2862                 :            :         struct inst_node *next)
    2863                 :            : {
    2864                 :            :         int32_t rc;
    2865                 :            :         struct bpf_eval_state *safe;
    2866                 :            : 
    2867                 :            :         rc = INT32_MIN;
    2868         [ +  + ]:      13372 :         SLIST_FOREACH(safe, &next->evst.safe, next) {
    2869                 :       7496 :                 rc = cmp_eval_state(safe, bvf->evst);
    2870         [ +  + ]:       7496 :                 if (rc >= 0)
    2871                 :            :                         break;
    2872                 :            :         }
    2873                 :            : 
    2874         [ +  + ]:       6225 :         rc = (rc >= 0) ? 0 : -1;
    2875                 :            : 
    2876                 :            :         /*
    2877                 :            :          * current state doesn't match any safe states,
    2878                 :            :          * so no prunning is possible right now,
    2879                 :            :          * track current state for future references.
    2880                 :            :          */
    2881                 :            :         if (rc != 0)
    2882                 :       5876 :                 save_start_eval_state(bvf, next);
    2883                 :            : 
    2884                 :       6225 :         RTE_BPF_LOG_LINE(DEBUG, "%s(bvf=%p,node=%u,next=%u) returns %d, "
    2885                 :            :                 "next->evst.start=%p, next->evst.nb_safe=%u",
    2886                 :            :                 __func__, bvf, get_node_idx(bvf, node),
    2887                 :            :                 get_node_idx(bvf, next), rc,
    2888                 :            :                 next->evst.start, next->evst.nb_safe);
    2889                 :       6225 :         return rc;
    2890                 :            : }
    2891                 :            : 
    2892                 :            : static bool
    2893                 :            : is_branch_start(const struct inst_node *node)
    2894                 :            : {
    2895   [ +  +  +  +  :      21436 :         return node->prev_node != NULL && node->prev_node->nb_edge > 1;
             +  +  +  + ]
    2896                 :            : }
    2897                 :            : 
    2898                 :            : static uint64_t
    2899                 :            : step_events(const struct inst_node *node, uint8_t op)
    2900                 :            : {
    2901                 :            :         uint64_t events = RTE_BIT64(RTE_BPF_VALIDATE_DEBUG_EVENT_STEP);
    2902                 :            : 
    2903         [ +  + ]:      10945 :         if (is_branch_start(node))
    2904                 :            :                 events |= RTE_BIT64(RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_ENTER);
    2905                 :            : 
    2906         [ +  + ]:      10945 :         if (op == (BPF_JMP | BPF_JA))
    2907                 :        199 :                 events |= RTE_BIT64(RTE_BPF_VALIDATE_DEBUG_EVENT_JUMP_ALWAYS);
    2908                 :            : 
    2909         [ +  + ]:      10945 :         if (node->nb_edge > 1)
    2910                 :       3213 :                 events |= RTE_BIT64(RTE_BPF_VALIDATE_DEBUG_EVENT_JUMP_CONDITIONAL);
    2911                 :            : 
    2912                 :            :         return events;
    2913                 :            : }
    2914                 :            : 
    2915                 :            : /* Do second pass through CFG and try to evaluate instructions
    2916                 :            :  * via each possible path. The verifier will try all paths, tracking types of
    2917                 :            :  * registers used as input to instructions, and updating resulting type via
    2918                 :            :  * register state values. Plus for each register and possible stack value it
    2919                 :            :  * tries to estimate possible max/min value.
    2920                 :            :  * For conditional jumps, a stack is used to save evaluation state, so one
    2921                 :            :  * path is explored while the state for the other path is pushed onto the stack.
    2922                 :            :  * Then later, we backtrack to the first pushed instruction and repeat the cycle
    2923                 :            :  * until the stack is empty and we're done.
    2924                 :            :  * For program with many conditional branches walking through all possible path
    2925                 :            :  * could be very excessive. So to minimize number of evaluations we use
    2926                 :            :  * heuristic similar to what Linux kernel does - state pruning:
    2927                 :            :  * If from given instruction for given program state we explore all possible
    2928                 :            :  * paths and for each of them reach _exit() without any complaints and a valid
    2929                 :            :  * R0 value, then for that instruction, that program state can be marked as
    2930                 :            :  * 'safe'. When we later arrive at the same instruction with a state
    2931                 :            :  * equivalent to an earlier instruction's 'safe' state, we can prune the search.
    2932                 :            :  * For now, only states for JCC  targets are saved/examined.
    2933                 :            :  */
    2934                 :            : static int
    2935                 :        838 : evaluate(struct bpf_verifier *bvf)
    2936                 :            : {
    2937                 :            :         uint32_t idx, op;
    2938                 :            :         const char *err;
    2939                 :            :         const struct rte_ebpf_insn *ins;
    2940                 :            :         struct inst_node *next, *node;
    2941                 :            :         int rc, debug_rc;
    2942                 :        838 :         struct rte_bpf_validate_debug *const debug = bvf->prm->debug;
    2943                 :            : 
    2944                 :            :         struct {
    2945                 :            :                 uint32_t nb_eval;
    2946                 :            :                 uint32_t nb_prune;
    2947                 :            :                 uint32_t nb_save;
    2948                 :            :                 uint32_t nb_restore;
    2949                 :            :         } stats;
    2950                 :            : 
    2951                 :            :         /* initial state of frame pointer */
    2952                 :            :         static const struct bpf_reg_val rvfp = {
    2953                 :            :                 .v = {
    2954                 :            :                         .type = BPF_ARG_PTR_STACK,
    2955                 :            :                         .size = MAX_BPF_STACK_SIZE,
    2956                 :            :                 },
    2957                 :            :                 .mask = UINT64_MAX,
    2958                 :            :                 .u = {.min = MAX_BPF_STACK_SIZE, .max = MAX_BPF_STACK_SIZE},
    2959                 :            :                 .s = {.min = MAX_BPF_STACK_SIZE, .max = MAX_BPF_STACK_SIZE},
    2960                 :            :         };
    2961                 :            : 
    2962         [ +  + ]:       1677 :         for (uint32_t pai = 0; pai != bvf->prm->nb_prog_arg; ++pai) {
    2963                 :        839 :                 struct bpf_reg_val *reg = &bvf->evst->rv[EBPF_REG_1 + pai];
    2964                 :            : 
    2965                 :        839 :                 reg->v = bvf->prm->prog_arg[pai];
    2966                 :        839 :                 reg->mask = UINT64_MAX;
    2967         [ +  + ]:        839 :                 if (reg->v.type == RTE_BPF_ARG_RAW)
    2968                 :            :                         eval_max_bound(reg, UINT64_MAX);
    2969                 :            :         }
    2970                 :            : 
    2971                 :        838 :         bvf->evst->rv[EBPF_REG_10] = rvfp;
    2972                 :            : 
    2973                 :        838 :         ins = bvf->prm->raw.ins;
    2974                 :        838 :         node = bvf->in;
    2975                 :            :         next = node;
    2976                 :            : 
    2977                 :            :         memset(&stats, 0, sizeof(stats));
    2978                 :            : 
    2979                 :        838 :         rc = __rte_bpf_validate_debug_evaluate_start(debug, bvf, bvf->prm);
    2980         [ +  - ]:        838 :         if (rc < 0)
    2981                 :            :                 return rc;
    2982                 :            : 
    2983         [ +  + ]:      21986 :         while (node != NULL) {
    2984                 :            :                 /*
    2985                 :            :                  * current node evaluation, make sure we evaluate
    2986                 :            :                  * each node only once.
    2987                 :            :                  */
    2988         [ +  + ]:      21375 :                 if (next != NULL) {
    2989                 :            :                         /* just started or stepped down the tree, node == next */
    2990                 :            : 
    2991                 :      10945 :                         bvf->evin = node;
    2992                 :            :                         idx = get_node_idx(bvf, node);
    2993                 :      10945 :                         op = ins[idx].code;
    2994                 :            : 
    2995                 :            :                         /* for jcc node make a copy of evaluation state */
    2996         [ +  + ]:      10945 :                         if (node->nb_edge > 1) {
    2997                 :       3213 :                                 rc = save_cur_eval_state(bvf, node);
    2998         [ +  - ]:       3213 :                                 if (rc < 0)
    2999                 :            :                                         break;
    3000                 :       3213 :                                 stats.nb_save++;
    3001                 :            :                         }
    3002                 :            : 
    3003         [ -  + ]:      10945 :                         if (ins_chk[op].eval == NULL) {
    3004                 :          0 :                                 RTE_BPF_LOG_FUNC_LINE(ERR,
    3005                 :            :                                         "Unrecognized instruction at pc: %u", idx);
    3006                 :            :                                 rc = -EINVAL;
    3007                 :          0 :                                 break;
    3008                 :            :                         }
    3009                 :            : 
    3010                 :      10945 :                         rc = __rte_bpf_validate_debug_evaluate_update(debug, idx,
    3011                 :            :                                 step_events(node, op));
    3012         [ +  - ]:      10945 :                         if (rc < 0)
    3013                 :            :                                 break;
    3014                 :            : 
    3015                 :      10945 :                         err = ins_chk[op].eval(bvf, ins + idx);
    3016                 :      10945 :                         stats.nb_eval++;
    3017         [ +  + ]:      10945 :                         if (err != NULL) {
    3018                 :        227 :                                 RTE_BPF_LOG_FUNC_LINE(ERR,
    3019                 :            :                                         "%s at pc: %u", err, idx);
    3020                 :            :                                 rc = -EINVAL;
    3021                 :        227 :                                 break;
    3022                 :            :                         }
    3023                 :            : 
    3024                 :      10718 :                         log_dbg_eval_state(bvf, ins + idx, idx);
    3025                 :      10718 :                         bvf->evin = NULL;
    3026                 :            :                 }
    3027                 :            : 
    3028                 :            :                 /* proceed through CFG */
    3029                 :            :                 next = get_next_node(bvf, node);
    3030                 :            : 
    3031         [ +  - ]:      10657 :                 if (next != NULL) {
    3032                 :            :                         /*
    3033                 :            :                          * proceed with next child
    3034                 :            :                          * next points to an unwalked subtree of node
    3035                 :            :                          */
    3036         [ +  + ]:      10657 :                         if (node->cur_edge == node->nb_edge &&
    3037         [ +  + ]:       7444 :                                         node->evst.cur != NULL) {
    3038                 :       3213 :                                 restore_cur_eval_state(bvf, node);
    3039                 :       3213 :                                 stats.nb_restore++;
    3040                 :            :                         }
    3041                 :            : 
    3042         [ +  + ]:      10657 :                         if (bvf->evst->unreachable) {
    3043                 :        201 :                                 rc = __rte_bpf_validate_debug_evaluate_update(
    3044                 :            :                                         debug, get_node_idx(bvf, next),
    3045                 :            :                                         RTE_BIT64(RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_UNREACHABLE));
    3046         [ +  - ]:        201 :                                 if (rc < 0)
    3047                 :            :                                         break;
    3048                 :            : 
    3049                 :            :                                 next = NULL;
    3050                 :            :                         /*
    3051                 :            :                          * for jcc targets: check did we already evaluated
    3052                 :            :                          * that path and can it's evaluation be skipped that
    3053                 :            :                          * time.
    3054                 :            :                          */
    3055   [ +  +  +  + ]:      16681 :                         } else if (node->nb_edge > 1 &&
    3056                 :       6225 :                                         prune_eval_state(bvf, node, next) == 0) {
    3057                 :        349 :                                 rc = __rte_bpf_validate_debug_evaluate_update(
    3058                 :            :                                         debug, get_node_idx(bvf, next),
    3059                 :            :                                         RTE_BIT64(RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_PRUNE));
    3060         [ +  - ]:        349 :                                 if (rc < 0)
    3061                 :            :                                         break;
    3062                 :            : 
    3063                 :            :                                 next = NULL;
    3064                 :        349 :                                 stats.nb_prune++;
    3065                 :            :                         } else {
    3066                 :      10107 :                                 next->prev_node = node;
    3067                 :            :                                 node = next;
    3068                 :            :                         }
    3069                 :            :                 } else {
    3070                 :            :                         /*
    3071                 :            :                          * finished with current node and all it's kids,
    3072                 :            :                          * mark it's @start state as safe for future references,
    3073                 :            :                          * and proceed with parent.
    3074                 :            :                          */
    3075                 :            : 
    3076         [ +  + ]:      10491 :                         if (is_branch_start(node)) {
    3077                 :       5874 :                                 rc = __rte_bpf_validate_debug_evaluate_update(
    3078                 :            :                                         debug, get_node_idx(bvf, node->prev_node),
    3079                 :            :                                         RTE_BIT64(RTE_BPF_VALIDATE_DEBUG_EVENT_BRANCH_RETURN));
    3080         [ +  - ]:       5874 :                                 if (rc < 0)
    3081                 :            :                                         break;
    3082                 :            :                         }
    3083                 :            : 
    3084                 :      10491 :                         node->cur_edge = 0;
    3085                 :      10491 :                         save_safe_eval_state(bvf, node);
    3086                 :      10491 :                         node = node->prev_node;
    3087                 :            : 
    3088                 :            :                         /* first node will not have prev, signalling finish */
    3089                 :            :                 }
    3090                 :            : 
    3091                 :            :                 /*
    3092                 :            :                  * next != NULL: stepped down the tree, node == next;
    3093                 :            :                  * next == NULL: stepped up after processing or pruning subtree;
    3094                 :            :                  */
    3095                 :            :         }
    3096                 :            : 
    3097                 :        838 :         RTE_LOG(DEBUG, BPF, "%s(%p) returns %d, stats:\n"
    3098                 :            :                 "node evaluations=%u;\n"
    3099                 :            :                 "state pruned=%u;\n"
    3100                 :            :                 "state saves=%u;\n"
    3101                 :            :                 "state restores=%u;\n",
    3102                 :            :                 __func__, bvf, rc,
    3103                 :            :                 stats.nb_eval, stats.nb_prune, stats.nb_save, stats.nb_restore);
    3104                 :            : 
    3105                 :        838 :         debug_rc = __rte_bpf_validate_debug_evaluate_finish(debug, rc);
    3106         [ +  - ]:        838 :         rc = debug_rc < 0 ? debug_rc : rc;
    3107                 :            : 
    3108                 :            :         /* Caller does not expect positive values. */
    3109                 :        838 :         return RTE_MIN(0, rc);
    3110                 :            : }
    3111                 :            : 
    3112                 :            : static bool
    3113                 :        844 : prog_arg_is_valid(const struct rte_bpf_arg *prog_arg)
    3114                 :            : {
    3115                 :            :         /* check input argument type, don't allow mbuf ptr on 32-bit */
    3116         [ +  + ]:        844 :         if (prog_arg->type != RTE_BPF_ARG_RAW &&
    3117         [ -  + ]:         57 :                         prog_arg->type != RTE_BPF_ARG_PTR &&
    3118                 :            :                         (sizeof(uint64_t) != sizeof(uintptr_t) ||
    3119                 :            :                         prog_arg->type != RTE_BPF_ARG_PTR_MBUF)) {
    3120                 :          0 :                 RTE_BPF_LOG_FUNC_LINE(ERR, "unsupported argument type");
    3121                 :          0 :                 return false;
    3122                 :            :         }
    3123                 :            : 
    3124                 :            :         return true;
    3125                 :            : }
    3126                 :            : 
    3127                 :            : int
    3128                 :        843 : __rte_bpf_validate(const struct rte_bpf_prm_ex *prm, uint32_t *stack_sz)
    3129                 :            : {
    3130                 :            :         int32_t rc;
    3131                 :            :         struct bpf_verifier bvf;
    3132                 :            : 
    3133         [ -  + ]:        843 :         if (prm->nb_prog_arg > RTE_BPF_FUNC_MAX_ARGS) {
    3134                 :          0 :                 RTE_BPF_LOG_FUNC_LINE(ERR,
    3135                 :            :                         "support up to %u arguments, found %u",
    3136                 :            :                         RTE_BPF_FUNC_MAX_ARGS, prm->nb_prog_arg);
    3137                 :          0 :                 return -ENOTSUP;
    3138                 :            :         }
    3139                 :            : 
    3140         [ +  + ]:       1687 :         for (uint32_t pai = 0; pai != prm->nb_prog_arg; ++pai)
    3141         [ -  + ]:        844 :                 if (!prog_arg_is_valid(&prm->prog_arg[pai])) {
    3142                 :          0 :                         RTE_BPF_LOG_FUNC_LINE(ERR,
    3143                 :            :                                 "unsupported argument %d (r%d) type",
    3144                 :            :                                 pai, EBPF_REG_1 + pai);
    3145                 :          0 :                         return -ENOTSUP;
    3146                 :            :                 }
    3147                 :            : 
    3148                 :            :         memset(&bvf, 0, sizeof(bvf));
    3149                 :        843 :         bvf.prm = prm;
    3150                 :        843 :         bvf.in = calloc(prm->raw.nb_ins, sizeof(bvf.in[0]));
    3151         [ +  - ]:        843 :         if (bvf.in == NULL)
    3152                 :            :                 return -ENOMEM;
    3153                 :            : 
    3154                 :        843 :         rc = validate(&bvf);
    3155                 :            : 
    3156         [ +  + ]:        843 :         if (rc == 0) {
    3157                 :        838 :                 rc = evst_pool_init(&bvf);
    3158         [ +  - ]:        838 :                 if (rc == 0)
    3159                 :        838 :                         rc = evaluate(&bvf);
    3160                 :        838 :                 evst_pool_fini(&bvf);
    3161                 :            :         }
    3162                 :            : 
    3163                 :        843 :         free(bvf.in);
    3164                 :            : 
    3165                 :            :         /* copy collected info */
    3166         [ +  + ]:        843 :         if (rc == 0) {
    3167                 :        611 :                 *stack_sz = bvf.stack_sz;
    3168                 :            : 
    3169                 :            :                 /* for LD_ABS/LD_IND, we'll need extra space on the stack */
    3170         [ +  + ]:        611 :                 if (bvf.nb_ldmb_nodes != 0)
    3171                 :         55 :                         *stack_sz = RTE_ALIGN_CEIL(*stack_sz +
    3172                 :            :                                 sizeof(uint64_t), sizeof(uint64_t));
    3173                 :            :         }
    3174                 :            : 
    3175                 :            :         return rc;
    3176                 :            : }

Generated by: LCOV version 1.14