<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/63325>63325</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
MSAN fails on musl-based systems to due ifguard mismatch
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
nmeum
</td>
</tr>
</table>
<pre>
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 <soeren+git@soeren-tempel.net>
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=5ecc3adbbc5ecd614199aa0ff9ece5fe4b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