[compiler-rt] [sanitizer_common] posix_spawn test should forward DYLD_LIBRARY_PATH (PR #168795)

Andrew Haberlandt via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 20 10:05:57 PST 2025


https://github.com/ndrewh updated https://github.com/llvm/llvm-project/pull/168795

>From 1f251c3c3d362e8e341571bb3e07900a71ac4901 Mon Sep 17 00:00:00 2001
From: Andrew Haberlandt <ahaberlandt at apple.com>
Date: Wed, 19 Nov 2025 15:34:45 -0800
Subject: [PATCH 1/5] posix_spawn test should forward DYLD_LIBRARY_PATH

---
 .../test/sanitizer_common/TestCases/Posix/posix_spawn.c  | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c
index ea58b92af6097..e570a61f9ac9a 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c
@@ -7,6 +7,8 @@
 #include <spawn.h>
 #include <stdio.h>
 #include <sys/wait.h>
+#include <string.h>
+#include <stdlib.h>
 
 int main(int argc, char **argv) {
   if (argc > 1) {
@@ -23,11 +25,16 @@ int main(int argc, char **argv) {
       argv[0], "2", "3", "4", "2", "3", "4", "2", "3", "4",
       "2",     "3", "4", "2", "3", "4", "2", "3", "4", NULL,
   };
-  char *const env[] = {
+  const char *env[] = {
       "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", "A=B",
       "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", NULL,
   };
 
+  char *lib_path = getenv("DYLD_LIRARY_PATH");
+  if (lib_path) {
+    env[0] = strdup(lib_path);
+  }
+
   pid_t pid;
   int s = posix_spawn(&pid, argv[0], &file_actions, &attr, args, env);
   assert(!s);

>From 672b7dc2bdce693b62926fae1eee2a283846b111 Mon Sep 17 00:00:00 2001
From: Andrew Haberlandt <ahaberlandt at apple.com>
Date: Wed, 19 Nov 2025 15:52:02 -0800
Subject: [PATCH 2/5] format

---
 .../test/sanitizer_common/TestCases/Posix/posix_spawn.c       | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c
index e570a61f9ac9a..b80b2cf53d988 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c
@@ -6,9 +6,9 @@
 #include <assert.h>
 #include <spawn.h>
 #include <stdio.h>
-#include <sys/wait.h>
-#include <string.h>
 #include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
 
 int main(int argc, char **argv) {
   if (argc > 1) {

>From db41a38c0b1dc9395cd5da38142340d9bfdfdffe Mon Sep 17 00:00:00 2001
From: Andrew Haberlandt <ahaberlandt at apple.com>
Date: Wed, 19 Nov 2025 17:03:59 -0800
Subject: [PATCH 3/5] Just walk environ instead of using getenv

---
 .../sanitizer_common/TestCases/Posix/posix_spawn.c     | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c
index b80b2cf53d988..009a2cd8b447c 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c
@@ -10,6 +10,8 @@
 #include <string.h>
 #include <sys/wait.h>
 
+extern char **environ;
+
 int main(int argc, char **argv) {
   if (argc > 1) {
     // CHECK: SPAWNED
@@ -30,9 +32,11 @@ int main(int argc, char **argv) {
       "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", NULL,
   };
 
-  char *lib_path = getenv("DYLD_LIRARY_PATH");
-  if (lib_path) {
-    env[0] = strdup(lib_path);
+  for (char **e = environ; *e; e++) {
+    if (strncmp(*e, "DYLD_LIBRARY_PATH=", sizeof("DYLD_LIBRARY_PATH=") - 1) ==
+        0) {
+      env[0] = *e;
+    }
   }
 
   pid_t pid;

>From 4766f0ebfdb79261f4d04c6b31bf87f61f280cb3 Mon Sep 17 00:00:00 2001
From: Andrew Haberlandt <ahaberlandt at apple.com>
Date: Wed, 19 Nov 2025 19:04:22 -0800
Subject: [PATCH 4/5] remove const; fixes warning

---
 compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c
index 009a2cd8b447c..4c411c9c1b5cd 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c
@@ -27,7 +27,7 @@ int main(int argc, char **argv) {
       argv[0], "2", "3", "4", "2", "3", "4", "2", "3", "4",
       "2",     "3", "4", "2", "3", "4", "2", "3", "4", NULL,
   };
-  const char *env[] = {
+  char *env[] = {
       "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", "A=B",
       "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", NULL,
   };

>From 43f6fb7f90dc45d6260174f0118af765250dbd33 Mon Sep 17 00:00:00 2001
From: Andrew Haberlandt <ahaberlandt at apple.com>
Date: Thu, 20 Nov 2025 10:01:48 -0800
Subject: [PATCH 5/5] nits

---
 .../sanitizer_common/TestCases/Posix/posix_spawn.c   | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c b/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c
index 4c411c9c1b5cd..1a66a6166dd1d 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/posix_spawn.c
@@ -32,12 +32,16 @@ int main(int argc, char **argv) {
       "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", "A=B", NULL,
   };
 
-  for (char **e = environ; *e; e++) {
+  // When this test runs with a runtime (e.g. ASAN), the spawned process needs
+  // to use the same runtime search path as the parent. Otherwise, it might
+  // try to load a runtime that doesn't work and crash before hitting main(),
+  // failing the test. We technically should forward the variable for the
+  // current platform, but some platforms have multiple such variables and
+  // it's quite difficult to plumb this through the lit config.
+  for (char **e = environ; *e; e++)
     if (strncmp(*e, "DYLD_LIBRARY_PATH=", sizeof("DYLD_LIBRARY_PATH=") - 1) ==
-        0) {
+        0)
       env[0] = *e;
-    }
-  }
 
   pid_t pid;
   int s = posix_spawn(&pid, argv[0], &file_actions, &attr, args, env);



More information about the llvm-commits mailing list