Skip to content

Commit abbf1f1

Browse files
authoredMar 9, 2024··
[Support] Fix Process::PreventCoreFiles() when coredumps are piped
On many current Linux systems, coredumps are no longer dumped in the CWD but instead piped to a utility such as systemd-coredumpd that stores them in a deterministic location. This can be done by setting the kernel.core_pattern sysctl to start with a '|'. However, when using such a setup the kernel ignores a coredump limit of 0 (since there is no file being written) and we can end up piping many gigabytes of data to systemd-coredumpd which causes the test suite to freeze for a long time. While most piped coredump handlers do respect the crashing processes' RLIMIT_CORE, this is notable not the case for Debian's systemd-coredump due to a local patch that changes sysctl.d/50-coredump.conf to ignore the specified limit and instead use RLIM_INFINITY (https://salsa.debian.org/systemd-team/systemd/-/commit/64599ffe44f0d). Fortunately there is a workaround: the kernel recognizes the magic value of 1 for RLIMIT_CORE to disable coredumps when piping. One byte is also too small to generate any coredump, so it effectively behaves as if we had set the value to zero. The alternative to using RLIMIT_CORE=1 would be to use prctl() with the PR_SET_DUMPABLE flag, however that also prevents ptrace(), so makes it impossible to attach a debugger. See #83701 and #45797 Reviewed By: MaskRay Pull Request: #83703
1 parent 578e66a commit abbf1f1

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed
 

‎llvm/lib/Support/Unix/Process.inc

+20-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,26 @@ void Process::GetTimeUsage(TimePoint<> &elapsed,
143143
void Process::PreventCoreFiles() {
144144
#if HAVE_SETRLIMIT
145145
struct rlimit rlim;
146-
rlim.rlim_cur = rlim.rlim_max = 0;
146+
getrlimit(RLIMIT_CORE, &rlim);
147+
#ifdef __linux__
148+
// On Linux, if the kernel.core_pattern sysctl starts with a '|' (i.e. it
149+
// is being piped to a coredump handler such as systemd-coredumpd), the
150+
// kernel ignores RLIMIT_CORE (since we aren't creating a file in the file
151+
// system) except for the magic value of 1, which disables coredumps when
152+
// piping. 1 byte is too small for any kind of valid core dump, so it
153+
// also disables coredumps if kernel.core_pattern creates files directly.
154+
// While most piped coredump handlers do respect the crashing processes'
155+
// RLIMIT_CORE, this is notable not the case for Debian's systemd-coredump
156+
// due to a local patch that changes sysctl.d/50-coredump.conf to ignore
157+
// the specified limit and instead use RLIM_INFINITY.
158+
//
159+
// The alternative to using RLIMIT_CORE=1 would be to use prctl() with the
160+
// PR_SET_DUMPABLE flag, however that also prevents ptrace(), so makes it
161+
// impossible to attach a debugger.
162+
rlim.rlim_cur = std::min<rlim_t>(1, rlim.rlim_max);
163+
#else
164+
rlim.rlim_cur = 0;
165+
#endif
147166
setrlimit(RLIMIT_CORE, &rlim);
148167
#endif
149168

0 commit comments

Comments
 (0)
Please sign in to comment.