Skip to content

eBPF-based security platform

testing: cover the runs the kernel skipped of loaded upatches

Jiri Olsa740bfaeac433
diff --git a/testing/functional/upatch/t3106-upatch-get.sh b/testing/functional/upatch/t3106-upatch-get.sh
index c4430077..15d888c8 100644
--- a/testing/functional/upatch/t3106-upatch-get.sh
+++ b/testing/functional/upatch/t3106-upatch-get.sh
@@ -6,7 +6,8 @@
# of a LOADED upatch read from the kernel (the BTF the signed gen_loader
# installed). Loads the marker fentry upatch, asserts `get` shows the
# marker source, in text and in its JSON form with the payload the
-# artifact names, then unloads + cleans up on EXIT.
+# artifact names and the runs the kernel skipped of its one program, then
+# unloads + cleans up on EXIT.
cd "$(dirname "$0")/../.."
. ./integration-lib.sh
@@ -51,7 +52,8 @@ text = open(sys.argv[3]).read()
assert set(doc) == {"schema", "upatch"} and doc["schema"] == 1, doc
u = doc["upatch"]
assert set(u) == {"name", "location", "prog_id", "loaded_at", "age",
- "payload_sha256", "source", "maps"}, u
+ "payload_sha256", "missed_runs", "programs", "source",
+ "maps"}, u
assert u["name"] == "shirudo-test-marker"
assert u["location"] == "fentry/do_nanosleep"
assert u["prog_id"] > 0
@@ -59,6 +61,10 @@ assert re.search(r"^Program ID: +%d$" % u["prog_id"], text, re.M)
assert re.fullmatch(r"\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ", u["loaded_at"])
assert u["age"]
assert u["payload_sha256"] == payload
+assert isinstance(u["missed_runs"], int) and u["missed_runs"] >= 0, u
+assert u["programs"] == [{"location": "fentry/do_nanosleep",
+ "missed_runs": u["missed_runs"]}], u
+assert re.search(r"^Missed runs: +%d$" % u["missed_runs"], text, re.M)
lines = [(f["path"], line["line"], line["src"])
for f in u["source"]["files"] for line in f["lines"]]
assert any(path.endswith("/marker.bpf.c") and n > 0 and
diff --git a/testing/functional/upatch/t3107-uprobe-multi.sh b/testing/functional/upatch/t3107-uprobe-multi.sh
index da4c7d0d..bdb306c5 100644
--- a/testing/functional/upatch/t3107-uprobe-multi.sh
+++ b/testing/functional/upatch/t3107-uprobe-multi.sh
@@ -77,6 +77,9 @@ import sys
doc = json.load(open(sys.argv[1], encoding="utf-8"))
assert doc["upatch"]["location"] == \
"uprobe.multi//tmp/shirudo-test-uprobe-target (2 funcs)"
+# The kernel counts none of the runs it skips of a uprobe.
+assert doc["upatch"]["missed_runs"] is None, doc
+assert doc["upatch"]["programs"] == [], doc
PY
'
diff --git a/testing/functional/upatch/t3108-status-many.sh b/testing/functional/upatch/t3108-status-many.sh
index 1928a5a6..47c1ee0c 100644
--- a/testing/functional/upatch/t3108-status-many.sh
+++ b/testing/functional/upatch/t3108-status-many.sh
@@ -43,8 +43,11 @@ test_expect_success 'status --json lists every loaded upatch' '
import json, sys
with open(sys.argv[1], encoding="utf-8") as stream:
loaded = json.load(stream)["upatches_loaded"]
-many = [u["name"] for u in loaded if u["name"].startswith("shirudo-test-many-")]
+many = [u for u in loaded if u["name"].startswith("shirudo-test-many-")]
assert len(many) == 80, many
+# A tp_btf program, whose skipped runs the kernel counts.
+for row in many:
+ assert isinstance(row["missed_runs"], int) and row["missed_runs"] >= 0, row
PY
'
diff --git a/testing/functional/upatch/t3113-kprobe-multi.sh b/testing/functional/upatch/t3113-kprobe-multi.sh
index dbefe90b..b4503b27 100755
--- a/testing/functional/upatch/t3113-kprobe-multi.sh
+++ b/testing/functional/upatch/t3113-kprobe-multi.sh
@@ -3,8 +3,9 @@
# Copyright (c) 2026 Authors of shirudo
#
# t3113 -- live kprobe.multi and kretprobe.multi: both links attach,
-# status names them, both programs run, an unload detaches them, and a
-# name the kernel does not know fails the whole link.
+# status names them, both programs run and report the runs the kernel
+# skipped of each, an unload detaches them, and a name the kernel does
+# not know fails the whole link.
cd "$(dirname "$0")/../.."
. ./integration-lib.sh
@@ -58,6 +59,25 @@ for value in values:
PY
'
+test_expect_success 'get and status report the runs the kernel skipped' '
+ "$SHIRUDO_BIN" upatch get "$NAME" --json >"$WORK/get.json" &&
+ "$SHIRUDO_BIN" status --json >"$WORK/status.json" &&
+ python3 - "$WORK/get.json" "$WORK/status.json" <<'PY'
+import json
+import sys
+upatch = json.load(open(sys.argv[1], encoding="utf-8"))["upatch"]
+loaded = json.load(open(sys.argv[2], encoding="utf-8"))["upatches_loaded"]
+programs = upatch["programs"]
+assert sorted(p["location"] for p in programs) == \
+ ["kprobe.multi (2 funcs)", "kretprobe.multi (2 funcs)"], upatch
+assert all(isinstance(p["missed_runs"], int) for p in programs), upatch
+assert upatch["missed_runs"] == sum(p["missed_runs"] for p in programs), upatch
+row = next(u for u in loaded if u["name"] == "shirudo-test-kprobe-multi")
+assert isinstance(row["missed_runs"], int), row
+assert row["missed_runs"] >= upatch["missed_runs"], (row, upatch)
+PY
+'
+
test_expect_success 'unload removes the links' '
"$SHIRUDO_BIN" upatch unload "$UNLOAD" --no-precheck >/dev/null 2>&1 &&
[ ! -e "$DIR/link_0" ] && [ ! -e "$DIR/link_1" ]
diff --git a/testing/unit/upatch_resolve.c b/testing/unit/upatch_resolve.c
index 6b3fcc2f..cb0fb115 100644
--- a/testing/unit/upatch_resolve.c
+++ b/testing/unit/upatch_resolve.c
@@ -2,6 +2,9 @@
/* Copyright (c) 2026 Authors of shirudo */
#include <errno.h>
+#include <linux/bpf.h>
+
+#include <shirudo/misc.h>
#include "unit.h"
#include "upatch_resolve.h"
@@ -22,8 +25,89 @@ static void links_of_an_absent_upatch_are_not_read(void)
EXPECT_EQ(calls, 0);
}
+static struct bpf_link_info tracing_link(__u32 attach_type)
+{
+ struct bpf_link_info info = {
+ .type = BPF_LINK_TYPE_TRACING,
+ .tracing.attach_type = attach_type,
+ };
+
+ return info;
+}
+
+static struct bpf_link_info perf_link(__u32 perf_type)
+{
+ struct bpf_link_info info = {
+ .type = BPF_LINK_TYPE_PERF_EVENT,
+ .perf_event.type = perf_type,
+ };
+
+ return info;
+}
+
+static void the_kinds_the_kernel_counts_skipped_runs_of_count(void)
+{
+ struct bpf_link_info info = { .type = BPF_LINK_TYPE_KPROBE_MULTI };
+
+ EXPECT_TRUE(upatch_link_counts_misses(&info));
+ info = perf_link(BPF_PERF_EVENT_KPROBE);
+ EXPECT_TRUE(upatch_link_counts_misses(&info));
+ info = perf_link(BPF_PERF_EVENT_KRETPROBE);
+ EXPECT_TRUE(upatch_link_counts_misses(&info));
+ info = tracing_link(BPF_TRACE_FENTRY);
+ EXPECT_TRUE(upatch_link_counts_misses(&info));
+ info = tracing_link(BPF_TRACE_FEXIT);
+ EXPECT_TRUE(upatch_link_counts_misses(&info));
+ info = tracing_link(BPF_MODIFY_RETURN);
+ EXPECT_TRUE(upatch_link_counts_misses(&info));
+ info = (struct bpf_link_info){ .type = BPF_LINK_TYPE_RAW_TRACEPOINT };
+ EXPECT_TRUE(upatch_link_counts_misses(&info));
+}
+
+static void the_kinds_it_never_skips_or_never_counts_do_not(void)
+{
+ static const __u32 types[] = {
+ BPF_LINK_TYPE_UPROBE_MULTI, BPF_LINK_TYPE_XDP,
+ BPF_LINK_TYPE_CGROUP, BPF_LINK_TYPE_NETFILTER,
+ };
+ struct bpf_link_info info = tracing_link(BPF_LSM_MAC);
+ size_t i;
+
+ EXPECT_FALSE(upatch_link_counts_misses(&info));
+ info = perf_link(BPF_PERF_EVENT_UPROBE);
+ EXPECT_FALSE(upatch_link_counts_misses(&info));
+ info = perf_link(BPF_PERF_EVENT_URETPROBE);
+ EXPECT_FALSE(upatch_link_counts_misses(&info));
+ for (i = 0; i < ARRAY_SIZE(types); i++) {
+ info = (struct bpf_link_info){ .type = types[i] };
+ EXPECT_FALSE(upatch_link_counts_misses(&info));
+ }
+}
+
+static void a_kprobe_link_adds_what_it_counts_itself(void)
+{
+ struct bpf_link_info info = {
+ .type = BPF_LINK_TYPE_KPROBE_MULTI,
+ .kprobe_multi.missed = 7,
+ };
+
+ EXPECT_UEQ(upatch_link_missed(&info), 7);
+ info = perf_link(BPF_PERF_EVENT_KRETPROBE);
+ info.perf_event.kprobe.missed = 3;
+ EXPECT_UEQ(upatch_link_missed(&info), 3);
+ info = tracing_link(BPF_TRACE_FENTRY);
+ EXPECT_UEQ(upatch_link_missed(&info), 0);
+ /* The union's kprobe member is not a uprobe's count. */
+ info = perf_link(BPF_PERF_EVENT_UPROBE);
+ info.perf_event.kprobe.missed = 5;
+ EXPECT_UEQ(upatch_link_missed(&info), 0);
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(links_of_an_absent_upatch_are_not_read),
+ UNIT_CASE(the_kinds_the_kernel_counts_skipped_runs_of_count),
+ UNIT_CASE(the_kinds_it_never_skips_or_never_counts_do_not),
+ UNIT_CASE(a_kprobe_link_adds_what_it_counts_itself),
{}
};