Skip to content

eBPF-based security platform

ci: fail on an extern function nothing calls

Daniel Borkmann3967a3c27412
diff --git a/.agents/skills/pre-submit/SKILL.md b/.agents/skills/pre-submit/SKILL.md
index 54d5ff74..6b30df8f 100644
--- a/.agents/skills/pre-submit/SKILL.md
+++ b/.agents/skills/pre-submit/SKILL.md
@@ -53,6 +53,7 @@ signs off.
```sh
.github/scripts/check-sparse.sh
.github/scripts/check-spdx.sh
+.github/scripts/check-unused.sh
make -C Documentation check # DOCS_CONTAINER=0 without podman or docker
make -B && make -B shirudo-build && make -B quote-check &&
make -B -f Makefile.shirudo test-helpers
@@ -74,7 +75,8 @@ A warning fails every build. Fix it rather than silence it; `WERROR=0`
is only for a compiler newer than the tree while its new warnings get
fixed. `check-sparse.sh` refuses a little- or big-endian field read or
written without `le32_to_cpu()`, `cpu_to_le32()` or their kin, and
-`check-spdx.sh` the wrong license tag for a file's kind.
+`check-spdx.sh` the wrong license tag for a file's kind, and
+`check-unused.sh` an extern function whose last caller went.
CI does not run the proofs, so this run is the only one a series gets
before it lands. `CHANGED` takes what `git diff` takes and runs the CBMC
diff --git a/.github/scripts/check-unused.sh b/.github/scripts/check-unused.sh
new file mode 100755
index 00000000..77972737
--- /dev/null
+++ b/.github/scripts/check-unused.sh
@@ -0,0 +1,86 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# .github/scripts/check-unused.sh -- fail on a function the userspace C
+# defines without static that nothing calls: the compiler reports an
+# unused static function, never an extern one, so one whose last caller
+# went stays behind. A function counts as used where its name appears in
+# another C file, or in a header's macro or inline function; one the
+# tests alone call is used.
+#
+# .github/scripts/check-unused.sh
+#
+# Runs from anywhere inside the checkout and needs no build.
+
+set -eu
+
+cd "$(git rev-parse --show-toplevel)"
+work=$(mktemp -d)
+trap 'rm -rf "$work"' EXIT
+
+# The C the project writes, as check-coccinelle.sh takes it.
+git ls-files -- '*.c' '*.h' ':(exclude)vendor/' ':(exclude)*vmlinux.h' \
+ ':(exclude)*_generated.h' >"$work/files"
+# Where an extern function is a definition to account for: not the BPF
+# programs, whose entry points the loader finds by section, nor the
+# proof harnesses, which stand in for functions of the code they prove.
+grep '\.c$' "$work/files" | grep -v -e '^bpf/' -e '^upatches/' \
+ -e '^testing/fixtures/' -e '^testing/proofs/' >"$work/defining"
+
+# file:line:name of each extern definition: a line that opens with the
+# name and its type, or with the name alone under its type, whose
+# parameters close before a { in the first column. main, the entry
+# points of libFuzzer and CBMC and the wrappers the linker's --wrap
+# names are called from outside the C.
+# shellcheck disable=SC2016 # awk program
+xargs awk '
+ FNR == 1 { pending = ""; prev = "" }
+ /^[A-Za-z_]/ && !/^(static|typedef|struct [a-z_0-9]+ *\{|enum|union|extern)/ &&
+ !/;[ \t]*$/ && !/=.*\(/ && match($0, /[A-Za-z_][A-Za-z0-9_]*\(/) {
+ name = substr($0, RSTART, RLENGTH - 1)
+ head = substr($0, 1, RSTART - 1)
+ if ((head != "" || prev !~ /(^|[^a-z_])static([^a-z_]|$)/) &&
+ prev !~ /^#define/)
+ pending = name
+ where = FNR
+ }
+ pending != "" && /\);[ \t]*$/ { pending = "" }
+ /^\{/ && pending != "" {
+ print FILENAME ":" where ":" pending
+ pending = ""
+ }
+ { prev = $0 }' <"$work/defining" |
+ grep -v -E ':(main|harness|LLVMFuzzer[A-Za-z]*|__wrap_[A-Za-z0-9_]+)$' \
+ >"$work/defined"
+
+bad=0
+while IFS=: read -r file line name; do
+ xargs git grep -n -w -F -e "$name" -- <"$work/files" >"$work/uses" ||
+ :
+ # The definition is one mention in its file; a declaration of a
+ # header opens its line, where a use in a macro or an inline
+ # function does not.
+ if awk -F: -v file="$file" -v name="$name" '
+ $1 == file { own++; next }
+ $1 ~ /\.h$/ && $3 !~ /^[ \t#]/ { next }
+ { found = 1 }
+ END { exit !(found || own > 1) }' "$work/uses"; then
+ continue
+ fi
+ bad=$((bad + 1))
+ printf '%s:%s: %s() is defined, but nothing calls it\n' \
+ "$file" "$line" "$name" >&2
+ if [ -n "${GITHUB_ACTIONS:-}" ]; then
+ printf '::error file=%s,line=%s::%s() is defined, but nothing calls it\n' \
+ "$file" "$line" "$name"
+ fi
+done <"$work/defined"
+
+n=$(wc -l <"$work/defined")
+if [ "$bad" -gt 0 ]; then
+ printf 'check-unused: %d of %d extern functions are not called\n' \
+ "$bad" "$n" >&2
+ exit 1
+fi
+printf 'check-unused: %d extern functions, each called\n' "$n"
diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml
index 51c4e292..d1a0e7c8 100644
--- a/.github/workflows/lint.yml
+++ b/.github/workflows/lint.yml
@@ -40,6 +40,17 @@ jobs:
- name: Check the SPDX tag of every tracked source
run: .github/scripts/check-spdx.sh
+ unused:
+ name: Unused functions
+ runs-on: ubuntu-latest
+
+ steps:
+ - name: Check out repository
+ uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+
+ - name: Check that every extern function has a caller
+ run: .github/scripts/check-unused.sh
+
sparse:
name: Sparse
runs-on: ubuntu-latest
diff --git a/AGENTS.md b/AGENTS.md
index 7ec1871b..ca880d4f 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -345,7 +345,8 @@ one usually still compiles and passes tests.
`proofs` skill says how.
- CI runs `.github/scripts/build-commits.sh`, `check-style.sh`,
`check-dco.sh`, `check-coccinelle.sh`, `check-sparse.sh`,
- `check-spdx.sh` and the documentation check on every pull request;
+ `check-spdx.sh`, `check-unused.sh` and the documentation check on
+ every pull request;
run them before handing work over, as the `pre-submit` skill says.
Shell that a workflow runs lives in `.github/scripts/`, not inline in
the workflow file.
diff --git a/Documentation/contribute/code/contributing.rst b/Documentation/contribute/code/contributing.rst
index bd924a59..358da1bd 100644
--- a/Documentation/contribute/code/contributing.rst
+++ b/Documentation/contribute/code/contributing.rst
@@ -234,6 +234,9 @@ Before sending
- Run ``make -C Documentation html`` when the change touches
``Documentation/``. The build turns every Sphinx warning into an
error. See :doc:`/contribute/documentation/writing-docs`.
+- Run :src:`.github/scripts/check-unused.sh` when the change removes or
+ renames a call: a function whose last caller went fails it. CI runs it
+ as the ``Unused functions`` job of the ``Lint`` workflow.
- Run :src:`.github/scripts/check-spdx.sh` when the change adds a file.
Every source carries an SPDX tag in the kernel's form for its type,
``BSD-2-Clause`` for userspace and ``GPL-2.0 OR BSD-2-Clause`` for
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rst
index 8d45fe78..7c9692d2 100644
--- a/Documentation/contribute/code/developer-guide.rst
+++ b/Documentation/contribute/code/developer-guide.rst
@@ -476,6 +476,10 @@ the commit at ``HEAD`` alone without them.
files.
* - ``check-spdx.sh``
- The SPDX tag of every tracked source. Takes no range.
+ * - ``check-unused.sh``
+ - An extern function of the userspace C that nothing calls: the
+ compiler reports an unused ``static`` function, never an extern
+ one. A call from a test counts. Takes no range.
* - ``check-sparse.sh``
- ``make sparse``: sparse over the userspace sources the binaries
are built from, failing on any warning. It finds a value moved
@@ -546,6 +550,7 @@ Before you send a change:
$ .github/scripts/check-dco.sh origin/main..HEAD
$ .github/scripts/check-style.sh origin/main..HEAD
$ .github/scripts/check-spdx.sh
+ $ .github/scripts/check-unused.sh
$ .github/scripts/check-sparse.sh
$ .github/scripts/check-coccinelle.sh
$ .github/scripts/build-commits.sh origin/main..HEAD
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index d687018e..4c172b7f 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -66,6 +66,11 @@ signed config image.
box, a hook by its section, each tamper and integrity counter's
entry on its page, the source files a proof reads or the proof
coverage page lists as read by none, and the tree itself
+ * - ``t0009-check-unused``
+ - ``check-unused.sh``: an extern function nothing calls, with its
+ type on its line or the one above, a header's declaration that is
+ no call, the calls that count and the definitions it leaves
+ alone, and the tree itself
``build`` (1)
~~~~~~~~~~~~~
diff --git a/Documentation/reference/cheat-sheet.rst b/Documentation/reference/cheat-sheet.rst
index e4687a96..f589f4f3 100644
--- a/Documentation/reference/cheat-sheet.rst
+++ b/Documentation/reference/cheat-sheet.rst
@@ -487,4 +487,5 @@ Check the commits of a branch as CI does (:doc:`/contribute/code/contributing`):
$ .github/scripts/check-dco.sh origin/main..HEAD
$ .github/scripts/check-style.sh origin/main..HEAD
$ .github/scripts/check-spdx.sh
+ $ .github/scripts/check-unused.sh
$ .github/scripts/build-commits.sh origin/main..HEAD
diff --git a/testing/ci-checks/t0009-check-unused.sh b/testing/ci-checks/t0009-check-unused.sh
new file mode 100755
index 00000000..521f69ca
--- /dev/null
+++ b/testing/ci-checks/t0009-check-unused.sh
@@ -0,0 +1,91 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+#
+# t0009 -- .github/scripts/check-unused.sh: an extern function nothing
+# calls fails the check, whichever line its type stands on, while a call
+# from another file, a header's inline function or macro, a test or the
+# function's own file counts, and static functions, BPF programs, entry
+# points and linker wrappers are not its business; on a throwaway
+# repository, and the tree itself.
+
+cd "$(dirname "$0")/.."
+. ./test-lib.sh
+command -v git >/dev/null 2>&1 || test_skip_all "git not installed"
+ROOT=$(cd .. && pwd)
+S=$ROOT/.github/scripts/check-unused.sh
+W=$(mktemp -d)
+trap 'rm -rf "$W"' EXIT
+R=$W/repo
+
+# A file with the given lines, each argument one line.
+put() {
+ f=$R/$1
+ shift
+ mkdir -p "$(dirname "$f")"
+ printf '%s\n' "$@" >"$f"
+}
+
+# A tree the check passes: every extern function has a caller of one kind.
+good() {
+ rm -rf "$R" && mkdir -p "$R" && git init -q "$R" &&
+ put util/a.h 'int called(void);' 'int inlined(void);' \
+ 'int by_macro(void);' 'int by_test(void);' \
+ 'static inline int wrap(void)' '{' ' return inlined();' '}' \
+ '#define BY_MACRO() by_macro()' &&
+ put util/a.c '#include "a.h"' '' \
+ 'static int helper(void)' '{' ' return 0;' '}' '' \
+ 'int called(void)' '{' ' return helper();' '}' '' \
+ 'int inlined(void)' '{' ' return 1;' '}' '' \
+ 'int' 'by_macro(void)' '{' ' return 2;' '}' '' \
+ 'int by_test(void)' '{' ' return 3;' '}' '' \
+ 'int local(void)' '{' ' return 4;' '}' '' \
+ 'int local_user(void)' '{' ' return local() + wrap() + BY_MACRO();' '}' &&
+ put util/b.c '#include "a.h"' '' \
+ 'int main(void)' '{' ' return called() + local_user();' '}' &&
+ put testing/unit/a.c '#include "a.h"' '' \
+ 'static void case_one(void)' '{' ' by_test();' '}' &&
+ put testing/helper/fault.c 'int __wrap_Esys_Startup(void)' '{' \
+ ' return 0;' '}' &&
+ put bpf/prog.c 'int prog_entry(void *ctx)' '{' ' return 0;' '}'
+}
+
+check() {
+ (cd "$R" && git add -A && sh "$S") >"$W/out" 2>"$W/err"
+}
+
+# The check fails and says so.
+refuses() {
+ ! check && grep -qF "$1" "$W/err"
+}
+
+test_expect_success 'a tree whose extern functions all have a caller passes' '
+ good && check
+'
+
+test_expect_success 'an extern function nothing calls is refused' '
+ good && put util/c.c "int orphan(const char *s)" "{" " return !s;" "}" &&
+ refuses "util/c.c:1: orphan() is defined, but nothing calls it"
+'
+
+test_expect_success 'one whose type stands on the line above is refused too' '
+ good && put util/c.c "unsigned long" "orphan(void)" "{" " return 0;" "}" &&
+ refuses "util/c.c:2: orphan() is defined, but nothing calls it"
+'
+
+test_expect_success 'a declaration in a header is no call' '
+ good && put util/c.c "int orphan(void)" "{" " return 0;" "}" &&
+ put util/c.h "int orphan(void);" &&
+ refuses "orphan() is defined, but nothing calls it"
+'
+
+test_expect_success 'a prototype in a C file is no definition' '
+ good && put util/c.c "int later(void);" &&
+ check
+'
+
+test_expect_success 'the tree itself passes' '
+ sh "$S"
+'
+
+test_done