File Index
Files and directories
Sign in
FILE

arena_strsearch.c

File preview with syntax highlighting.

linux-6.19.14/tools/testing/selftests/bpf/progs/arena_strsearch.c C
  1// SPDX-License-Identifier: GPL-2.0
  2/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
  3#include <vmlinux.h>
  4#include "bpf_experimental.h"
  5
  6struct {
  7	__uint(type, BPF_MAP_TYPE_ARENA);
  8	__uint(map_flags, BPF_F_MMAPABLE);
  9	__uint(max_entries, 100); /* number of pages */
 10} arena SEC(".maps");
 11
 12#include "bpf_arena_strsearch.h"
 13
 14struct glob_test {
 15	char const __arena *pat, *str;
 16	bool expected;
 17};
 18
 19static bool test(char const __arena *pat, char const __arena *str, bool expected)
 20{
 21	bool match = glob_match(pat, str);
 22	bool success = match == expected;
 23
 24	/* bpf_printk("glob_match %s %s res %d ok %d", pat, str, match, success); */
 25	return success;
 26}
 27
 28/*
 29 * The tests are all jammed together in one array to make it simpler
 30 * to place that array in the .init.rodata section.  The obvious
 31 * "array of structures containing char *" has no way to force the
 32 * pointed-to strings to be in a particular section.
 33 *
 34 * Anyway, a test consists of:
 35 * 1. Expected glob_match result: '1' or '0'.
 36 * 2. Pattern to match: null-terminated string
 37 * 3. String to match against: null-terminated string
 38 *
 39 * The list of tests is terminated with a final '\0' instead of
 40 * a glob_match result character.
 41 */
 42static const char __arena glob_tests[] =
 43	/* Some basic tests */
 44	"1" "a\0" "a\0"
 45	"0" "a\0" "b\0"
 46	"0" "a\0" "aa\0"
 47	"0" "a\0" "\0"
 48	"1" "\0" "\0"
 49	"0" "\0" "a\0"
 50	/* Simple character class tests */
 51	"1" "[a]\0" "a\0"
 52	"0" "[a]\0" "b\0"
 53	"0" "[!a]\0" "a\0"
 54	"1" "[!a]\0" "b\0"
 55	"1" "[ab]\0" "a\0"
 56	"1" "[ab]\0" "b\0"
 57	"0" "[ab]\0" "c\0"
 58	"1" "[!ab]\0" "c\0"
 59	"1" "[a-c]\0" "b\0"
 60	"0" "[a-c]\0" "d\0"
 61	/* Corner cases in character class parsing */
 62	"1" "[a-c-e-g]\0" "-\0"
 63	"0" "[a-c-e-g]\0" "d\0"
 64	"1" "[a-c-e-g]\0" "f\0"
 65	"1" "[]a-ceg-ik[]\0" "a\0"
 66	"1" "[]a-ceg-ik[]\0" "]\0"
 67	"1" "[]a-ceg-ik[]\0" "[\0"
 68	"1" "[]a-ceg-ik[]\0" "h\0"
 69	"0" "[]a-ceg-ik[]\0" "f\0"
 70	"0" "[!]a-ceg-ik[]\0" "h\0"
 71	"0" "[!]a-ceg-ik[]\0" "]\0"
 72	"1" "[!]a-ceg-ik[]\0" "f\0"
 73	/* Simple wild cards */
 74	"1" "?\0" "a\0"
 75	"0" "?\0" "aa\0"
 76	"0" "??\0" "a\0"
 77	"1" "?x?\0" "axb\0"
 78	"0" "?x?\0" "abx\0"
 79	"0" "?x?\0" "xab\0"
 80	/* Asterisk wild cards (backtracking) */
 81	"0" "*??\0" "a\0"
 82	"1" "*??\0" "ab\0"
 83	"1" "*??\0" "abc\0"
 84	"1" "*??\0" "abcd\0"
 85	"0" "??*\0" "a\0"
 86	"1" "??*\0" "ab\0"
 87	"1" "??*\0" "abc\0"
 88	"1" "??*\0" "abcd\0"
 89	"0" "?*?\0" "a\0"
 90	"1" "?*?\0" "ab\0"
 91	"1" "?*?\0" "abc\0"
 92	"1" "?*?\0" "abcd\0"
 93	"1" "*b\0" "b\0"
 94	"1" "*b\0" "ab\0"
 95	"0" "*b\0" "ba\0"
 96	"1" "*b\0" "bb\0"
 97	"1" "*b\0" "abb\0"
 98	"1" "*b\0" "bab\0"
 99	"1" "*bc\0" "abbc\0"
100	"1" "*bc\0" "bc\0"
101	"1" "*bc\0" "bbc\0"
102	"1" "*bc\0" "bcbc\0"
103	/* Multiple asterisks (complex backtracking) */
104	"1" "*ac*\0" "abacadaeafag\0"
105	"1" "*ac*ae*ag*\0" "abacadaeafag\0"
106	"1" "*a*b*[bc]*[ef]*g*\0" "abacadaeafag\0"
107	"0" "*a*b*[ef]*[cd]*g*\0" "abacadaeafag\0"
108	"1" "*abcd*\0" "abcabcabcabcdefg\0"
109	"1" "*ab*cd*\0" "abcabcabcabcdefg\0"
110	"1" "*abcd*abcdef*\0" "abcabcdabcdeabcdefg\0"
111	"0" "*abcd*\0" "abcabcabcabcefg\0"
112	"0" "*ab*cd*\0" "abcabcabcabcefg\0";
113
114bool skip = false;
115
116SEC("syscall")
117int arena_strsearch(void *ctx)
118{
119	unsigned successes = 0;
120	unsigned n = 0;
121	char const __arena *p = glob_tests;
122
123	/*
124	 * Tests are jammed together in a string.  The first byte is '1'
125	 * or '0' to indicate the expected outcome, or '\0' to indicate the
126	 * end of the tests.  Then come two null-terminated strings: the
127	 * pattern and the string to match it against.
128	 */
129	while (*p) {
130		bool expected = *p++ & 1;
131		char const __arena *pat = p;
132
133		cond_break;
134		p += bpf_arena_strlen(p) + 1;
135		successes += test(pat, p, expected);
136		p += bpf_arena_strlen(p) + 1;
137		n++;
138	}
139
140	n -= successes;
141	/* bpf_printk("glob: %u self-tests passed, %u failed\n", successes, n); */
142
143	return n ? -1 : 0;
144}
145
146char _license[] SEC("license") = "GPL";
~/ Read-only file preview.