llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Vitaly Buka (vitalybuka)

<details>
<summary>Changes</summary>

`COMMON_INTERCEPTOR_COPY_STRING` defaulted to a no-op outside MSan, so
`strlcpy` and `strlcat` did not check destination writes under ASan,
TSan, HWASan, or MemProf. Define the default fallback as
`COMMON_INTERCEPTOR_WRITE_RANGE(ctx, to, (size) + 1)`.

Assisted-by: Gemini

---
Full diff: https://github.com/llvm/llvm-project/pull/239765.diff


3 Files Affected:

- (modified) compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc 
(+5-1) 
- (added) compiler-rt/test/sanitizer_common/TestCases/Posix/strlcpy_dst_oob.cpp 
(+39) 
- (added) compiler-rt/test/sanitizer_common/TestCases/Posix/strlcpy_tsan.cpp 
(+46) 


``````````diff
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc 
b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
index f855fa9081591..f775ae2b5ea2f 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -341,7 +341,11 @@ extern const short *_tolower_tab_;
 #endif
 
 #ifndef COMMON_INTERCEPTOR_COPY_STRING
-#define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) {}
+#  define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) \
+    do {                                                      \
+      (void)(from);                                           \
+      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, to, (size) + 1);    \
+    } while (false)
 #endif
 
 #ifndef COMMON_INTERCEPTOR_STRNDUP_IMPL
diff --git 
a/compiler-rt/test/sanitizer_common/TestCases/Posix/strlcpy_dst_oob.cpp 
b/compiler-rt/test/sanitizer_common/TestCases/Posix/strlcpy_dst_oob.cpp
new file mode 100644
index 0000000000000..c8abc7a7c591d
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/strlcpy_dst_oob.cpp
@@ -0,0 +1,39 @@
+// RUN: %clangxx -O0 -g %s -o %t
+// RUN: not %run %t cpy 2>&1 | FileCheck %s --check-prefix=CPY
+// RUN: not %run %t cat 2>&1 | FileCheck %s --check-prefix=CAT
+
+// REQUIRES: asan
+// UNSUPPORTED: target={{.*linux.*}} && !glibc-2.38, target={{.*solaris.*}}
+
+#include <stdlib.h>
+#include <string.h>
+
+int main(int argc, char **argv) {
+  if (argc < 2)
+    return 1;
+
+  if (strcmp(argv[1], "cpy") == 0) {
+    volatile size_t alloc_size = 3;
+    char *dst = (char *)malloc(alloc_size);
+    // CPY: ERROR: AddressSanitizer: heap-buffer-overflow
+    // CPY: WRITE of size 4
+    // CPY: #0 {{.*}} in {{(wrap_|___interceptor_)?}}strlcpy
+    size_t res = strlcpy(dst, "abc", 4);
+    free(dst);
+    return res == 99;
+  }
+
+  if (strcmp(argv[1], "cat") == 0) {
+    volatile size_t alloc_size = 4;
+    char *dst = (char *)malloc(alloc_size);
+    memcpy(dst, "ab", 3);
+    // CAT: ERROR: AddressSanitizer: heap-buffer-overflow
+    // CAT: WRITE of size 3
+    // CAT: #0 {{.*}} in {{(wrap_|___interceptor_)?}}strlcat
+    size_t res = strlcat(dst, "cd", 5);
+    free(dst);
+    return res == 99;
+  }
+
+  return 1;
+}
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/strlcpy_tsan.cpp 
b/compiler-rt/test/sanitizer_common/TestCases/Posix/strlcpy_tsan.cpp
new file mode 100644
index 0000000000000..7400cd3bd052b
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/strlcpy_tsan.cpp
@@ -0,0 +1,46 @@
+// RUN: %clangxx -O0 -g %s -o %t
+// RUN: not %run %t cpy 2>&1 | FileCheck %s --check-prefix=CPY
+// RUN: not %run %t cat 2>&1 | FileCheck %s --check-prefix=CAT
+
+// REQUIRES: tsan
+// UNSUPPORTED: target={{.*linux.*}} && !glibc-2.38, target={{.*solaris.*}}
+
+#include <pthread.h>
+#include <string.h>
+
+#include "tsan/test.h"
+
+static char buf[8] = "ab";
+static volatile char sink;
+
+static void *reader_thread(void *arg) {
+  sink = buf[3];
+  barrier_wait(&barrier);
+  return nullptr;
+}
+
+int main(int argc, char **argv) {
+  if (argc < 2)
+    return 1;
+
+  barrier_init(&barrier, 2);
+  pthread_t th;
+  pthread_create(&th, nullptr, reader_thread, nullptr);
+  barrier_wait(&barrier);
+
+  size_t res = 0;
+  if (strcmp(argv[1], "cpy") == 0) {
+    // CPY: WARNING: ThreadSanitizer: data race
+    // CPY: Write of size 4 at {{.*}} by main thread:
+    // CPY: #0 {{(wrap_|___interceptor_)?}}strlcpy
+    res = strlcpy(buf, "abc", sizeof(buf));
+  } else if (strcmp(argv[1], "cat") == 0) {
+    // CAT: WARNING: ThreadSanitizer: data race
+    // CAT: Write of size 3 at {{.*}} by main thread:
+    // CAT: #0 {{(wrap_|___interceptor_)?}}strlcat
+    res = strlcat(buf, "cd", sizeof(buf));
+  }
+
+  pthread_join(th, nullptr);
+  return res == 99;
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/239765
_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to