blob: ee10a9f06b97c126d962460c347ccfc7518d45e7 [file] [log] [blame]
Thomas Gleixner25763b32019-05-28 10:10:09 -07001/* SPDX-License-Identifier: GPL-2.0-only */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01002/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +01003 */
4#ifndef _LINUX_BPF_VERIFIER_H
5#define _LINUX_BPF_VERIFIER_H 1
6
7#include <linux/bpf.h> /* for enum bpf_reg_type */
8#include <linux/filter.h> /* for MAX_BPF_STACK */
Edward Creef1174f72017-08-07 15:26:19 +01009#include <linux/tnum.h>
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +010010
Edward Creeb03c9f92017-08-07 15:26:36 +010011/* Maximum variable offset umax_value permitted when resolving memory accesses.
12 * In practice this is far bigger than any realistic pointer offset; this limit
13 * ensures that umax_value + (int)off + (int)size cannot overflow a u64.
14 */
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -080015#define BPF_MAX_VAR_OFF (1 << 29)
Edward Creeb03c9f92017-08-07 15:26:36 +010016/* Maximum variable size permitted for ARG_CONST_SIZE[_OR_ZERO]. This ensures
17 * that converting umax_value to int cannot overflow.
18 */
Alexei Starovoitovbb7f0f92017-12-18 20:12:00 -080019#define BPF_MAX_VAR_SIZ (1 << 29)
Josef Bacik48461132016-09-28 10:54:32 -040020
Edward Cree8e9cd9c2017-08-23 15:11:21 +010021/* Liveness marks, used for registers and spilled-regs (in stack slots).
22 * Read marks propagate upwards until they find a write mark; they record that
23 * "one of this state's descendants read this reg" (and therefore the reg is
24 * relevant for states_equal() checks).
25 * Write marks collect downwards and do not propagate; they record that "the
26 * straight-line code that reached this state (from its parent) wrote this reg"
27 * (and therefore that reads propagated from this state or its descendants
28 * should not propagate to its parent).
29 * A state with a write mark can receive read marks; it just won't propagate
30 * them to its parent, since the write mark is a property, not of the state,
31 * but of the link between it and its parent. See mark_reg_read() and
32 * mark_stack_slot_read() in kernel/bpf/verifier.c.
33 */
Edward Creedc503a82017-08-15 20:34:35 +010034enum bpf_reg_liveness {
35 REG_LIVE_NONE = 0, /* reg hasn't been read or written this branch */
Jiong Wang5327ed32019-05-24 23:25:12 +010036 REG_LIVE_READ32 = 0x1, /* reg was read, so we're sensitive to initial value */
37 REG_LIVE_READ64 = 0x2, /* likewise, but full 64-bit content matters */
38 REG_LIVE_READ = REG_LIVE_READ32 | REG_LIVE_READ64,
39 REG_LIVE_WRITTEN = 0x4, /* reg was written first, screening off later reads */
40 REG_LIVE_DONE = 0x8, /* liveness won't be updating this register anymore */
Edward Creedc503a82017-08-15 20:34:35 +010041};
42
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +010043struct bpf_reg_state {
Edward Cree679c7822018-08-22 20:02:19 +010044 /* Ordering of fields matters. See states_equal() */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +010045 enum bpf_reg_type type;
46 union {
Edward Creef1174f72017-08-07 15:26:19 +010047 /* valid when type == PTR_TO_PACKET */
48 u16 range;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +010049
50 /* valid when type == CONST_PTR_TO_MAP | PTR_TO_MAP_VALUE |
51 * PTR_TO_MAP_VALUE_OR_NULL
52 */
53 struct bpf_map *map_ptr;
Daniel Borkmann09625902018-11-01 00:05:52 +010054
55 /* Max size from any of the above. */
56 unsigned long raw;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +010057 };
Edward Creef1174f72017-08-07 15:26:19 +010058 /* Fixed part of pointer offset, pointer types only */
59 s32 off;
60 /* For PTR_TO_PACKET, used to find other pointers with the same variable
61 * offset, so they can share range knowledge.
62 * For PTR_TO_MAP_VALUE_OR_NULL this is used to share which map value we
63 * came from, when one is tested for != NULL.
Joe Stringerc64b7982018-10-02 13:35:33 -070064 * For PTR_TO_SOCKET this is used to share which pointers retain the
65 * same reference to the socket, to determine proper reference freeing.
Edward Creef1174f72017-08-07 15:26:19 +010066 */
Alexei Starovoitovd2a4dd32016-12-07 10:57:59 -080067 u32 id;
Martin KaFai Lau1b986582019-03-12 10:23:02 -070068 /* PTR_TO_SOCKET and PTR_TO_TCP_SOCK could be a ptr returned
69 * from a pointer-cast helper, bpf_sk_fullsock() and
70 * bpf_tcp_sock().
71 *
72 * Consider the following where "sk" is a reference counted
73 * pointer returned from "sk = bpf_sk_lookup_tcp();":
74 *
75 * 1: sk = bpf_sk_lookup_tcp();
76 * 2: if (!sk) { return 0; }
77 * 3: fullsock = bpf_sk_fullsock(sk);
78 * 4: if (!fullsock) { bpf_sk_release(sk); return 0; }
79 * 5: tp = bpf_tcp_sock(fullsock);
80 * 6: if (!tp) { bpf_sk_release(sk); return 0; }
81 * 7: bpf_sk_release(sk);
82 * 8: snd_cwnd = tp->snd_cwnd; // verifier will complain
83 *
84 * After bpf_sk_release(sk) at line 7, both "fullsock" ptr and
85 * "tp" ptr should be invalidated also. In order to do that,
86 * the reg holding "fullsock" and "sk" need to remember
87 * the original refcounted ptr id (i.e. sk_reg->id) in ref_obj_id
88 * such that the verifier can reset all regs which have
89 * ref_obj_id matching the sk_reg->id.
90 *
91 * sk_reg->ref_obj_id is set to sk_reg->id at line 1.
92 * sk_reg->id will stay as NULL-marking purpose only.
93 * After NULL-marking is done, sk_reg->id can be reset to 0.
94 *
95 * After "fullsock = bpf_sk_fullsock(sk);" at line 3,
96 * fullsock_reg->ref_obj_id is set to sk_reg->ref_obj_id.
97 *
98 * After "tp = bpf_tcp_sock(fullsock);" at line 5,
99 * tp_reg->ref_obj_id is set to fullsock_reg->ref_obj_id
100 * which is the same as sk_reg->ref_obj_id.
101 *
102 * From the verifier perspective, if sk, fullsock and tp
103 * are not NULL, they are the same ptr with different
104 * reg->type. In particular, bpf_sk_release(tp) is also
105 * allowed and has the same effect as bpf_sk_release(sk).
106 */
107 u32 ref_obj_id;
Edward Creef1174f72017-08-07 15:26:19 +0100108 /* For scalar types (SCALAR_VALUE), this represents our knowledge of
109 * the actual value.
110 * For pointer types, this represents the variable part of the offset
111 * from the pointed-to object, and is shared with all bpf_reg_states
112 * with the same id as us.
113 */
114 struct tnum var_off;
Alexei Starovoitovd2a4dd32016-12-07 10:57:59 -0800115 /* Used to determine if any memory access using this register will
Edward Creef1174f72017-08-07 15:26:19 +0100116 * result in a bad access.
117 * These refer to the same value as var_off, not necessarily the actual
118 * contents of the register.
Alexei Starovoitovd2a4dd32016-12-07 10:57:59 -0800119 */
Edward Creeb03c9f92017-08-07 15:26:36 +0100120 s64 smin_value; /* minimum possible (s64)value */
121 s64 smax_value; /* maximum possible (s64)value */
122 u64 umin_value; /* minimum possible (u64)value */
123 u64 umax_value; /* maximum possible (u64)value */
Edward Cree679c7822018-08-22 20:02:19 +0100124 /* parentage chain for liveness checking */
125 struct bpf_reg_state *parent;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800126 /* Inside the callee two registers can be both PTR_TO_STACK like
127 * R1=fp-8 and R2=fp-8, but one of them points to this function stack
128 * while another to the caller's stack. To differentiate them 'frameno'
129 * is used which is an index in bpf_verifier_state->frame[] array
130 * pointing to bpf_func_state.
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800131 */
132 u32 frameno;
Jiong Wang5327ed32019-05-24 23:25:12 +0100133 /* Tracks subreg definition. The stored value is the insn_idx of the
134 * writing insn. This is safe because subreg_def is used before any insn
135 * patching which only happens after main verification finished.
136 */
137 s32 subreg_def;
Edward Creedc503a82017-08-15 20:34:35 +0100138 enum bpf_reg_liveness live;
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -0700139 /* if (!precise && SCALAR_VALUE) min/max/tnum don't affect safety */
140 bool precise;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100141};
142
143enum bpf_stack_slot_type {
144 STACK_INVALID, /* nothing was stored in this stack slot */
145 STACK_SPILL, /* register spilled into stack */
Alexei Starovoitovcc2b14d2017-12-14 17:55:08 -0800146 STACK_MISC, /* BPF program wrote some data into this slot */
147 STACK_ZERO, /* BPF program wrote constant zero */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100148};
149
150#define BPF_REG_SIZE 8 /* size of eBPF register in bytes */
151
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700152struct bpf_stack_state {
153 struct bpf_reg_state spilled_ptr;
154 u8 slot_type[BPF_REG_SIZE];
155};
156
Joe Stringerfd978bf72018-10-02 13:35:35 -0700157struct bpf_reference_state {
158 /* Track each reference created with a unique id, even if the same
159 * instruction creates the reference multiple times (eg, via CALL).
160 */
161 int id;
162 /* Instruction where the allocation of this reference occurred. This
163 * is used purely to inform the user of a reference leak.
164 */
165 int insn_idx;
166};
167
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100168/* state of the program:
169 * type of all registers and stack info
170 */
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800171struct bpf_func_state {
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100172 struct bpf_reg_state regs[MAX_BPF_REG];
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800173 /* index of call instruction that called into this func */
174 int callsite;
175 /* stack frame number of this function state from pov of
176 * enclosing bpf_verifier_state.
177 * 0 = main function, 1 = first callee.
178 */
179 u32 frameno;
180 /* subprog number == index within subprog_stack_depth
181 * zero == main subprog
182 */
183 u32 subprogno;
184
Joe Stringerfd978bf72018-10-02 13:35:35 -0700185 /* The following fields should be last. See copy_func_state() */
186 int acquired_refs;
187 struct bpf_reference_state *refs;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700188 int allocated_stack;
189 struct bpf_stack_state *stack;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100190};
191
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -0700192struct bpf_idx_pair {
193 u32 prev_idx;
194 u32 idx;
195};
196
Lorenz Bauera0a47782021-09-07 16:17:00 +0300197struct bpf_id_pair {
198 u32 old;
199 u32 cur;
200};
201
202/* Maximum number of register states that can exist at once */
203#define BPF_ID_MAP_SIZE (MAX_BPF_REG + MAX_BPF_STACK / BPF_REG_SIZE)
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800204#define MAX_CALL_FRAMES 8
205struct bpf_verifier_state {
206 /* call stack tracking */
207 struct bpf_func_state *frame[MAX_CALL_FRAMES];
Alexei Starovoitov25897262019-06-15 12:12:20 -0700208 struct bpf_verifier_state *parent;
209 /*
210 * 'branches' field is the number of branches left to explore:
211 * 0 - all possible paths from this state reached bpf_exit or
212 * were safely pruned
213 * 1 - at least one path is being explored.
214 * This state hasn't reached bpf_exit
215 * 2 - at least two paths are being explored.
216 * This state is an immediate parent of two children.
217 * One is fallthrough branch with branches==1 and another
218 * state is pushed into stack (to be explored later) also with
219 * branches==1. The parent of this state has branches==1.
220 * The verifier state tree connected via 'parent' pointer looks like:
221 * 1
222 * 1
223 * 2 -> 1 (first 'if' pushed into stack)
224 * 1
225 * 2 -> 1 (second 'if' pushed into stack)
226 * 1
227 * 1
228 * 1 bpf_exit.
229 *
230 * Once do_check() reaches bpf_exit, it calls update_branch_counts()
231 * and the verifier state tree will look:
232 * 1
233 * 1
234 * 2 -> 1 (first 'if' pushed into stack)
235 * 1
236 * 1 -> 1 (second 'if' pushed into stack)
237 * 0
238 * 0
239 * 0 bpf_exit.
240 * After pop_stack() the do_check() will resume at second 'if'.
241 *
242 * If is_state_visited() sees a state with branches > 0 it means
243 * there is a loop. If such state is exactly equal to the current state
244 * it's an infinite loop. Note states_equal() checks for states
245 * equvalency, so two states being 'states_equal' does not mean
246 * infinite loop. The exact comparison is provided by
247 * states_maybe_looping() function. It's a stronger pre-check and
248 * much faster than states_equal().
249 *
250 * This algorithm may not find all possible infinite loops or
251 * loop iteration count may be too high.
252 * In such cases BPF_COMPLEXITY_LIMIT_INSNS limit kicks in.
253 */
254 u32 branches;
Alexei Starovoitovdc2a4eb2019-05-21 20:17:07 -0700255 u32 insn_idx;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800256 u32 curframe;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800257 u32 active_spin_lock;
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100258 bool speculative;
Alexei Starovoitovb5dc0162019-06-15 12:12:25 -0700259
260 /* first and last insn idx of this verifier state */
261 u32 first_insn_idx;
262 u32 last_insn_idx;
263 /* jmp history recorded from first to last.
264 * backtracking is using it to go from last to first.
265 * For most states jmp_history_cnt is [0-3].
266 * For loops can go up to ~40.
267 */
268 struct bpf_idx_pair *jmp_history;
269 u32 jmp_history_cnt;
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800270};
271
Joe Stringerf3709f62018-10-02 13:35:29 -0700272#define bpf_get_spilled_reg(slot, frame) \
273 (((slot < frame->allocated_stack / BPF_REG_SIZE) && \
274 (frame->stack[slot].slot_type[0] == STACK_SPILL)) \
275 ? &frame->stack[slot].spilled_ptr : NULL)
276
277/* Iterate over 'frame', setting 'reg' to either NULL or a spilled register. */
278#define bpf_for_each_spilled_reg(iter, frame, reg) \
279 for (iter = 0, reg = bpf_get_spilled_reg(iter, frame); \
280 iter < frame->allocated_stack / BPF_REG_SIZE; \
281 iter++, reg = bpf_get_spilled_reg(iter, frame))
282
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100283/* linked list of verifier states used to prune search */
284struct bpf_verifier_state_list {
285 struct bpf_verifier_state state;
286 struct bpf_verifier_state_list *next;
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -0700287 int miss_cnt, hit_cnt;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100288};
289
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100290/* Possible states for alu_state member. */
Daniel Borkmann8ba25a92021-04-29 15:19:37 +0000291#define BPF_ALU_SANITIZE_SRC (1U << 0)
292#define BPF_ALU_SANITIZE_DST (1U << 1)
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100293#define BPF_ALU_NEG_VALUE (1U << 2)
Daniel Borkmannd3bd7412019-01-06 00:54:37 +0100294#define BPF_ALU_NON_POINTER (1U << 3)
Daniel Borkmann8ba25a92021-04-29 15:19:37 +0000295#define BPF_ALU_IMMEDIATE (1U << 4)
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100296#define BPF_ALU_SANITIZE (BPF_ALU_SANITIZE_SRC | \
297 BPF_ALU_SANITIZE_DST)
298
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100299struct bpf_insn_aux_data {
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700300 union {
301 enum bpf_reg_type ptr_type; /* pointer type for load/store insns */
Daniel Borkmannc93552c2018-05-24 02:32:53 +0200302 unsigned long map_state; /* pointer/poison value for maps */
Alexei Starovoitov1c2a0882017-12-14 17:55:15 -0800303 s32 call_imm; /* saved imm field of call insn */
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100304 u32 alu_limit; /* limit for add/sub register with pointer */
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +0200305 struct {
306 u32 map_index; /* index into used_maps[] */
307 u32 map_off; /* offset from value base address */
308 };
Alexei Starovoitov81ed18a2017-03-15 18:26:42 -0700309 };
Yonghong Song23994632017-06-22 15:07:39 -0700310 int ctx_field_size; /* the ctx field size for load insn, maybe 0 */
Alexei Starovoitovc1311872017-11-22 16:42:05 -0800311 bool seen; /* this insn was processed by the verifier */
Daniel Borkmannf5893af2021-09-07 16:16:59 +0300312 bool sanitize_stack_spill; /* subject to Spectre v4 sanitation */
Jiong Wang5327ed32019-05-24 23:25:12 +0100313 bool zext_dst; /* this insn zero extends dst reg */
Daniel Borkmann979d63d2019-01-03 00:58:34 +0100314 u8 alu_state; /* used in combination with alu_limit */
Alexei Starovoitova8f500a2019-05-21 20:17:06 -0700315 bool prune_point;
Jakub Kicinski9e4c24e2019-01-22 22:45:23 -0800316 unsigned int orig_idx; /* original instruction index */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100317};
318
319#define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */
320
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700321#define BPF_VERIFIER_TMP_LOG_SIZE 1024
322
Martin KaFai Laub9193c12018-03-24 11:44:22 -0700323struct bpf_verifier_log {
Jakub Kicinskie7bf8242017-10-09 10:30:10 -0700324 u32 level;
Jakub Kicinskia2a7d572017-10-09 10:30:15 -0700325 char kbuf[BPF_VERIFIER_TMP_LOG_SIZE];
Jakub Kicinskie7bf8242017-10-09 10:30:10 -0700326 char __user *ubuf;
327 u32 len_used;
328 u32 len_total;
329};
330
Martin KaFai Laub9193c12018-03-24 11:44:22 -0700331static inline bool bpf_verifier_log_full(const struct bpf_verifier_log *log)
Jakub Kicinskie7bf8242017-10-09 10:30:10 -0700332{
333 return log->len_used >= log->len_total - 1;
334}
335
Alexei Starovoitov06ee7112019-04-01 21:27:40 -0700336#define BPF_LOG_LEVEL1 1
337#define BPF_LOG_LEVEL2 2
338#define BPF_LOG_STATS 4
339#define BPF_LOG_LEVEL (BPF_LOG_LEVEL1 | BPF_LOG_LEVEL2)
340#define BPF_LOG_MASK (BPF_LOG_LEVEL | BPF_LOG_STATS)
341
Martin KaFai Lau77d2e052018-03-24 11:44:23 -0700342static inline bool bpf_verifier_log_needed(const struct bpf_verifier_log *log)
343{
344 return log->level && log->ubuf && !bpf_verifier_log_full(log);
345}
346
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800347#define BPF_MAX_SUBPROGS 256
348
Jiong Wang9c8105b2018-05-02 16:17:18 -0400349struct bpf_subprog_info {
350 u32 start; /* insn idx of function entry point */
Martin KaFai Lauc454a462018-12-07 16:42:25 -0800351 u32 linfo_idx; /* The idx to the main_prog->aux->linfo */
Jiong Wang9c8105b2018-05-02 16:17:18 -0400352 u16 stack_depth; /* max. stack depth used by this function */
Maciej Fijalkowski3a8d86d2020-09-16 23:10:07 +0200353 bool has_tail_call;
Jiong Wang9c8105b2018-05-02 16:17:18 -0400354};
355
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100356/* single container for all structs
357 * one verifier_env per bpf_check() call
358 */
359struct bpf_verifier_env {
Daniel Borkmannc08435e2019-01-03 00:58:27 +0100360 u32 insn_idx;
361 u32 prev_insn_idx;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100362 struct bpf_prog *prog; /* eBPF program being verified */
Jakub Kicinski00176a32017-10-16 16:40:54 -0700363 const struct bpf_verifier_ops *ops;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100364 struct bpf_verifier_stack_elem *head; /* stack of verifier states to be processed */
365 int stack_size; /* number of states to be processed */
David S. Millere07b98d2017-05-10 11:38:07 -0700366 bool strict_alignment; /* perform strict pointer alignment checks */
Alexei Starovoitov10d274e2019-08-22 22:52:12 -0700367 bool test_state_freq; /* test verifier with different pruning frequency */
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700368 struct bpf_verifier_state *cur_state; /* current verifier state */
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100369 struct bpf_verifier_state_list **explored_states; /* search pruning optimization */
Alexei Starovoitov9f4686c2019-04-01 21:27:41 -0700370 struct bpf_verifier_state_list *free_list;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100371 struct bpf_map *used_maps[MAX_USED_MAPS]; /* array of map's used by eBPF program */
372 u32 used_map_cnt; /* number of used maps */
373 u32 id_gen; /* used to generate unique reg IDs */
Daniel Borkmannae968e22021-09-07 16:17:01 +0300374 bool explore_alu_limits;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100375 bool allow_ptr_leaks;
376 bool seen_direct_write;
377 struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */
Martin KaFai Laud9762e82018-12-13 10:41:48 -0800378 const struct bpf_line_info *prev_linfo;
Martin KaFai Laub9193c12018-03-24 11:44:22 -0700379 struct bpf_verifier_log log;
Jiong Wang9c8105b2018-05-02 16:17:18 -0400380 struct bpf_subprog_info subprog_info[BPF_MAX_SUBPROGS + 1];
Lorenz Bauera0a47782021-09-07 16:17:00 +0300381 struct bpf_id_pair idmap_scratch[BPF_ID_MAP_SIZE];
Alexei Starovoitov7df737e2019-04-19 07:44:54 -0700382 struct {
383 int *insn_state;
384 int *insn_stack;
385 int cur_stack;
386 } cfg;
Alexei Starovoitovcc8b0b92017-12-14 17:55:05 -0800387 u32 subprog_cnt;
Alexei Starovoitov06ee7112019-04-01 21:27:40 -0700388 /* number of instructions analyzed by the verifier */
Alexei Starovoitov25897262019-06-15 12:12:20 -0700389 u32 prev_insn_processed, insn_processed;
390 /* number of jmps, calls, exits analyzed so far */
391 u32 prev_jmps_processed, jmps_processed;
Alexei Starovoitov06ee7112019-04-01 21:27:40 -0700392 /* total verification time */
393 u64 verification_time;
394 /* maximum number of verifier states kept in 'branching' instructions */
395 u32 max_states_per_insn;
396 /* total number of allocated verifier states */
397 u32 total_states;
398 /* some states are freed during program analysis.
399 * this is peak number of states. this number dominates kernel
400 * memory consumption during verification
401 */
402 u32 peak_states;
403 /* longest register parentage chain walked for liveness marking */
404 u32 longest_mark_read_walk;
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100405};
406
Mathieu Malaterrebe2d04d2018-05-16 22:27:41 +0200407__printf(2, 0) void bpf_verifier_vlog(struct bpf_verifier_log *log,
408 const char *fmt, va_list args);
Quentin Monnet430e68d2018-01-10 12:26:06 +0000409__printf(2, 3) void bpf_verifier_log_write(struct bpf_verifier_env *env,
410 const char *fmt, ...);
411
Joe Stringerfd978bf72018-10-02 13:35:35 -0700412static inline struct bpf_func_state *cur_func(struct bpf_verifier_env *env)
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700413{
Alexei Starovoitovf4d7e402017-12-14 17:55:06 -0800414 struct bpf_verifier_state *cur = env->cur_state;
415
Joe Stringerfd978bf72018-10-02 13:35:35 -0700416 return cur->frame[cur->curframe];
417}
418
419static inline struct bpf_reg_state *cur_regs(struct bpf_verifier_env *env)
420{
421 return cur_func(env)->regs;
Alexei Starovoitov638f5b92017-10-31 18:16:05 -0700422}
423
Quentin Monneta40a2632018-11-09 13:03:31 +0000424int bpf_prog_offload_verifier_prep(struct bpf_prog *prog);
Jakub Kicinskicae19272017-12-27 18:39:05 -0800425int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env,
426 int insn_idx, int prev_insn_idx);
Quentin Monnetc941ce92018-10-07 12:56:47 +0100427int bpf_prog_offload_finalize(struct bpf_verifier_env *env);
Jakub Kicinski08ca90a2019-01-22 22:45:24 -0800428void
429bpf_prog_offload_replace_insn(struct bpf_verifier_env *env, u32 off,
430 struct bpf_insn *insn);
431void
432bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt);
Jakub Kicinskiab3f0062017-11-03 13:56:17 -0700433
Jakub Kicinski58e2af8b2016-09-21 11:43:57 +0100434#endif /* _LINUX_BPF_VERIFIER_H */