|
| 1 | +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause |
| 2 | +/* Copyright (c) 2020 Facebook */ |
| 3 | +#include <vmlinux.h> |
| 4 | +#include <bpf/bpf_helpers.h> |
| 5 | +#include <bpf/bpf_core_read.h> |
| 6 | + |
| 7 | +char LICENSE[] SEC("license") = "Dual BSD/GPL"; |
| 8 | + |
| 9 | +int my_pid = 0; |
| 10 | + |
| 11 | +#ifndef likely |
| 12 | +#define likely(x) __builtin_expect(!!(x), 1) |
| 13 | +#endif |
| 14 | + |
| 15 | +#define MAX_STR_LEN 128 |
| 16 | +struct cstr { |
| 17 | + char data[MAX_STR_LEN]; |
| 18 | +}; |
| 19 | + |
| 20 | +static __always_inline u64 cstr_pos(u64 pos) |
| 21 | +{ |
| 22 | + /* prevent compiler reordering comparison below with array access in cstr_char() */ |
| 23 | + barrier_var(pos); |
| 24 | + /* `pos >= MAX_STR_LEN` never happens, but we need to make verifier happy */ |
| 25 | + pos = likely(pos < MAX_STR_LEN) ? pos : 0; |
| 26 | + barrier_var(pos); |
| 27 | + return pos; |
| 28 | +} |
| 29 | + |
| 30 | +static __always_inline char cstr_char(const struct cstr *s, u64 pos) |
| 31 | +{ |
| 32 | + return s->data[cstr_pos(pos)]; |
| 33 | +} |
| 34 | + |
| 35 | +unsigned zero = 0, one = 1; /* obfuscate integers for verifier */ |
| 36 | + |
| 37 | +static bool __substr_match(const struct cstr *haystack __arg_nonnull, |
| 38 | + const struct cstr *needle __arg_nonnull, |
| 39 | + int pos) |
| 40 | +{ |
| 41 | + u64 i; |
| 42 | + char c; |
| 43 | + |
| 44 | + bpf_for(i, 0, MAX_STR_LEN) { |
| 45 | + c = cstr_char(needle, i); |
| 46 | + if (c == '\0') |
| 47 | + return true; |
| 48 | + if (c != cstr_char(haystack, pos + i)) |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + return true; |
| 53 | +} |
| 54 | + |
| 55 | +/* |
| 56 | + * Find substring `needle` in a string `haystack`, starting from position |
| 57 | + * `start` (zero-indexed). Returns substring start position (>= `start`) if |
| 58 | + * match is found; negative result, otherwise. |
| 59 | + */ |
| 60 | +__noinline int substr_hashed(const struct cstr *haystack __arg_nonnull, |
| 61 | + const struct cstr *needle __arg_nonnull, |
| 62 | + int start) |
| 63 | +{ |
| 64 | + u32 i, need_hash = zero, hay_hash = zero, mul = one; |
| 65 | + int need_len = zero, hay_len = zero, p; |
| 66 | + |
| 67 | + bpf_for(i, 0, MAX_STR_LEN) { |
| 68 | + if (needle->data[i] == '\0') |
| 69 | + break; |
| 70 | + |
| 71 | + need_len += 1; |
| 72 | + need_hash = need_hash * 31 + (u32)needle->data[i]; |
| 73 | + mul *= 31; |
| 74 | + } |
| 75 | + |
| 76 | + if (need_len == 0) /* emtpy substring always matches */ |
| 77 | + return start; |
| 78 | + |
| 79 | + bpf_for(i, start, MAX_STR_LEN) { |
| 80 | + if (haystack->data[i] == '\0') |
| 81 | + return -1; |
| 82 | + |
| 83 | + hay_hash = hay_hash * 31 + (u32)haystack->data[i]; |
| 84 | + hay_len += 1; |
| 85 | + if (hay_len < need_len) { |
| 86 | + continue; |
| 87 | + } else if (hay_len > need_len) { |
| 88 | + hay_len -= 1; |
| 89 | + hay_hash -= mul * cstr_char(haystack, i - hay_len); |
| 90 | + } |
| 91 | + |
| 92 | + /* now hay_len == need_len */ |
| 93 | + p = i - (hay_len - 1); |
| 94 | + if (hay_hash == need_hash && __substr_match(haystack, needle, p)) |
| 95 | + return p; |
| 96 | + } |
| 97 | + |
| 98 | + return -1; |
| 99 | +} |
| 100 | + |
| 101 | +__noinline int substr_naive(const struct cstr *haystack __arg_nonnull, |
| 102 | + const struct cstr *needle __arg_nonnull, |
| 103 | + int start) |
| 104 | +{ |
| 105 | + int *p; |
| 106 | + |
| 107 | + bpf_for_each(num, p, start, MAX_STR_LEN) { |
| 108 | + if (cstr_char(haystack, *p) == '\0') |
| 109 | + break; |
| 110 | + |
| 111 | + if (__substr_match(haystack, needle, *p)) |
| 112 | + return *p; |
| 113 | + } |
| 114 | + |
| 115 | + return -1; |
| 116 | +} |
| 117 | + |
| 118 | +#define BENCH 0 |
| 119 | +#define BENCH_ITERS 25000 |
| 120 | + |
| 121 | +#if BENCH |
| 122 | +static struct cstr haystack = { "abacabadabacabaeabacabadabacaba" }; |
| 123 | +static struct cstr needle = { "eaba" }; |
| 124 | +#else |
| 125 | +static struct cstr haystack = { "abracadabra" }; |
| 126 | +static struct cstr needle = { "a" }; |
| 127 | +#endif |
| 128 | + |
| 129 | +SEC("raw_tp/sys_enter") |
| 130 | +int test_substr_hashed(void *ctx) |
| 131 | +{ |
| 132 | + int pid = bpf_get_current_pid_tgid() >> 32; |
| 133 | + int i, p; |
| 134 | + |
| 135 | + if (pid != my_pid) |
| 136 | + return 0; |
| 137 | + |
| 138 | +#if BENCH |
| 139 | + u64 start, end; |
| 140 | + start = bpf_ktime_get_ns(); |
| 141 | + bpf_repeat(BENCH_ITERS) { |
| 142 | +#endif |
| 143 | + p = -1; |
| 144 | + bpf_repeat(MAX_STR_LEN) { |
| 145 | + p = substr_hashed(&haystack, &needle, p + 1); |
| 146 | + if (p < 0) |
| 147 | + break; |
| 148 | +#if !BENCH |
| 149 | + bpf_printk("HASHED match at pos #%d!", p); |
| 150 | +#endif |
| 151 | + } |
| 152 | + |
| 153 | +#if BENCH |
| 154 | + } |
| 155 | + end = bpf_ktime_get_ns(); |
| 156 | + bpf_printk("BENCH HASHED %lu ns/iter", (end - start) / BENCH_ITERS); |
| 157 | +#endif |
| 158 | + return 0; |
| 159 | +} |
| 160 | + |
| 161 | +SEC("raw_tp/sys_enter") |
| 162 | +int test_substr_naive(void *ctx) |
| 163 | +{ |
| 164 | + int pid = bpf_get_current_pid_tgid() >> 32; |
| 165 | + int i, p; |
| 166 | + u64 start, end; |
| 167 | + |
| 168 | + if (pid != my_pid) |
| 169 | + return 0; |
| 170 | + |
| 171 | +#if BENCH |
| 172 | + start = bpf_ktime_get_ns(); |
| 173 | + bpf_repeat(BENCH_ITERS) { |
| 174 | +#endif |
| 175 | + p = -1; |
| 176 | + bpf_repeat(MAX_STR_LEN) { |
| 177 | + p = substr_naive(&haystack, &needle, p + 1); |
| 178 | + if (p < 0) |
| 179 | + break; |
| 180 | +#if !BENCH |
| 181 | + bpf_printk("NAIVE match at pos #%d!", p); |
| 182 | +#endif |
| 183 | + } |
| 184 | + |
| 185 | +#if BENCH |
| 186 | + } |
| 187 | + end = bpf_ktime_get_ns(); |
| 188 | + bpf_printk("BENCH NAIVE %lu ns/iter", (end - start) / BENCH_ITERS); |
| 189 | +#endif |
| 190 | + |
| 191 | + return 0; |
| 192 | +} |
0 commit comments