Commit 181a9bcb by Thomas Lively

Subzero: Removed unnecessary global access checks

parent ac27c516
...@@ -132,6 +132,8 @@ void ASanInstrumentation::instrumentGlobals(VariableDeclarationList &Globals) { ...@@ -132,6 +132,8 @@ void ASanInstrumentation::instrumentGlobals(VariableDeclarationList &Globals) {
NewGlobals.push_back(Global); NewGlobals.push_back(Global);
NewGlobals.push_back(RzRight); NewGlobals.push_back(RzRight);
RzGlobalsNum += 2; RzGlobalsNum += 2;
GlobalSizes.insert({Global->getName(), Global->getNumBytes()});
} }
// Replace old list of globals, without messing up arena allocators // Replace old list of globals, without messing up arena allocators
...@@ -297,6 +299,8 @@ void ASanInstrumentation::instrumentAccess(LoweringContext &Context, ...@@ -297,6 +299,8 @@ void ASanInstrumentation::instrumentAccess(LoweringContext &Context,
if (LocalSize != ICE_TLS_GET_FIELD(LocalVars)->end() && if (LocalSize != ICE_TLS_GET_FIELD(LocalVars)->end() &&
LocalSize->second >= Size) LocalSize->second >= Size)
return; return;
if (isOkGlobalAccess(Op, Size))
return;
constexpr SizeT NumArgs = 2; constexpr SizeT NumArgs = 2;
constexpr Variable *Void = nullptr; constexpr Variable *Void = nullptr;
constexpr bool NoTailCall = false; constexpr bool NoTailCall = false;
...@@ -311,6 +315,16 @@ void ASanInstrumentation::instrumentAccess(LoweringContext &Context, ...@@ -311,6 +315,16 @@ void ASanInstrumentation::instrumentAccess(LoweringContext &Context,
Context.setNext(Next); 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 *) { void ASanInstrumentation::instrumentRet(LoweringContext &Context, InstRet *) {
Cfg *Func = Context.getNode()->getCfg(); Cfg *Func = Context.getNode()->getCfg();
InstList::iterator Next = Context.getNext(); InstList::iterator Next = Context.getNext();
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
namespace Ice { namespace Ice {
using VarSizeMap = std::unordered_map<Operand *, SizeT>; using VarSizeMap = std::unordered_map<Operand *, SizeT>;
using GlobalSizeMap = std::unordered_map<GlobalString, SizeT>;
class ASanInstrumentation : public Instrumentation { class ASanInstrumentation : public Instrumentation {
ASanInstrumentation() = delete; ASanInstrumentation() = delete;
...@@ -41,6 +42,7 @@ public: ...@@ -41,6 +42,7 @@ public:
private: private:
std::string nextRzName(); std::string nextRzName();
bool isOkGlobalAccess(Operand *Op, SizeT Size);
bool isInstrumentable(Cfg *Func) override; bool isInstrumentable(Cfg *Func) override;
void instrumentFuncStart(LoweringContext &Context) override; void instrumentFuncStart(LoweringContext &Context) override;
void instrumentCall(LoweringContext &Context, InstCall *Instr) override; void instrumentCall(LoweringContext &Context, InstCall *Instr) override;
...@@ -53,6 +55,7 @@ private: ...@@ -53,6 +55,7 @@ private:
void finishFunc(Cfg *Func) override; void finishFunc(Cfg *Func) override;
ICE_TLS_DECLARE_FIELD(VarSizeMap *, LocalVars); ICE_TLS_DECLARE_FIELD(VarSizeMap *, LocalVars);
ICE_TLS_DECLARE_FIELD(std::vector<InstCall *> *, LocalDtors); ICE_TLS_DECLARE_FIELD(std::vector<InstCall *> *, LocalDtors);
GlobalSizeMap GlobalSizes;
std::atomic<uint32_t> RzNum; std::atomic<uint32_t> RzNum;
bool DidProcessGlobals = false; bool DidProcessGlobals = false;
SizeT RzGlobalsNum = 0; SizeT RzGlobalsNum = 0;
......
...@@ -5,20 +5,6 @@ ...@@ -5,20 +5,6 @@
; RUN: %p2i -i %s --args -verbose=inst -threads=0 -fsanitize-address \ ; RUN: %p2i -i %s --args -verbose=inst -threads=0 -fsanitize-address \
; RUN: | FileCheck --check-prefix=DUMP %s ; 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 ; A function with a local variable that does the loads
define internal void @doLoads(i32 %arg8, i32 %arg16, i32 %arg32, i32 %arg64, define internal void @doLoads(i32 %arg8, i32 %arg16, i32 %arg32, i32 %arg64,
i32 %arg128) { i32 %arg128) {
...@@ -28,30 +14,6 @@ define internal void @doLoads(i32 %arg8, i32 %arg16, i32 %arg32, i32 %arg64, ...@@ -28,30 +14,6 @@ define internal void @doLoads(i32 %arg8, i32 %arg16, i32 %arg32, i32 %arg64,
%srcLocal64 = inttoptr i32 %arg64 to i64* %srcLocal64 = inttoptr i32 %arg64 to i64*
%srcLocal128 = inttoptr i32 %arg128 to <4 x i32>* %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 %dest11 = load i8, i8* %srcLocal8, align 1
%dest12 = load i16, i16* %srcLocal16, align 1 %dest12 = load i16, i16* %srcLocal16, align 1
%dest13 = load i32, i32* %srcLocal32, align 1 %dest13 = load i32, i32* %srcLocal32, align 1
...@@ -64,26 +26,6 @@ define internal void @doLoads(i32 %arg8, i32 %arg16, i32 %arg32, i32 %arg64, ...@@ -64,26 +26,6 @@ define internal void @doLoads(i32 %arg8, i32 %arg16, i32 %arg32, i32 %arg64,
; DUMP-LABEL: ================ Instrumented CFG ================ ; DUMP-LABEL: ================ Instrumented CFG ================
; DUMP-NEXT: define internal void @doLoads( ; DUMP-NEXT: define internal void @doLoads(
; DUMP-NEXT: __0: ; 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: call void @__asan_check_load(i32 %arg8, i32 1)
; DUMP-NEXT: %dest11 = load i8, i8* %arg8, align 1 ; DUMP-NEXT: %dest11 = load i8, i8* %arg8, align 1
; DUMP-NEXT: call void @__asan_check_load(i32 %arg16, i32 2) ; DUMP-NEXT: call void @__asan_check_load(i32 %arg16, i32 2)
......
...@@ -5,13 +5,6 @@ ...@@ -5,13 +5,6 @@
; RUN: %p2i -i %s --args -verbose=inst -threads=0 -fsanitize-address \ ; RUN: %p2i -i %s --args -verbose=inst -threads=0 -fsanitize-address \
; RUN: | FileCheck --check-prefix=DUMP %s ; 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 ; A function with a local variable that does the stores
define internal void @doStores(<4 x i32> %vecSrc, i32 %arg8, i32 %arg16, define internal void @doStores(<4 x i32> %vecSrc, i32 %arg8, i32 %arg16,
i32 %arg32, i32 %arg64, i32 %arg128) { i32 %arg32, i32 %arg64, i32 %arg128) {
...@@ -21,18 +14,6 @@ define internal void @doStores(<4 x i32> %vecSrc, i32 %arg8, i32 %arg16, ...@@ -21,18 +14,6 @@ define internal void @doStores(<4 x i32> %vecSrc, i32 %arg8, i32 %arg16,
%destLocal64 = inttoptr i32 %arg64 to i64* %destLocal64 = inttoptr i32 %arg64 to i64*
%destLocal128 = inttoptr i32 %arg128 to <4 x i32>* %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 i8 42, i8* %destLocal8, align 1
store i16 42, i16* %destLocal16, align 1 store i16 42, i16* %destLocal16, align 1
store i32 42, i32* %destLocal32, align 1 store i32 42, i32* %destLocal32, align 1
...@@ -45,16 +26,6 @@ define internal void @doStores(<4 x i32> %vecSrc, i32 %arg8, i32 %arg16, ...@@ -45,16 +26,6 @@ define internal void @doStores(<4 x i32> %vecSrc, i32 %arg8, i32 %arg16,
; DUMP-LABEL: ================ Instrumented CFG ================ ; DUMP-LABEL: ================ Instrumented CFG ================
; DUMP-NEXT: define internal void @doStores( ; DUMP-NEXT: define internal void @doStores(
; DUMP-NEXT: __0: ; 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: call void @__asan_check_store(i32 %arg8, i32 1)
; DUMP-NEXT: store i8 42, i8* %arg8, align 1 ; DUMP-NEXT: store i8 42, i8* %arg8, align 1
; DUMP-NEXT: call void @__asan_check_store(i32 %arg16, i32 2) ; DUMP-NEXT: call void @__asan_check_store(i32 %arg16, i32 2)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment