[PATCH] D59548: [Sanitizer] Add interceptor for wctomb

Pavel Labath via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 19 08:37:07 PDT 2019


labath created this revision.
labath added reviewers: eugenis, EricWF.
Herald added subscribers: delcypher, fedor.sergeev, kubamracek, srhines.
Herald added projects: LLVM, Sanitizers.

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).

The interceptor and test code have been based on wcrtomb, with obvious
modifications.


Repository:
  rCRT Compiler Runtime

https://reviews.llvm.org/D59548

Files:
  lib/msan/tests/msan_test.cc
  lib/sanitizer_common/sanitizer_common_interceptors.inc
  lib/sanitizer_common/sanitizer_platform_interceptors.h


Index: lib/sanitizer_common/sanitizer_platform_interceptors.h
===================================================================
--- lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ lib/sanitizer_common/sanitizer_platform_interceptors.h
@@ -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 \
Index: lib/sanitizer_common/sanitizer_common_interceptors.inc
===================================================================
--- lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -3540,6 +3540,26 @@
 #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);
+  // FIXME: under ASan the call below may write to freed memory and corrupt
+  // its metadata. See
+  // https://github.com/google/sanitizers/issues/321.
+  int res = REAL(wctomb)(dest, src);
+  if (res != -1 && dest) {
+    SIZE_T write_cnt = res;
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
+  }
+  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 +9636,7 @@
   INIT_WCSTOMBS;
   INIT_WCSNRTOMBS;
   INIT_WCRTOMB;
+  INIT_WCTOMB;
   INIT_TCGETATTR;
   INIT_REALPATH;
   INIT_CANONICALIZE_FILE_NAME;
Index: lib/msan/tests/msan_test.cc
===================================================================
--- lib/msan/tests/msan_test.cc
+++ lib/msan/tests/msan_test.cc
@@ -2123,6 +2123,15 @@
   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');
+}
+
 TEST(MemorySanitizer, wmemset) {
     wchar_t x[25];
     break_optimization(x);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59548.191312.patch
Type: text/x-patch
Size: 2432 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190319/98af2913/attachment.bin>


More information about the llvm-commits mailing list