[llvm-branch-commits] [compiler-rt] [sanitizer_common] Check destination writes in strlcpy and strlcat (PR #239765)
Vitaly Buka via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Nov 6 11:48:49 PST 2026
https://github.com/vitalybuka updated https://github.com/llvm/llvm-project/pull/239765
>From 8e1c02e418192a5b4d48d70f637aaf56e45d30c8 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Fri, 6 Nov 2026 11:45:07 -0800
Subject: [PATCH] [spr] update
Created using spr 2.0.0-beta.2
---
.../sanitizer_common_interceptors.inc | 6 ++-
.../TestCases/Posix/strlcpy_dst_oob.cpp | 39 ++++++++++++++++
.../TestCases/Posix/strlcpy_tsan.cpp | 46 +++++++++++++++++++
3 files changed, 90 insertions(+), 1 deletion(-)
create mode 100644 compiler-rt/test/sanitizer_common/TestCases/Posix/strlcpy_dst_oob.cpp
create mode 100644 compiler-rt/test/sanitizer_common/TestCases/Posix/strlcpy_tsan.cpp
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;
+}
More information about the llvm-branch-commits
mailing list