Skip to content

Commit 4043373

Browse files
committedMay 14, 2017
Fix DynamicLibraryTest.cpp on FreeBSD and NetBSD
Summary: After rL301562, on FreeBSD the DynamicLibrary unittests fail, because the test uses getMainExecutable("DynamicLibraryTests", Ptr), and since the path does not contain any slashes, retrieving the main executable will not work. Reimplement getMainExecutable() for FreeBSD and NetBSD using sysctl(3), which is more reliable than fiddling with relative or absolute paths. Also add retrieval of the original argv[] from the GoogleTest framework, to use as a fallback for other OSes. Reviewers: emaste, marsupial, hans, krytarowski Reviewed By: krytarowski Subscribers: krytarowski, llvm-commits Differential Revision: https://reviews.llvm.org/D33171 llvm-svn: 303015
1 parent ee97c5f commit 4043373

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed
 

‎llvm/lib/Support/Unix/Path.inc

+24-6
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,16 @@
103103
#define STATVFS_F_FLAG(vfs) (vfs).f_flags
104104
#endif
105105

106+
#if defined(__FreeBSD__) || defined(__NetBSD__)
107+
#include <sys/sysctl.h>
108+
#endif
109+
106110
using namespace llvm;
107111

108112
namespace llvm {
109113
namespace sys {
110114
namespace fs {
111-
#if defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \
112-
defined(__OpenBSD__) || defined(__minix) || defined(__FreeBSD_kernel__) || \
115+
#if defined(__Bitrig__) || defined(__OpenBSD__) || defined(__minix) || \
113116
defined(__linux__) || defined(__CYGWIN__) || defined(__DragonFly__) || \
114117
defined(_AIX)
115118
static int
@@ -164,7 +167,7 @@ getprogpath(char ret[PATH_MAX], const char *bin)
164167
free(pv);
165168
return nullptr;
166169
}
167-
#endif // __FreeBSD__ || __NetBSD__ || __FreeBSD_kernel__
170+
#endif // Bitrig || OpenBSD || minix || linux || CYGWIN || DragonFly || AIX
168171

169172
/// GetMainExecutable - Return the path to the main executable, given the
170173
/// value of argv[0] from program startup.
@@ -180,9 +183,24 @@ std::string getMainExecutable(const char *argv0, void *MainAddr) {
180183
if (realpath(exe_path, link_path))
181184
return link_path;
182185
}
183-
#elif defined(__FreeBSD__) || defined (__NetBSD__) || defined(__Bitrig__) || \
184-
defined(__OpenBSD__) || defined(__minix) || defined(__DragonFly__) || \
185-
defined(__FreeBSD_kernel__) || defined(_AIX)
186+
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
187+
int mib[4];
188+
mib[0] = CTL_KERN;
189+
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
190+
mib[1] = KERN_PROC;
191+
mib[2] = KERN_PROC_PATHNAME;
192+
mib[3] = -1;
193+
#else
194+
mib[1] = KERN_PROC_ARGS;
195+
mib[2] = -1;
196+
mib[3] = KERN_PROC_PATHNAME;
197+
#endif
198+
char exe_path[PATH_MAX];
199+
size_t cb = sizeof(exe_path);
200+
if (sysctl(mib, 4, exe_path, &cb, NULL, 0) == 0)
201+
return exe_path;
202+
#elif defined(__Bitrig__) || defined(__OpenBSD__) || defined(__minix) || \
203+
defined(__DragonFly__) || defined(_AIX)
186204
char exe_path[PATH_MAX];
187205

188206
if (getprogpath(exe_path, argv0) != NULL)

‎llvm/unittests/Support/DynamicLibrary/DynamicLibraryTest.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ using namespace llvm::sys;
2323
extern "C" PIPSQUEAK_EXPORT const char *TestA() { return "ProcessCall"; }
2424

2525
std::string LibPath() {
26+
const std::vector<testing::internal::string>& Argvs = testing::internal::GetArgvs();
27+
const char *Argv0 = Argvs.size() > 0 ? Argvs[0].c_str() : "DynamicLibraryTests";
2628
void *Ptr = (void*)(intptr_t)TestA;
27-
std::string Path = fs::getMainExecutable("DynamicLibraryTests", Ptr);
29+
std::string Path = fs::getMainExecutable(Argv0, Ptr);
2830
llvm::SmallString<256> Buf(path::parent_path(Path));
2931
path::append(Buf, "PipSqueak.so");
3032
return Buf.str();

0 commit comments

Comments
 (0)
Please sign in to comment.