Repository navigation
Expand file tree
/
Copy pathdiagnostics.c
More file actions
2396 lines (2027 loc) · 65.5 KB
/
Copy pathdiagnostics.c
File metadata and controls
2396 lines (2027 loc) · 65.5 KB
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (c) 2026 Meta Platforms, Inc. and affiliates.
#include <linux/bpf.h>
#include <linux/bpf_verifier.h>
#include <linux/btf.h>
#include <linux/ctype.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/seq_buf.h>
#include <linux/overflow.h>
#include <linux/slab.h>
#include <linux/stdarg.h>
#include <linux/string.h>
#include "disasm.h"
#include "diagnostics.h"
#define REGISTER_TYPE_SAFETY "Register Type Safety"
#define MEMORY_SAFETY "Memory Safety"
#define RESOURCE_LIFETIME_SAFETY "Resource Lifetime Safety"
#define CALL_TYPE_SAFETY "Call Type Safety"
#define EXECUTION_CONTEXT_SAFETY "Execution Context Safety"
#define PROGRAM_STRUCTURE "Program Structure"
#define POLICY "Policy"
#define BPF_DIAG_TEXT_WIDTH 100
#define BPF_DIAG_TEXT_INDENT " "
#define BPF_DIAG_CONTEXT 2
#define BPF_DIAG_CONTEXT_CNT (1 + BPF_DIAG_CONTEXT * 2)
#define BPF_DIAG_HISTORY_RENDER_MAX 64
#define BPF_DIAG_SOURCE_LANE_WIDTH 88
#define BPF_DIAG_TAB_WIDTH 8
#define BPF_DIAG_FMT_CHUNK_SIZE (PAGE_SIZE - sizeof(struct diag_fmt_chunk))
#define BPF_DIAG_FMT_BUF_SIZE 256
#define BPF_DIAG_EVENT_LOG_MAX_SIZE (64U << 20)
#define DISASM_LINE_LEN 160
enum bpf_diag_mod_target_kind {
BPF_DIAG_MOD_TARGET_NONE,
BPF_DIAG_MOD_TARGET_REG,
BPF_DIAG_MOD_TARGET_STACK_ARG,
BPF_DIAG_MOD_TARGET_STACK_SLOT,
BPF_DIAG_MOD_TARGET_STACK_RANGE,
};
struct bpf_diag_mod_target {
u32 frame_id;
union {
struct {
s16 min_off;
s16 max_off;
} range;
u16 spi;
u8 regno;
u8 stack_arg;
};
u8 frameno;
u8 kind;
};
static struct bpf_diag_mod_target diag_reg_target(u32 frame_id, u8 frameno, u8 regno)
{
return (struct bpf_diag_mod_target){
.frame_id = frame_id,
.frameno = frameno,
.kind = BPF_DIAG_MOD_TARGET_REG,
.regno = regno,
};
}
static struct bpf_diag_mod_target diag_stack_arg_target(u32 frame_id, u8 frameno, u8 slot)
{
return (struct bpf_diag_mod_target){
.frame_id = frame_id,
.frameno = frameno,
.kind = BPF_DIAG_MOD_TARGET_STACK_ARG,
.stack_arg = slot,
};
}
static struct bpf_diag_mod_target diag_stack_slot_target(u32 frame_id, u8 frameno, u16 spi)
{
return (struct bpf_diag_mod_target){
.frame_id = frame_id,
.frameno = frameno,
.kind = BPF_DIAG_MOD_TARGET_STACK_SLOT,
.spi = spi,
};
}
static struct bpf_diag_mod_target diag_stack_range_target(u32 frame_id, u8 frameno,
s16 min_off, s16 max_off)
{
return (struct bpf_diag_mod_target){
.frame_id = frame_id,
.frameno = frameno,
.kind = BPF_DIAG_MOD_TARGET_STACK_RANGE,
.range.min_off = min_off,
.range.max_off = max_off,
};
}
struct bpf_diag_reg_snapshot {
u32 type;
u32 btf_id;
const struct bpf_map *map_ptr;
const struct btf *btf;
struct tnum var_off;
struct cnum64 r64;
};
enum bpf_diag_history_kind {
BPF_DIAG_HISTORY_BRANCH,
BPF_DIAG_HISTORY_MOD,
BPF_DIAG_HISTORY_REF_ACQUIRE,
BPF_DIAG_HISTORY_REF_RELEASE,
BPF_DIAG_HISTORY_CONTEXT,
};
struct bpf_diag_history_event {
u32 insn_idx : 24;
u32 kind : 8;
u8 in_lineage : 1;
union {
struct {
bool cond_true;
} branch;
struct {
struct bpf_diag_mod_target target;
struct bpf_diag_mod_target origin;
struct bpf_diag_reg_snapshot old, new;
u8 reason;
bool origin_valid;
} mod;
struct {
u32 ref_id;
} ref;
struct {
u32 depth;
u8 kind;
bool enter;
} ctx;
};
};
enum bpf_diag_history_scope {
BPF_DIAG_HISTORY_SCOPE_REG,
BPF_DIAG_HISTORY_SCOPE_STACK_ARG,
BPF_DIAG_HISTORY_SCOPE_REF,
BPF_DIAG_HISTORY_SCOPE_CONTEXT,
};
struct bpf_diag_history_opts {
enum bpf_diag_history_scope scope;
u32 frame_id;
u32 frameno;
int regno;
int stack_arg_slot;
u32 ref_id;
enum bpf_diag_context_kind ctx_kind;
u32 ctx_depth;
};
static void diag_print_history(struct bpf_verifier_env *env,
const struct bpf_diag_history_opts *opts);
static bool diag_target_matches(const struct bpf_diag_mod_target *event_target,
const struct bpf_diag_mod_target *target);
static const char *diag_context_name(enum bpf_diag_context_kind kind);
struct disasm_line {
char text[DISASM_LINE_LEN];
int idx;
bool valid;
};
struct disasm_ctx {
struct bpf_verifier_env *env;
struct seq_buf seq;
};
struct diag_fmt_chunk {
struct list_head node;
struct seq_buf seq;
char data[];
};
struct diag_fmt_mark {
struct diag_fmt_chunk *chunk;
size_t len;
};
struct bpf_diag_log {
struct bpf_diag_history_event *events;
/* Sequence number of the oldest retained event on the active path. */
u64 first_seq;
u32 cnt;
u32 cap;
u32 head;
bool growth_failed;
};
struct bpf_diag_scratch {
struct bpf_linfo_source source_lines[BPF_DIAG_CONTEXT_CNT];
struct disasm_line disasm_lines[BPF_DIAG_CONTEXT_CNT];
};
struct bpf_diag_mod_scope {
struct bpf_reg_state target_reg_snapshot;
struct bpf_diag_mod_target target;
struct bpf_diag_mod_target origin;
enum bpf_diag_mod_reason reason;
u32 insn_idx;
bool active;
bool origin_valid;
};
struct bpf_diag {
struct bpf_diag_log log;
struct bpf_diag_scratch scratch;
struct list_head fmt_chunks;
struct bpf_diag_mod_scope mod;
u32 frame_id_gen;
};
bool bpf_diag_enabled(const struct bpf_verifier_env *env)
{
return env->log.level & BPF_LOG_LEVEL;
}
static void diag_write(struct bpf_verifier_env *env, const char *fmt, ...) __printf(2, 3);
int bpf_diag_init(struct bpf_verifier_env *env)
{
if (!bpf_diag_enabled(env))
return 0;
env->diag = kzalloc_obj(struct bpf_diag, GFP_KERNEL_ACCOUNT);
if (!env->diag)
return -ENOMEM;
INIT_LIST_HEAD(&env->diag->fmt_chunks);
return 0;
}
void bpf_diag_init_frame(struct bpf_verifier_env *env, struct bpf_func_state *state)
{
if (env->diag)
state->diag_frame_id = ++env->diag->frame_id_gen;
}
static char *diag_fmt_alloc(struct bpf_verifier_env *env, size_t size)
{
struct bpf_diag *diag = env->diag;
struct diag_fmt_chunk *chunk;
size_t capacity, available;
char *buf;
if (!diag || !size || size > INT_MAX)
return NULL;
if (!list_empty(&diag->fmt_chunks)) {
chunk = list_last_entry(&diag->fmt_chunks, struct diag_fmt_chunk, node);
available = seq_buf_get_buf(&chunk->seq, &buf);
if (available >= size)
goto commit;
}
capacity = max_t(size_t, BPF_DIAG_FMT_CHUNK_SIZE, size);
chunk = kmalloc_flex(*chunk, data, capacity, GFP_KERNEL_ACCOUNT);
if (!chunk)
return NULL;
seq_buf_init(&chunk->seq, chunk->data, capacity);
list_add_tail(&chunk->node, &diag->fmt_chunks);
available = seq_buf_get_buf(&chunk->seq, &buf);
if (WARN_ON_ONCE(available < size))
return NULL;
commit:
seq_buf_commit(&chunk->seq, size);
return buf;
}
char *bpf_diag_fmt_buf(struct bpf_verifier_env *env, size_t size)
{
char *buf;
buf = diag_fmt_alloc(env, size);
if (buf)
buf[0] = '\0';
return buf;
}
const char *bpf_diag_vfmt(struct bpf_verifier_env *env, const char *fmt, va_list args)
{
va_list copy;
char *buf;
int len;
va_copy(copy, args);
len = vsnprintf(NULL, 0, fmt, copy);
va_end(copy);
if (len < 0 || len == INT_MAX)
return "";
buf = diag_fmt_alloc(env, len + 1);
if (buf)
vsnprintf(buf, len + 1, fmt, args);
return buf ?: "";
}
const char *bpf_diag_fmt(struct bpf_verifier_env *env, const char *fmt, ...)
{
const char *buf;
va_list args;
va_start(args, fmt);
buf = bpf_diag_vfmt(env, fmt, args);
va_end(args);
return buf;
}
static struct diag_fmt_mark diag_fmt_save(struct bpf_verifier_env *env)
{
struct bpf_diag *diag = env->diag;
struct diag_fmt_mark mark = {};
if (!diag || list_empty(&diag->fmt_chunks))
return mark;
mark.chunk = list_last_entry(&diag->fmt_chunks, struct diag_fmt_chunk, node);
mark.len = mark.chunk->seq.len;
return mark;
}
static void diag_fmt_restore(struct bpf_verifier_env *env, struct diag_fmt_mark mark)
{
struct bpf_diag *diag = env->diag;
struct diag_fmt_chunk *chunk;
if (!diag)
return;
while (!list_empty(&diag->fmt_chunks)) {
chunk = list_last_entry(&diag->fmt_chunks, struct diag_fmt_chunk, node);
if (chunk == mark.chunk)
break;
list_del(&chunk->node);
kfree(chunk);
}
if (mark.chunk) {
mark.chunk->seq.len = mark.len;
seq_buf_terminate(&mark.chunk->seq);
}
}
void bpf_diag_free(struct bpf_verifier_env *env)
{
struct bpf_diag *diag = env->diag;
if (!diag)
return;
diag_fmt_restore(env, (struct diag_fmt_mark){});
kvfree(diag->log.events);
kfree(diag);
env->diag = NULL;
}
static void diag_write(struct bpf_verifier_env *env, const char *fmt, ...)
{
va_list args;
if (!bpf_diag_enabled(env))
return;
va_start(args, fmt);
bpf_verifier_vlog(&env->log, fmt, args);
va_end(args);
}
static u64 log_end(const struct bpf_diag_log *log)
{
return log->first_seq + log->cnt;
}
static u32 log_pos(const struct bpf_diag_log *log, u32 idx)
{
u32 pos = log->head + idx;
return pos < log->cap ? pos : pos - log->cap;
}
u64 bpf_diag_event_log_save(struct bpf_verifier_env *env)
{
struct bpf_diag *diag = env->diag;
return diag ? log_end(&diag->log) : 0;
}
void bpf_diag_event_log_restore(struct bpf_verifier_env *env, u64 log_pos)
{
struct bpf_diag *diag = env->diag;
struct bpf_diag_log *log;
u64 end_seq;
if (!diag)
return;
log = &diag->log;
end_seq = log_end(log);
if (WARN_ON_ONCE(log_pos > end_seq))
log_pos = end_seq;
/*
* A deep abandoned path may have rotated away the shared prefix. In
* that case, restart with an empty retained suffix and remember that
* every event before the restored mark is unavailable.
*/
if (log_pos <= log->first_seq) {
log->first_seq = log_pos;
log->head = 0;
log->cnt = 0;
return;
}
log->cnt = log_pos - log->first_seq;
}
u32 bpf_diag_irq_depth(const struct bpf_verifier_state *state)
{
u32 depth = 0;
int i;
for (i = 0; i < state->acquired_refs; i++) {
if (state->refs[i].type == REF_TYPE_IRQ)
depth++;
}
return depth;
}
static void diag_append_history(struct bpf_verifier_env *env,
const struct bpf_diag_history_event *event)
{
struct bpf_diag_history_event *events;
struct bpf_diag *diag = env->diag;
struct bpf_diag_log *log;
u32 cap, max_events;
if (!diag)
return;
log = &diag->log;
if (log->cnt < log->cap) {
log->events[log_pos(log, log->cnt++)] = *event;
return;
}
max_events = BPF_DIAG_EVENT_LOG_MAX_SIZE / sizeof(*events);
if (log->growth_failed || log->cap == max_events)
goto rotate;
cap = min(log->cap ? log->cap * 2 : 64, max_events);
events = kvrealloc(log->events, array_size(cap, sizeof(*events)), GFP_KERNEL_ACCOUNT);
if (!events) {
log->growth_failed = true;
goto rotate;
}
log->events = events;
log->cap = cap;
log->events[log->cnt++] = *event;
return;
rotate:
if (log->cap) {
log->events[log->head++] = *event;
if (log->head == log->cap)
log->head = 0;
}
log->first_seq++;
}
static void diag_print_wrapped_prefixed(struct bpf_verifier_env *env, const char *first_prefix,
const char *next_prefix, const char *text)
{
const char *prefix = first_prefix;
while (*text) {
const char *line = text;
int prefix_len = strlen(prefix);
int text_width = BPF_DIAG_TEXT_WIDTH - prefix_len;
int len = 0, last_space = -1;
if (text_width < 1)
text_width = 1;
while (line[len] && line[len] != '\n' && len < text_width) {
if (line[len] == ' ')
last_space = len;
len++;
}
if (line[len] && line[len] != '\n' && line[len] != ' ' && last_space > 0)
len = last_space;
diag_write(env, "%s%.*s\n", prefix, len, line);
text = line + len;
while (*text == ' ')
text++;
if (*text == '\n')
text++;
prefix = next_prefix;
}
}
const char *bpf_diag_fmt_btf_type(struct bpf_verifier_env *env, const struct btf *btf, u32 type_id)
{
char *buf = bpf_diag_fmt_buf(env, BPF_DIAG_FMT_BUF_SIZE);
size_t len;
int ret;
if (!buf)
return "";
buf[0] = '\0';
ret = btf_type_name_to_buf(btf, type_id, buf, BPF_DIAG_FMT_BUF_SIZE);
if (ret < 0 || !buf[0]) {
scnprintf(buf, BPF_DIAG_FMT_BUF_SIZE, "BTF type ID %u", type_id);
return buf;
}
len = strlen(buf);
if (len && buf[len - 1] == '{')
buf[len - 1] = '\0';
return buf;
}
static void diag_vprint_indented(struct bpf_verifier_env *env, const char *fmt, va_list args)
__printf(2, 0);
static void diag_vprint_indented(struct bpf_verifier_env *env, const char *fmt, va_list args)
{
char *buf;
if (!bpf_diag_enabled(env))
return;
buf = kvasprintf(GFP_KERNEL_ACCOUNT, fmt, args);
if (!buf) {
diag_write(env, "%s<failed to allocate diagnostic text>\n", BPF_DIAG_TEXT_INDENT);
return;
}
diag_print_wrapped_prefixed(env, BPF_DIAG_TEXT_INDENT, BPF_DIAG_TEXT_INDENT, buf);
kfree(buf);
}
static int diag_line_width(unsigned int line)
{
int width = 1;
while (line >= 10) {
line /= 10;
width++;
}
return width;
}
static int diag_line_indent(const char *line)
{
int indent = 0;
while (*line == ' ' || *line == '\t') {
if (*line == '\t')
indent = round_up(indent + 1, BPF_DIAG_TAB_WIDTH);
else
indent++;
line++;
}
return indent;
}
static void disasm_print(void *private_data, const char *fmt, ...) __printf(2, 3);
static void disasm_print(void *private_data, const char *fmt, ...)
{
struct disasm_ctx *ctx = private_data;
va_list args;
va_start(args, fmt);
seq_buf_vprintf(&ctx->seq, fmt, args);
va_end(args);
}
static const char *disasm_kfunc_name(void *private_data, const struct bpf_insn *insn)
{
struct disasm_ctx *ctx = private_data;
return bpf_disasm_kfunc_name(ctx->env, insn);
}
static void format_disasm_line(struct bpf_verifier_env *env, int insn_idx,
struct disasm_line *line)
{
struct disasm_ctx ctx = { .env = env };
struct bpf_insn *insn;
const struct bpf_insn_cbs cbs = {
.cb_call = disasm_kfunc_name,
.cb_print = disasm_print,
.private_data = &ctx,
};
line->idx = insn_idx;
line->valid = false;
seq_buf_init(&ctx.seq, line->text, sizeof(line->text));
if (insn_idx < 0 || insn_idx >= env->prog->len)
return;
if (insn_idx > 0 && bpf_is_ldimm64(&env->prog->insnsi[insn_idx - 1]))
return;
insn = &env->prog->insnsi[insn_idx];
if (bpf_is_ldimm64(insn) && insn_idx + 1 >= env->prog->len)
return;
print_bpf_insn(&cbs, insn, env->allow_ptr_leaks);
seq_buf_terminate(&ctx.seq);
line->valid = true;
}
static void diag_format_source_text(char *buf, size_t size, const char *line, int width)
{
int col = 0, len = 0;
if (!size)
return;
if (width <= 0) {
buf[0] = '\0';
return;
}
line = line ?: "...";
while (*line && col < width && len + 1 < size) {
if (*line == '\t') {
int next = round_up(col + 1, BPF_DIAG_TAB_WIDTH);
while (col < next && col < width && len + 1 < size) {
buf[len++] = ' ';
col++;
}
line++;
continue;
}
buf[len++] = *line++;
col++;
}
if (*line) {
int ellipsis_len = min(3, width);
while (len > 0 && col > width - ellipsis_len) {
len--;
col--;
}
while (ellipsis_len-- && len + 1 < size)
buf[len++] = '.';
}
buf[len] = '\0';
}
static void diag_format_source_lane(char *buf, size_t size, const char *source_prefix,
int source_line_width, int line_num, const char *line)
{
int len, text_width;
if (line_num <= 0) {
buf[0] = '\0';
return;
}
len = scnprintf(buf, size, "%s%*d | ", source_prefix, source_line_width, line_num);
text_width = BPF_DIAG_SOURCE_LANE_WIDTH - len;
diag_format_source_text(buf + len, size - len, line, text_width);
}
static void bpf_diag_header(struct bpf_verifier_env *env, const char *category,
const char *problem)
{
char first;
if (!bpf_diag_enabled(env))
return;
category = category ?: "Verifier Error";
problem = problem ?: "";
if (!problem[0]) {
diag_write(env, "\nVerification failed: %s\n", category);
return;
}
first = toupper(problem[0]);
diag_write(env, "\nVerification failed: %s: %c%s\n", category, first, problem + 1);
}
static void diag_reason(struct bpf_verifier_env *env, const char *fmt, ...) __printf(2, 3);
static void diag_suggestion(struct bpf_verifier_env *env, const char *fmt, ...)
__printf(2, 3);
static void diag_section(struct bpf_verifier_env *env, const char *title)
{
if (!bpf_diag_enabled(env))
return;
diag_write(env, "\n%s:\n", title);
}
static void diag_reason(struct bpf_verifier_env *env, const char *fmt, ...)
{
va_list args;
if (!bpf_diag_enabled(env))
return;
diag_section(env, "Reason");
va_start(args, fmt);
diag_vprint_indented(env, fmt, args);
va_end(args);
}
static void diag_suggestion(struct bpf_verifier_env *env, const char *fmt, ...)
{
va_list args;
if (!bpf_diag_enabled(env))
return;
diag_section(env, "Suggestion");
va_start(args, fmt);
diag_vprint_indented(env, fmt, args);
va_end(args);
diag_write(env, "\n");
}
static void diag_print_source_annotation(struct bpf_verifier_env *env, int line_width, int indent,
const char *label, const char *msg)
{
const char *first_prefix, *next_prefix, *text;
indent = min_t(int, indent, max_t(int, 0, BPF_DIAG_SOURCE_LANE_WIDTH - line_width - 8));
text = bpf_diag_fmt(env, "%s: %s", label, msg);
first_prefix = bpf_diag_fmt(env, " %*s | %*s^-- ", line_width + 4, "", indent, "");
next_prefix = bpf_diag_fmt(env, " %*s | %*s ", line_width + 4, "", indent, "");
diag_print_wrapped_prefixed(env, first_prefix, next_prefix, text);
}
static void diag_print_insn_context(struct bpf_verifier_env *env, u32 insn_idx,
struct disasm_line *disasm_lines)
{
int insn_width = diag_line_width(env->prog->len ? env->prog->len - 1 : 0);
int i;
for (i = 0; i < BPF_DIAG_CONTEXT_CNT; i++) {
int row = i - BPF_DIAG_CONTEXT;
format_disasm_line(env, insn_idx + row, &disasm_lines[i]);
}
diag_write(env, " Instruction context:\n");
for (i = 0; i < BPF_DIAG_CONTEXT_CNT; i++) {
struct disasm_line *line = &disasm_lines[i];
if (line->valid)
diag_write(env, " %s%*d | %s\n",
line->idx == insn_idx ? ">>> " : " ",
insn_width, line->idx, line->text);
}
}
static __printf(4, 5) void bpf_diag_source(struct bpf_verifier_env *env,
u32 insn_idx, const char *label,
const char *fmt, ...)
{
struct bpf_diag_scratch *scratch;
struct bpf_linfo_source *source_lines;
struct disasm_line *disasm_lines;
struct bpf_linfo_source src = {};
struct diag_fmt_mark mark;
const struct bpf_line_info *linfo;
const struct bpf_subprog_info *subprog;
struct btf *btf = env->prog->aux->btf;
char *source_lane;
const char *msg;
const char *func;
int start_line, end_line, width, indent, subprogno, linfo_start, linfo_end, i;
va_list args;
if (!bpf_diag_enabled(env))
return;
if (!env->diag)
return;
mark = diag_fmt_save(env);
label = label ?: "note";
scratch = &env->diag->scratch;
source_lines = scratch->source_lines;
disasm_lines = scratch->disasm_lines;
memset(source_lines, 0, sizeof(scratch->source_lines));
memset(disasm_lines, 0, sizeof(scratch->disasm_lines));
va_start(args, fmt);
msg = bpf_diag_vfmt(env, fmt, args);
va_end(args);
if (!*msg)
msg = "<failed to allocate diagnostic text>";
linfo = bpf_find_linfo(env->prog, insn_idx);
if (btf && linfo)
bpf_get_linfo_source(btf, linfo, &src);
if (!src.file || !*src.file) {
diag_write(env, " insn %u\n", insn_idx);
goto out_annotation;
}
subprog = bpf_find_containing_subprog(env, insn_idx);
subprogno = subprog ? subprog - env->subprog_info : -ENOENT;
func = subprogno >= 0 ? bpf_subprog_name(env, subprogno) : NULL;
if (func && *func)
diag_write(env, " %s @ %s:%d:%d\n", func, src.file, src.line_num, src.line_col);
else
diag_write(env, " %s:%d:%d\n", src.file, src.line_num, src.line_col);
if (!src.line || !*src.line)
goto out_annotation;
start_line = src.line_num - BPF_DIAG_CONTEXT;
end_line = src.line_num + BPF_DIAG_CONTEXT;
width = diag_line_width(end_line);
indent = diag_line_indent(src.line);
for (i = 0; i < BPF_DIAG_CONTEXT_CNT; i++)
source_lines[i].line_num = start_line + i;
linfo = env->prog->aux->linfo;
linfo_start = subprog ? subprog->linfo_idx : 0;
linfo_end = subprogno >= 0 && subprogno + 1 < env->subprog_cnt ?
env->subprog_info[subprogno + 1].linfo_idx : env->prog->aux->nr_linfo;
for (i = linfo_start; i < linfo_end; i++) {
struct bpf_linfo_source line_src;
int idx;
bpf_get_linfo_source(btf, &linfo[i], &line_src);
if (line_src.file_name_off != src.file_name_off ||
line_src.line_num < start_line || line_src.line_num > end_line ||
!line_src.line || !*line_src.line)
continue;
idx = line_src.line_num - start_line;
if (!source_lines[idx].line)
source_lines[idx] = line_src;
}
diag_write(env, " Source context:\n");
source_lane = bpf_diag_fmt_buf(env, BPF_DIAG_FMT_BUF_SIZE);
if (!source_lane)
goto out_restore;
for (i = 0; i < BPF_DIAG_CONTEXT_CNT; i++) {
const char *source_prefix;
source_prefix = source_lines[i].line_num == src.line_num ? ">>> " : " ";
diag_format_source_lane(source_lane, BPF_DIAG_FMT_BUF_SIZE, source_prefix, width,
source_lines[i].line_num, source_lines[i].line);
diag_write(env, " %s\n", source_lane);
if (source_lines[i].line_num == src.line_num)
diag_print_source_annotation(env, width, indent, label, msg);
}
diag_print_insn_context(env, insn_idx, disasm_lines);
goto out_restore;
out_annotation:
diag_print_source_annotation(env, 0, 0, label, msg);
diag_print_insn_context(env, insn_idx, disasm_lines);
out_restore:
diag_fmt_restore(env, mark);
}
static const struct bpf_func_state *diag_current_frame(const struct bpf_verifier_env *env)
{
return env->cur_state->frame[env->cur_state->curframe];
}
void bpf_diag_register_type(struct bpf_verifier_env *env, u32 insn_idx, int regno,
const char *problem, const char *reason, const char *suggestion)
{
const struct bpf_func_state *frame = diag_current_frame(env);
struct bpf_diag_history_opts opts = {
.scope = BPF_DIAG_HISTORY_SCOPE_REG,
.frame_id = frame->diag_frame_id,
.frameno = frame->frameno,
.regno = regno,
};
bpf_diag_header(env, REGISTER_TYPE_SAFETY, problem);
diag_reason(env, "%s", reason);
diag_section(env, "At");
bpf_diag_source(env, insn_idx, "error", "%s", problem);
if (regno >= 0)
diag_print_history(env, &opts);
diag_suggestion(env, "%s", suggestion);
}
const char *bpf_diag_reg_type_plain(struct bpf_verifier_env *env, enum bpf_reg_type type)
{
switch (base_type(type)) {
case NOT_INIT:
return "an uninitialized value";
case SCALAR_VALUE:
return "an integer scalar";
case PTR_TO_CTX:
return "a context pointer";
case PTR_TO_STACK:
return "a stack pointer";
case PTR_TO_MAP_VALUE:
if (type_may_be_null(type))
return "a nullable map value pointer";
return "a map value pointer";
case PTR_TO_MEM:
if (type_may_be_null(type))
return "a nullable memory pointer";
return "a memory pointer";
case PTR_TO_BTF_ID:
if (type_may_be_null(type))
return "a nullable kernel object pointer";
if (type_is_non_owning_ref(type))
return "a borrowed allocated object pointer";
if (type_is_ptr_alloc_obj(type))
return "an owned allocated object pointer";
if (type_flag(type) & PTR_UNTRUSTED)
return "an untrusted kernel object pointer";
return "a kernel object pointer";
default:
return reg_type_str(env, type);
}
}
const char *bpf_diag_arg_type_plain(enum bpf_arg_type type)
{
switch (base_type(type)) {
case ARG_MEM_SIZE:
case ARG_CONST_MEM_SIZE:
return "an integer scalar length for this memory argument";
case ARG_PTR_TO_CTX:
return "the original program context pointer or preserve it before modifying registers";
case ARG_SCALAR:
case ARG_CONST_SCALAR:
case ARG_CONST_ALLOC_SIZE_OR_ZERO:
return "an integer scalar value for this argument, not a pointer or resource object";
case ARG_PTR_TO_CONST_STR:
return "a constant string pointer that the verifier recognizes, such as a string stored in a read-only map value";
case ARG_PTR_TO_DYNPTR:
return "the address of a stack dynptr object, or use a const dynptr pointer returned by the verifier-supported path";
case ARG_PTR_TO_ALLOC_BTF_ID:
return "a pointer returned by the matching BPF object allocation path";
case ARG_PTR_TO_REFCOUNTED_KPTR:
return "an owning or non-owning pointer to a BPF-managed object containing a bpf_refcount field";
case ARG_PTR_TO_ITER:
return "the address of a stack iterator object for iterator new, next, and destroy calls";
case ARG_PTR_TO_IRQ_FLAG:
return "the same stack slot used by bpf_local_irq_save() or bpf_res_spin_lock_irqsave()";
case ARG_PTR_TO_CTX_OUT:
return "the attach hook's own output argument, loaded directly from the program context";
default:
return "a value with one of the accepted pointer or scalar types for this call";
}
}
static const char *diag_arg_ordinal(int argno)
{
switch (argno) {
case 1:
return "first";
case 2:
return "second";
case 3:
return "third";
case 4: