[compiler-rt] 29e6b15 - [compiler-rt][msan] Ignore addrinfo padding in getaddrinfo interceptor (#219124)

via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 1 22:00:56 PDT 2026


Author: Jeewoong Kim
Date: 2026-10-02T05:00:47Z
New Revision: 29e6b15d01aef1b82b6fc55c723c48b550585a41

URL: https://github.com/llvm/llvm-project/commit/29e6b15d01aef1b82b6fc55c723c48b550585a41
DIFF: https://github.com/llvm/llvm-project/commit/29e6b15d01aef1b82b6fc55c723c48b550585a41.diff

LOG: [compiler-rt][msan] Ignore addrinfo padding in getaddrinfo interceptor (#219124)

Avoid checking padding bytes in `struct addrinfo` in the `getaddrinfo`
interceptor.

The interceptor used a single `COMMON_INTERCEPTOR_READ_RANGE` for the
whole structure, which caused MSan to report uninitialized padding even
when all
named members were initialized.

Check each member separately instead.

Testing
- Added `getaddrinfo-padding.cpp` to cover the padding case
- Verified the new test fails before the fix and passes after the change
- Verified `getaddrinfo-positive.cpp` still reports uninitialized fields
- Ran `ninja -C build/runtimes/runtimes-bins check-msan` on AArch64
Linux

Fixes #162216

Added: 
    compiler-rt/test/msan/getaddrinfo-padding.cpp
    compiler-rt/test/sanitizer_common/TestCases/getaddrinfo.cpp

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
    compiler-rt/test/msan/getaddrinfo-positive.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
index f24630fa1b64c..3f31a4ef53a3e 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -2971,8 +2971,30 @@ INTERCEPTOR(int, getaddrinfo, char *node, char *service,
   if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, internal_strlen(node) + 1);
   if (service)
     COMMON_INTERCEPTOR_READ_RANGE(ctx, service, internal_strlen(service) + 1);
-  if (hints)
-    COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
+  if (hints) {
+    // Check individual members instead of the whole structure to avoid
+    // requiring callers to initialize padding bytes.
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, &hints->ai_flags,
+                                  sizeof(hints->ai_flags));
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, &hints->ai_family,
+                                  sizeof(hints->ai_family));
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, &hints->ai_socktype,
+                                  sizeof(hints->ai_socktype));
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, &hints->ai_protocol,
+                                  sizeof(hints->ai_protocol));
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, &hints->ai_addrlen,
+                                  sizeof(hints->ai_addrlen));
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, &hints->ai_addr,
+                                  sizeof(hints->ai_addr));
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, &hints->ai_canonname,
+                                  sizeof(hints->ai_canonname));
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, &hints->ai_next,
+                                  sizeof(hints->ai_next));
+#if SANITIZER_AIX
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, &hints->ai_eflags,
+                                  sizeof(hints->ai_eflags));
+#endif
+  }
   // FIXME: under ASan the call below may write to freed memory and corrupt
   // its metadata. See
   // https://github.com/google/sanitizers/issues/321.

diff  --git a/compiler-rt/test/msan/getaddrinfo-padding.cpp b/compiler-rt/test/msan/getaddrinfo-padding.cpp
new file mode 100644
index 0000000000000..8c735bb5b7f43
--- /dev/null
+++ b/compiler-rt/test/msan/getaddrinfo-padding.cpp
@@ -0,0 +1,27 @@
+// RUN: %clangxx_msan -O0 %s -o %t && %run %t
+// RUN: %clangxx_msan -O3 %s -o %t && %run %t
+
+#include <assert.h>
+#include <netdb.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+
+int main(void) {
+  struct addrinfo hints;
+
+  hints.ai_flags = 0;
+  hints.ai_family = AF_UNSPEC;
+  hints.ai_socktype = SOCK_STREAM;
+  hints.ai_protocol = 0;
+  hints.ai_addrlen = 0;
+  hints.ai_addr = nullptr;
+  hints.ai_canonname = nullptr;
+  hints.ai_next = nullptr;
+
+  struct addrinfo *res = nullptr;
+  int ret = getaddrinfo("127.0.0.1", "4567", &hints, &res);
+  assert(ret == 0);
+
+  freeaddrinfo(res);
+  return 0;
+}

diff  --git a/compiler-rt/test/msan/getaddrinfo-positive.cpp b/compiler-rt/test/msan/getaddrinfo-positive.cpp
index a793374133967..b92edc2fa4cac 100644
--- a/compiler-rt/test/msan/getaddrinfo-positive.cpp
+++ b/compiler-rt/test/msan/getaddrinfo-positive.cpp
@@ -16,7 +16,7 @@ int main(void) {
   int res = getaddrinfo("localhost", NULL, NULL, &ai);
   if (ai) z = 1; // OK
   res = getaddrinfo("localhost", NULL, &hint, &ai);
-  // CHECK: Uninitialized bytes in getaddrinfo at offset 0 inside [0x{{.*}}, 48)
+  // CHECK: Uninitialized bytes in getaddrinfo at offset 0 inside [0x{{.*}}, 4)
   // CHECK: WARNING: MemorySanitizer: use-of-uninitialized-value
   // CHECK: #0 {{.*}} in main {{.*}}getaddrinfo-positive.cpp:[[@LINE-3]]
   return 0;

diff  --git a/compiler-rt/test/sanitizer_common/TestCases/getaddrinfo.cpp b/compiler-rt/test/sanitizer_common/TestCases/getaddrinfo.cpp
new file mode 100644
index 0000000000000..30877f00e24d4
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/getaddrinfo.cpp
@@ -0,0 +1,25 @@
+// RUN: %clangxx -O0 -g %s -o %t && %run %t
+
+#include <assert.h>
+#include <netdb.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+
+int main() {
+  struct addrinfo hints;
+  memset(&hints, 0, sizeof(hints));
+
+  hints.ai_family = AF_INET;
+  hints.ai_socktype = SOCK_STREAM;
+  hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
+
+  struct addrinfo *res = nullptr;
+  int ret = getaddrinfo("127.0.0.1", "4567", &hints, &res);
+
+  assert(ret == 0);
+  assert(res != nullptr);
+
+  freeaddrinfo(res);
+  return 0;
+}


        


More information about the llvm-commits mailing list