[PATCH] D33171: Fix DynamicLibraryTest.cpp on FreeBSD

Dimitry Andric via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun May 14 07:01:41 PDT 2017


dim created this revision.
Herald added a subscriber: krytarowski.

After https://reviews.llvm.org/rL301562, on FreeBSD the following unittest failures are being reported:

- TEST 'LLVM-Unit :: Support/DynamicLibrary/DynamicLibraryTests/DynamicLibrary.Shutdown' FAILED ******************** Note: Google Test filter = DynamicLibrary.Shutdown [==========] Running 1 test from 1 test case. [----------] Global test environment set-up. [----------] 1 test from DynamicLibrary [ RUN      ] DynamicLibrary.Shutdown /share/dim/src/llvm/trunk/unittests/Support/DynamicLibrary/DynamicLibraryTest.cpp:107: Failure Value of: DL.isValid() Actual: false Expected: true /share/dim/src/llvm/trunk/unittests/Support/DynamicLibrary/DynamicLibraryTest.cpp:108: Failure Value of: Err.empty() Actual: false Expected: true /share/dim/src/llvm/trunk/unittests/Support/DynamicLibrary/DynamicLibraryTest.cpp:112: Failure Value of: SS != nullptr Actual: false Expected: true
- TEST 'LLVM-Unit :: Support/DynamicLibrary/DynamicLibraryTests/DynamicLibrary.Overload' FAILED ******************** Note: Google Test filter = DynamicLibrary.Overload [==========] Running 1 test from 1 test case. [----------] Global test environment set-up. [----------] 1 test from DynamicLibrary [ RUN      ] DynamicLibrary.Overload /share/dim/src/llvm/trunk/unittests/Support/DynamicLibrary/DynamicLibraryTest.cpp:65: Failure Value of: DL.isValid() Actual: false Expected: true /share/dim/src/llvm/trunk/unittests/Support/DynamicLibrary/DynamicLibraryTest.cpp:66: Failure Value of: Err.empty() Actual: false Expected: true /share/dim/src/llvm/trunk/unittests/Support/DynamicLibrary/DynamicLibraryTest.cpp:69: Failure Value of: GS != nullptr && GS != &TestA Actual: false Expected: true

This is because the test uses `getMainExecutable("DynamicLibraryTests", Ptr);`, and since the path does not contain any slashes, retrieving the main executable will not work.

I chose to reimplement `getMainExecutable()` for FreeBSD using `sysctl(3)`, which is more reliable than fiddling with relative or absolute paths.

However, this approach might not fix any failures for other OSes.  So I also added retrieval of the original argv[] from the GoogleTest framework, to use as a fallback.

I also thought that the final fallback name might be "./DynamicLibraryTests", but that will not work on Windows.  Is this test run on Windows at all?


https://reviews.llvm.org/D33171

Files:
  lib/Support/Unix/Path.inc
  unittests/Support/DynamicLibrary/DynamicLibraryTest.cpp


Index: unittests/Support/DynamicLibrary/DynamicLibraryTest.cpp
===================================================================
--- unittests/Support/DynamicLibrary/DynamicLibraryTest.cpp
+++ unittests/Support/DynamicLibrary/DynamicLibraryTest.cpp
@@ -23,8 +23,10 @@
 extern "C" PIPSQUEAK_EXPORT const char *TestA() { return "ProcessCall"; }
 
 std::string LibPath() {
+  const std::vector<testing::internal::string>& Argvs = testing::internal::GetArgvs();
+  const char *Argv0 = Argvs.size() > 0 ? Argvs[0].c_str() : "DynamicLibraryTests";
   void *Ptr = (void*)(intptr_t)TestA;
-  std::string Path = fs::getMainExecutable("DynamicLibraryTests", Ptr);
+  std::string Path = fs::getMainExecutable(Argv0, Ptr);
   llvm::SmallString<256> Buf(path::parent_path(Path));
   path::append(Buf, "PipSqueak.so");
   return Buf.str();
Index: lib/Support/Unix/Path.inc
===================================================================
--- lib/Support/Unix/Path.inc
+++ lib/Support/Unix/Path.inc
@@ -77,6 +77,9 @@
 #if defined(__OpenBSD__) || defined(__FreeBSD__)
 #include <sys/param.h>
 #include <sys/mount.h>
+#if defined(__FreeBSD__)
+#include <sys/sysctl.h>
+#endif
 #elif defined(__linux__)
 #if defined(HAVE_LINUX_MAGIC_H)
 #include <linux/magic.h>
@@ -108,10 +111,9 @@
 namespace llvm {
 namespace sys  {
 namespace fs {
-#if defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \
-    defined(__OpenBSD__) || defined(__minix) || defined(__FreeBSD_kernel__) || \
-    defined(__linux__) || defined(__CYGWIN__) || defined(__DragonFly__) || \
-    defined(_AIX)
+#if defined (__NetBSD__) || defined(__Bitrig__) || defined(__OpenBSD__) || \
+    defined(__minix) || defined(__linux__) || defined(__CYGWIN__) || \
+    defined(__DragonFly__) || defined(_AIX)
 static int
 test_dir(char ret[PATH_MAX], const char *dir, const char *bin)
 {
@@ -164,7 +166,7 @@
   free(pv);
   return nullptr;
 }
-#endif // __FreeBSD__ || __NetBSD__ || __FreeBSD_kernel__
+#endif // NetBSD || _Bitrig || OpenBSD || minix || linux || CYGWIN || DragonFly || _AIX
 
 /// GetMainExecutable - Return the path to the main executable, given the
 /// value of argv[0] from program startup.
@@ -180,9 +182,18 @@
     if (realpath(exe_path, link_path))
       return link_path;
   }
-#elif defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \
-      defined(__OpenBSD__) || defined(__minix) || defined(__DragonFly__) || \
-      defined(__FreeBSD_kernel__) || defined(_AIX)
+#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
+  int mib[4];
+  mib[0] = CTL_KERN;
+  mib[1] = KERN_PROC;
+  mib[2] = KERN_PROC_PATHNAME;
+  mib[3] = -1;
+  char exe_path[PATH_MAX];
+  size_t cb = sizeof(exe_path);
+  if (sysctl(mib, 4, exe_path, &cb, NULL, 0) == 0)
+    return exe_path;
+#elif defined (__NetBSD__) || defined(__Bitrig__) || defined(__OpenBSD__) || \
+      defined(__minix) || defined(__DragonFly__) || defined(_AIX)
   char exe_path[PATH_MAX];
 
   if (getprogpath(exe_path, argv0) != NULL)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33171.98925.patch
Type: text/x-patch
Size: 3003 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170514/d5707507/attachment.bin>


More information about the llvm-commits mailing list