[compiler-rt] r357240 - [Sanitizer] Add interceptor for wctomb

Pavel Labath via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 29 01:39:03 PDT 2019


Author: labath
Date: Fri Mar 29 01:39:03 2019
New Revision: 357240

URL: http://llvm.org/viewvc/llvm-project?rev=357240&view=rev
Log:
[Sanitizer] Add interceptor for wctomb

Summary:
This is required to avoid msan false positives for code using this
function (although generally one should avoid using this function in
favor of wcrtomb).

Reviewers: eugenis, EricWF, vitalybuka

Subscribers: srhines, kubamracek, fedor.sergeev, delcypher, llvm-commits, #sanitizers

Tags: #llvm, #sanitizers

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

Added:
    compiler-rt/trunk/test/sanitizer_common/TestCases/wctomb.c
Modified:
    compiler-rt/trunk/lib/msan/tests/msan_test.cc
    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/msan/tests/msan_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/tests/msan_test.cc?rev=357240&r1=357239&r2=357240&view=diff
==============================================================================
--- compiler-rt/trunk/lib/msan/tests/msan_test.cc (original)
+++ compiler-rt/trunk/lib/msan/tests/msan_test.cc Fri Mar 29 01:39:03 2019
@@ -2123,6 +2123,16 @@ TEST(MemorySanitizer, wcrtomb) {
   EXPECT_EQ(buff[0], 'a');
 }
 
+TEST(MemorySanitizer, wctomb) {
+  wchar_t x = L'a';
+  char buff[10];
+  wctomb(nullptr, x);
+  int res = wctomb(buff, x);
+  EXPECT_EQ(res, 1);
+  EXPECT_EQ(buff[0], 'a');
+  EXPECT_POISONED(buff[1]);
+}
+
 TEST(MemorySanitizer, wmemset) {
     wchar_t x[25];
     break_optimization(x);

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=357240&r1=357239&r2=357240&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc Fri Mar 29 01:39:03 2019
@@ -3540,6 +3540,28 @@ INTERCEPTOR(SIZE_T, wcrtomb, char *dest,
 #define INIT_WCRTOMB
 #endif
 
+#if SANITIZER_INTERCEPT_WCTOMB
+INTERCEPTOR(int, wctomb, char *dest, wchar_t src) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, wctomb, dest, src);
+  if (!dest)
+    return REAL(wctomb)(dest, src);
+
+  char local_dest[32];
+  int res = REAL(wctomb)(local_dest, src);
+  if (res != -1) {
+    CHECK_LE(res, sizeof(local_dest));
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res);
+    REAL(memcpy)(dest, local_dest, res);
+  }
+  return res;
+}
+
+#define INIT_WCTOMB COMMON_INTERCEPT_FUNCTION(wctomb);
+#else
+#define INIT_WCTOMB
+#endif
+
 #if SANITIZER_INTERCEPT_TCGETATTR
 INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
   void *ctx;
@@ -9616,6 +9638,7 @@ static void InitializeCommonInterceptors
   INIT_WCSTOMBS;
   INIT_WCSNRTOMBS;
   INIT_WCRTOMB;
+  INIT_WCTOMB;
   INIT_TCGETATTR;
   INIT_REALPATH;
   INIT_CANONICALIZE_FILE_NAME;

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=357240&r1=357239&r2=357240&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h Fri Mar 29 01:39:03 2019
@@ -283,6 +283,9 @@
 #define SANITIZER_INTERCEPT_WCRTOMB                                           \
   (SI_FREEBSD || SI_NETBSD || SI_OPENBSD || SI_MAC || SI_LINUX_NOT_ANDROID || \
   SI_SOLARIS)
+#define SANITIZER_INTERCEPT_WCTOMB                                           \
+  (SI_FREEBSD || SI_NETBSD || SI_OPENBSD || SI_MAC || SI_LINUX_NOT_ANDROID || \
+  SI_SOLARIS)
 #define SANITIZER_INTERCEPT_TCGETATTR SI_LINUX_NOT_ANDROID || SI_SOLARIS
 #define SANITIZER_INTERCEPT_REALPATH SI_POSIX
 #define SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME \

Added: compiler-rt/trunk/test/sanitizer_common/TestCases/wctomb.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/wctomb.c?rev=357240&view=auto
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/wctomb.c (added)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/wctomb.c Fri Mar 29 01:39:03 2019
@@ -0,0 +1,14 @@
+// RUN: %clang %s -o %t && %run %t 2>&1
+
+#include <assert.h>
+#include <stdlib.h>
+
+int main(int argc, char **argv) {
+  char buff[10];
+  wchar_t x = L'a';
+  wctomb(NULL, x);
+  int res = wctomb(buff, x);
+  assert(res == 1);
+  assert(buff[0] == 'a');
+  return 0;
+}




More information about the llvm-commits mailing list