1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <linux/bpf.h> 4 #include <bpf/bpf_helpers.h> 5 #include "bpf_misc.h" 6 7 #if defined(__TARGET_ARCH_x86) && __clang_major__ >= 18 8 9 SEC("socket") 10 __description("LDSX, S8") 11 __success __success_unpriv __retval(-2) 12 __naked void ldsx_s8(void) 13 { 14 asm volatile (" \ 15 r1 = 0x3fe; \ 16 *(u64 *)(r10 - 8) = r1; \ 17 r0 = *(s8 *)(r10 - 8); \ 18 exit; \ 19 " ::: __clobber_all); 20 } 21 22 SEC("socket") 23 __description("LDSX, S16") 24 __success __success_unpriv __retval(-2) 25 __naked void ldsx_s16(void) 26 { 27 asm volatile (" \ 28 r1 = 0x3fffe; \ 29 *(u64 *)(r10 - 8) = r1; \ 30 r0 = *(s16 *)(r10 - 8); \ 31 exit; \ 32 " ::: __clobber_all); 33 } 34 35 SEC("socket") 36 __description("LDSX, S32") 37 __success __success_unpriv __retval(-1) 38 __naked void ldsx_s32(void) 39 { 40 asm volatile (" \ 41 r1 = 0xfffffffe; \ 42 *(u64 *)(r10 - 8) = r1; \ 43 r0 = *(s32 *)(r10 - 8); \ 44 r0 >>= 1; \ 45 exit; \ 46 " ::: __clobber_all); 47 } 48 49 SEC("socket") 50 __description("LDSX, S8 range checking, privileged") 51 __log_level(2) __success __retval(1) 52 __msg("R1_w=scalar(smin=-128,smax=127)") 53 __naked void ldsx_s8_range_priv(void) 54 { 55 asm volatile (" \ 56 call %[bpf_get_prandom_u32]; \ 57 *(u64 *)(r10 - 8) = r0; \ 58 r1 = *(s8 *)(r10 - 8); \ 59 /* r1 with s8 range */ \ 60 if r1 s> 0x7f goto l0_%=; \ 61 if r1 s< -0x80 goto l0_%=; \ 62 r0 = 1; \ 63 l1_%=: \ 64 exit; \ 65 l0_%=: \ 66 r0 = 2; \ 67 goto l1_%=; \ 68 " : 69 : __imm(bpf_get_prandom_u32) 70 : __clobber_all); 71 } 72 73 SEC("socket") 74 __description("LDSX, S16 range checking") 75 __success __success_unpriv __retval(1) 76 __naked void ldsx_s16_range(void) 77 { 78 asm volatile (" \ 79 call %[bpf_get_prandom_u32]; \ 80 *(u64 *)(r10 - 8) = r0; \ 81 r1 = *(s16 *)(r10 - 8); \ 82 /* r1 with s16 range */ \ 83 if r1 s> 0x7fff goto l0_%=; \ 84 if r1 s< -0x8000 goto l0_%=; \ 85 r0 = 1; \ 86 l1_%=: \ 87 exit; \ 88 l0_%=: \ 89 r0 = 2; \ 90 goto l1_%=; \ 91 " : 92 : __imm(bpf_get_prandom_u32) 93 : __clobber_all); 94 } 95 96 SEC("socket") 97 __description("LDSX, S32 range checking") 98 __success __success_unpriv __retval(1) 99 __naked void ldsx_s32_range(void) 100 { 101 asm volatile (" \ 102 call %[bpf_get_prandom_u32]; \ 103 *(u64 *)(r10 - 8) = r0; \ 104 r1 = *(s32 *)(r10 - 8); \ 105 /* r1 with s16 range */ \ 106 if r1 s> 0x7fffFFFF goto l0_%=; \ 107 if r1 s< -0x80000000 goto l0_%=; \ 108 r0 = 1; \ 109 l1_%=: \ 110 exit; \ 111 l0_%=: \ 112 r0 = 2; \ 113 goto l1_%=; \ 114 " : 115 : __imm(bpf_get_prandom_u32) 116 : __clobber_all); 117 } 118 119 #else 120 121 SEC("socket") 122 __description("cpuv4 is not supported by compiler or jit, use a dummy test") 123 __success 124 int dummy_test(void) 125 { 126 return 0; 127 } 128 129 #endif 130 131 char _license[] SEC("license") = "GPL"; 132