eBPF-based security platform
testing: catch a count by hand in a macro and a second ARRAY_SIZE()
Daniel Borkmann71c708f7aa5e
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rstindex 87e3a87b..8ba52112 100644--- a/Documentation/contribute/code/developer-guide.rst+++ b/Documentation/contribute/code/developer-guide.rst@@ -615,7 +615,9 @@ can rewrite what it finds does so with ``-D patch``:- Refuses* - ``array_size``- An element count divided out by hand rather than- ``ARRAY_SIZE()``. Rewrites.+ ``ARRAY_SIZE()``, in a macro too, which it rewrites, and an+ ``ARRAY_SIZE()`` defined outside ``<shirudo/misc.h>``, which it+ leaves to be removed.* - ``zero_init``- ``= {0}`` rather than ``= {}``. Rewrites.* - ``minmax``diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex f74cbf88..1469bc0a 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -56,8 +56,10 @@ signed config image.alone, the rewrite of ``-D patch``, a case for each rule takenfrom CERT C, a case for each BPF hazard rule beside the safeform it leaves alone, a dead store beside the reads- ``dead_store`` leaves alone, a file a rule gives up on at its- time limit named rather than failed, and the tree itself+ ``dead_store`` leaves alone, a count by hand in a macro and an+ ``ARRAY_SIZE()`` of a file's own for ``array_size``, which+ rewrites the one and leaves the other, a file a rule gives up on+ at its time limit named rather than failed, and the tree itself* - ``t0007-check-operator-linkage``- ``check-operator-linkage.sh``: the libraries an operator binarymay link and its own interpreter, another loader, a librarydiff --git a/testing/ci-checks/t0006-check-coccinelle.sh b/testing/ci-checks/t0006-check-coccinelle.shindex 00509803..a653e88f 100755--- a/testing/ci-checks/t0006-check-coccinelle.sh+++ b/testing/ci-checks/t0006-check-coccinelle.sh@@ -7,8 +7,9 @@# a rule that can rewrite does with -D patch, a rule reads the headers# its #spatch line asks for, on a throwaway repository, each rule taken# from CERT C, each BPF hazard rule, each scrub rule and dead_store report-# a sample of what they refuse, a file a rule gives up on at its time-# limit is named rather than failed, and the tree itself.+# a sample of what they refuse, array_size a count by hand in a macro and+# an ARRAY_SIZE() outside <shirudo/misc.h>, a file a rule gives up on at+# its time limit is named rather than failed, and the tree itself.cd "$(dirname "$0")/..". ./test-lib.sh@@ -637,6 +638,51 @@ test_expect_success 'option_by_hand reports an option compared outside the parse[ "$(grep -c "^option_by_hand:" "$W/err")" = 1 ]'+# A count by hand in a macro, which writes its argument in parentheses,+# and an ARRAY_SIZE() of a file's own, while the one of <shirudo/misc.h>+# is spared.+write_array_size_samples() {+ mkdir -p "$1/include/shirudo" &&+ cat >"$1/include/shirudo/misc.h" <<'EOF'+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]) + __must_be_array(a))+EOF+ cat >"$1/samples.c" <<'EOF'+#define COUNT(a) (sizeof(a) / sizeof((a)[0]) + __must_be_array(a))+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))++int counted(void)+{+ int x[4];++ return sizeof(x) / sizeof(*(x)) + ARRAY_SIZE(x);+}+EOF+}++test_expect_success 'array_size reports counts by hand and a second ARRAY_SIZE()' '+ R8=$W/array &&+ mkdir -p "$R8/testing/coccinelle" &&+ git init -q "$R8" &&+ cp "$ROOT/testing/coccinelle/array_size.cocci" \+ "$R8/testing/coccinelle/" &&+ write_array_size_samples "$R8" &&+ ! (cd "$R8" && git add -A && sh "$S") >"$W/out" 2>"$W/err" &&+ grep -q "^array_size: samples.c:1:" "$W/err" &&+ grep -q "^array_size: samples.c:2:" "$W/err" &&+ grep -q "^array_size: samples.c:8:" "$W/err" &&+ [ "$(grep -c "^array_size:" "$W/err")" = 3 ]+'++test_expect_success 'array_size rewrites the counts but not a definition' '+ R8=$W/array &&+ (cd "$R8" && spatch -D patch --very-quiet --in-place --no-includes \+ --sp-file testing/coccinelle/array_size.cocci samples.c) &&+ grep -qxF "#define COUNT(a) (ARRAY_SIZE(a))" "$R8/samples.c" &&+ grep -qxF "#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))" \+ "$R8/samples.c" &&+ grep -qF "return ARRAY_SIZE(x) + ARRAY_SIZE(x);" "$R8/samples.c"+'+# A spatch that gives up, as a rule with --timeout makes it: the file it# gave up on is named and the check passes, while another failure fails.write_fake_spatch() {diff --git a/testing/coccinelle/array_size.cocci b/testing/coccinelle/array_size.cocciindex 15fffec0..28c74f41 100644--- a/testing/coccinelle/array_size.cocci+++ b/testing/coccinelle/array_size.cocci@@ -1,7 +1,9 @@// SPDX-License-Identifier: BSD-2-Clause// Copyright (c) 2026 Authors of shirudo//-// Count an array's elements with ARRAY_SIZE(), which refuses a pointer.+// Count an array's elements with ARRAY_SIZE(), which refuses a pointer,+// and define it in <shirudo/misc.h> alone. A macro writes its argument+// in parentheses, (a)[0], which a pattern of a[0] alone does not match.virtual patchvirtual report@@ -12,14 +14,32 @@ position p;@@(sizeof(E)@p / sizeof(E[...])+|+ sizeof(E)@p / sizeof((E)[...])|sizeof(E)@p / sizeof(*E)+|+ sizeof(E)@p / sizeof(*(E)))+// The definition itself is spared, and another one is reported but not+// rewritten into one that names itself: it goes, by hand. Coccinelle+// names no macro a match is in, so the line tells.@script:python@p << r.p;@@-if p[0].current_element == "ARRAY_SIZE":+with open(p[0].file) as f:+ line = f.readlines()[int(p[0].line) - 1]+if line.startswith("#define ARRAY_SIZE(") and \+ p[0].file.endswith("include/shirudo/misc.h"):+ cocci.include_match(False)++@script:python depends on patch@+p << r.p;+@@+with open(p[0].file) as f:+ line = f.readlines()[int(p[0].line) - 1]+if line.startswith("#define ARRAY_SIZE("):cocci.include_match(False)@script:python depends on report@@@ -35,6 +55,19 @@ position r.p;- sizeof(E)@p / sizeof(E[...])+ ARRAY_SIZE(E)|+- sizeof(E)@p / sizeof((E)[...])++ ARRAY_SIZE(E)+|- sizeof(E)@p / sizeof(*E)+ ARRAY_SIZE(E)+|+- sizeof(E)@p / sizeof(*(E))++ ARRAY_SIZE(E))++// ARRAY_SIZE() checks for an array itself.+@depends on patch@+expression E;+@@+- ARRAY_SIZE(E) + __must_be_array(E)++ ARRAY_SIZE(E)