From 3d5d80e75b94ce0602ba2939084afe5af8494f60 Mon Sep 17 00:00:00 2001 From: Ilya Leoshkevich Date: Thu, 18 Jul 2024 09:28:32 +0200 Subject: [PATCH] [sanitizer] Fix running sanitizer_bad_report_path_test on Linux as root (#97732) Summary: 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. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251653 --- .../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 fd4abf448b09da..00000000000000 --- 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 -#include -#include -#include - -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 17cee722749d6a..286eafc315bafb 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 #include @@ -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