Issue 63325
Summary MSAN fails on musl-based systems to due ifguard mismatch
Labels new issue
Assignees
Reporter nmeum
    MSAN does presently not work on [musl](https://musl.libc.org)-based systems (e.g. [Alpine Linux](https://alpinelinux)):

```
$ cat test.c
#include <stdio.h>

int main(void) {
        char b[20];
 printf("%c\n", b[5]);
}
$ clang -fsanitize=memory -o test test.c
/usr/bin/ld: /usr/lib/llvm16/lib/clang/16/lib/linux/libclang_rt.msan-x86_64.a(msan_interceptors.cpp.o): in function `__interceptor_getrlimit64':
/home/buildozer/aports/main/llvm-runtimes/src/llvm-project-16.0.6.src/compiler-rt/lib/msan/msan_interceptors.cpp:833: undefined reference to `__sanitizer::struct_rlimit64_sz'
/usr/bin/ld: /usr/lib/llvm16/lib/clang/16/lib/linux/libclang_rt.msan-x86_64.a(msan_interceptors.cpp.o): in function `__interceptor_prlimit64':
/home/buildozer/aports/main/llvm-runtimes/src/llvm-project-16.0.6.src/compiler-rt/lib/msan/msan_interceptors.cpp:853: undefined reference to `__sanitizer::struct_rlimit64_sz'
/usr/bin/ld: /home/buildozer/aports/main/llvm-runtimes/src/llvm-project-16.0.6.src/compiler-rt/lib/msan/msan_interceptors.cpp:853: undefined reference to `__sanitizer::struct_rlimit64_sz'
/usr/bin/ld: /home/buildozer/aports/main/llvm-runtimes/src/llvm-project-16.0.6.src/compiler-rt/lib/msan/msan_interceptors.cpp:855: undefined reference to `__sanitizer::struct_rlimit64_sz'
clang-16: error: linker command failed with exit code 1 (use -v to see invocation)
```

This is due to an ifguard mismatch:

https://github.com/llvm/llvm-project/blob/9aa026e9ffc32f1d0531abd306933e249fdc8059/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_posix.cpp#L276

and:

https://github.com/llvm/llvm-project/blob/9aa026e9ffc32f1d0531abd306933e249fdc8059/compiler-rt/lib/msan/msan_interceptors.cpp#L825

Here is a git-format-patch which fixes this issue:

```diff
>From 8904ed80c262e973c0da7758337f586c9854f38a Mon Sep 17 00:00:00 2001
From: Sören Tempel <[email protected]>
Date: Thu, 15 Jun 2023 09:28:57 +0200
Subject: [PATCH] msan: fix ifdef guard for getrlimit etc interceptors

These interceptors need struct_ustat_sz, struct_rlimit64_sz, and
struct_statvfs64_sz which are defined in the following file:

	compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_posix.cpp

However, these variables are only defined for GLIBC sanitizers.
As such, if we attempt to use MSAN on a Linux system that does not
utilize glibc (e.g. Alpine Linux) then we will get a linker error
complaining about undefined references to __sanitizer::struct_rlimit64_sz
and __sanitizer::struct_rlimit64_sz.

This patch fixes this by only defining the interceptors that require
these constants if SANITIZER_GLIBC is defined. Thereby aligning the
macro guards of msan_interceptors.cpp with those of
sanitizer_platform_limits_posix.cpp.
---
 compiler-rt/lib/msan/msan_interceptors.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/compiler-rt/lib/msan/msan_interceptors.cpp b/compiler-rt/lib/msan/msan_interceptors.cpp
index 6f57c33ee..349eff549 100644
--- a/compiler-rt/lib/msan/msan_interceptors.cpp
+++ b/compiler-rt/lib/msan/msan_interceptors.cpp
@@ -822,7 +822,7 @@ INTERCEPTOR(int, getrlimit, int resource, void *rlim) {
 INTERCEPTOR_GETRLIMIT_BODY(getrlimit, resource, rlim);
 }
 
-#if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
+#if SANITIZER_GLIBC
 INTERCEPTOR(int, __getrlimit, int resource, void *rlim) {
 INTERCEPTOR_GETRLIMIT_BODY(__getrlimit, resource, rlim);
 }
```

This patch is presently [applied for the Alpine downstream package](https://git.alpinelinux.org/aports/commit/?id=5ecc3adbbc5ecd614199aa0ff9ece5fe4b43a097).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to