Skip to content

Commit

Permalink
[msan] Intercept mallinfo2 only on GLIBC 2.33+
Browse files Browse the repository at this point in the history
Followup to #73729
  • Loading branch information
vitalybuka committed Nov 29, 2023
1 parent e947f95 commit 771e9cd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
12 changes: 8 additions & 4 deletions compiler-rt/lib/msan/msan_interceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,32 +243,36 @@ INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
#define MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE
#endif

#if !SANITIZER_FREEBSD && !SANITIZER_NETBSD

#if (!SANITIZER_FREEBSD && !SANITIZER_NETBSD) || __GLIBC_PREREQ(2, 33)
template <class T>
static NOINLINE void clear_mallinfo(T *sret) {
ENSURE_MSAN_INITED();
internal_memset(sret, 0, sizeof(*sret));
__msan_unpoison(sret, sizeof(*sret));
}
#endif

#if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
// Interceptors use NRVO and assume 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

#if __GLIBC_PREREQ(2, 33)
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

Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/test/lit.common.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ def add_glibc_versions(ver_string):

ver = LooseVersion(ver_string)
any_glibc = False
for required in ["2.19", "2.27", "2.30", "2.34", "2.37"]:
for required in ["2.19", "2.27", "2.30", "2.33", "2.34", "2.37"]:
if ver >= LooseVersion(required):
config.available_features.add("glibc-" + required)
any_glibc = True
Expand Down
3 changes: 0 additions & 3 deletions compiler-rt/test/msan/Linux/mallinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@
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;
}
13 changes: 13 additions & 0 deletions compiler-rt/test/msan/Linux/mallinfo2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %clangxx_msan -O0 -g %s -o %t && %run %t
// REQUIRES: glibc-2.33

#include <assert.h>
#include <malloc.h>

#include <sanitizer/msan_interface.h>

int main(void) {
struct mallinfo2 mi2 = mallinfo2();
assert(__msan_test_shadow(&mi2, sizeof(mi2)) == -1);
return 0;
}

0 comments on commit 771e9cd

Please sign in to comment.