llvm.org GIT mirror llvm / 6eba425
[esan|cfrag] Add the struct field offset array in StructInfo Summary: Adds the struct field offset array in struct StructInfo. Updates test struct_field_count_basic.ll. Reviewers: aizatsky Subscribers: llvm-commits, bruening, eugenis, kcc, zhaoqin, vitalybuka Differential Revision: http://reviews.llvm.org/D21192 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@272362 91177308-0d34-0410-b5e6-96231b3b80d8 Qin Zhao 4 years ago
2 changed file(s) with 66 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
157157 bool shouldIgnoreStructType(StructType *StructTy);
158158 void createStructCounterName(
159159 StructType *StructTy, SmallString &NameStr);
160 GlobalVariable *createCacheFragInfoGV(Module &M, Constant *UnitName);
161 Constant *createEsanInitToolInfoArg(Module &M);
160 GlobalVariable *createCacheFragInfoGV(Module &M, const DataLayout &DL,
161 Constant *UnitName);
162 Constant *createEsanInitToolInfoArg(Module &M, const DataLayout &DL);
162163 void createDestructor(Module &M, Constant *ToolInfoArg);
163164 bool runOnFunction(Function &F, Module &M);
164165 bool instrumentLoadOrStore(Instruction *I, const DataLayout &DL);
286287
287288 // Create the global variable for the cache-fragmentation tool.
288289 GlobalVariable *EfficiencySanitizer::createCacheFragInfoGV(
289 Module &M, Constant *UnitName) {
290 Module &M, const DataLayout &DL, Constant *UnitName) {
290291 assert(Options.ToolType == EfficiencySanitizerOptions::ESAN_CacheFrag);
291292
292293 auto *Int8PtrTy = Type::getInt8PtrTy(*Ctx);
293294 auto *Int8PtrPtrTy = Int8PtrTy->getPointerTo();
294295 auto *Int32Ty = Type::getInt32Ty(*Ctx);
296 auto *Int32PtrTy = Type::getInt32PtrTy(*Ctx);
295297 auto *Int64Ty = Type::getInt64Ty(*Ctx);
296298 auto *Int64PtrTy = Type::getInt64PtrTy(*Ctx);
297299 // This structure should be kept consistent with the StructInfo struct
298300 // in the runtime library.
299301 // struct StructInfo {
300302 // const char *StructName;
303 // u32 Size;
301304 // u32 NumFields;
305 // u32 *FieldOffsets;
302306 // u64 *FieldCounters;
303307 // const char **FieldTypeNames;
304308 // };
305309 auto *StructInfoTy =
306 StructType::get(Int8PtrTy, Int32Ty, Int64PtrTy, Int8PtrPtrTy, nullptr);
310 StructType::get(Int8PtrTy, Int32Ty, Int32Ty, Int32PtrTy, Int64PtrTy,
311 Int8PtrPtrTy, nullptr);
307312 auto *StructInfoPtrTy = StructInfoTy->getPointerTo();
308313 // This structure should be kept consistent with the CacheFragInfo struct
309314 // in the runtime library.
346351 // Remember the counter variable for each struct type.
347352 StructTyMap.insert(std::pair(StructTy, Counters));
348353
354 // We pass the field type name array and offset array to the runtime for
355 // better reporting.
349356 // FieldTypeNames.
350 // We pass the field type name array to the runtime for better reporting.
351357 auto *TypeNameArrayTy = ArrayType::get(Int8PtrTy, StructTy->getNumElements());
352 GlobalVariable *TypeName =
358 GlobalVariable *TypeNames =
353359 new GlobalVariable(M, TypeNameArrayTy, true,
354360 GlobalVariable::InternalLinkage, nullptr);
355361 SmallVector TypeNameVec;
362 // FieldOffsets.
363 const StructLayout *SL = DL.getStructLayout(StructTy);
364 auto *OffsetArrayTy = ArrayType::get(Int32Ty, StructTy->getNumElements());
365 GlobalVariable *Offsets =
366 new GlobalVariable(M, OffsetArrayTy, true,
367 GlobalVariable::InternalLinkage, nullptr);
368 SmallVector OffsetVec;
356369 for (unsigned i = 0; i < StructTy->getNumElements(); ++i) {
357370 Type *Ty = StructTy->getElementType(i);
358371 std::string Str;
362375 ConstantExpr::getPointerCast(
363376 createPrivateGlobalForString(M, StrOS.str(), true),
364377 Int8PtrTy));
378 OffsetVec.push_back(ConstantInt::get(Int32Ty, SL->getElementOffset(i)));
365379 }
366 TypeName->setInitializer(ConstantArray::get(TypeNameArrayTy, TypeNameVec));
380 TypeNames->setInitializer(ConstantArray::get(TypeNameArrayTy, TypeNameVec));
381 Offsets->setInitializer(ConstantArray::get(OffsetArrayTy, OffsetVec));
367382
368383 Initializers.push_back(
369384 ConstantStruct::get(
370385 StructInfoTy,
371386 ConstantExpr::getPointerCast(StructCounterName, Int8PtrTy),
387 ConstantInt::get(Int32Ty, SL->getSizeInBytes()),
372388 ConstantInt::get(Int32Ty, StructTy->getNumElements()),
389 ConstantExpr::getPointerCast(Offsets, Int32PtrTy),
373390 ConstantExpr::getPointerCast(Counters, Int64PtrTy),
374 ConstantExpr::getPointerCast(TypeName, Int8PtrPtrTy),
391 ConstantExpr::getPointerCast(TypeNames, Int8PtrPtrTy),
375392 nullptr));
376393 }
377394 // Structs.
398415 }
399416
400417 // Create the tool-specific argument passed to EsanInit and EsanExit.
401 Constant *EfficiencySanitizer::createEsanInitToolInfoArg(Module &M) {
418 Constant *EfficiencySanitizer::createEsanInitToolInfoArg(Module &M,
419 const DataLayout &DL) {
402420 // This structure contains tool-specific information about each compilation
403421 // unit (module) and is passed to the runtime library.
404422 GlobalVariable *ToolInfoGV = nullptr;
411429
412430 // Create the tool-specific variable.
413431 if (Options.ToolType == EfficiencySanitizerOptions::ESAN_CacheFrag)
414 ToolInfoGV = createCacheFragInfoGV(M, UnitName);
432 ToolInfoGV = createCacheFragInfoGV(M, DL, UnitName);
415433
416434 if (ToolInfoGV != nullptr)
417435 return ConstantExpr::getPointerCast(ToolInfoGV, Int8PtrTy);
444462 PointerType *Int8PtrTy = Type::getInt8PtrTy(*Ctx);
445463 IntptrTy = DL.getIntPtrType(M.getContext());
446464 // Create the variable passed to EsanInit and EsanExit.
447 Constant *ToolInfoArg = createEsanInitToolInfoArg(M);
465 Constant *ToolInfoArg = createEsanInitToolInfoArg(M, DL);
448466 // Constructor
449467 // We specify the tool type both in the EsanWhichToolName global
450468 // and as an arg to the init routine as a sanity check.
66 %struct.C = type { %struct.anon, %union.anon, [10 x i8] }
77 %struct.anon = type { i32, i32 }
88 %union.anon = type { double }
9
10 ; CHECK: @0 = private unnamed_addr constant [8 x i8] c"\00", align 1
11 ; CHECK-NEXT: @1 = private unnamed_addr constant [17 x i8] c"struct.A#2#11#11\00", align 1
12 ; CHECK-NEXT: @"struct.A#2#11#11" = weak global [2 x i64] zeroinitializer
13 ; CHECK-NEXT: @2 = internal constant [2 x i8*] [i8* getelementptr inbounds ([4 x i8], [4 x i8]* @4, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @5, i32 0, i32 0)]
14 ; CHECK-NEXT: @3 = internal constant [2 x i32] [i32 0, i32 4]
15 ; CHECK-NEXT: @4 = private unnamed_addr constant [4 x i8] c"i32\00", align 1
16 ; CHECK-NEXT: @5 = private unnamed_addr constant [4 x i8] c"i32\00", align 1
17 ; CHECK-NEXT: @6 = private unnamed_addr constant [12 x i8] c"union.U#1#3\00", align 1
18 ; CHECK-NEXT: @"union.U#1#3" = weak global [1 x i64] zeroinitializer
19 ; CHECK-NEXT: @7 = internal constant [1 x i8*] [i8* getelementptr inbounds ([7 x i8], [7 x i8]* @9, i32 0, i32 0)]
20 ; CHECK-NEXT: @8 = internal constant [1 x i32] zeroinitializer
21 ; CHECK-NEXT: @9 = private unnamed_addr constant [7 x i8] c"double\00", align 1
22 ; CHECK-NEXT: @10 = private unnamed_addr constant [20 x i8] c"struct.C#3#14#13#13\00", align 1
23 ; CHECK-NEXT: @"struct.C#3#14#13#13" = weak global [3 x i64] zeroinitializer
24 ; CHECK-NEXT: @11 = internal constant [3 x i8*] [i8* getelementptr inbounds ([33 x i8], [33 x i8]* @13, i32 0, i32 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @14, i32 0, i32 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @15, i32 0, i32 0)]
25 ; CHECK-NEXT: @12 = internal constant [3 x i32] [i32 0, i32 8, i32 16]
26 ; CHECK-NEXT: @13 = private unnamed_addr constant [33 x i8] c"%struct.anon = type { i32, i32 }\00", align 1
27 ; CHECK-NEXT: @14 = private unnamed_addr constant [30 x i8] c"%union.anon = type { double }\00", align 1
28 ; CHECK-NEXT: @15 = private unnamed_addr constant [10 x i8] c"[10 x i8]\00", align 1
29 ; CHECK-NEXT: @16 = private unnamed_addr constant [20 x i8] c"struct.anon#2#11#11\00", align 1
30 ; CHECK-NEXT: @"struct.anon#2#11#11" = weak global [2 x i64] zeroinitializer
31 ; CHECK-NEXT: @17 = internal constant [2 x i8*] [i8* getelementptr inbounds ([4 x i8], [4 x i8]* @19, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @20, i32 0, i32 0)]
32 ; CHECK-NEXT: @18 = internal constant [2 x i32] [i32 0, i32 4]
33 ; CHECK-NEXT: @19 = private unnamed_addr constant [4 x i8] c"i32\00", align 1
34 ; CHECK-NEXT: @20 = private unnamed_addr constant [4 x i8] c"i32\00", align 1
35 ; CHECK-NEXT: @21 = private unnamed_addr constant [15 x i8] c"union.anon#1#3\00", align 1
36 ; CHECK-NEXT: @"union.anon#1#3" = weak global [1 x i64] zeroinitializer
37 ; CHECK-NEXT: @22 = internal constant [1 x i8*] [i8* getelementptr inbounds ([7 x i8], [7 x i8]* @24, i32 0, i32 0)]
38 ; CHECK-NEXT: @23 = internal constant [1 x i32] zeroinitializer
39 ; CHECK-NEXT: @24 = private unnamed_addr constant [7 x i8] c"double\00", align 1
40 ; CHECK-NEXT: @25 = internal global [5 x { i8*, i32, i32, i32*, i64*, i8** }] [{ i8*, i32, i32, i32*, i64*, i8** } { i8* getelementptr inbounds ([17 x i8], [17 x i8]* @1, i32 0, i32 0), i32 8, i32 2, i32* getelementptr inbounds ([2 x i32], [2 x i32]* @3, i32 0, i32 0), i64* getelementptr inbounds ([2 x i64], [2 x i64]* @"struct.A#2#11#11", i32 0, i32 0), i8** getelementptr inbounds ([2 x i8*], [2 x i8*]* @2, i32 0, i32 0) }, { i8*, i32, i32, i32*, i64*, i8** } { i8* getelementptr inbounds ([12 x i8], [12 x i8]* @6, i32 0, i32 0), i32 8, i32 1, i32* getelementptr inbounds ([1 x i32], [1 x i32]* @8, i32 0, i32 0), i64* getelementptr inbounds ([1 x i64], [1 x i64]* @"union.U#1#3", i32 0, i32 0), i8** getelementptr inbounds ([1 x i8*], [1 x i8*]* @7, i32 0, i32 0) }, { i8*, i32, i32, i32*, i64*, i8** } { i8* getelementptr inbounds ([20 x i8], [20 x i8]* @10, i32 0, i32 0), i32 32, i32 3, i32* getelementptr inbounds ([3 x i32], [3 x i32]* @12, i32 0, i32 0), i64* getelementptr inbounds ([3 x i64], [3 x i64]* @"struct.C#3#14#13#13", i32 0, i32 0), i8** getelementptr inbounds ([3 x i8*], [3 x i8*]* @11, i32 0, i32 0) }, { i8*, i32, i32, i32*, i64*, i8** } { i8* getelementptr inbounds ([20 x i8], [20 x i8]* @16, i32 0, i32 0), i32 8, i32 2, i32* getelementptr inbounds ([2 x i32], [2 x i32]* @18, i32 0, i32 0), i64* getelementptr inbounds ([2 x i64], [2 x i64]* @"struct.anon#2#11#11", i32 0, i32 0), i8** getelementptr inbounds ([2 x i8*], [2 x i8*]* @17, i32 0, i32 0) }, { i8*, i32, i32, i32*, i64*, i8** } { i8* getelementptr inbounds ([15 x i8], [15 x i8]* @21, i32 0, i32 0), i32 8, i32 1, i32* getelementptr inbounds ([1 x i32], [1 x i32]* @23, i32 0, i32 0), i64* getelementptr inbounds ([1 x i64], [1 x i64]* @"union.anon#1#3", i32 0, i32 0), i8** getelementptr inbounds ([1 x i8*], [1 x i8*]* @22, i32 0, i32 0) }]
41 ; CHECK-NEXT: @26 = internal constant { i8*, i32, { i8*, i32, i32, i32*, i64*, i8** }* } { i8* getelementptr inbounds ([8 x i8], [8 x i8]* @0, i32 0, i32 0), i32 5, { i8*, i32, i32, i32*, i64*, i8** }* getelementptr inbounds ([5 x { i8*, i32, i32, i32*, i64*, i8** }], [5 x { i8*, i32, i32, i32*, i64*, i8** }]* @25, i32 0, i32 0) }
42
943
1044 define i32 @main() {
1145 entry:
3872 }
3973
4074 ; CHECK: @llvm.global_ctors = {{.*}}@esan.module_ctor
75 ; CHECK: @llvm.global_dtors = {{.*}}@esan.module_dtor
4176
4277 ; CHECK: %a = alloca %struct.A, align 4
4378 ; CHECK-NEXT: %u = alloca %union.U, align 8
97132 ; Top-level:
98133
99134 ; CHECK: define internal void @esan.module_ctor()
100 ; CHECK: call void @__esan_init(i32 1, i8* bitcast ({ i8*, i32, { i8*, i32, i64*, i8** }* }* @21 to i8*))
135 ; CHECK: call void @__esan_init(i32 1, i8* bitcast ({ i8*, i32, { i8*, i32, i32, i32*, i64*, i8** }* }* @26 to i8*))
101136 ; CHECK: define internal void @esan.module_dtor()
102 ; CHECK: call void @__esan_exit(i8* bitcast ({ i8*, i32, { i8*, i32, i64*, i8** }* }* @21 to i8*))
137 ; CHECK: call void @__esan_exit(i8* bitcast ({ i8*, i32, { i8*, i32, i32, i32*, i64*, i8** }* }* @26 to i8*))