[PATCH] D17832: Retrieve command line arguments and environment correctly on FreeBSD

Dimitry Andric via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 2 15:21:56 PST 2016


dim created this revision.
dim added reviewers: kcc, emaste, davide.
dim added a subscriber: llvm-commits.
Herald added a subscriber: emaste.

Recently I saw the test `TestCases/Posix/print_cmdline.cc` failing on
FreeBSD, with "expected string not found in input".  This is because
asan could not retrieve the command line arguments properly.

In `lib/sanitizer_common/sanitizer_linux.cc`, this is taken care of by
the `GetArgsAndEnv()` function, but it uses `__libc_stack_end` to get at
the required data.  This variable does not exist on BSDs; the regular
way to retrieve the arguments and environment information is via the
`kern.ps_strings` sysctl.

I added this functionality in sanitizer_linux.cc, as a separate #ifdef
block in `GetArgsAndEnv()`.  Also, `ReadNullSepFileToArray()` becomes
unused due to this change.  (It won't work on FreeBSD anyway, since
`/proc` is not mounted by default.)

http://reviews.llvm.org/D17832

Files:
  lib/sanitizer_common/sanitizer_linux.cc

Index: lib/sanitizer_common/sanitizer_linux.cc
===================================================================
--- lib/sanitizer_common/sanitizer_linux.cc
+++ lib/sanitizer_common/sanitizer_linux.cc
@@ -60,7 +60,10 @@
 #include <unistd.h>
 
 #if SANITIZER_FREEBSD
+#include <sys/exec.h>
 #include <sys/sysctl.h>
+#include <vm/vm_param.h>
+#include <vm/pmap.h>
 #include <machine/atomic.h>
 extern "C" {
 // <sys/umtx.h> must be included after <errno.h> and <sys/types.h> on
@@ -395,11 +398,13 @@
 #endif
 }
 
+#if !SANITIZER_FREEBSD
 extern "C" {
   SANITIZER_WEAK_ATTRIBUTE extern void *__libc_stack_end;
 }
+#endif
 
-#if !SANITIZER_GO
+#if !SANITIZER_GO && !SANITIZER_FREEBSD
 static void ReadNullSepFileToArray(const char *path, char ***arr,
                                    int arr_size) {
   char *buff;
@@ -425,6 +430,7 @@
 #endif
 
 static void GetArgsAndEnv(char ***argv, char ***envp) {
+#if !SANITIZER_FREEBSD
 #if !SANITIZER_GO
   if (&__libc_stack_end) {
 #endif
@@ -439,6 +445,14 @@
     ReadNullSepFileToArray("/proc/self/environ", envp, kMaxEnvp);
   }
 #endif
+#else // FreeBSD
+  ps_strings *pss;
+  size_t sz = sizeof(pss);
+  if (sysctlbyname("kern.ps_strings", &pss, &sz, NULL, 0) == -1)
+    pss = (ps_strings*)PS_STRINGS;
+  *argv = pss->ps_argvstr;
+  *envp = pss->ps_envstr;
+#endif
 }
 
 char **GetArgv() {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D17832.49680.patch
Type: text/x-patch
Size: 1340 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160302/6b4a0d7d/attachment.bin>


More information about the llvm-commits mailing list