[compiler-rt] r182579 - [ASan] Introduce SymbolizerPrepareForSandboxing(), which is a no-op on every platform except Linux (because we don't support sandboxing anywhere else yet)

Alexander Potapenko glider at google.com
Thu May 23 04:53:36 PDT 2013


Author: glider
Date: Thu May 23 06:53:36 2013
New Revision: 182579

URL: http://llvm.org/viewvc/llvm-project?rev=182579&view=rev
Log:
[ASan] Introduce SymbolizerPrepareForSandboxing(), which is a no-op on every platform except Linux (because we don't support sandboxing anywhere else yet)
On Linux we pre-cache the value of readlink("/proc/self/exe"), so that it can be later used when the sandbox has been turned on.

Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer_linux_libcdep.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer_mac.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer_win.cc

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc?rev=182579&r1=182578&r2=182579&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc Thu May 23 06:53:36 2013
@@ -23,6 +23,7 @@
 #include "sanitizer_placement_new.h"
 #include "sanitizer_procmaps.h"
 #include "sanitizer_stacktrace.h"
+#include "sanitizer_symbolizer.h"
 
 #include <asm/param.h>
 #include <dlfcn.h>
@@ -305,6 +306,8 @@ void PrepareForSandboxing() {
   // process will be able to load additional libraries, so it's fine to use the
   // cached mappings.
   MemoryMappingLayout::CacheMemoryMappings();
+  // Same for /proc/self/exe in the symbolizer.
+  SymbolizerPrepareForSandboxing();
 }
 
 // ----------------- sanitizer_procmaps.h

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer.h?rev=182579&r1=182578&r2=182579&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer.h Thu May 23 06:53:36 2013
@@ -114,6 +114,8 @@ typedef bool (*string_predicate_t)(const
 uptr GetListOfModules(LoadedModule *modules, uptr max_modules,
                       string_predicate_t filter);
 
+void SymbolizerPrepareForSandboxing();
+
 }  // namespace __sanitizer
 
 #endif  // SANITIZER_SYMBOLIZER_H

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer_linux_libcdep.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer_linux_libcdep.cc?rev=182579&r1=182578&r2=182579&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer_linux_libcdep.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer_linux_libcdep.cc Thu May 23 06:53:36 2013
@@ -131,6 +131,10 @@ uptr GetListOfModules(LoadedModule *modu
                       string_predicate_t filter) {
   return 0;
 }
+
+void SymbolizerPrepareForSandboxing() {
+  // Do nothing on Android.
+}
 #else  // SANITIZER_ANDROID
 typedef ElfW(Phdr) Elf_Phdr;
 
@@ -144,6 +148,32 @@ struct DlIteratePhdrData {
 
 static const uptr kMaxPathLength = 512;
 
+static char proc_self_exe_cache_str[kMaxPathLength];
+static uptr proc_self_exe_cache_len = 0;
+
+static uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
+  uptr module_name_len = internal_readlink(
+      "/proc/self/exe", buf, buf_len);
+  int readlink_error;
+  if (internal_iserror(buf_len, &readlink_error)) {
+    if (proc_self_exe_cache_len) {
+      // If available, use the cached module name.
+      CHECK_LE(proc_self_exe_cache_len, buf_len);
+      internal_strncpy(buf, proc_self_exe_cache_str, buf_len);
+      module_name_len = internal_strlen(proc_self_exe_cache_str);
+    } else {
+      // We can't read /proc/self/exe for some reason, assume the name of the
+      // binary is unknown.
+      Report("WARNING: readlink(\"/proc/self/exe\") failed with errno %d, "
+             "some stack frames may not be symbolized\n", readlink_error);
+      module_name_len = internal_snprintf(buf, buf_len, "/proc/self/exe");
+    }
+    CHECK_LT(module_name_len, buf_len);
+    buf[module_name_len] = '\0';
+  }
+  return module_name_len;
+}
+
 static int dl_iterate_phdr_cb(dl_phdr_info *info, size_t size, void *arg) {
   DlIteratePhdrData *data = (DlIteratePhdrData*)arg;
   if (data->current_n == data->max_n)
@@ -153,19 +183,7 @@ static int dl_iterate_phdr_cb(dl_phdr_in
   if (data->first) {
     data->first = false;
     // First module is the binary itself.
-    uptr module_name_len = internal_readlink(
-        "/proc/self/exe", module_name.data(), module_name.size());
-    int readlink_error;
-    if (internal_iserror(module_name_len, &readlink_error)) {
-      // We can't read /proc/self/exe for some reason, assume the name of the
-      // binary is unknown.
-      Report("WARNING: readlink(\"/proc/self/exe\") failed with errno %d, some "
-             "stack frames may not be symbolized\n", readlink_error);
-      module_name_len = internal_snprintf(module_name.data(),
-                                          module_name.size(), "/proc/self/exe");
-    }
-    CHECK_LT(module_name_len, module_name.size());
-    module_name[module_name_len] = '\0';
+    ReadBinaryName(module_name.data(), module_name.size());
   } else if (info->dlpi_name) {
     internal_strncpy(module_name.data(), info->dlpi_name, module_name.size());
   }
@@ -195,6 +213,13 @@ uptr GetListOfModules(LoadedModule *modu
   dl_iterate_phdr(dl_iterate_phdr_cb, &data);
   return data.current_n;
 }
+
+void SymbolizerPrepareForSandboxing() {
+  if (!proc_self_exe_cache_len) {
+    proc_self_exe_cache_len =
+        ReadBinaryName(proc_self_exe_cache_str, kMaxPathLength);
+  }
+}
 #endif  // SANITIZER_ANDROID
 
 }  // namespace __sanitizer

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer_mac.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer_mac.cc?rev=182579&r1=182578&r2=182579&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer_mac.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer_mac.cc Thu May 23 06:53:36 2013
@@ -31,6 +31,10 @@ uptr GetListOfModules(LoadedModule *modu
   return 0;
 }
 
+void SymbolizerPrepareForSandboxing() {
+  // Do nothing on Mac.
+}
+
 }  // namespace __sanitizer
 
 #endif  // SANITIZER_MAC

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer_win.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer_win.cc?rev=182579&r1=182578&r2=182579&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer_win.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_symbolizer_win.cc Thu May 23 06:53:36 2013
@@ -31,6 +31,10 @@ uptr GetListOfModules(LoadedModule *modu
   UNIMPLEMENTED();
 };
 
+void SymbolizerPrepareForSandboxing() {
+  // Do nothing on Windows.
+}
+
 const char *Demangle(const char *MangledName) {
   return MangledName;
 }





More information about the llvm-commits mailing list