summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/verifier_callx_rodata.c
blob: 6cf58ca9ac5bb6d9c9be4ffa8b753e4af9a9bb1c (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0
/* Tests for callx through pointers to functions in read-only data */

#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
#include "../../../include/linux/filter.h"

/* gcc doesn't support indirect calls */
#if defined(__clang__) && (defined(__TARGET_ARCH_x86) || defined(__TARGET_ARCH_arm64) || \
    (defined(__TARGET_ARCH_riscv) && __riscv_xlen == 64))

/*
 * Read-only data with pointers to functions, where the compiler puts tables
 * of functions, structures of operations and vtables. libbpf resolves
 * a pointer to the offset of the function in the program and the kernel
 * recognizes it by that value.
 */
#define DATA(SECTION, NAME, ...)				\
	".pushsection " SECTION ",@progbits;"			\
	".balign 8;"						\
	#NAME "_%=:"						\
	__VA_ARGS__						\
	".type " #NAME "_%=, @object;"				\
	".size " #NAME "_%=, .-" #NAME "_%=;"			\
	".popsection;"

#define RODATA(NAME, ...) DATA(".rodata,\"a\"", NAME, __VA_ARGS__)

#define FUNC_TABLE2(NAME, F0, F1) RODATA(NAME, ".quad " #F0 "; .quad " #F1 ";")

__naked __noinline __used
static unsigned long add1(void)
{
	asm volatile (
		"r0 = r1;"
		"r0 += 1;"
		"exit;"
	);
}

__naked __noinline __used
static unsigned long add2(void)
{
	asm volatile (
		"r0 = r1;"
		"r0 += 2;"
		"exit;"
	);
}

/* the second element of the table is called */
SEC("socket")
__success __retval(12)
__naked void callx_rodata_const_index(void)
{
	asm volatile (
		FUNC_TABLE2(tbl, add1, add2)
		"r2 = tbl_%= ll;"
		"r2 = *(u64 *)(r2 + 8);"
		"r1 = 10;"
		"callx r2;"
		"exit;"
		::: __clobber_all);
}

/*
 * The program reads the address of a function from the data, so pointers to
 * functions in data are recognized only for programs that may leak pointers.
 * Otherwise nothing refers to the functions.
 */
SEC("socket")
__success __retval(12)
__failure_unpriv __msg_unpriv("unreachable insn")
__caps_unpriv(CAP_BPF)
__naked void callx_rodata_needs_perfmon(void)
{
	asm volatile (
		FUNC_TABLE2(tbl, add1, add2)
		"r2 = tbl_%= ll;"
		"r2 = *(u64 *)(r2 + 8);"
		"r1 = 10;"
		"callx r2;"
		"exit;"
		::: __clobber_all);
}

/* the address of an element is used instead of the address of the table */
SEC("socket")
__success __retval(12)
__naked void callx_rodata_elem_addr(void)
{
	asm volatile (
		FUNC_TABLE2(tbl, add1, add2)
		"r2 = tbl_%= + 8 ll;"
		"r2 = *(u64 *)(r2 + 0);"
		"r1 = 10;"
		"callx r2;"
		"exit;"
		::: __clobber_all);
}

/* pointers to functions are mixed with other data like in a vtable */
SEC("socket")
__success __retval(23)
__log_level(2)
__msg("r1 = *(u64 *)(r6 +0)          ; R1=7")
__msg("r2 = *(u64 *)(r6 +8)          ; R2=func()")
__naked void callx_rodata_mixed_with_data(void)
{
	asm volatile (
		RODATA(vt, ".quad 7; .quad add1; .quad 13; .quad add2;")
		"r6 = vt_%= ll;"
		"r1 = *(u64 *)(r6 + 0);"
		"r2 = *(u64 *)(r6 + 8);"
		/* add1(7) */
		"callx r2;"
		"r7 = r0;"
		"r1 = *(u64 *)(r6 + 16);"
		"r2 = *(u64 *)(r6 + 24);"
		/* add2(13) */
		"callx r2;"
		"r0 += r7;"
		"exit;"
		::: __clobber_all);
}

/*
 * Position independent code keeps constants with pointers in .data.rel.ro.
 * libbpf treats it as read-only data.
 */
SEC("socket")
__success __retval(23)
__naked void callx_data_rel_ro(void)
{
	asm volatile (
		DATA(".data.rel.ro,\"aw\"", vt, ".quad 7; .quad add1; .quad 13; .quad add2;")
		"r6 = vt_%= ll;"
		"r1 = *(u64 *)(r6 + 0);"
		"r2 = *(u64 *)(r6 + 8);"
		"callx r2;"
		"r7 = r0;"
		"r1 = *(u64 *)(r6 + 16);"
		"r2 = *(u64 *)(r6 + 24);"
		"callx r2;"
		"r0 += r7;"
		"exit;"
		::: __clobber_all);
}

/* an array of structures: the index selects one of the functions, not the data */
SEC("socket")
__success __retval(11)
__naked void callx_rodata_array_of_structs(void)
{
	asm volatile (
		RODATA(arr, ".quad add1; .quad 0x1111; .quad add2; .quad 0x2222;")
		"call %[bpf_get_prandom_u32];"
		"r6 = r0;"
		"r6 &= 1;"
		"r3 = r6;"
		"r3 <<= 4;"
		"r2 = arr_%= ll;"
		"r2 += r3;"
		"r2 = *(u64 *)(r2 + 0);"
		"r1 = 10;"
		"callx r2;"
		/* add1(10) - 0 or add2(10) - 1 */
		"r0 -= r6;"
		"exit;"
		:
		: __imm(bpf_get_prandom_u32)
		: __clobber_all);
}

/* and the data of the element that the index selects can be read */
SEC("socket")
__success __retval(16)
__naked void callx_rodata_array_of_structs_data(void)
{
	asm volatile (
		RODATA(arr, ".quad add1; .quad 5; .quad add2; .quad 5;")
		"call %[bpf_get_prandom_u32];"
		"r6 = r0;"
		"r6 &= 1;"
		"r3 = r6;"
		"r3 <<= 4;"
		"r7 = arr_%= ll;"
		"r7 += r3;"
		"r2 = *(u64 *)(r7 + 0);"
		"r1 = 10;"
		"callx r2;"
		/* add1(10) - 0 or add2(10) - 1 */
		"r0 -= r6;"
		"r1 = *(u64 *)(r7 + 8);"
		"r0 += r1;"
		"exit;"
		:
		: __imm(bpf_get_prandom_u32)
		: __clobber_all);
}

/* but not the data and the pointer with the same load */
SEC("socket")
__failure __msg("overlaps with a pointer to a function")
__naked void callx_rodata_array_of_structs_any(void)
{
	asm volatile (
		RODATA(arr, ".quad add1; .quad 5; .quad add2; .quad 5;")
		"call %[bpf_get_prandom_u32];"
		"r0 &= 3;"
		"r0 <<= 3;"
		"r2 = arr_%= ll;"
		"r2 += r0;"
		"r2 = *(u64 *)(r2 + 0);"
		"r1 = 10;"
		"callx r2;"
		"exit;"
		:
		: __imm(bpf_get_prandom_u32)
		: __clobber_all);
}

/* dynamic dispatch: which vtable is used is not known until run time */
SEC("socket")
__success __retval(2)
__naked void callx_rodata_two_vtables(void)
{
	asm volatile (
		RODATA(vt_a, ".quad 1; .quad add1;")
		RODATA(vt_b, ".quad 0; .quad add2;")
		"call %[bpf_get_prandom_u32];"
		"r6 = vt_a_%= ll;"
		"r0 &= 1;"
		"if r0 == 0 goto +2;"
		"r6 = vt_b_%= ll;"
		"r1 = *(u64 *)(r6 + 0);"
		"r2 = *(u64 *)(r6 + 8);"
		/* add1(1) or add2(0) */
		"callx r2;"
		"exit;"
		:
		: __imm(bpf_get_prandom_u32)
		: __clobber_all);
}

/* both functions are verified, either of them is called */
SEC("socket")
__success __retval(11)
__naked void callx_rodata_var_index(void)
{
	asm volatile (
		FUNC_TABLE2(tbl, add1, add2)
		"call %[bpf_get_prandom_u32];"
		"r6 = r0;"
		"r6 &= 1;"
		"r3 = r6;"
		"r3 <<= 3;"
		"r2 = tbl_%= ll;"
		"r2 += r3;"
		"r2 = *(u64 *)(r2 + 0);"
		"r1 = 10;"
		"callx r2;"
		/* add1(10) - 0 or add2(10) - 1 */
		"r0 -= r6;"
		"exit;"
		:
		: __imm(bpf_get_prandom_u32)
		: __clobber_all);
}

/* a table that has no symbol, the compiler generates such for a switch statement */
SEC("socket")
__success __retval(11)
__naked void callx_rodata_no_symbol(void)
{
	asm volatile (
		".pushsection .rodata,\"a\",@progbits;"
		".balign 8;"
	".Lanon_%=:"
		".quad add1;"
		".quad add2;"
		".popsection;"
		"call %[bpf_get_prandom_u32];"
		"r6 = r0;"
		"r6 &= 1;"
		"r3 = r6;"
		"r3 <<= 3;"
		"r2 = .Lanon_%= ll;"
		"r2 += r3;"
		"r2 = *(u64 *)(r2 + 0);"
		"r1 = 10;"
		"callx r2;"
		"r0 -= r6;"
		"exit;"
		:
		: __imm(bpf_get_prandom_u32)
		: __clobber_all);
}

/* the first instruction of the function is removed by the verifier */
__naked __noinline __used
static unsigned long nop_add1(void)
{
	asm volatile (
		"goto +0;"
		"r0 = r1;"
		"r0 += 1;"
		"exit;"
	);
}

/* the pointer follows the function when instructions are removed */
SEC("socket")
__success __retval(11)
__naked void callx_rodata_func_starts_with_nop(void)
{
	asm volatile (
		RODATA(tbl, ".quad nop_add1; .quad 0;")
		"r2 = tbl_%= ll;"
		"r2 = *(u64 *)(r2 + 0);"
		"r1 = 10;"
		"callx r2;"
		"exit;"
		::: __clobber_all);
}

/* can't be called with a scalar in r1 */
__naked __noinline __used
static unsigned long deref_r1(void)
{
	asm volatile (
		"r0 = *(u64 *)(r1 + 0);"
		"exit;"
	);
}

__naked __noinline __used
static unsigned long ret0(void)
{
	asm volatile (
		"r0 = 0;"
		"exit;"
	);
}

/* every function that might be called is verified */
SEC("socket")
__failure __msg("R1 invalid mem access 'scalar'")
__naked void callx_rodata_all_callees_verified(void)
{
	asm volatile (
		FUNC_TABLE2(tbl, ret0, deref_r1)
		"call %[bpf_get_prandom_u32];"
		"r0 &= 1;"
		"r0 <<= 3;"
		"r2 = tbl_%= ll;"
		"r2 += r0;"
		"r2 = *(u64 *)(r2 + 0);"
		"r1 = 0;"
		"callx r2;"
		"exit;"
		:
		: __imm(bpf_get_prandom_u32)
		: __clobber_all);
}

/* a function that is never called is not verified, it's dead code */
SEC("socket")
__success __retval(0)
__naked void callx_rodata_unused_callee(void)
{
	asm volatile (
		FUNC_TABLE2(tbl, ret0, deref_r1)
		"r2 = tbl_%= ll;"
		"r2 = *(u64 *)(r2 + 0);"
		"r1 = 0;"
		"callx r2;"
		"exit;"
		::: __clobber_all);
}

/* a pointer that is read and never called doesn't keep the function, it's NULL */
SEC("socket")
__success __retval(1)
__naked void callx_rodata_read_unused_callee(void)
{
	asm volatile (
		FUNC_TABLE2(tbl, ret0, deref_r1)
		"r6 = tbl_%= ll;"
		"r2 = *(u64 *)(r6 + 0);"
		"r1 = 0;"
		"callx r2;"
		"r2 = *(u64 *)(r6 + 8);"
		"if r2 != 0 goto +1;"
		"r0 = 1;"
		"exit;"
		::: __clobber_all);
}

/* the index may select what is not a pointer to a function */
SEC("socket")
__failure __msg("overlaps with a pointer to a function")
__naked void callx_rodata_index_beyond_table(void)
{
	asm volatile (
		RODATA(tbl, ".quad add1; .quad add2; .quad 0x1234; .quad 0x5678;")
		"call %[bpf_get_prandom_u32];"
		"r0 &= 3;"
		"r0 <<= 3;"
		"r2 = tbl_%= ll;"
		"r2 += r0;"
		"r2 = *(u64 *)(r2 + 0);"
		"r1 = 10;"
		"callx r2;"
		"exit;"
		:
		: __imm(bpf_get_prandom_u32)
		: __clobber_all);
}

/* the address of a function is not known until the program is jitted */
SEC("socket")
__failure __msg("read of 4 bytes at offset")
__msg("overlaps with a pointer to a function")
__naked void callx_rodata_partial_read(void)
{
	asm volatile (
		FUNC_TABLE2(tbl, add1, add2)
		"r6 = tbl_%= ll;"
		"r2 = *(u64 *)(r6 + 8);"
		"r1 = 10;"
		"callx r2;"
		"r0 = *(u32 *)(r6 + 0);"
		"exit;"
		::: __clobber_all);
}

SEC("socket")
__failure __msg("read of 8 bytes at offset")
__msg("overlaps with a pointer to a function")
__naked void callx_rodata_misaligned_read(void)
{
	asm volatile (
		RODATA(tbl, ".quad add1; .quad add2; .quad 0;")
		"r6 = tbl_%= ll;"
		"r2 = *(u64 *)(r6 + 0);"
		"r1 = 10;"
		"callx r2;"
		"r0 = *(u64 *)(r6 + 4);"
		"exit;"
		::: __clobber_all);
}

/*
 * The kernel doesn't look for pointers to functions in the data of a program
 * that doesn't have callx. It's a number there.
 */
SEC("socket")
__success
__naked void callx_rodata_no_callx(void)
{
	asm volatile (
		FUNC_TABLE2(tbl, add1, add2)
		"r2 = tbl_%= ll;"
		"r0 = *(u32 *)(r2 + 0);"
		"r0 = 0;"
		"exit;"
		::: __clobber_all);
}

/* the data next to a pointer is still known to the verifier */
SEC("socket")
__success __retval(0x1234)
__log_level(2)
__msg("R0=4660")
__naked void callx_rodata_data_is_const(void)
{
	asm volatile (
		RODATA(tbl, ".quad add1; .quad 0x1234;")
		"r6 = tbl_%= ll;"
		"r2 = *(u64 *)(r6 + 0);"
		"r1 = 10;"
		"callx r2;"
		"r0 = *(u64 *)(r6 + 8);"
		"exit;"
		::: __clobber_all);
}

/*
 * The map has the offset of the function until the program is jitted.
 * bpf_compute_const_regs() must not take it for a constant, otherwise
 * bpf_prune_dead_branches() turns the branch below into the wrong one.
 */
SEC("socket")
__success __retval(11)
__naked void callx_rodata_ptr_is_not_const(void)
{
	asm volatile (
		FUNC_TABLE2(tbl, add1, add2)
		"r2 = tbl_%= ll;"
		"r2 = *(u64 *)(r2 + 0);"
		"r0 = 0;"
		/* the offset is less, the address is greater */
		"if r2 < 0x100000 goto 1f;"
		"r1 = 10;"
		"callx r2;"
	"1:"
		"exit;"
		::: __clobber_all);
}

/* the pointer read from the data can't be modified */
SEC("socket")
__failure __msg("dereference of modified func ptr R2 off=8 disallowed")
__naked void callx_rodata_modified_ptr(void)
{
	asm volatile (
		FUNC_TABLE2(tbl, add1, add2)
		"r2 = tbl_%= ll;"
		"r2 = *(u64 *)(r2 + 0);"
		"r2 += 8;"
		"r1 = 10;"
		"callx r2;"
		"exit;"
		::: __clobber_all);
}

__noinline __used
int global_add3(int x)
{
	return x + 3;
}

/*
 * A pointer to a global function is not recognized, it's a number. The function
 * is called directly as well, so that libbpf adds it to the program and stores
 * its offset into the table.
 */
SEC("socket")
__failure __msg("R2 has type scalar, expected func")
__naked void callx_rodata_global_func(void)
{
	asm volatile (
		FUNC_TABLE2(tbl, add1, global_add3)
		"r1 = 1;"
		"call global_add3;"
		"r2 = tbl_%= ll;"
		"r2 = *(u64 *)(r2 + 8);"
		"r1 = 10;"
		"callx r2;"
		"exit;"
		::: __clobber_all);
}

/* selfcall(n) { return n ? tbl[0](n - 1) : 0; }, where tbl[0] == selfcall */
__naked __noinline __used
static unsigned long selfcall(void)
{
	asm volatile (
		FUNC_TABLE2(tbl, selfcall, ret0)
		"r0 = 0;"
		"if r1 == 0 goto 1f;"
		"r1 += -1;"
		"r2 = tbl_%= ll;"
		"r2 = *(u64 *)(r2 + 0);"
		"callx r2;"
	"1:"
		"exit;"
		::: __clobber_all);
}

SEC("socket")
__failure __msg("recursive call from selfcall() to selfcall()")
__naked void callx_rodata_recursion(void)
{
	asm volatile (
		"r1 = 2;"
		"call selfcall;"
		"exit;"
		::: __clobber_all);
}


/* Four 480-byte frames, deeper than any budget together with their caller */
__naked __noinline __used
static unsigned long use_stack_480_0(void)
{
	asm volatile (
		"r0 = 0;"
		"*(u64 *)(r10 - 480) = r0;"
		"exit;"
	);
}

__naked __noinline __used
static unsigned long use_stack_480_1(void)
{
	asm volatile (
		"r0 = 0;"
		"*(u64 *)(r10 - 480) = r0;"
		"call use_stack_480_0;"
		"exit;"
	);
}

__naked __noinline __used
static unsigned long use_stack_480_2(void)
{
	asm volatile (
		"r0 = 0;"
		"*(u64 *)(r10 - 480) = r0;"
		"call use_stack_480_1;"
		"exit;"
	);
}

__naked __noinline __used
static unsigned long use_stack_480_3(void)
{
	asm volatile (
		"r0 = 0;"
		"*(u64 *)(r10 - 480) = r0;"
		"call use_stack_480_2;"
		"exit;"
	);
}

/* stack of all possible callees is accounted */
SEC("socket")
__failure __msg("combined stack size of {{[0-9]+}} calls is")
__naked void callx_rodata_stack_depth(void)
{
	asm volatile (
		FUNC_TABLE2(tbl, ret0, use_stack_480_3)
		"r0 = 0;"
		"*(u64 *)(r10 - 304) = r0;"
		"call %[bpf_get_prandom_u32];"
		"r0 &= 1;"
		"r0 <<= 3;"
		"r2 = tbl_%= ll;"
		"r2 += r0;"
		"r2 = *(u64 *)(r2 + 0);"
		"callx r2;"
		"exit;"
		:
		: __imm(bpf_get_prandom_u32)
		: __clobber_all);
}

/* stack of a callback that is read from the data is accounted too */
SEC("socket")
__failure __msg("combined stack size of {{[0-9]+}} calls is")
__naked void callx_rodata_callback_stack_depth(void)
{
	asm volatile (
		FUNC_TABLE2(tbl, use_stack_480_3, ret0)
		"r0 = 0;"
		"*(u64 *)(r10 - 304) = r0;"
		"r6 = tbl_%= ll;"
		"r1 = 1;"
		"r2 = *(u64 *)(r6 + 0);"
		"r3 = 0;"
		"r4 = 0;"
		"call %[bpf_loop];"
		"r2 = *(u64 *)(r6 + 8);"
		"r1 = 0;"
		"callx r2;"
		"exit;"
		:
		: __imm(bpf_loop)
		: __clobber_all);
}

static __noinline __used int pull_data(struct __sk_buff *skb)
{
	return bpf_skb_pull_data(skb, 0);
}

static __noinline __used int calls_pull_data(struct __sk_buff *skb)
{
	return pull_data(skb) + 1;
}

__noinline __used int global_calls_pull_data(struct __sk_buff *skb)
{
	return calls_pull_data(skb) + 2;
}

/*
 * check_cfg() explores the functions that the data points to when it sees
 * the address of the data. It must not lose the effects of the functions
 * that call each other: global_calls_pull_data() changes packet data.
 */
SEC("tc")
__failure __msg("R7 invalid mem access 'scalar'")
__naked void callx_rodata_callee_effects(void)
{
	asm volatile (
		FUNC_TABLE2(tbl, pull_data, calls_pull_data)
		"r6 = r1;"
		"r7 = *(u32 *)(r6 + %[data]);"
		"r8 = *(u32 *)(r6 + %[data_end]);"
		"r2 = r7;"
		"r2 += 8;"
		"if r2 > r8 goto 1f;"
		"r1 = r6;"
		"call global_calls_pull_data;"
		"r0 = *(u8 *)(r7 + 0);"
	"1:"
		"r9 = tbl_%= ll;"
		"r2 = *(u64 *)(r9 + 8);"
		"r1 = r6;"
		"callx r2;"
		"r0 = 0;"
		"exit;"
		:
		: __imm_const(data, offsetof(struct __sk_buff, data)),
		  __imm_const(data_end, offsetof(struct __sk_buff, data_end))
		: __clobber_all);
}

/* pointers to functions in C */

typedef int (*op_fn)(int);

#define DEFINE_OP(N) static __noinline int op##N(int x) { return x * (N + 2) + N; }

DEFINE_OP(0)  DEFINE_OP(1)  DEFINE_OP(2)  DEFINE_OP(3)
DEFINE_OP(4)  DEFINE_OP(5)  DEFINE_OP(6)  DEFINE_OP(7)
DEFINE_OP(8)  DEFINE_OP(9)  DEFINE_OP(10) DEFINE_OP(11)
DEFINE_OP(12) DEFINE_OP(13) DEFINE_OP(14) DEFINE_OP(15)

static op_fn const ops[] = {
	op0, op1, op2,  op3,  op4,  op5,  op6,  op7,
	op8, op9, op10, op11, op12, op13, op14, op15,
};

int op_idx = 11;

SEC("socket")
__success __retval(50)
int callx_c_table(void *ctx)
{
	unsigned int i = op_idx;

	if (i >= sizeof(ops) / sizeof(ops[0]))
		return -1;
	/* op11(3) = 3 * 13 + 11 */
	return ops[i](3);
}

SEC("socket")
__success __retval(50)
int callx_c_table_null_check(void *ctx)
{
	unsigned int i = op_idx;
	op_fn op;

	if (i >= sizeof(ops) / sizeof(ops[0]))
		return -1;
	/* the address of a function is not known until the program is jitted */
	op = ops[i];
	if (!op)
		return -2;
	return op(3);
}

/* a structure of operations, where pointers to functions are mixed with data */
struct shape_ops {
	int id;
	op_fn area;
	long scale;
	op_fn perimeter;
};

static const struct shape_ops square_ops = { 1, op1, 10, op2 };
static const struct shape_ops circle_ops = { 2, op3, 20, op1 };

static __noinline int use_shape(const struct shape_ops *ops, int x)
{
	return ops->area(x) * ops->scale + ops->perimeter(ops->id);
}

SEC("socket")
__success __retval(433)
int callx_c_ops_mixed_with_data(void *ctx)
{
	/*
	 * square: op1(2) * 10 + op2(1) = 7 * 10 + 6 = 76
	 * circle: op3(3) * 20 + op1(2) = 18 * 20 + 7 = 367
	 * minus 10, since op_idx is 11
	 */
	return use_shape(&square_ops, 2) + use_shape(&circle_ops, 3) - (op_idx == 11 ? 10 : 0);
}

/* the ops are selected at run time */
SEC("socket")
__success __retval(367)
int callx_c_ops_selected(void *ctx)
{
	const struct shape_ops *ops = op_idx == 11 ? &circle_ops : &square_ops;

	return use_shape(ops, 3);
}

/*
 * clang-22 with -mcpu=v4 drops the lookup table it builds for this switch
 * ("Undefined temporary symbol .Lswitch.table.call_by_switch").
 * Fixed in clang-23 by llvm commit 9fc458a1d95b.
 *
 * https://github.com/llvm/llvm-project/commit/9fc458a1d95b
 */
#if __clang_major__ != 22 || __BPF_CPU_VERSION__ < 4
/* the compiler might turn the switch into a table that has no symbol */
static __noinline int call_by_switch(unsigned int idx, int x)
{
	op_fn op;

	switch (idx) {
	case 0:
		op = op8;
		break;
	case 1:
		op = op9;
		break;
	case 2:
		op = op10;
		break;
	case 3:
		op = op11;
		break;
	case 4:
		op = op12;
		break;
	case 5:
		op = op13;
		break;
	case 6:
		op = op14;
		break;
	case 7:
		op = op15;
		break;
	default:
		return -1;
	}
	return op(x);
}

SEC("socket")
__success __retval(50)
int callx_c_switch_table(void *ctx)
{
	/* op11(3) = 3 * 13 + 11 */
	return call_by_switch(op_idx - 8, 3);
}

#else

SEC("socket")
__skip("clang-22 bug with -mcpu=v4, switch table test cannot be built")
__success
int callx_c_switch_table(void *ctx)
{
	return 0;
}

#endif

/*
 * Misaligned pointers to functions are ignored, the rest of the data is
 * accessible as before. The program doesn't have callx, so libbpf doesn't
 * append the functions that .rodata points to and doesn't make a copy of
 * .rodata for it.
 */
static const struct {
	char tag;
	op_fn op;
} __attribute__((packed)) packed_ops[] = {
	{ 5, op0 },
	{ 7, op1 },
};

SEC("socket")
__success __retval(7)
int callx_c_packed_struct(void *ctx)
{
	return packed_ops[op_idx & 1].tag;
}

#else

SEC("socket")
__success
int dummy(void *ctx)
{
	return 0;
}

#endif

char _license[] SEC("license") = "GPL";