[llvm] [libc] [libc] Fix read under msan (PR #80203)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 31 14:51:43 PST 2024


https://github.com/michaelrj-google updated https://github.com/llvm/llvm-project/pull/80203

>From 5fc126b4c45e7495d6e14457062513d7d10ab35b Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Wed, 31 Jan 2024 13:43:06 -0800
Subject: [PATCH 1/2] [libc] Fix read under msan

The read function wasn't properly unpoisoning its result under msan,
causing test failures downstream when I tried to roll it out. This patch
adds the msan unpoison call that fixes the issue.
---
 libc/src/unistd/linux/read.cpp                    | 5 +++++
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 1 +
 2 files changed, 6 insertions(+)

diff --git a/libc/src/unistd/linux/read.cpp b/libc/src/unistd/linux/read.cpp
index 691a236982e37..f5248a8547d44 100644
--- a/libc/src/unistd/linux/read.cpp
+++ b/libc/src/unistd/linux/read.cpp
@@ -11,6 +11,8 @@
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 
+#include "src/__support/macros/sanitizer.h" // for MSAN_UNPOISON
+
 #include "src/errno/libc_errno.h"
 #include <sys/syscall.h> // For syscall numbers.
 
@@ -22,6 +24,9 @@ LLVM_LIBC_FUNCTION(ssize_t, read, (int fd, void *buf, size_t count)) {
     libc_errno = static_cast<int>(-ret);
     return -1;
   }
+  // The cast is important since there is a check that dereferences the pointer
+  // which fails on void*.
+  MSAN_UNPOISON(reinterpret_cast<char *>(buf), count);
   return ret;
 }
 
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 3b8ce044b7fc7..3d8c5eb178ec9 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2599,6 +2599,7 @@ libc_function(
     hdrs = ["src/unistd/read.h"],
     deps = [
         ":__support_common",
+        ":__support_macros_sanitizer",
         ":__support_osutil_syscall",
         ":errno",
     ],

>From ae48411ff19370a5b9754fee5721fb38380147ec Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Wed, 31 Jan 2024 14:51:25 -0800
Subject: [PATCH 2/2] clean up spaces between includes

---
 libc/src/unistd/linux/read.cpp | 2 --
 1 file changed, 2 deletions(-)

diff --git a/libc/src/unistd/linux/read.cpp b/libc/src/unistd/linux/read.cpp
index f5248a8547d44..41be1eb10c0de 100644
--- a/libc/src/unistd/linux/read.cpp
+++ b/libc/src/unistd/linux/read.cpp
@@ -10,9 +10,7 @@
 
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
-
 #include "src/__support/macros/sanitizer.h" // for MSAN_UNPOISON
-
 #include "src/errno/libc_errno.h"
 #include <sys/syscall.h> // For syscall numbers.
 



More information about the llvm-commits mailing list