[llvm-commits] [compiler-rt] r162673 - in /compiler-rt/trunk/lib: asan/asan_globals.cc asan/asan_rtl.cc asan/asan_thread.cc sanitizer_common/sanitizer_common.h sanitizer_common/sanitizer_flags.cc

Alexey Samsonov samsonov at google.com
Mon Aug 27 07:04:54 PDT 2012


Author: samsonov
Date: Mon Aug 27 09:04:54 2012
New Revision: 162673

URL: http://llvm.org/viewvc/llvm-project?rev=162673&view=rev
Log:
[Sanitizer] Use low-level allocator in flag parsing to avoid calling malloc() before ASan/TSan initialization is done

Modified:
    compiler-rt/trunk/lib/asan/asan_globals.cc
    compiler-rt/trunk/lib/asan/asan_rtl.cc
    compiler-rt/trunk/lib/asan/asan_thread.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.cc

Modified: compiler-rt/trunk/lib/asan/asan_globals.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_globals.cc?rev=162673&r1=162672&r2=162673&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_globals.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_globals.cc Mon Aug 27 09:04:54 2012
@@ -31,7 +31,7 @@
 };
 
 static AsanLock mu_for_globals(LINKER_INITIALIZED);
-static LowLevelAllocator allocator_for_globals(LINKER_INITIALIZED);
+static LowLevelAllocator allocator_for_globals;
 static ListOfGlobals *list_of_all_globals;
 static ListOfGlobals *list_of_dynamic_init_globals;
 

Modified: compiler-rt/trunk/lib/asan/asan_rtl.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_rtl.cc?rev=162673&r1=162672&r2=162673&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_rtl.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_rtl.cc Mon Aug 27 09:04:54 2012
@@ -281,9 +281,6 @@
   CHECK(!asan_init_is_running && "ASan init calls itself!");
   asan_init_is_running = true;
 
-  // Setup internal allocator callback.
-  SetLowLevelAllocateCallback(OnLowLevelAllocate);
-
   // Make sure we are not statically linked.
   AsanDoesNotSupportStaticLinkage();
 
@@ -299,6 +296,9 @@
   // Re-exec ourselves if we need to set additional env or command line args.
   MaybeReexec();
 
+  // Setup internal allocator callback.
+  SetLowLevelAllocateCallback(OnLowLevelAllocate);
+
   if (flags()->atexit) {
     Atexit(asan_atexit);
   }

Modified: compiler-rt/trunk/lib/asan/asan_thread.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_thread.cc?rev=162673&r1=162672&r2=162673&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_thread.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_thread.cc Mon Aug 27 09:04:54 2012
@@ -27,7 +27,7 @@
       stats_(x) { }
 
 static AsanLock mu_for_thread_summary(LINKER_INITIALIZED);
-static LowLevelAllocator allocator_for_thread_summary(LINKER_INITIALIZED);
+static LowLevelAllocator allocator_for_thread_summary;
 
 AsanThread *AsanThread::Create(u32 parent_tid, thread_callback_t start_routine,
                                void *arg, AsanStackTrace *stack) {

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h?rev=162673&r1=162672&r2=162673&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h Mon Aug 27 09:04:54 2012
@@ -81,7 +81,6 @@
 // Simple low-level (mmap-based) allocator for internal use.
 class LowLevelAllocator {
  public:
-  explicit LowLevelAllocator(LinkerInitialized) {}
   // 'size' must be a power of two. Requires an external lock.
   void *Allocate(uptr size);
  private:

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.cc?rev=162673&r1=162672&r2=162673&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_flags.cc Mon Aug 27 09:04:54 2012
@@ -18,13 +18,14 @@
 
 namespace __sanitizer {
 
-static char *GetFlagValue(const char *env, const char *name) {
+static bool GetFlagValue(const char *env, const char *name,
+                         const char **value, int *value_length) {
   if (env == 0)
-    return 0;
+    return false;
   const char *pos = internal_strstr(env, name);
   const char *end;
   if (pos == 0)
-    return 0;
+    return false;
   pos += internal_strlen(name);
   if (pos[0] != '=') {
     end = pos;
@@ -42,41 +43,54 @@
     if (end == 0)
       end = pos + internal_strlen(pos);
   }
-  int len = end - pos;
-  char *f = (char*)InternalAlloc(len + 1);
-  internal_memcpy(f, pos, len);
-  f[len] = '\0';
-  return f;
+  *value = pos;
+  *value_length = end - pos;
+  return true;
+}
+
+static bool StartsWith(const char *flag, int flag_length, const char *value) {
+  if (!flag || !value)
+    return false;
+  int value_length = internal_strlen(value);
+  return (flag_length >= value_length) &&
+         (0 == internal_strncmp(flag, value, value_length));
 }
 
 void ParseFlag(const char *env, bool *flag, const char *name) {
-  char *val = GetFlagValue(env, name);
-  if (val == 0)
+  const char *value;
+  int value_length;
+  if (!GetFlagValue(env, name, &value, &value_length))
     return;
-  if (0 == internal_strcmp(val, "0") ||
-      0 == internal_strcmp(val, "no") ||
-      0 == internal_strcmp(val, "false"))
+  if (StartsWith(value, value_length, "0") ||
+      StartsWith(value, value_length, "no") ||
+      StartsWith(value, value_length, "false"))
     *flag = false;
-  if (0 == internal_strcmp(val, "1") ||
-      0 == internal_strcmp(val, "yes") ||
-      0 == internal_strcmp(val, "true"))
+  if (StartsWith(value, value_length, "1") ||
+      StartsWith(value, value_length, "yes") ||
+      StartsWith(value, value_length, "true"))
     *flag = true;
-  InternalFree(val);
 }
 
 void ParseFlag(const char *env, int *flag, const char *name) {
-  char *val = GetFlagValue(env, name);
-  if (val == 0)
+  const char *value;
+  int value_length;
+  if (!GetFlagValue(env, name, &value, &value_length))
     return;
-  *flag = internal_atoll(val);
-  InternalFree(val);
+  *flag = internal_atoll(value);
 }
 
+static LowLevelAllocator allocator_for_flags;
+
 void ParseFlag(const char *env, const char **flag, const char *name) {
-  const char *val = GetFlagValue(env, name);
-  if (val == 0)
+  const char *value;
+  int value_length;
+  if (!GetFlagValue(env, name, &value, &value_length))
     return;
-  *flag = val;
+  // Copy the flag value.
+  char *value_copy = (char*)(allocator_for_flags.Allocate(value_length + 1));
+  internal_memcpy(value_copy, value, value_length);
+  value_copy[value_length] = '\0';
+  *flag = value_copy;
 }
 
 }  // namespace __sanitizer





More information about the llvm-commits mailing list