summaryrefslogtreecommitdiff
path: root/internal/generate/codegen_test.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-05-30 21:50:29 +0300
committerPaul Buetow <paul@buetow.org>2026-05-30 21:50:29 +0300
commit654048bb6cb052a3b5f414508ab4839635865725 (patch)
treee14e5d1c5499091bdfa595fa43944ce6b4ca8c93 /internal/generate/codegen_test.go
parentb3e6798b340af13a1a2a4c670f5d55fae9f48a40 (diff)
test(generate): lock in listen(2) handler classification
Audit of syscall listen(2): int listen(int sockfd, int backlog). Confirmed the tracing implementation already matches the man page and its socket siblings (bind/connect/accept/getsockname/getpeername): - KindFd, capturing ev->fd = args[0] (sockfd) - FamilyNetwork - exit ret_event UNCLASSIFIED (returns 0/-1, no byte count) listen was already covered by the name-based classify/family/retclassify tests but lacked a dedicated generated-handler lock-in test like its bind/getsockname siblings. Add FormatListen/FormatExitListen tracepoint fixtures and TestGenerateListenHandler asserting the enter captures fd=args[0] (and never backlog at args[1]) and the exit stays UNCLASSIFIED. No classification or generated-code changes; mage generate produces no diff. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Diffstat (limited to 'internal/generate/codegen_test.go')
-rw-r--r--internal/generate/codegen_test.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/internal/generate/codegen_test.go b/internal/generate/codegen_test.go
index da86b86..818a2d2 100644
--- a/internal/generate/codegen_test.go
+++ b/internal/generate/codegen_test.go
@@ -123,6 +123,43 @@ func TestGenerateGetsocknameHandler(t *testing.T) {
requireNotContains(t, output, "ev->ret_type = TRANSFER_CLASSIFIED;")
}
+// TestGenerateListenHandler locks in the generated BPF C for listen(2):
+//
+// int listen(int sockfd, int backlog)
+//
+// listen marks the socket referred to by sockfd as passive (it will accept
+// incoming connections via accept(2)) and returns 0 on success or -1 on error.
+// Its sockfd is at args[0], so the enter handler is a KindFd fd_event capturing
+// ev->fd = args[0] — matching its socket siblings bind/connect/accept/
+// getsockname/getpeername. The backlog argument (args[1]) is a plain int queue
+// length, NOT a second fd, so it must NOT be captured. The exit handler is a
+// plain ret_event marked UNCLASSIFIED (0/-1, no byte count), so it must not
+// carry a READ/WRITE/TRANSFER classification — guarding against any mistaken
+// recvfrom/sendto-style byte-transfer accounting.
+func TestGenerateListenHandler(t *testing.T) {
+ output := generateFromPair(t, FormatListen, FormatExitListen)
+
+ // Enter: KindFd fd_event capturing the sockfd from args[0].
+ requireContains(t, output, `SEC("tracepoint/syscalls/sys_enter_listen")`)
+ requireContains(t, output, "struct fd_event *ev = bpf_ringbuf_reserve(&event_map, sizeof(struct fd_event), 0);")
+ requireContains(t, output, "ev->event_type = ENTER_FD_EVENT;")
+ requireContains(t, output, "ev->trace_id = SYS_ENTER_LISTEN;")
+ requireContains(t, output, "ev->fd = (__s32)ctx->args[0];")
+
+ // Negative guards: the backlog (args[1]) must not be captured as another fd,
+ // and listen reads no userspace path/buffer.
+ requireNotContains(t, output, "bpf_probe_read_user_str")
+ requireNotContains(t, output, "ev->fd = (__s32)ctx->args[1];")
+
+ // Exit: plain ret_event, UNCLASSIFIED (listen returns 0/-1, no byte count).
+ requireContains(t, output, `SEC("tracepoint/syscalls/sys_exit_listen")`)
+ requireContains(t, output, "struct ret_event *ev = bpf_ringbuf_reserve(&event_map, sizeof(struct ret_event), 0);")
+ requireContains(t, output, "ev->ret_type = UNCLASSIFIED;")
+ requireNotContains(t, output, "ev->ret_type = READ_CLASSIFIED;")
+ requireNotContains(t, output, "ev->ret_type = WRITE_CLASSIFIED;")
+ requireNotContains(t, output, "ev->ret_type = TRANSFER_CLASSIFIED;")
+}
+
func TestGeneratePidfdGetfdHandlerUsesPidfdArgument(t *testing.T) {
output := generateFromPair(t, FormatPidfdGetfd, FormatExitPidfdGetfd)