[compiler-rt] r291268 - [sanitizer] Track which modules are instrumented in LoadedModule objects

Kuba Mracek via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 6 11:34:54 PST 2017


Author: kuba.brecka
Date: Fri Jan  6 13:34:54 2017
New Revision: 291268

URL: http://llvm.org/viewvc/llvm-project?rev=291268&view=rev
Log:
[sanitizer] Track which modules are instrumented in LoadedModule objects

This patch adds tracking which modules are instrumented and which are not. On macOS, instrumented modules link against the ASan/TSan/... dylib, so we can just check if such a load command exists or not.

Differential Revision: https://reviews.llvm.org/D28263


Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_procmaps.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_procmaps_mac.cc

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc?rev=291268&r1=291267&r2=291268&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc Fri Jan  6 13:34:54 2017
@@ -260,10 +260,12 @@ void LoadedModule::set(const char *modul
 }
 
 void LoadedModule::set(const char *module_name, uptr base_address,
-                       ModuleArch arch, u8 uuid[kModuleUUIDSize]) {
+                       ModuleArch arch, u8 uuid[kModuleUUIDSize],
+                       bool instrumented) {
   set(module_name, base_address);
   arch_ = arch;
   internal_memcpy(uuid_, uuid, sizeof(uuid_));
+  instrumented_ = instrumented;
 }
 
 void LoadedModule::clear() {
@@ -271,6 +273,7 @@ void LoadedModule::clear() {
   full_name_ = nullptr;
   arch_ = kModuleArchUnknown;
   internal_memset(uuid_, 0, kModuleUUIDSize);
+  instrumented_ = false;
   while (!ranges_.empty()) {
     AddressRange *r = ranges_.front();
     ranges_.pop_front();

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=291268&r1=291267&r2=291268&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h Fri Jan  6 13:34:54 2017
@@ -672,13 +672,16 @@ const uptr kModuleUUIDSize = 16;
 class LoadedModule {
  public:
   LoadedModule()
-      : full_name_(nullptr), base_address_(0), arch_(kModuleArchUnknown) {
+      : full_name_(nullptr),
+        base_address_(0),
+        arch_(kModuleArchUnknown),
+        instrumented_(false) {
     internal_memset(uuid_, 0, kModuleUUIDSize);
     ranges_.clear();
   }
   void set(const char *module_name, uptr base_address);
   void set(const char *module_name, uptr base_address, ModuleArch arch,
-           u8 uuid[kModuleUUIDSize]);
+           u8 uuid[kModuleUUIDSize], bool instrumented);
   void clear();
   void addAddressRange(uptr beg, uptr end, bool executable);
   bool containsAddress(uptr address) const;
@@ -687,6 +690,7 @@ class LoadedModule {
   uptr base_address() const { return base_address_; }
   ModuleArch arch() const { return arch_; }
   const u8 *uuid() const { return uuid_; }
+  bool instrumented() const { return instrumented_; }
 
   struct AddressRange {
     AddressRange *next;
@@ -705,6 +709,7 @@ class LoadedModule {
   uptr base_address_;
   ModuleArch arch_;
   u8 uuid_[kModuleUUIDSize];
+  bool instrumented_;
   IntrusiveList<AddressRange> ranges_;
 };
 

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_procmaps.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_procmaps.h?rev=291268&r1=291267&r2=291268&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_procmaps.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_procmaps.h Fri Jan  6 13:34:54 2017
@@ -77,6 +77,7 @@ class MemoryMappingLayout {
   u8 current_uuid_[kModuleUUIDSize];
   int current_load_cmd_count_;
   char *current_load_cmd_addr_;
+  bool current_instrumented_;
 # endif
 };
 

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_procmaps_mac.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_procmaps_mac.cc?rev=291268&r1=291267&r2=291268&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_procmaps_mac.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_procmaps_mac.cc Fri Jan  6 13:34:54 2017
@@ -150,22 +150,36 @@ ModuleArch ModuleArchFromCpuType(cpu_typ
   }
 }
 
+static const load_command *NextCommand(const load_command *lc) {
+  return (const load_command *)((char *)lc + lc->cmdsize);
+}
+
 static void FindUUID(const load_command *first_lc, u8 *uuid_output) {
-  const load_command *current_lc = first_lc;
-  while (1) {
-    if (current_lc->cmd == 0) return;
-    if (current_lc->cmd == LC_UUID) {
-      const uuid_command *uuid_lc = (const uuid_command *)current_lc;
-      const uint8_t *uuid = &uuid_lc->uuid[0];
-      internal_memcpy(uuid_output, uuid, kModuleUUIDSize);
-      return;
-    }
+  for (const load_command *lc = first_lc; lc->cmd != 0; lc = NextCommand(lc)) {
+    if (lc->cmd != LC_UUID) continue;
 
-    current_lc =
-        (const load_command *)(((char *)current_lc) + current_lc->cmdsize);
+    const uuid_command *uuid_lc = (const uuid_command *)lc;
+    const uint8_t *uuid = &uuid_lc->uuid[0];
+    internal_memcpy(uuid_output, uuid, kModuleUUIDSize);
+    return;
   }
 }
 
+static bool IsModuleInstrumented(const load_command *first_lc) {
+  for (const load_command *lc = first_lc; lc->cmd != 0; lc = NextCommand(lc)) {
+    if (lc->cmd != LC_LOAD_DYLIB) continue;
+
+    const dylib_command *dylib_lc = (const dylib_command *)lc;
+    uint32_t dylib_name_offset = dylib_lc->dylib.name.offset;
+    const char *dylib_name = ((const char *)dylib_lc) + dylib_name_offset;
+    dylib_name = StripModuleName(dylib_name);
+    if (dylib_name != 0 && (internal_strstr(dylib_name, "libclang_rt."))) {
+      return true;
+    }
+  }
+  return false;
+}
+
 bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
                                char filename[], uptr filename_size,
                                uptr *protection, ModuleArch *arch, u8 *uuid) {
@@ -193,10 +207,11 @@ bool MemoryMappingLayout::Next(uptr *sta
           continue;
         }
       }
+      FindUUID((const load_command *)current_load_cmd_addr_, &current_uuid_[0]);
+      current_instrumented_ =
+          IsModuleInstrumented((const load_command *)current_load_cmd_addr_);
     }
 
-    FindUUID((const load_command *)current_load_cmd_addr_, &current_uuid_[0]);
-
     for (; current_load_cmd_count_ >= 0; current_load_cmd_count_--) {
       switch (current_magic_) {
         // current_magic_ may be only one of MH_MAGIC, MH_MAGIC_64.
@@ -244,7 +259,8 @@ void MemoryMappingLayout::DumpListOfModu
     } else {
       modules->push_back(LoadedModule());
       cur_module = &modules->back();
-      cur_module->set(cur_name, cur_beg, cur_arch, cur_uuid);
+      cur_module->set(cur_name, cur_beg, cur_arch, cur_uuid,
+                      current_instrumented_);
     }
     cur_module->addAddressRange(cur_beg, cur_end, prot & kProtectionExecute);
   }




More information about the llvm-commits mailing list