[compiler-rt] 8afb395 - [sanitizer] Fix running sanitizer_bad_report_path_test on Linux as root (#97732)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 18 00:28:36 PDT 2024


Author: Ilya Leoshkevich
Date: 2024-07-18T09:28:32+02:00
New Revision: 8afb395aeef5335554c623f92f48cbdc9ffe927d

URL: https://github.com/llvm/llvm-project/commit/8afb395aeef5335554c623f92f48cbdc9ffe927d
DIFF: https://github.com/llvm/llvm-project/commit/8afb395aeef5335554c623f92f48cbdc9ffe927d.diff

LOG: [sanitizer] Fix running sanitizer_bad_report_path_test on Linux as root (#97732)

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.

Added: 
    

Modified: 
    compiler-rt/test/sanitizer_common/TestCases/Posix/sanitizer_set_report_path_test.cpp

Removed: 
    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