at v6.7 135 lines 4.3 kB view raw
1// SPDX-License-Identifier: GPL-2.0 2#include <vmlinux.h> 3#include <limits.h> 4#include <bpf/bpf_tracing.h> 5#include <bpf/bpf_helpers.h> 6#include <bpf/bpf_core_read.h> 7#include <bpf/bpf_endian.h> 8#include "bpf_misc.h" 9#include "bpf_experimental.h" 10 11#define check_assert(type, op, name, value) \ 12 SEC("?tc") \ 13 __log_level(2) __failure \ 14 int check_assert_##op##_##name(void *ctx) \ 15 { \ 16 type num = bpf_ktime_get_ns(); \ 17 bpf_assert_##op(num, value); \ 18 return *(u64 *)num; \ 19 } 20 21__msg(": R0_w=-2147483648 R10=fp0") 22check_assert(s64, eq, int_min, INT_MIN); 23__msg(": R0_w=2147483647 R10=fp0") 24check_assert(s64, eq, int_max, INT_MAX); 25__msg(": R0_w=0 R10=fp0") 26check_assert(s64, eq, zero, 0); 27__msg(": R0_w=-9223372036854775808 R1_w=-9223372036854775808 R10=fp0") 28check_assert(s64, eq, llong_min, LLONG_MIN); 29__msg(": R0_w=9223372036854775807 R1_w=9223372036854775807 R10=fp0") 30check_assert(s64, eq, llong_max, LLONG_MAX); 31 32__msg(": R0_w=scalar(smax=2147483646) R10=fp0") 33check_assert(s64, lt, pos, INT_MAX); 34__msg(": R0_w=scalar(smax=-1,umin=9223372036854775808,var_off=(0x8000000000000000; 0x7fffffffffffffff))") 35check_assert(s64, lt, zero, 0); 36__msg(": R0_w=scalar(smax=-2147483649,umin=9223372036854775808,umax=18446744071562067967,var_off=(0x8000000000000000; 0x7fffffffffffffff))") 37check_assert(s64, lt, neg, INT_MIN); 38 39__msg(": R0_w=scalar(smax=2147483647) R10=fp0") 40check_assert(s64, le, pos, INT_MAX); 41__msg(": R0_w=scalar(smax=0) R10=fp0") 42check_assert(s64, le, zero, 0); 43__msg(": R0_w=scalar(smax=-2147483648,umin=9223372036854775808,umax=18446744071562067968,var_off=(0x8000000000000000; 0x7fffffffffffffff))") 44check_assert(s64, le, neg, INT_MIN); 45 46__msg(": R0_w=scalar(smin=umin=2147483648,umax=9223372036854775807,var_off=(0x0; 0x7fffffffffffffff))") 47check_assert(s64, gt, pos, INT_MAX); 48__msg(": R0_w=scalar(smin=umin=1,umax=9223372036854775807,var_off=(0x0; 0x7fffffffffffffff))") 49check_assert(s64, gt, zero, 0); 50__msg(": R0_w=scalar(smin=-2147483647) R10=fp0") 51check_assert(s64, gt, neg, INT_MIN); 52 53__msg(": R0_w=scalar(smin=umin=2147483647,umax=9223372036854775807,var_off=(0x0; 0x7fffffffffffffff))") 54check_assert(s64, ge, pos, INT_MAX); 55__msg(": R0_w=scalar(smin=0,umax=9223372036854775807,var_off=(0x0; 0x7fffffffffffffff)) R10=fp0") 56check_assert(s64, ge, zero, 0); 57__msg(": R0_w=scalar(smin=-2147483648) R10=fp0") 58check_assert(s64, ge, neg, INT_MIN); 59 60SEC("?tc") 61__log_level(2) __failure 62__msg(": R0=0 R1=ctx(off=0,imm=0) R2=scalar(smin=smin32=-2147483646,smax=smax32=2147483645) R10=fp0") 63int check_assert_range_s64(struct __sk_buff *ctx) 64{ 65 struct bpf_sock *sk = ctx->sk; 66 s64 num; 67 68 _Static_assert(_Generic((sk->rx_queue_mapping), s32: 1, default: 0), "type match"); 69 if (!sk) 70 return 0; 71 num = sk->rx_queue_mapping; 72 bpf_assert_range(num, INT_MIN + 2, INT_MAX - 2); 73 return *((u8 *)ctx + num); 74} 75 76SEC("?tc") 77__log_level(2) __failure 78__msg(": R1=ctx(off=0,imm=0) R2=scalar(smin=umin=smin32=umin32=4096,smax=umax=smax32=umax32=8192,var_off=(0x0; 0x3fff))") 79int check_assert_range_u64(struct __sk_buff *ctx) 80{ 81 u64 num = ctx->len; 82 83 bpf_assert_range(num, 4096, 8192); 84 return *((u8 *)ctx + num); 85} 86 87SEC("?tc") 88__log_level(2) __failure 89__msg(": R0=0 R1=ctx(off=0,imm=0) R2=4096 R10=fp0") 90int check_assert_single_range_s64(struct __sk_buff *ctx) 91{ 92 struct bpf_sock *sk = ctx->sk; 93 s64 num; 94 95 _Static_assert(_Generic((sk->rx_queue_mapping), s32: 1, default: 0), "type match"); 96 if (!sk) 97 return 0; 98 num = sk->rx_queue_mapping; 99 100 bpf_assert_range(num, 4096, 4096); 101 return *((u8 *)ctx + num); 102} 103 104SEC("?tc") 105__log_level(2) __failure 106__msg(": R1=ctx(off=0,imm=0) R2=4096 R10=fp0") 107int check_assert_single_range_u64(struct __sk_buff *ctx) 108{ 109 u64 num = ctx->len; 110 111 bpf_assert_range(num, 4096, 4096); 112 return *((u8 *)ctx + num); 113} 114 115SEC("?tc") 116__log_level(2) __failure 117__msg(": R1=pkt(off=64,r=64,imm=0) R2=pkt_end(off=0,imm=0) R6=pkt(off=0,r=64,imm=0) R10=fp0") 118int check_assert_generic(struct __sk_buff *ctx) 119{ 120 u8 *data_end = (void *)(long)ctx->data_end; 121 u8 *data = (void *)(long)ctx->data; 122 123 bpf_assert(data + 64 <= data_end); 124 return data[128]; 125} 126 127SEC("?fentry/bpf_check") 128__failure __msg("At program exit the register R0 has value (0x40; 0x0)") 129int check_assert_with_return(void *ctx) 130{ 131 bpf_assert_with(!ctx, 64); 132 return 0; 133} 134 135char _license[] SEC("license") = "GPL";