[msan] strsignal interceptor
authorVitaly Buka <vitalybuka@google.com>
Thu, 17 Feb 2022 21:06:11 +0000 (13:06 -0800)
committerVitaly Buka <vitalybuka@google.com>
Fri, 18 Feb 2022 02:13:35 +0000 (18:13 -0800)
Reviewed By: kstoimenov

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

compiler-rt/lib/msan/msan_interceptors.cpp
compiler-rt/test/msan/strsignal.cpp [new file with mode: 0644]

index 5317af6..dbe18ce 100644 (file)
@@ -1436,6 +1436,15 @@ static uptr signal_impl(int signo, uptr cb) {
 #include "sanitizer_common/sanitizer_common_syscalls.inc"
 #include "sanitizer_common/sanitizer_syscalls_netbsd.inc"
 
+INTERCEPTOR(const char *, strsignal, int sig) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, strsignal, sig);
+  const char *res = REAL(strsignal)(sig);
+  if (res)
+    __msan_unpoison(res, internal_strlen(res) + 1);
+  return res;
+}
+
 struct dlinfo {
   char *dli_fname;
   void *dli_fbase;
@@ -1699,6 +1708,7 @@ void InitializeInterceptors() {
   INTERCEPT_FUNCTION(gethostname);
   MSAN_MAYBE_INTERCEPT_EPOLL_WAIT;
   MSAN_MAYBE_INTERCEPT_EPOLL_PWAIT;
+  INTERCEPT_FUNCTION(strsignal);
   INTERCEPT_FUNCTION(dladdr);
   INTERCEPT_FUNCTION(dlerror);
   INTERCEPT_FUNCTION(dl_iterate_phdr);
diff --git a/compiler-rt/test/msan/strsignal.cpp b/compiler-rt/test/msan/strsignal.cpp
new file mode 100644 (file)
index 0000000..62b68e0
--- /dev/null
@@ -0,0 +1,13 @@
+// RUN: %clangxx_msan -O0 %s -o %t && %run %t
+
+#include <assert.h>
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+
+int main(void) {
+  const char *p = strsignal(SIGSEGV);
+  assert(p);
+  printf("%s %zu\n", p, strlen(p));
+  return 0;
+}