forked from OSchip/llvm-project
[Sanitizer] Add the way to find binary in PATH
llvm-svn: 189799
This commit is contained in:
parent
147984a8ad
commit
de647ddc8b
|
@ -134,6 +134,7 @@ bool FileExists(const char *filename);
|
|||
const char *GetEnv(const char *name);
|
||||
bool SetEnv(const char *name, const char *value);
|
||||
const char *GetPwd();
|
||||
char *FindPathToBinary(const char *name);
|
||||
u32 GetUid();
|
||||
void ReExec();
|
||||
bool StackSizeIsUnlimited();
|
||||
|
|
|
@ -173,6 +173,30 @@ const char *GetPwd() {
|
|||
return GetEnv("PWD");
|
||||
}
|
||||
|
||||
char *FindPathToBinary(const char *name) {
|
||||
const char *path = GetEnv("PATH");
|
||||
if (!path)
|
||||
return 0;
|
||||
uptr name_len = internal_strlen(name);
|
||||
InternalScopedBuffer<char> buffer(kMaxPathLength);
|
||||
const char *beg = path;
|
||||
while (true) {
|
||||
const char *end = internal_strchrnul(beg, ':');
|
||||
uptr prefix_len = end - beg;
|
||||
if (prefix_len + name_len + 2 <= kMaxPathLength) {
|
||||
internal_memcpy(buffer.data(), beg, prefix_len);
|
||||
buffer[prefix_len] = '/';
|
||||
internal_memcpy(&buffer[prefix_len + 1], name, name_len);
|
||||
buffer[prefix_len + 1 + name_len] = '\0';
|
||||
if (FileExists(buffer.data()))
|
||||
return internal_strdup(buffer.data());
|
||||
}
|
||||
if (*end == '\0') break;
|
||||
beg = end + 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace __sanitizer
|
||||
|
||||
#endif // SANITIZER_LINUX || SANITIZER_MAC
|
||||
|
|
|
@ -196,6 +196,10 @@ void SetStackSizeLimitInBytes(uptr limit) {
|
|||
UNIMPLEMENTED();
|
||||
}
|
||||
|
||||
char *FindPathToBinary(const char *name) {
|
||||
UNIMPLEMENTED();
|
||||
}
|
||||
|
||||
void SleepForSeconds(int seconds) {
|
||||
Sleep(seconds * 1000);
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
// This file is a part of ThreadSanitizer/AddressSanitizer runtime.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
#include "sanitizer_common/sanitizer_allocator_internal.h"
|
||||
#include "sanitizer_common/sanitizer_common.h"
|
||||
#include "sanitizer_common/sanitizer_libc.h"
|
||||
#include "sanitizer_common/sanitizer_platform.h"
|
||||
|
@ -173,4 +174,13 @@ TEST(SanitizerCommon, InternalBinarySearch) {
|
|||
ASSERT_EQ(InternalBinarySearch(arr, 0, kSize, 7, UptrLess), kSize + 1);
|
||||
}
|
||||
|
||||
#if SANITIZER_LINUX
|
||||
TEST(SanitizerCommon, FindPathToBinary) {
|
||||
char *true_path = FindPathToBinary("true");
|
||||
EXPECT_NE((char*)0, internal_strstr(true_path, "/bin/true"));
|
||||
InternalFree(true_path);
|
||||
EXPECT_EQ(0, FindPathToBinary("unexisting_binary.ergjeorj"));
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace __sanitizer
|
||||
|
|
Loading…
Reference in New Issue