[msandr] Add NATIVE_EXEC macro for building client for running in DynamoRIO hybrid mode only.

When running application in DynamoRIO hybrid mode only, only uninstrumented
modules will run in DynamoRIO and be instrumented by the client, so we do not
need module table in MSanDR.

Patch by Qin Zhao.



git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@193411 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/msandr/CMakeLists.txt b/lib/msandr/CMakeLists.txt
index 5a96a9d..e302726 100644
--- a/lib/msandr/CMakeLists.txt
+++ b/lib/msandr/CMakeLists.txt
@@ -4,6 +4,11 @@
   find_package(DynamoRIO)
   find_package(DrMemoryFramework)
 
+  option(MSANDR_NATIVE_EXEC "Building msandr client for running in DynamoRIO hybrid mode, which allows some module running natively" OFF)
+  if (MSANDR_NATIVE_EXEC)
+    add_definitions(-DMSANDR_NATIVE_EXEC)
+  endif (MSANDR_NATIVE_EXEC)
+
   set(arch "x86_64")
   add_library(clang_rt.msandr-${arch} SHARED msandr.cc)
   configure_DynamoRIO_client(clang_rt.msandr-${arch})
diff --git a/lib/msandr/msandr.cc b/lib/msandr/msandr.cc
index 27b1c94..7aef0c4 100644
--- a/lib/msandr/msandr.cc
+++ b/lib/msandr/msandr.cc
@@ -62,6 +62,12 @@
 
 namespace {
 
+std::string g_app_path;
+
+int msan_retval_tls_offset;
+int msan_param_tls_offset;
+
+#ifndef MSANDR_NATIVE_EXEC
 class ModuleData {
 public:
   ModuleData();
@@ -78,11 +84,6 @@
   bool executed_;
 };
 
-std::string g_app_path;
-
-int msan_retval_tls_offset;
-int msan_param_tls_offset;
-
 // A vector of loaded modules sorted by module bounds.  We lookup the current PC
 // in here from the bb event.  This is better than an rb tree because the lookup
 // is faster and the bb event occurs far more than the module load event.
@@ -99,6 +100,7 @@
       // We'll check the black/white lists later and adjust this.
       should_instrument_(true), executed_(false) {
 }
+#endif /* !MSANDR_NATIVE_EXEC */
 
 int(*__msan_get_retval_tls_offset)();
 int(*__msan_get_param_tls_offset)();
@@ -319,6 +321,7 @@
   // a prefix.
 }
 
+#ifndef MSANDR_NATIVE_EXEC
 // For use with binary search.  Modules shouldn't overlap, so we shouldn't have
 // to look at end_.  If that can happen, we won't support such an application.
 bool ModuleDataCompareStart(const ModuleData &left, const ModuleData &right) {
@@ -373,22 +376,26 @@
   }
   return true;
 }
+#endif /* !NATIVE_CLIENT */
 
 // TODO(rnk): Make sure we instrument after __msan_init.
 dr_emit_flags_t
 event_basic_block_app2app(void *drcontext, void *tag, instrlist_t *bb,
                           bool for_trace, bool translating) {
+#ifndef MSANDR_NATIVE_EXEC
   app_pc pc = dr_fragment_app_pc(tag);
-
   if (ShouldInstrumentPc(pc, NULL))
     CHECK(drutil_expand_rep_string(drcontext, bb));
-
+#else  /* MSANDR_NATIVE_EXEC */
+  CHECK(drutil_expand_rep_string(drcontext, bb));
+#endif /* MSANDR_NATIVE_EXEC */
   return DR_EMIT_PERSISTABLE;
 }
 
 dr_emit_flags_t event_basic_block(void *drcontext, void *tag, instrlist_t *bb,
                                   bool for_trace, bool translating) {
   app_pc pc = dr_fragment_app_pc(tag);
+#ifndef MSANDR_NATIVE_EXEC
   ModuleData *mod_data;
 
   if (!ShouldInstrumentPc(pc, &mod_data))
@@ -411,6 +418,8 @@
           pc - mod_data->start_);
     }
   }
+#endif /* !MSANDR_NATIVE_EXEC */
+
   if (VERBOSITY > 1) {
     instrlist_disassemble(drcontext, pc, bb, STDOUT);
     instr_t *instr;
@@ -474,6 +483,7 @@
   return DR_EMIT_PERSISTABLE;
 }
 
+#ifndef MSANDR_NATIVE_EXEC
 void event_module_load(void *drcontext, const module_data_t *info,
                        bool loaded) {
   // Insert the module into the list while maintaining the ordering.
@@ -507,6 +517,7 @@
         it->end_ == mod_data.end_ && it->path_ == mod_data.path_);
   g_module_list.erase(it);
 }
+#endif /* !MSANDR_NATIVE_EXEC */
 
 void event_exit() {
   // Clean up so DR doesn't tell us we're leaking memory.
@@ -551,6 +562,7 @@
     drsys_syscall_t *syscall = (drsys_syscall_t *)user_data;
     const char *name;
     res = drsys_syscall_name(syscall, &name);
+    CHECK(res == DRMF_SUCCESS);
     dr_printf("drsyscall: syscall '%s' arg %d wrote range [%p, %p)\n",
               name, arg->ordinal, arg->start_addr,
               (char *)arg->start_addr + sz);
@@ -719,8 +731,10 @@
 
   drmgr_register_bb_app2app_event(event_basic_block_app2app, &priority);
   drmgr_register_bb_instru2instru_event(event_basic_block, &priority);
+#ifndef MSANDR_NATIVE_EXEC
   drmgr_register_module_load_event(event_module_load);
   drmgr_register_module_unload_event(event_module_unload);
+#endif /* MSANDR_NATIVE_EXEC */
   if (VERBOSITY > 0)
     dr_printf("==MSANDR== Starting!\n");
 }