[compiler-rt] [sanitizer] Fix running sanitizer_bad_report_path_test on Linux as root (PR #97732)
Ilya Leoshkevich via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 12 05:21:48 PDT 2024
https://github.com/iii-i updated https://github.com/llvm/llvm-project/pull/97732
>From 069eb2c8d482a81fce7ce724dfcdc4be8999cb41 Mon Sep 17 00:00:00 2001
From: Ilya Leoshkevich <iii at linux.ibm.com>
Date: Fri, 12 Jul 2024 14:11:52 +0200
Subject: [PATCH 1/3] [sanitizer] Suggest checking ulimit -d in addition to
ulimit -v
Since Linux 4.7, RLIMIT_DATA may result in mmap() returning ENOMEM.
Example:
$ clang -fsanitize=address -o hello hello.c
$ ulimit -d 100000
$ ./hello
==3349007==ERROR: AddressSanitizer failed to allocate 0x10000000 (268435456) bytes at address 7fff7000 (errno: 12)
==3349007==ReserveShadowMemoryRange failed while trying to map 0x10000000 bytes. Perhaps you're using ulimit -v
Suggest checking ulimit -d in addition to ulimit -v.
---
compiler-rt/lib/sanitizer_common/sanitizer_common_libcdep.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_common_libcdep.cpp
index a174ae7be991d..3966d82d51ee9 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_libcdep.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_libcdep.cpp
@@ -169,7 +169,7 @@ void ReserveShadowMemoryRange(uptr beg, uptr end, const char *name,
: !MmapFixedNoReserve(beg, size, name)) {
Report(
"ReserveShadowMemoryRange failed while trying to map 0x%zx bytes. "
- "Perhaps you're using ulimit -v\n",
+ "Perhaps you're using ulimit -v or ulimit -d\n",
size);
Abort();
}
>From 730ac1e26f26b7f4b8df744b890e172ba6317d14 Mon Sep 17 00:00:00 2001
From: Ilya Leoshkevich <iii at linux.ibm.com>
Date: Fri, 12 Jul 2024 14:02:57 +0200
Subject: [PATCH 2/3] [sanitizer] Add missing newlines to
__sanitizer_set_report_path() error messages
"Can't open file:" and "Can't create directory:" are lacking a newline.
---
compiler-rt/lib/sanitizer_common/sanitizer_file.cpp | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_file.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_file.cpp
index 7ef499ce07b13..96af270f9d8b5 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_file.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_file.cpp
@@ -69,7 +69,7 @@ void ReportFile::ReopenIfNecessary() {
WriteToFile(kStderrFd, ErrorMsgPrefix, internal_strlen(ErrorMsgPrefix));
WriteToFile(kStderrFd, full_path, internal_strlen(full_path));
char errmsg[100];
- internal_snprintf(errmsg, sizeof(errmsg), " (reason: %d)", err);
+ internal_snprintf(errmsg, sizeof(errmsg), " (reason: %d)\n", err);
WriteToFile(kStderrFd, errmsg, internal_strlen(errmsg));
Die();
}
@@ -88,6 +88,8 @@ static void RecursiveCreateParentDirs(char *path) {
const char *ErrorMsgPrefix = "ERROR: Can't create directory: ";
WriteToFile(kStderrFd, ErrorMsgPrefix, internal_strlen(ErrorMsgPrefix));
WriteToFile(kStderrFd, path, internal_strlen(path));
+ const char *ErrorMsgSuffix = "\n";
+ WriteToFile(kStderrFd, ErrorMsgSuffix, internal_strlen(ErrorMsgSuffix));
Die();
}
path[i] = save;
>From a52001c02e1b2c25f2f4512f9227a5596122df08 Mon Sep 17 00:00:00 2001
From: Ilya Leoshkevich <iii at linux.ibm.com>
Date: Fri, 12 Jul 2024 12:04:16 +0200
Subject: [PATCH 3/3] [sanitizer] Fix running sanitizer_bad_report_path_test on
Linux as root
Running tests as root is not the greatest idea, however, there is one
valid use case - running them in a container in order to verify LLVM on
different distros. There is no reason to configure unprivileged users
in a container, so one works as root.
sanitizer_bad_report_path_test assumes that creating a file in a
non-writable directory would fail, which is not the always case. For
example, when we are on Linux and CAP_DAC_OVERRIDE, which root has, is
in effect. Therefore, one solution is to drop it. However, that would
be Linux-specific.
Instead, use argv[0] as if it were a directory. mkdir() on top of a
file should be prohibited by all supported Posix operating systems.
Combine this with a partial revert of commit f4214e1469ad ("[sanitizer]
Skip test on Android where chmod is not working"), since we shouldn't
need to exclude Android anymore.
---
.../Posix/sanitizer_bad_report_path_test.cpp | 27 -------------------
.../Posix/sanitizer_set_report_path_test.cpp | 10 ++++++-
2 files changed, 9 insertions(+), 28 deletions(-)
delete mode 100644 compiler-rt/test/sanitizer_common/TestCases/Posix/sanitizer_bad_report_path_test.cpp
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/sanitizer_bad_report_path_test.cpp b/compiler-rt/test/sanitizer_common/TestCases/Posix/sanitizer_bad_report_path_test.cpp
deleted file mode 100644
index fd4abf448b09d..0000000000000
--- a/compiler-rt/test/sanitizer_common/TestCases/Posix/sanitizer_bad_report_path_test.cpp
+++ /dev/null
@@ -1,27 +0,0 @@
-// Test __sanitizer_set_report_path and __sanitizer_get_report_path with an
-// unwritable directory.
-// RUN: rm -rf %t.report_path && mkdir -p %t.report_path
-// RUN: chmod u-w %t.report_path || true
-// RUN: %clangxx -O2 %s -o %t
-// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=FAIL
-
-// The chmod is not working on the android bot for some reason.
-// UNSUPPORTED: android
-
-#include <assert.h>
-#include <sanitizer/common_interface_defs.h>
-#include <stdio.h>
-#include <string.h>
-
-volatile int *null = 0;
-
-int main(int argc, char **argv) {
- char buff[1000];
- sprintf(buff, "%s.report_path/report", argv[0]);
- __sanitizer_set_report_path(buff);
- assert(strncmp(buff, __sanitizer_get_report_path(), strlen(buff)) == 0);
- printf("Path %s\n", __sanitizer_get_report_path());
-}
-
-// FAIL: ERROR: Can't open file: {{.*}}Posix/Output/sanitizer_bad_report_path_test.cpp.tmp.report_path/report.
-// FAIL-NOT: Path {{.*}}Posix/Output/sanitizer_bad_report_path_test.cpp.tmp.report_path/report.
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/sanitizer_set_report_path_test.cpp b/compiler-rt/test/sanitizer_common/TestCases/Posix/sanitizer_set_report_path_test.cpp
index 17cee722749d6..286eafc315baf 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Posix/sanitizer_set_report_path_test.cpp
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/sanitizer_set_report_path_test.cpp
@@ -1,6 +1,6 @@
// Test __sanitizer_set_report_path and __sanitizer_get_report_path:
// RUN: %clangxx -O2 %s -o %t
-// RUN: %run %t | FileCheck %s
+// RUN: not %run %t 2>&1 | FileCheck %s
#include <assert.h>
#include <sanitizer/common_interface_defs.h>
@@ -15,6 +15,14 @@ int main(int argc, char **argv) {
__sanitizer_set_report_path(buff);
assert(strncmp(buff, __sanitizer_get_report_path(), strlen(buff)) == 0);
printf("Path %s\n", __sanitizer_get_report_path());
+ fflush(stdout);
+
+ // Try setting again with an invalid/inaccessible directory.
+ sprintf(buff, "%s/report", argv[0]);
+ __sanitizer_set_report_path(buff);
+ printf("Path %s\n", __sanitizer_get_report_path());
}
// CHECK: Path {{.*}}Posix/Output/sanitizer_set_report_path_test.cpp.tmp.report_path/report.
+// CHECK: ERROR: Can't create directory: {{.*}}Posix/Output/sanitizer_set_report_path_test.cpp.tmp
+// CHECK-NOT: Path {{.*}}Posix/Output/sanitizer_set_report_path_test.cpp.tmp
More information about the llvm-commits
mailing list