Skip to content

MSAN fails on musl-based systems to due ifguard mismatch #63325

@nmeum

Description

@nmeum

MSan does presently not work on musl-based systems (e.g. Alpine Linux):

$ 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:

and:

#if !SANITIZER_FREEBSD && !SANITIZER_NETBSD

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

From 8904ed80c262e973c0da7758337f586c9854f38a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=B6ren=20Tempel?= <[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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Needs Triage

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions