Subzero: Removed unnecessary global access checks

BUG=https://bugs.chromium.org/p/nativeclient/issues/detail?id=4374
R=stichnot@chromium.org

Review URL: https://codereview.chromium.org/2183683003 .
diff --git a/src/IceASanInstrumentation.cpp b/src/IceASanInstrumentation.cpp
index b53de0d..e3203b3 100644
--- a/src/IceASanInstrumentation.cpp
+++ b/src/IceASanInstrumentation.cpp
@@ -132,6 +132,8 @@
     NewGlobals.push_back(Global);
     NewGlobals.push_back(RzRight);
     RzGlobalsNum += 2;
+
+    GlobalSizes.insert({Global->getName(), Global->getNumBytes()});
   }
 
   // Replace old list of globals, without messing up arena allocators
@@ -297,6 +299,8 @@
   if (LocalSize != ICE_TLS_GET_FIELD(LocalVars)->end() &&
       LocalSize->second >= Size)
     return;
+  if (isOkGlobalAccess(Op, Size))
+    return;
   constexpr SizeT NumArgs = 2;
   constexpr Variable *Void = nullptr;
   constexpr bool NoTailCall = false;
@@ -311,6 +315,16 @@
   Context.setNext(Next);
 }
 
+// TODO(tlively): Trace back load and store addresses to find their real offsets
+bool ASanInstrumentation::isOkGlobalAccess(Operand *Op, SizeT Size) {
+  auto *Reloc = llvm::dyn_cast<ConstantRelocatable>(Op);
+  if (Reloc == nullptr)
+    return false;
+  RelocOffsetT Offset = Reloc->getOffset();
+  GlobalSizeMap::iterator GlobalSize = GlobalSizes.find(Reloc->getName());
+  return GlobalSize != GlobalSizes.end() && GlobalSize->second - Offset >= Size;
+}
+
 void ASanInstrumentation::instrumentRet(LoweringContext &Context, InstRet *) {
   Cfg *Func = Context.getNode()->getCfg();
   InstList::iterator Next = Context.getNext();
diff --git a/src/IceASanInstrumentation.h b/src/IceASanInstrumentation.h
index ef212f5..f3090ef 100644
--- a/src/IceASanInstrumentation.h
+++ b/src/IceASanInstrumentation.h
@@ -26,6 +26,7 @@
 namespace Ice {
 
 using VarSizeMap = std::unordered_map<Operand *, SizeT>;
+using GlobalSizeMap = std::unordered_map<GlobalString, SizeT>;
 
 class ASanInstrumentation : public Instrumentation {
   ASanInstrumentation() = delete;
@@ -41,6 +42,7 @@
 
 private:
   std::string nextRzName();
+  bool isOkGlobalAccess(Operand *Op, SizeT Size);
   bool isInstrumentable(Cfg *Func) override;
   void instrumentFuncStart(LoweringContext &Context) override;
   void instrumentCall(LoweringContext &Context, InstCall *Instr) override;
@@ -53,6 +55,7 @@
   void finishFunc(Cfg *Func) override;
   ICE_TLS_DECLARE_FIELD(VarSizeMap *, LocalVars);
   ICE_TLS_DECLARE_FIELD(std::vector<InstCall *> *, LocalDtors);
+  GlobalSizeMap GlobalSizes;
   std::atomic<uint32_t> RzNum;
   bool DidProcessGlobals = false;
   SizeT RzGlobalsNum = 0;
diff --git a/tests_lit/asan_tests/instrumentload.ll b/tests_lit/asan_tests/instrumentload.ll
index b015905..fae7ea6 100644
--- a/tests_lit/asan_tests/instrumentload.ll
+++ b/tests_lit/asan_tests/instrumentload.ll
@@ -5,20 +5,6 @@
 ; RUN: %p2i -i %s --args -verbose=inst -threads=0 -fsanitize-address \
 ; RUN:     | FileCheck --check-prefix=DUMP %s
 
-; Constants to load data from
-@srcConst8 = internal constant [1 x i8] c"D"
-@srcConst16 = internal constant [2 x i8] c"DA"
-@srcConst32 = internal constant [4 x i8] c"DATA"
-@srcConst64 = internal constant [8 x i8] c"DATADATA"
-@srcConst128 = internal constant [16 x i8] c"DATADATADATADATA"
-
-; A global to load data from
-@srcGlobal8 = internal global [1 x i8] c"D"
-@srcGlobal16 = internal global [2 x i8] c"DA"
-@srcGlobal32 = internal global [4 x i8] c"DATA"
-@srcGlobal64 = internal global [8 x i8] c"DATADATA"
-@srcGlobal128 = internal global [16 x i8] c"DATADATADATADATA"
-
 ; A function with a local variable that does the loads
 define internal void @doLoads(i32 %arg8, i32 %arg16, i32 %arg32, i32 %arg64,
                               i32 %arg128) {
@@ -28,30 +14,6 @@
   %srcLocal64 = inttoptr i32 %arg64 to i64*
   %srcLocal128 = inttoptr i32 %arg128 to <4 x i32>*
 
-  %ptrConst8 = bitcast [1 x i8]* @srcConst8 to i8*
-  %ptrConst16 = bitcast [2 x i8]* @srcConst16 to i16*
-  %ptrConst32 = bitcast [4 x i8]* @srcConst32 to i32*
-  %ptrConst64 = bitcast [8 x i8]* @srcConst64 to i64*
-  %ptrConst128 = bitcast [16 x i8]* @srcConst128 to <4 x i32>*
-
-  %ptrGlobal8 = bitcast [1 x i8]* @srcGlobal8 to i8*
-  %ptrGlobal16 = bitcast [2 x i8]* @srcGlobal16 to i16*
-  %ptrGlobal32 = bitcast [4 x i8]* @srcGlobal32 to i32*
-  %ptrGlobal64 = bitcast [8 x i8]* @srcGlobal64 to i64*
-  %ptrGlobal128 = bitcast [16 x i8]* @srcGlobal128 to <4 x i32>*
-
-  %dest1 = load i8, i8* %ptrConst8, align 1
-  %dest2 = load i16, i16* %ptrConst16, align 1
-  %dest3 = load i32, i32* %ptrConst32, align 1
-  %dest4 = load i64, i64* %ptrConst64, align 1
-  %dest5 = load <4 x i32>, <4 x i32>* %ptrConst128, align 4
-
-  %dest6 = load i8, i8* %ptrGlobal8, align 1
-  %dest7 = load i16, i16* %ptrGlobal16, align 1
-  %dest8 = load i32, i32* %ptrGlobal32, align 1
-  %dest9 = load i64, i64* %ptrGlobal64, align 1
-  %dest10 = load <4 x i32>, <4 x i32>* %ptrGlobal128, align 4
-
   %dest11 = load i8, i8* %srcLocal8, align 1
   %dest12 = load i16, i16* %srcLocal16, align 1
   %dest13 = load i32, i32* %srcLocal32, align 1
@@ -64,26 +26,6 @@
 ; DUMP-LABEL: ================ Instrumented CFG ================
 ; DUMP-NEXT: define internal void @doLoads(
 ; DUMP-NEXT: __0:
-; DUMP-NEXT: call void @__asan_check_load(i32 @srcConst8, i32 1)
-; DUMP-NEXT: %dest1 = load i8, i8* @srcConst8, align 1
-; DUMP-NEXT: call void @__asan_check_load(i32 @srcConst16, i32 2)
-; DUMP-NEXT: %dest2 = load i16, i16* @srcConst16, align 1
-; DUMP-NEXT: call void @__asan_check_load(i32 @srcConst32, i32 4)
-; DUMP-NEXT: %dest3 = load i32, i32* @srcConst32, align 1
-; DUMP-NEXT: call void @__asan_check_load(i32 @srcConst64, i32 8)
-; DUMP-NEXT: %dest4 = load i64, i64* @srcConst64, align 1
-; DUMP-NEXT: call void @__asan_check_load(i32 @srcConst128, i32 16)
-; DUMP-NEXT: %dest5 = load <4 x i32>, <4 x i32>* @srcConst128, align 4
-; DUMP-NEXT: call void @__asan_check_load(i32 @srcGlobal8, i32 1)
-; DUMP-NEXT: %dest6 = load i8, i8* @srcGlobal8, align 1
-; DUMP-NEXT: call void @__asan_check_load(i32 @srcGlobal16, i32 2)
-; DUMP-NEXT: %dest7 = load i16, i16* @srcGlobal16, align 1
-; DUMP-NEXT: call void @__asan_check_load(i32 @srcGlobal32, i32 4)
-; DUMP-NEXT: %dest8 = load i32, i32* @srcGlobal32, align 1
-; DUMP-NEXT: call void @__asan_check_load(i32 @srcGlobal64, i32 8)
-; DUMP-NEXT: %dest9 = load i64, i64* @srcGlobal64, align 1
-; DUMP-NEXT: call void @__asan_check_load(i32 @srcGlobal128, i32 16)
-; DUMP-NEXT: %dest10 = load <4 x i32>, <4 x i32>* @srcGlobal128, align 4
 ; DUMP-NEXT: call void @__asan_check_load(i32 %arg8, i32 1)
 ; DUMP-NEXT: %dest11 = load i8, i8* %arg8, align 1
 ; DUMP-NEXT: call void @__asan_check_load(i32 %arg16, i32 2)
diff --git a/tests_lit/asan_tests/instrumentstore.ll b/tests_lit/asan_tests/instrumentstore.ll
index 4401315..d486f91 100644
--- a/tests_lit/asan_tests/instrumentstore.ll
+++ b/tests_lit/asan_tests/instrumentstore.ll
@@ -5,13 +5,6 @@
 ; RUN: %p2i -i %s --args -verbose=inst -threads=0 -fsanitize-address \
 ; RUN:     | FileCheck --check-prefix=DUMP %s
 
-; A global to store data to
-@destGlobal8 = internal global [1 x i8] zeroinitializer
-@destGlobal16 = internal global [2 x i8] zeroinitializer
-@destGlobal32 = internal global [4 x i8] zeroinitializer
-@destGlobal64 = internal global [8 x i8] zeroinitializer
-@destGlobal128 = internal global [16 x i8] zeroinitializer
-
 ; A function with a local variable that does the stores
 define internal void @doStores(<4 x i32> %vecSrc, i32 %arg8, i32 %arg16,
                                i32 %arg32, i32 %arg64, i32 %arg128) {
@@ -21,18 +14,6 @@
   %destLocal64 = inttoptr i32 %arg64 to i64*
   %destLocal128 = inttoptr i32 %arg128 to <4 x i32>*
 
-  %ptrGlobal8 = bitcast [1 x i8]* @destGlobal8 to i8*
-  %ptrGlobal16 = bitcast [2 x i8]* @destGlobal16 to i16*
-  %ptrGlobal32 = bitcast [4 x i8]* @destGlobal32 to i32*
-  %ptrGlobal64 = bitcast [8 x i8]* @destGlobal64 to i64*
-  %ptrGlobal128 = bitcast [16 x i8]* @destGlobal128 to <4 x i32>*
-
-  store i8 42, i8* %ptrGlobal8, align 1
-  store i16 42, i16* %ptrGlobal16, align 1
-  store i32 42, i32* %ptrGlobal32, align 1
-  store i64 42, i64* %ptrGlobal64, align 1
-  store <4 x i32> %vecSrc, <4 x i32>* %ptrGlobal128, align 4
-
   store i8 42, i8* %destLocal8, align 1
   store i16 42, i16* %destLocal16, align 1
   store i32 42, i32* %destLocal32, align 1
@@ -45,16 +26,6 @@
 ; DUMP-LABEL: ================ Instrumented CFG ================
 ; DUMP-NEXT: define internal void @doStores(
 ; DUMP-NEXT: __0:
-; DUMP-NEXT: call void @__asan_check_store(i32 @destGlobal8, i32 1)
-; DUMP-NEXT: store i8 42, i8* @destGlobal8, align 1
-; DUMP-NEXT: call void @__asan_check_store(i32 @destGlobal16, i32 2)
-; DUMP-NEXT: store i16 42, i16* @destGlobal16, align 1
-; DUMP-NEXT: call void @__asan_check_store(i32 @destGlobal32, i32 4)
-; DUMP-NEXT: store i32 42, i32* @destGlobal32, align 1
-; DUMP-NEXT: call void @__asan_check_store(i32 @destGlobal64, i32 8)
-; DUMP-NEXT: store i64 42, i64* @destGlobal64, align 1
-; DUMP-NEXT: call void @__asan_check_store(i32 @destGlobal128, i32 16)
-; DUMP-NEXT: store <4 x i32> %vecSrc, <4 x i32>* @destGlobal128, align 4
 ; DUMP-NEXT: call void @__asan_check_store(i32 %arg8, i32 1)
 ; DUMP-NEXT: store i8 42, i8* %arg8, align 1
 ; DUMP-NEXT: call void @__asan_check_store(i32 %arg16, i32 2)