Skip to content

[msan][aarch64] Fix mallinfo interceptor #73728

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

vitalybuka
Copy link
Collaborator

Not sure how the previous implementation supposed to work, but the test
was disabled.

This implementation works for x86_64 and aarch64.

Created using spr 1.3.4
@llvmbot
Copy link
Member

llvmbot commented Nov 29, 2023

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Vitaly Buka (vitalybuka)

Changes

Not sure how the previous implementation supposed to work, but the test
was disabled.

This implementation works for x86_64 and aarch64.


Full diff: https://github.com/llvm/llvm-project/pull/73728.diff

2 Files Affected:

  • (modified) compiler-rt/lib/msan/msan_interceptors.cpp (+15-12)
  • (modified) compiler-rt/test/msan/Linux/mallinfo.cpp (-1)
diff --git a/compiler-rt/lib/msan/msan_interceptors.cpp b/compiler-rt/lib/msan/msan_interceptors.cpp
index bac756424e28ca7..dfecf6f7c470a6c 100644
--- a/compiler-rt/lib/msan/msan_interceptors.cpp
+++ b/compiler-rt/lib/msan/msan_interceptors.cpp
@@ -244,20 +244,23 @@ INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
 #endif
 
 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
-// This function actually returns a struct by value, but we can't unpoison a
-// temporary! The following is equivalent on all supported platforms but
-// aarch64 (which uses a different register for sret value).  We have a test
-// to confirm that.
-INTERCEPTOR(void, mallinfo, __sanitizer_struct_mallinfo *sret) {
-#ifdef __aarch64__
-  uptr r8;
-  asm volatile("mov %0,x8" : "=r" (r8));
-  sret = reinterpret_cast<__sanitizer_struct_mallinfo*>(r8);
-#endif
-  REAL(memset)(sret, 0, sizeof(*sret));
+
+template <class T>
+static NOINLINE void clear_mallinfo(T *sret) {
+  ENSURE_MSAN_INITED();
+  internal_memset(sret, 0, sizeof(*sret));
   __msan_unpoison(sret, sizeof(*sret));
 }
-#define MSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo)
+
+// Interceptor relies on NRVO and assumes that sret will be pre-allocated in
+// caller frame.
+INTERCEPTOR(__sanitizer_struct_mallinfo, mallinfo) {
+  __sanitizer_struct_mallinfo sret;
+  clear_mallinfo(&sret);
+  return sret;
+}
+
+#  define MSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo)
 #else
 #define MSAN_MAYBE_INTERCEPT_MALLINFO
 #endif
diff --git a/compiler-rt/test/msan/Linux/mallinfo.cpp b/compiler-rt/test/msan/Linux/mallinfo.cpp
index b2021c5df3cec84..3c3692969852f9b 100644
--- a/compiler-rt/test/msan/Linux/mallinfo.cpp
+++ b/compiler-rt/test/msan/Linux/mallinfo.cpp
@@ -1,5 +1,4 @@
 // RUN: %clangxx_msan -O0 -g %s -o %t && %run %t
-// UNSUPPORTED: aarch64-target-arch
 
 #include <assert.h>
 #include <malloc.h>

@thurstond
Copy link
Contributor

AFAICS the interceptor zeros out the struct - which is enough to pass compiler-rt/test/msan/Linux/mallinfo.cpp, because it only tests that the struct is unpoisoned, not the actual contents - but doesn't actually give any useful info. Do any apps need the real info?

@vitalybuka
Copy link
Collaborator Author

vitalybuka commented Nov 29, 2023

AFAICS the interceptor zeros out the struct - which is enough to pass compiler-rt/test/msan/Linux/mallinfo.cpp, because it only tests that the struct is unpoisoned, not the actual contents - but doesn't actually give any useful info. Do any apps need the real info?

This fact is unchanged by the patch.
Real libc malloc info is irrelevant as we use msan allocator.
And some fields are irrelevant mallinfo.

@vitalybuka vitalybuka changed the title [msan][aarch64] mallinfo interceptor [msan][aarch64] Fix mallinfo interceptor Nov 29, 2023
@vitalybuka vitalybuka merged commit c954414 into main Nov 29, 2023
@vitalybuka vitalybuka deleted the users/vitalybuka/spr/msanaarch64-mallinfo-interceptor branch November 29, 2023 05:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants