From a8e8a695919679c9aac83541b01953ee9f2439dd Mon Sep 17 00:00:00 2001 From: George Burgess IV Date: Fri, 24 Jun 2022 12:03:52 -0700 Subject: [PATCH] elf_core_dump_unittest: skip test if setrlimit will fail Some systems have constrained rlimits for core files (the CrOS chroot is an example of this). Fail gracefully in this case, rather than breaking the user's tests. Bug: b:235999011 Change-Id: I5649b42d3e6fd9b4f9b11e1fd9d0d4a1083d300f Reviewed-on: https://chromium-review.googlesource.com/c/breakpad/breakpad/+/3722724 Reviewed-by: Mark Mentovai Reviewed-by: Mike Frysinger --- src/common/linux/elf_core_dump_unittest.cc | 10 +++++++--- src/common/linux/tests/crash_generator.cc | 9 +++++++++ src/common/linux/tests/crash_generator.h | 4 ++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/common/linux/elf_core_dump_unittest.cc b/src/common/linux/elf_core_dump_unittest.cc index 2399c12f..7854f31c 100644 --- a/src/common/linux/elf_core_dump_unittest.cc +++ b/src/common/linux/elf_core_dump_unittest.cc @@ -130,9 +130,13 @@ TEST(ElfCoreDumpTest, TestElfHeader) { TEST(ElfCoreDumpTest, ValidCoreFile) { CrashGenerator crash_generator; if (!crash_generator.HasDefaultCorePattern()) { - fprintf(stderr, "ElfCoreDumpTest.ValidCoreFile test is skipped " - "due to non-default core pattern"); - return; + GTEST_SKIP() << "ElfCoreDumpTest.ValidCoreFile test is skipped " + "due to non-default core pattern"; + } + + if (!crash_generator.HasResourceLimitsAmenableToCrashCollection()) { + GTEST_SKIP() << "ElfCoreDumpTest.ValidCoreFile test is skipped " + "due to inadequate system resource limits"; } const unsigned kNumOfThreads = 3; diff --git a/src/common/linux/tests/crash_generator.cc b/src/common/linux/tests/crash_generator.cc index a70df28a..976f5e45 100644 --- a/src/common/linux/tests/crash_generator.cc +++ b/src/common/linux/tests/crash_generator.cc @@ -169,6 +169,15 @@ bool CrashGenerator::SetCoreFileSizeLimit(rlim_t limit) const { return true; } +bool CrashGenerator::HasResourceLimitsAmenableToCrashCollection() const { + struct rlimit limits; + if (getrlimit(RLIMIT_CORE, &limits) == -1) { + perror("CrashGenerator: Failed to get core file size limit"); + return false; + } + return limits.rlim_max >= kCoreSizeLimit; +} + bool CrashGenerator::CreateChildCrash( unsigned num_threads, unsigned crash_thread, int crash_signal, pid_t* child_pid) { diff --git a/src/common/linux/tests/crash_generator.h b/src/common/linux/tests/crash_generator.h index 7e2fcbf9..6d1c2eae 100644 --- a/src/common/linux/tests/crash_generator.h +++ b/src/common/linux/tests/crash_generator.h @@ -65,6 +65,10 @@ class CrashGenerator { // Returns the directory of a copy of proc files of the child process. string GetDirectoryOfProcFilesCopy() const; + // Returns whether current resource limits would prevent `CreateChildCrash` + // from operating. + bool HasResourceLimitsAmenableToCrashCollection() const; + // Creates a crash (and a core dump file) by creating a child process with // |num_threads| threads, and the terminating the child process by sending // a signal with number |crash_signal| to the |crash_thread|-th thread.