1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
|
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
#include <linux/slab.h>
#include <linux/sched/signal.h>
#include <linux/bpf_verifier.h>
static bool is_cfg_jump(struct bpf_insn *insn)
{
u8 class = BPF_CLASS(insn->code);
u8 opcode = BPF_OP(insn->code);
switch (class) {
case BPF_JMP:
case BPF_JMP32:
return opcode != BPF_CALL;
default:
return false;
}
}
static struct bpf_iarray **compute_predecessors(struct bpf_verifier_env *env)
{
struct bpf_insn_aux_data *aux = env->insn_aux_data;
struct bpf_iarray *succ, *preds, **result;
struct bpf_prog *prog = env->prog;
u32 *num_preds, i, s, len = prog->len;
struct bpf_insn *insn;
size_t sz;
void *tmp;
num_preds = kvcalloc(prog->len, sizeof(u32), GFP_KERNEL_ACCOUNT);
if (!num_preds)
return NULL;
/*
* 'result' layout:
* - array of pointers (struct bpf_iarray *)[len]
* - struct bpf_iarray one after another
*/
sz = size_mul(len, sizeof(*result) + sizeof(**result));
for (i = 0; i < len; i++) {
insn = env->prog->insnsi + i;
succ = bpf_insn_successors(env, i);
sz = size_add(sz, array_size(succ->cnt, sizeof(*succ->items)));
iarray_for_each(s, succ) {
num_preds[s]++;
}
if (bpf_is_ldimm64(insn))
i++;
}
result = kvzalloc(sz, GFP_KERNEL_ACCOUNT);
if (!result) {
kvfree(num_preds);
return NULL;
}
tmp = (void *)&result[len];
for (i = 0; i < len; i++) {
result[i] = tmp;
tmp += struct_size(result[i], items, num_preds[i]);
}
for (i = 0; i < len; i++) {
insn = env->prog->insnsi + i;
succ = bpf_insn_successors(env, i);
iarray_for_each(s, succ) {
preds = result[s];
preds->items[preds->cnt++] = i;
}
if (succ->cnt > 1 ||
(succ->cnt == 1 && num_preds[succ->items[0]] > 1) ||
is_cfg_jump(insn))
aux[i].bb_end = true;
if (bpf_is_ldimm64(insn))
i++;
}
kvfree(num_preds);
return result;
}
static int idoms_intersect(struct bpf_verifier_env *env, int a, int b)
{
int *postorder_nums = env->cfg.postorder_nums;
int *idoms = env->idoms;
while (a != b) {
while (postorder_nums[a] < postorder_nums[b]) {
a = idoms[a];
}
while (postorder_nums[b] < postorder_nums[a]) {
b = idoms[b];
}
}
return a;
}
/* See "A Simple, Fast Dominance Algorithm" by Cooper et al. for details. */
static int compute_subprog_idoms(struct bpf_verifier_env *env, struct bpf_iarray **preds,
int subprog_idx)
{
struct bpf_subprog_info *subprog = &env->subprog_info[subprog_idx];
int start = subprog->start;
int po_first = subprog->postorder_start;
int po_last = (subprog + 1)->postorder_start - 1;
int *idoms = env->idoms;
int po_num, pred;
u32 work = 0;
bool changed;
idoms[start] = start;
changed = true;
do {
changed = false;
/*
* Iterate in reverse postorder, skipping the subprog entry:
* insn_postorder[po_last] is the subprogram entry.
*/
for (po_num = po_last - 1; po_num >= po_first; po_num--) {
int idx = env->cfg.insn_postorder[po_num];
int new_idom = -1;
iarray_for_each(pred, preds[idx]) {
if (++work % 1024 == 0) {
if (signal_pending(current))
return -EAGAIN;
cond_resched();
}
if (idoms[pred] == -1)
continue;
if (new_idom == -1)
new_idom = pred;
else
new_idom = idoms_intersect(env, pred, new_idom);
}
if (new_idom != -1 && idoms[idx] != new_idom) {
idoms[idx] = new_idom;
changed = true;
}
}
} while (changed);
idoms[start] = -1;
return 0;
}
int bpf_compute_idoms(struct bpf_verifier_env *env)
{
struct bpf_iarray **preds;
u32 len = env->prog->len;
int *idoms, i, err = 0;
preds = compute_predecessors(env);
if (!preds)
return -ENOMEM;
idoms = kvcalloc(len, sizeof(*idoms), GFP_KERNEL_ACCOUNT);
if (!idoms) {
kvfree(preds);
return -ENOMEM;
}
env->idoms = idoms;
for (i = 0; i < len; i++)
idoms[i] = -1;
for (i = 0; i < env->subprog_cnt; i++) {
err = compute_subprog_idoms(env, preds, i);
if (err)
break;
}
kvfree(preds);
return err;
}
struct dfs_state {
u32 traversed:1;
u32 next_succ:31;
};
struct loops_dfs {
struct dfs_state *state;
int *dfs_pos;
int *stack;
};
static void mark_irreducible(struct bpf_verifier_env *env, int h)
{
env->insn_aux_data[h].loop->irreducible = true;
}
static void add_backedge(struct bpf_verifier_env *env, int from, int h)
{
struct bpf_insn_aux_data *aux = env->insn_aux_data;
struct bpf_loop *loop = aux[h].loop;
int cnt = loop->backedges_cnt;
if (cnt == MAX_BACKEDGES) {
loop->backedges_overflow = true;
return;
}
loop->backedges[cnt].from = from;
loop->backedges[cnt].latches_cnt = 0;
loop->backedges_cnt++;
}
static int add_exit(struct bpf_loop *loop, int from, int to)
{
if (loop->exits_overflow)
return 0;
if (loop->exits_cnt == MAX_LOOP_EXITS) {
loop->exits_overflow = true;
return 0;
}
if (!loop->exits) {
loop->exits = kvcalloc(MAX_LOOP_EXITS, sizeof(*loop->exits), GFP_KERNEL_ACCOUNT);
if (!loop->exits)
return -ENOMEM;
}
loop->exits[loop->exits_cnt] = (struct bpf_loop_exit) {
.from = from,
.to = to,
};
loop->exits_cnt++;
return 0;
}
static int mark_as_header(struct bpf_verifier_env *env, int h)
{
struct bpf_insn_aux_data *aux = env->insn_aux_data;
if (!aux[h].loop) {
aux[h].loop = kvzalloc_obj(struct bpf_loop, GFP_KERNEL_ACCOUNT);
if (!aux[h].loop)
return -ENOMEM;
}
return 0;
}
static int assign_header(struct bpf_verifier_env *env, struct loops_dfs *dfs, int n, int h)
{
struct bpf_insn_aux_data *aux = env->insn_aux_data;
int *dfs_pos = dfs->dfs_pos;
int err, nh;
err = mark_as_header(env, h);
if (err)
return err;
/* Don't encode self-loops, otherwise can't reflect loops nesting structure. */
if (n == h)
return 0;
/* Make sure that loop headers up the chain are sorted by dfs_pos. */
while (aux[n].loop_header != -1) {
nh = aux[n].loop_header;
if (nh == h)
return 0;
if (dfs_pos[nh] < dfs_pos[h]) {
aux[n].loop_header = h;
n = h;
h = nh;
} else {
n = nh;
}
}
aux[n].loop_header = h;
return 0;
}
static bool is_cond_jmp_insn(struct bpf_insn *insn)
{
u8 class = BPF_CLASS(insn->code);
u8 opcode = BPF_OP(insn->code);
if (class != BPF_JMP && class != BPF_JMP32)
return false;
switch (opcode) {
case BPF_JEQ:
case BPF_JGE:
case BPF_JGT:
case BPF_JLE:
case BPF_JLT:
case BPF_JNE:
case BPF_JSET:
case BPF_JSGE:
case BPF_JSGT:
case BPF_JSLE:
case BPF_JSLT:
return true;
default:
return false;
}
}
int bpf_loop_at_index(struct bpf_verifier_env *env, u32 idx)
{
struct bpf_insn_aux_data *aux = env->insn_aux_data;
return aux[idx].loop ? idx : aux[idx].loop_header;
}
/*
* Collect conditional jumps within n's loop that dominate 'n' and have
* a successor outside the loop, nearest first.
*/
static void find_dominating_conditions(struct bpf_verifier_env *env, int n,
struct bpf_backedge *backedge)
{
struct bpf_insn *insns = env->prog->insnsi;
bool t_exits_loop, f_exits_loop;
int n_loop, t_loop, f_loop;
int *idoms = env->idoms;
n_loop = bpf_loop_at_index(env, n);
if (idoms_intersect(env, n, n_loop) != n_loop)
return;
while (n >= 0) {
if (is_cond_jmp_insn(&insns[n]) && bpf_loop_at_index(env, n) == n_loop) {
t_loop = bpf_loop_at_index(env, n + insns[n].off + 1);
f_loop = bpf_loop_at_index(env, n + 1);
t_exits_loop = t_loop != n_loop && !bpf_is_nested_loop(env, t_loop, n_loop);
f_exits_loop = f_loop != n_loop && !bpf_is_nested_loop(env, f_loop, n_loop);
if (t_exits_loop || f_exits_loop) {
if (backedge->latches_cnt == MAX_LATCHES)
return;
backedge->latches[backedge->latches_cnt++] = n;
}
}
if (n == n_loop)
break;
n = idoms[n];
}
}
/*
* As described in "A New Algorithm for Identifying Loops in Decompilation" by Wei et al,
* adapted to be non-recursive.
*/
static int compute_loops_in_subprog(struct bpf_verifier_env *env, struct loops_dfs *dfs,
int subprog_idx)
{
struct bpf_insn_aux_data *aux = env->insn_aux_data;
struct dfs_state *state = dfs->state;
int start = env->subprog_info[subprog_idx].start;
int *dfs_pos = dfs->dfs_pos;
int *stack = dfs->stack;
int s, h, err, cur, stack_sz;
struct bpf_iarray *succ;
u32 i;
stack[0] = start;
state[start].traversed = true;
state[start].next_succ = 0;
dfs_pos[start] = 1;
stack_sz = 1;
i = 0;
do {
/*
* The algorithm should be very fast in practice,
* guard against pathological inputs, just in case.
*/
if ((++i % 1024) == 0) {
if (signal_pending(current))
return -EAGAIN;
cond_resched();
}
cur = stack[stack_sz - 1];
succ = bpf_insn_successors(env, cur);
if (state[cur].next_succ == succ->cnt) {
dfs_pos[cur] = 0;
stack_sz--;
continue;
}
s = succ->items[state[cur].next_succ];
if (!state[s].traversed) {
/* Case A: start -> ... -> cur -> s [unexplored] */
state[s].traversed = true;
state[s].next_succ = 0;
stack[stack_sz] = s;
dfs_pos[s] = stack_sz + 1;
stack_sz++;
continue;
}
/* 's' is fully explored at this point */
if (dfs_pos[s]) {
/*
* start -> ... -> s -> cur --.
* ^ |
* '----------'
* Case B: 's' is in the current DFS path.
*/
err = assign_header(env, dfs, cur, s);
if (err)
return err;
add_backedge(env, cur, s);
} else if (aux[s].loop_header == -1) {
/*
* start -> ... -> ... -> s -> ... -> end
* | ^
* '---> cur ---'
* Case C: 's' is explored, not in the current DFS path,
* and not a part of any loop.
*/
} else if (dfs_pos[aux[s].loop_header]) {
/*
* .----------------------.
* v |
* start -> ... -> h -> ... -> ... -> s --'
* | ^
* '---> cur ---'
* Case D: 's' is explored, not in current DFS path,
* but its innermost loop header is.
*/
err = assign_header(env, dfs, cur, aux[s].loop_header);
if (err)
return err;
} else {
/*
* case E: 's' is explored, not in current DFS path,
* its innermost loop header is not in current DFS path,
* hence 's' is another entry into the same loop.
*/
h = aux[s].loop_header;
mark_irreducible(env, h);
while (aux[h].loop_header != -1) {
h = aux[h].loop_header;
if (dfs_pos[h]) {
err = assign_header(env, dfs, cur, h);
if (err)
return err;
break;
}
mark_irreducible(env, h);
}
}
state[cur].next_succ++;
} while (stack_sz);
return 0;
}
bool bpf_is_nested_loop(struct bpf_verifier_env *env, int inner_header, int outer_header)
{
struct bpf_insn_aux_data *aux = env->insn_aux_data;
int idx;
for (idx = inner_header; idx >= 0; idx = aux[idx].loop_header)
if (aux[idx].loop_header == outer_header)
return true;
return false;
}
int bpf_compute_loops(struct bpf_verifier_env *env)
{
int i, j, k, s, t, iloop, sloop, err = 0, len = env->prog->len;
struct bpf_insn_aux_data *aux = env->insn_aux_data;
struct bpf_verifier_log *log = &env->log;
struct bpf_backedge *backedge;
struct loops_dfs dfs = {};
struct bpf_iarray *succ;
struct bpf_loop *loop;
dfs.dfs_pos = kvcalloc(len, sizeof(int), GFP_KERNEL_ACCOUNT);
dfs.state = kvcalloc(len, sizeof(struct dfs_state), GFP_KERNEL_ACCOUNT);
dfs.stack = kvcalloc(len, sizeof(int), GFP_KERNEL_ACCOUNT);
if (!dfs.dfs_pos || !dfs.state || !dfs.stack) {
err = -ENOMEM;
goto out;
}
for (i = 0; i < len; i++)
aux[i].loop_header = -1;
for (i = 0; i < env->subprog_cnt; i++) {
err = compute_loops_in_subprog(env, &dfs, i);
if (err)
goto out;
}
/* find latches */
for (i = 0; i < len; i++) {
loop = aux[i].loop;
if (!loop)
continue;
for (j = 0; j < loop->backedges_cnt; j++) {
backedge = &loop->backedges[j];
/*
* In theory, the backedge->from and it's latch can reside in
* an inner loop of `i`, e.g.:
*
* 1: r7 += 1;
* 2: r6 += 1;
* if r7 == 2 goto 1b;
* if r6 < 2 goto 2b;
*
* For now, let's assume that latches are unknown for such cases.
* (GCC/LLVM handle this by inserting artificial cfg nodes).
*/
if (bpf_loop_at_index(env, backedge->from) != i)
continue;
find_dominating_conditions(env, backedge->from, backedge);
}
}
/* find exits */
for (i = 0; i < len; i++) {
iloop = aux[i].loop ? i : aux[i].loop_header;
if (iloop < 0)
continue;
succ = bpf_insn_successors(env, i);
iarray_for_each(s, succ) {
/*
* Nothing left to record once the innermost loop of 'i'
* overflowed: the walk below would stop at it right away.
*/
if (aux[iloop].loop->exits_overflow)
break;
sloop = aux[s].loop ? s : aux[s].loop_header;
if (iloop == sloop)
continue;
if (bpf_is_nested_loop(env, sloop, iloop))
continue;
/*
* At this point 'sloop' is either -1, an outer loop,
* or a loop in another branch of the loop hierarchy.
*/
for (t = iloop; t >= 0 && t != sloop; t = aux[t].loop_header) {
/*
* Account for the following configuration:
*
* tloop {
* iloop {
* ... i: goto s;
* }
* sloop {
* s:
* ...
* }
* }
*/
if (bpf_is_nested_loop(env, sloop, t))
break;
/*
* Record edges i -> s as exits from tloop when:
*
* sloop {
* tloop {
* iloop {
* ... i: goto s;
* }
* }
* s: ...
* }
*/
/* Enclosing loops inherit the overflow, see below */
if (aux[t].loop->exits_overflow)
break;
err = add_exit(aux[t].loop, i, s);
if (err)
goto out;
}
}
if (bpf_is_ldimm64(env->prog->insnsi + i))
i++;
}
/* A nested loop with a truncated exit list can't be abstracted by SCEV */
for (i = 0; i < len; i++) {
loop = aux[i].loop;
if (!loop || !loop->exits_overflow)
continue;
for (t = aux[i].loop_header; t >= 0; t = aux[t].loop_header)
aux[t].loop->exits_overflow = true;
}
if (env->log.level & BPF_LOG_LEVEL2) {
for (i = 0; i < len; i++) {
loop = aux[i].loop;
if (!loop)
continue;
bpf_log(log, "loop at %d", i);
if (aux[i].loop_header >= 0)
bpf_log(log, ", nested in %d", aux[i].loop_header);
if (loop->irreducible)
bpf_log(log, ", irreducible");
if (loop->exits_overflow)
bpf_log(log, ", too many exits");
bpf_log(log, "\n");
for (j = 0; j < loop->backedges_cnt; j++) {
backedge = &loop->backedges[j];
bpf_log(log, " backedge from %d", backedge->from);
for (k = 0; k < backedge->latches_cnt; k++) {
if (k == 0)
bpf_log(log, ", latch at ");
if (k == 1)
bpf_log(log, ", dominating exits at ");
if (k > 1)
bpf_log(log, ", ");
bpf_log(log, "%d", backedge->latches[k]);
}
bpf_log(log, "\n");
}
for (j = 0; j < loop->exits_cnt; j++)
bpf_log(log, " exit from %d to %d\n",
loop->exits[j].from, loop->exits[j].to);
}
}
out:
kvfree(dfs.dfs_pos);
kvfree(dfs.stack);
kvfree(dfs.state);
return err;
}
|