[compiler-rt] r324034 - Add new interceptors: strlcpy(3) and strlcat(3)

Kamil Rytarowski via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 1 15:34:30 PST 2018


Author: kamil
Date: Thu Feb  1 15:34:30 2018
New Revision: 324034

URL: http://llvm.org/viewvc/llvm-project?rev=324034&view=rev
Log:
Add new interceptors: strlcpy(3) and strlcat(3)

Summary:
NetBSD ships with strlcpy(3) and strlcat(3), a safe
replacement of strcpy(3) and strcat(3).

Hide both functions under SANITIZER_INTERCEPT_STRLCPY.

Sponsored by <The NetBSD Foundation>

Reviewers: joerg, vitalybuka

Reviewed By: vitalybuka

Subscribers: llvm-commits, kubamracek, #sanitizers

Tags: #sanitizers

Differential Revision: https://reviews.llvm.org/D42061

Added:
    compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/strlcat.cc
    compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/strlcpy.cc
Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc?rev=324034&r1=324033&r2=324034&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc Thu Feb  1 15:34:30 2018
@@ -6724,6 +6724,41 @@ INTERCEPTOR(int, open_by_handle_at, int
 #define INIT_OPEN_BY_HANDLE_AT
 #endif
 
+#if SANITIZER_INTERCEPT_STRLCPY
+INTERCEPTOR(SIZE_T, strlcpy, char *dst, char *src, SIZE_T size) {
+  void *ctx;
+  SIZE_T res;
+  COMMON_INTERCEPTOR_ENTER(ctx, strlcpy, dst, src, size);
+  if (src) {
+    // Keep strnlen as macro argument, as macro may ignore it.
+    COMMON_INTERCEPTOR_READ_STRING(ctx, src,
+      Min(REAL(strnlen)(src, size), size - 1) + 1);
+  }
+  res = REAL(strlcpy)(dst, src, size);
+  COMMON_INTERCEPTOR_COPY_STRING(ctx, dst, src, REAL(strlen)(dst) + 1);
+  return res;
+}
+
+INTERCEPTOR(SIZE_T, strlcat, char *dst, char *src, SIZE_T size) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, strlcat, dst, src, size);
+  // src is checked in the strlcpy() interceptor
+  if (dst) {
+    SIZE_T len = REAL(strnlen)(dst, size);
+    COMMON_INTERCEPTOR_READ_STRING(ctx, dst, Min(len, size - 1) + 1);
+    // Reuse the rest of the code in the strlcpy() interceptor
+    dst += len;
+    size -= len;
+  }
+  return WRAP(strlcpy)(dst, src, size);
+}
+#define INIT_STRLCPY \
+  COMMON_INTERCEPT_FUNCTION(strlcpy); \
+  COMMON_INTERCEPT_FUNCTION(strlcat);
+#else
+#define INIT_STRLCPY
+#endif
+
 static void InitializeCommonInterceptors() {
   static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
   interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
@@ -6946,9 +6981,9 @@ static void InitializeCommonInterceptors
   INIT_GETGROUPLIST;
   INIT_READLINK;
   INIT_READLINKAT;
-
   INIT_NAME_TO_HANDLE_AT;
   INIT_OPEN_BY_HANDLE_AT;
+  INIT_STRLCPY;
 
 #if SANITIZER_NETBSD
   COMMON_INTERCEPT_FUNCTION(__libc_mutex_lock);

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h?rev=324034&r1=324033&r2=324034&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h Thu Feb  1 15:34:30 2018
@@ -441,6 +441,7 @@
 #define SANITIZER_INTERCEPT_ACCESS SI_NETBSD
 #define SANITIZER_INTERCEPT_FACCESSAT SI_NETBSD
 #define SANITIZER_INTERCEPT_GETGROUPLIST SI_NETBSD
+#define SANITIZER_INTERCEPT_STRLCPY SI_NETBSD
 
 #define SANITIZER_INTERCEPT_NAME_TO_HANDLE_AT SI_LINUX_NOT_ANDROID
 #define SANITIZER_INTERCEPT_OPEN_BY_HANDLE_AT SI_LINUX_NOT_ANDROID

Added: compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/strlcat.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/strlcat.cc?rev=324034&view=auto
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/strlcat.cc (added)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/strlcat.cc Thu Feb  1 15:34:30 2018
@@ -0,0 +1,52 @@
+// RUN: %clangxx -O0 -g %s -o %t && %run %t
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void test1() {
+  const char src[] = "abc";
+  char dst[7] = {'x', 'y', 'z', 0};
+  size_t len;
+
+  len = strlcat(dst, src, sizeof(dst));
+  printf("%s %zu ", dst, len);
+}
+
+void test2() {
+  const char src[] = "abc";
+  char dst[7] = {0};
+  size_t len;
+
+  len = strlcat(dst, src, sizeof(dst));
+  printf("%s %zu ", dst, len);
+}
+
+void test3() {
+  const char src[] = "abc";
+  char dst[4] = {'x', 'y', 'z', 0};
+  size_t len;
+
+  len = strlcat(dst, src, sizeof(dst));
+  printf("%s %zu ", dst, len);
+}
+
+void test4() {
+  const char src[] = "";
+  char dst[4] = {'x', 'y', 'z', 0};
+  size_t len;
+
+  len = strlcat(dst, src, sizeof(dst));
+  printf("%s %zu\n", dst, len);
+}
+
+int main(void) {
+  test1();
+  test2();
+  test3();
+  test4();
+
+  // CHECK: xyzabc 6 abc 3 xyz 3 xyz 3
+
+  return 0;
+}

Added: compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/strlcpy.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/strlcpy.cc?rev=324034&view=auto
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/strlcpy.cc (added)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/strlcpy.cc Thu Feb  1 15:34:30 2018
@@ -0,0 +1,52 @@
+// RUN: %clangxx -O0 -g %s -o %t && %run %t
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void test1() {
+  const char src[] = "abc";
+  char dst[7] = {'x', 'y', 'z', 0};
+  size_t len;
+
+  len = strlcpy(dst, src, sizeof(dst));
+  printf("%s %zu ", dst, len);
+}
+
+void test2() {
+  const char src[] = "abc";
+  char dst[7] = {0};
+  size_t len;
+
+  len = strlcat(dst, src, sizeof(dst));
+  printf("%s %zu ", dst, len);
+}
+
+void test3() {
+  const char src[] = "abc";
+  char dst[4] = {'x', 'y', 'z', 0};
+  size_t len;
+
+  len = strlcat(dst, src, sizeof(dst));
+  printf("%s %zu ", dst, len);
+}
+
+void test4() {
+  const char src[] = "";
+  char dst[4] = {'x', 'y', 'z', 0};
+  size_t len;
+
+  len = strlcat(dst, src, sizeof(dst));
+  printf("%s %zu\n", dst, len);
+}
+
+int main(void) {
+  test1();
+  test2();
+  test3();
+  test4();
+
+  // CHECK: abc 3 abc 3 xyz 3  0
+
+  return 0;
+}




More information about the llvm-commits mailing list