Skip to content
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

[msan] Intercept mallinfo2 #73729

Merged
merged 4 commits into from
Nov 29, 2023

Conversation

vitalybuka
Copy link
Collaborator

No description provided.

@llvmbot
Copy link
Collaborator

llvmbot commented Nov 29, 2023

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

Author: Vitaly Buka (vitalybuka)

Changes

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

3 Files Affected:

  • (modified) compiler-rt/lib/msan/msan_interceptors.cpp (+10)
  • (modified) compiler-rt/lib/sanitizer_common/sanitizer_mallinfo.h (+4)
  • (modified) compiler-rt/test/msan/Linux/mallinfo.cpp (+3)
diff --git a/compiler-rt/lib/msan/msan_interceptors.cpp b/compiler-rt/lib/msan/msan_interceptors.cpp
index dfecf6f7c470a6c..680b7d397ff0622 100644
--- a/compiler-rt/lib/msan/msan_interceptors.cpp
+++ b/compiler-rt/lib/msan/msan_interceptors.cpp
@@ -260,9 +260,18 @@ INTERCEPTOR(__sanitizer_struct_mallinfo, mallinfo) {
   return sret;
 }
 
+// Interceptor relies on NRVO and assumes that sret will be pre-allocated in
+// caller frame.
+INTERCEPTOR(__sanitizer_struct_mallinfo2, mallinfo2) {
+  __sanitizer_struct_mallinfo2 sret;
+  clear_mallinfo(&sret);
+  return sret;
+}
 #  define MSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo)
+#  define MSAN_MAYBE_INTERCEPT_MALLINFO2 INTERCEPT_FUNCTION(mallinfo2)
 #else
 #define MSAN_MAYBE_INTERCEPT_MALLINFO
+#  define MSAN_MAYBE_INTERCEPT_MALLINFO2
 #endif
 
 #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
@@ -1787,6 +1796,7 @@ void InitializeInterceptors() {
   MSAN_MAYBE_INTERCEPT_CFREE;
   MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE;
   MSAN_MAYBE_INTERCEPT_MALLINFO;
+  MSAN_MAYBE_INTERCEPT_MALLINFO2;
   MSAN_MAYBE_INTERCEPT_MALLOPT;
   MSAN_MAYBE_INTERCEPT_MALLOC_STATS;
   INTERCEPT_FUNCTION(fread);
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_mallinfo.h b/compiler-rt/lib/sanitizer_common/sanitizer_mallinfo.h
index 4e58c02df835190..1c07e68e55a7d3b 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_mallinfo.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_mallinfo.h
@@ -31,6 +31,10 @@ struct __sanitizer_struct_mallinfo {
   int v[10];
 };
 
+struct __sanitizer_struct_mallinfo2 {
+  uptr v[10];
+};
+
 #endif
 
 }  // namespace __sanitizer
diff --git a/compiler-rt/test/msan/Linux/mallinfo.cpp b/compiler-rt/test/msan/Linux/mallinfo.cpp
index 3c3692969852f9b..f061218c615a3a9 100644
--- a/compiler-rt/test/msan/Linux/mallinfo.cpp
+++ b/compiler-rt/test/msan/Linux/mallinfo.cpp
@@ -8,5 +8,8 @@
 int main(void) {
   struct mallinfo mi = mallinfo();
   assert(__msan_test_shadow(&mi, sizeof(mi)) == -1);
+
+  struct mallinfo2 mi2 = mallinfo2();
+  assert(__msan_test_shadow(&mi2, sizeof(mi2)) == -1);
   return 0;
 }

Created using spr 1.3.4
@vitalybuka vitalybuka changed the base branch from users/vitalybuka/spr/main.msan-intercept-mallinfo2 to main November 29, 2023 06:22
Created using spr 1.3.4
@vitalybuka vitalybuka merged commit 6a63495 into main Nov 29, 2023
3 checks passed
@vitalybuka vitalybuka deleted the users/vitalybuka/spr/msan-intercept-mallinfo2 branch November 29, 2023 19:00
@artagnon
Copy link
Contributor

Hi, this commit broke the buildbot:

/home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/llvm-project/compiler-rt/test/msan/Linux/mallinfo.cpp:12:26: error: invalid use of incomplete type 'mallinfo2'
   12 |   struct mallinfo2 mi2 = mallinfo2();
      |                          ^~~~~~~~~~~

vitalybuka added a commit that referenced this pull request Nov 29, 2023
@vitalybuka
Copy link
Collaborator Author

Should be fixed with 771e9cd

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.

None yet

4 participants