diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-03 14:16:24 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-03 14:16:24 +0200 |
| commit | 1f8b6804f69632914ad0ab64892021315e99f421 (patch) | |
| tree | 0d5a37489c42beaaeeab2a255606919708712609 /internal/eventloop_test.go | |
| parent | 1d3a888c8db742ac5f2ba1c6cbb10603855dacb1 (diff) | |
Invalidate proc-fd cache on close_range
Diffstat (limited to 'internal/eventloop_test.go')
| -rw-r--r-- | internal/eventloop_test.go | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/internal/eventloop_test.go b/internal/eventloop_test.go index 49ba3e8..4ae8597 100644 --- a/internal/eventloop_test.go +++ b/internal/eventloop_test.go @@ -141,6 +141,67 @@ func TestEventloop(t *testing.T) { } } +func TestHandleFdExitCloseClearsProcFdCache(t *testing.T) { + el := newEventLoop(eventLoopConfig{}) + pid := uint32(1001) + fd := int32(55) + + el.setProcFdCache(fd, pid, file.NewFd(fd, "stale", syscall.O_RDONLY)) + verifyProcFdCached(t, el, pid, fd) + + enter := &types.FdEvent{ + TraceId: types.SYS_ENTER_CLOSE, + Pid: pid, + Tid: pid, + Fd: fd, + } + exit := &types.FdEvent{ + TraceId: types.SYS_EXIT_CLOSE, + Pid: pid, + Tid: pid, + Fd: fd, + } + ep := &event.Pair{EnterEv: enter, ExitEv: exit} + + if ok := el.handleFdExit(ep, enter); !ok { + t.Fatal("handleFdExit(close) returned false") + } + verifyProcFdNotCached(t, el, pid, fd) +} + +func TestHandleFdExitCloseRangeClearsProcFdCacheRange(t *testing.T) { + el := newEventLoop(eventLoopConfig{}) + pid := uint32(2002) + + el.setProcFdCache(10, pid, file.NewFd(10, "keep", syscall.O_RDONLY)) + el.setProcFdCache(20, pid, file.NewFd(20, "drop", syscall.O_RDONLY)) + el.setProcFdCache(30, pid, file.NewFd(30, "drop", syscall.O_RDONLY)) + el.setProcFdCache(20, pid+1, file.NewFd(20, "other-pid", syscall.O_RDONLY)) + + enter := &types.FdEvent{ + TraceId: types.SYS_ENTER_CLOSE_RANGE, + Pid: pid, + Tid: pid, + Fd: 20, + } + exit := &types.RetEvent{ + TraceId: types.SYS_EXIT_CLOSE_RANGE, + Pid: pid, + Tid: pid, + Ret: 0, + } + ep := &event.Pair{EnterEv: enter, ExitEv: exit} + + if ok := el.handleFdExit(ep, enter); !ok { + t.Fatal("handleFdExit(close_range) returned false") + } + + verifyProcFdCached(t, el, pid, 10) + verifyProcFdNotCached(t, el, pid, 20) + verifyProcFdNotCached(t, el, pid, 30) + verifyProcFdCached(t, el, pid+1, 20) +} + // Tests a simple enter/exit pair of tracepoints. func makeOpenEventTestData1(t *testing.T) (td testData) { enterEv, enterEvBytes := makeEnterOpenEvent(t, defaulTime, defaultPid, defaultTid) @@ -1616,6 +1677,18 @@ func verifyFdNotTracked(t *testing.T, el *eventLoop, fd int32) { } } +func verifyProcFdCached(t *testing.T, el *eventLoop, pid uint32, fd int32) { + if _, ok := el.cachedProcFdFile(fd, pid); !ok { + t.Errorf("Expected proc fd cache to contain pid=%d fd=%d", pid, fd) + } +} + +func verifyProcFdNotCached(t *testing.T, el *eventLoop, pid uint32, fd int32) { + if _, ok := el.cachedProcFdFile(fd, pid); ok { + t.Errorf("Expected proc fd cache to not contain pid=%d fd=%d", pid, fd) + } +} + // Helper functions for edge case tests func verifyNoEventOutput(t *testing.T, outCh <-chan *event.Pair, timeout time.Duration) { select { |
