diff options
| author | Alexey Samsonov <samsonov@google.com> | 2012-06-14 14:07:21 +0000 |
|---|---|---|
| committer | Alexey Samsonov <samsonov@google.com> | 2012-06-14 14:07:21 +0000 |
| commit | 0c53a38abb8e90558eb00c79408e8ad613383463 (patch) | |
| tree | 1e9781453b4089a3a8c1df5bbe8793402add47c1 | |
| parent | 96ef49a71eff756b0da5822c23ee6592db5b86a2 (diff) | |
| download | bcm5719-llvm-0c53a38abb8e90558eb00c79408e8ad613383463.tar.gz bcm5719-llvm-0c53a38abb8e90558eb00c79408e8ad613383463.zip | |
[Sanitizer] move portable GetEnv to common sanitizer runtime
llvm-svn: 158451
| -rw-r--r-- | compiler-rt/lib/asan/asan_internal.h | 1 | ||||
| -rw-r--r-- | compiler-rt/lib/asan/asan_linux.cc | 28 | ||||
| -rw-r--r-- | compiler-rt/lib/asan/asan_mac.cc | 20 | ||||
| -rw-r--r-- | compiler-rt/lib/asan/asan_rtl.cc | 2 | ||||
| -rw-r--r-- | compiler-rt/lib/asan/asan_win.cc | 16 | ||||
| -rw-r--r-- | compiler-rt/lib/sanitizer_common/sanitizer_common.h | 1 | ||||
| -rw-r--r-- | compiler-rt/lib/sanitizer_common/sanitizer_linux.cc | 28 | ||||
| -rw-r--r-- | compiler-rt/lib/sanitizer_common/sanitizer_mac.cc | 20 | ||||
| -rw-r--r-- | compiler-rt/lib/sanitizer_common/sanitizer_win.cc | 16 |
9 files changed, 66 insertions, 66 deletions
diff --git a/compiler-rt/lib/asan/asan_internal.h b/compiler-rt/lib/asan/asan_internal.h index 9dad2130919..ec487c81196 100644 --- a/compiler-rt/lib/asan/asan_internal.h +++ b/compiler-rt/lib/asan/asan_internal.h @@ -122,7 +122,6 @@ void ReplaceSystemMalloc(); // asan_linux.cc / asan_mac.cc / asan_win.cc void *AsanDoesNotSupportStaticLinkage(); bool AsanShadowRangeIsAvailable(); -const char *AsanGetEnv(const char *name); void AsanDumpProcessMap(); void *AsanMmapFixedNoReserve(uptr fixed_addr, uptr size); diff --git a/compiler-rt/lib/asan/asan_linux.cc b/compiler-rt/lib/asan/asan_linux.cc index 984aa7e48dd..8af417fab82 100644 --- a/compiler-rt/lib/asan/asan_linux.cc +++ b/compiler-rt/lib/asan/asan_linux.cc @@ -86,34 +86,6 @@ void *AsanMprotect(uptr fixed_addr, uptr size) { 0, 0); } -// Like getenv, but reads env directly from /proc and does not use libc. -// This function should be called first inside __asan_init. -const char* AsanGetEnv(const char* name) { - static char *environ; - static uptr len; - static bool inited; - if (!inited) { - inited = true; - uptr environ_size; - len = ReadFileToBuffer("/proc/self/environ", - &environ, &environ_size, 1 << 26); - } - if (!environ || len == 0) return 0; - uptr namelen = internal_strlen(name); - const char *p = environ; - while (*p != '\0') { // will happen at the \0\0 that terminates the buffer - // proc file has the format NAME=value\0NAME=value\0NAME=value\0... - const char* endp = - (char*)internal_memchr(p, '\0', len - (p - environ)); - if (endp == 0) // this entry isn't NUL terminated - return 0; - else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=') // Match. - return p + namelen + 1; // point after = - p = endp + 1; - } - return 0; // Not found. -} - AsanLock::AsanLock(LinkerInitialized) { // We assume that pthread_mutex_t initialized to all zeroes is a valid // unlocked mutex. We can not use PTHREAD_MUTEX_INITIALIZER as it triggers diff --git a/compiler-rt/lib/asan/asan_mac.cc b/compiler-rt/lib/asan/asan_mac.cc index f0e9e3ed35c..8ef13ab4b83 100644 --- a/compiler-rt/lib/asan/asan_mac.cc +++ b/compiler-rt/lib/asan/asan_mac.cc @@ -111,26 +111,6 @@ void *AsanMprotect(uptr fixed_addr, uptr size) { 0, 0); } -const char *AsanGetEnv(const char *name) { - char ***env_ptr = _NSGetEnviron(); - CHECK(env_ptr); - char **environ = *env_ptr; - CHECK(environ); - uptr name_len = internal_strlen(name); - while (*environ != 0) { - uptr len = internal_strlen(*environ); - if (len > name_len) { - const char *p = *environ; - if (!internal_memcmp(p, name, name_len) && - p[name_len] == '=') { // Match. - return *environ + name_len + 1; // String starting after =. - } - } - environ++; - } - return 0; -} - AsanLock::AsanLock(LinkerInitialized) { // We assume that OS_SPINLOCK_INIT is zero } diff --git a/compiler-rt/lib/asan/asan_rtl.cc b/compiler-rt/lib/asan/asan_rtl.cc index 4aa5fc5fca2..39c7ef2aab6 100644 --- a/compiler-rt/lib/asan/asan_rtl.cc +++ b/compiler-rt/lib/asan/asan_rtl.cc @@ -493,7 +493,7 @@ void __asan_init() { } #endif // flags - const char *options = AsanGetEnv("ASAN_OPTIONS"); + const char *options = GetEnv("ASAN_OPTIONS"); ParseAsanOptions(options); if (FLAG_v && options) { diff --git a/compiler-rt/lib/asan/asan_win.cc b/compiler-rt/lib/asan/asan_win.cc index 10f9339eb35..fc531eb3255 100644 --- a/compiler-rt/lib/asan/asan_win.cc +++ b/compiler-rt/lib/asan/asan_win.cc @@ -203,22 +203,6 @@ u8 AtomicExchange(u8 *a, u8 new_val) { return t; } -const char* AsanGetEnv(const char* name) { - static char env_buffer[32767] = {}; - - // Note: this implementation stores the result in a static buffer so we only - // allow it to be called just once. - static bool called_once = false; - if (called_once) - UNIMPLEMENTED(); - called_once = true; - - DWORD rv = GetEnvironmentVariableA(name, env_buffer, sizeof(env_buffer)); - if (rv > 0 && rv < sizeof(env_buffer)) - return env_buffer; - return 0; -} - void AsanDumpProcessMap() { UNIMPLEMENTED(); } diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.h b/compiler-rt/lib/sanitizer_common/sanitizer_common.h index 1feda711980..4de57bf14a2 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.h @@ -48,6 +48,7 @@ void Report(const char *format, ...); // Returns the number of read bytes or 0 if file can not be opened. uptr ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size, uptr max_len); +const char *GetEnv(const char *name); // Bit twiddling. inline bool IsPowerOfTwo(uptr x) { diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc index 24c8d234a8c..c3e1fa7e5a0 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cc @@ -120,6 +120,34 @@ void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top, CHECK(stacksize < kMaxThreadStackSize); // Sanity check. } +// Like getenv, but reads env directly from /proc and does not use libc. +// This function should be called first inside __asan_init. +const char *GetEnv(const char *name) { + static char *environ; + static uptr len; + static bool inited; + if (!inited) { + inited = true; + uptr environ_size; + len = ReadFileToBuffer("/proc/self/environ", + &environ, &environ_size, 1 << 26); + } + if (!environ || len == 0) return 0; + uptr namelen = internal_strlen(name); + const char *p = environ; + while (*p != '\0') { // will happen at the \0\0 that terminates the buffer + // proc file has the format NAME=value\0NAME=value\0NAME=value\0... + const char* endp = + (char*)internal_memchr(p, '\0', len - (p - environ)); + if (endp == 0) // this entry isn't NUL terminated + return 0; + else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=') // Match. + return p + namelen + 1; // point after = + p = endp + 1; + } + return 0; // Not found. +} + // ----------------- sanitizer_procmaps.h ProcessMaps::ProcessMaps() { proc_self_maps_buff_len_ = diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cc b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cc index 4dc8686f762..27396eda49d 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cc @@ -19,6 +19,7 @@ #include "sanitizer_libc.h" #include "sanitizer_procmaps.h" +#include <crt_externs.h> // for _NSGetEnviron #include <fcntl.h> #include <mach-o/dyld.h> #include <mach-o/loader.h> @@ -80,6 +81,25 @@ void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top, *stack_bottom = *stack_top - stacksize; } +const char *GetEnv(const char *name) { + char ***env_ptr = _NSGetEnviron(); + CHECK(env_ptr); + char **environ = *env_ptr; + CHECK(environ); + uptr name_len = internal_strlen(name); + while (*environ != 0) { + uptr len = internal_strlen(*environ); + if (len > name_len) { + const char *p = *environ; + if (!internal_memcmp(p, name, name_len) && + p[name_len] == '=') { // Match. + return *environ + name_len + 1; // String starting after =. + } + } + environ++; + } + return 0; +} // ----------------- sanitizer_procmaps.h diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_win.cc b/compiler-rt/lib/sanitizer_common/sanitizer_win.cc index 6aeb9258be4..6166f150b22 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_win.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_win.cc @@ -56,6 +56,22 @@ void UnmapOrDie(void *addr, uptr size) { } } +const char *GetEnv(const char *name) { + static char env_buffer[32767] = {}; + + // Note: this implementation stores the result in a static buffer so we only + // allow it to be called just once. + static bool called_once = false; + if (called_once) + UNIMPLEMENTED(); + called_once = true; + + DWORD rv = GetEnvironmentVariableA(name, env_buffer, sizeof(env_buffer)); + if (rv > 0 && rv < sizeof(env_buffer)) + return env_buffer; + return 0; +} + // ------------------ sanitizer_libc.h void *internal_mmap(void *addr, uptr length, int prot, int flags, int fd, u64 offset) { |

