With --track-fds=yes warn when file descriptor is closed a second time
commit6a28e665f8dd7c031aef5aa0eaa4acbbd8ba27a9
authorAlexandra Hájková <ahajkova@redhat.com>
Wed, 28 Feb 2024 08:02:15 +0000 (28 09:02 +0100)
committerMark Wielaard <mark@klomp.org>
Wed, 13 Mar 2024 14:46:06 +0000 (13 15:46 +0100)
tree464981944e726cf09d4e3e9701cbb94162fd8769
parent0d44801e395934dee02d831e35321ebc65b26322
With --track-fds=yes warn when file descriptor is closed a second time

We moved the record_fd_close call from POST to PRE sys_close handler,
because the POST handler is only called on success. Even if the close
syscall fails the file descriptor is still really closed/invalid.
In the PRE handler the file descriptor is about to be closed, but hasn't
been yet so we can capture also the description.

This patch add new field fd_closed to OpenFd structure to record if
the file descriptor was already closed.

We now capture a backtrace when closing file descriptors to be able to
print it in a case of a double close.  Always add '<' brackets '>' around
"unbound" in the description for consistency.

getsockdetails now takes and returns a buffer describing the socket
because we want to record it, not just print it.

Note that close_range is handled similar to closing each descriptor
individually. But the case when the close_range is called with an
infinite end (~0U) is treated special. Add a new record_fd_close_range
function which handles close_range with an infinite end so double
close by close_range isn't an error because we don't want to loop
over such a wide range.

Add a new test cases:
 - none/tests/socket_close.vgtest
   - tests double closing a socket
 - none/tests/double_close_range.vgtest
   - uses close_range to double close the file descriptors
 - none/tests/file_dclose.vgtest
   - double closing regular file with regular close syscall

https://bugs.kde.org/show_bug.cgi?id=471222

Co-Authored-By: Mark Wielaard <mark@klomp.org>
30 files changed:
NEWS
coregrind/m_syswrap/priv_syswrap-generic.h
coregrind/m_syswrap/syswrap-amd64-linux.c
coregrind/m_syswrap/syswrap-arm-linux.c
coregrind/m_syswrap/syswrap-arm64-linux.c
coregrind/m_syswrap/syswrap-darwin.c
coregrind/m_syswrap/syswrap-freebsd.c
coregrind/m_syswrap/syswrap-generic.c
coregrind/m_syswrap/syswrap-linux.c
coregrind/m_syswrap/syswrap-mips32-linux.c
coregrind/m_syswrap/syswrap-mips64-linux.c
coregrind/m_syswrap/syswrap-nanomips-linux.c
coregrind/m_syswrap/syswrap-ppc32-linux.c
coregrind/m_syswrap/syswrap-ppc64-linux.c
coregrind/m_syswrap/syswrap-s390x-linux.c
coregrind/m_syswrap/syswrap-solaris.c
coregrind/m_syswrap/syswrap-x86-linux.c
none/tests/Makefile.am
none/tests/double_close_range.c [new file with mode: 0644]
none/tests/double_close_range.stderr.exp [new file with mode: 0644]
none/tests/double_close_range.vgtest [new file with mode: 0644]
none/tests/fdleak.h
none/tests/fdleak_ipv4.c
none/tests/fdleak_ipv4.stderr.exp
none/tests/file_dclose.c [new file with mode: 0644]
none/tests/file_dclose.stderr.exp [new file with mode: 0644]
none/tests/file_dclose.vgtest [new file with mode: 0644]
none/tests/socket_close.c [new file with mode: 0644]
none/tests/socket_close.stderr.exp [new file with mode: 0644]
none/tests/socket_close.vgtest [new file with mode: 0644]