15
15
// / We associate a few shadow bits with every byte of the application memory,
16
16
// / poison the shadow of the malloc-ed or alloca-ed memory, load the shadow,
17
17
// / bits on every memory read, propagate the shadow bits through some of the
18
- // / arithmetic instruction (including MOV), store the shadow bits on every memory
19
- // / write, report a bug on some other instructions (e.g. JMP) if the
18
+ // / arithmetic instruction (including MOV), store the shadow bits on every
19
+ // / memory write, report a bug on some other instructions (e.g. JMP) if the
20
20
// / associated shadow is poisoned.
21
21
// /
22
22
// / But there are differences too. The first and the major one:
@@ -580,7 +580,7 @@ class MemorySanitizer {
580
580
581
581
Triple TargetTriple;
582
582
LLVMContext *C;
583
- Type *IntptrTy; // /< Integer type with the size of a ptr in default AS.
583
+ Type *IntptrTy; // /< Integer type with the size of a ptr in default AS.
584
584
Type *OriginTy;
585
585
PointerType *PtrTy; // /< Integer type with the size of a ptr in default AS.
586
586
@@ -841,7 +841,8 @@ static Constant *getOrInsertGlobal(Module &M, StringRef Name, Type *Ty) {
841
841
}
842
842
843
843
// / Insert declarations for userspace-specific functions and globals.
844
- void MemorySanitizer::createUserspaceApi (Module &M, const TargetLibraryInfo &TLI) {
844
+ void MemorySanitizer::createUserspaceApi (Module &M,
845
+ const TargetLibraryInfo &TLI) {
845
846
IRBuilder<> IRB (*C);
846
847
847
848
// Create the callback.
@@ -911,7 +912,8 @@ void MemorySanitizer::createUserspaceApi(Module &M, const TargetLibraryInfo &TLI
911
912
}
912
913
913
914
// / Insert extern declaration of runtime-provided functions and globals.
914
- void MemorySanitizer::initializeCallbacks (Module &M, const TargetLibraryInfo &TLI) {
915
+ void MemorySanitizer::initializeCallbacks (Module &M,
916
+ const TargetLibraryInfo &TLI) {
915
917
// Only do this once.
916
918
if (CallbacksInitialized)
917
919
return ;
@@ -1008,12 +1010,12 @@ void MemorySanitizer::initializeModule(Module &M) {
1008
1010
break ;
1009
1011
case Triple::Linux:
1010
1012
switch (TargetTriple.getArch ()) {
1011
- case Triple::x86:
1012
- MapParams = Linux_X86_MemoryMapParams.bits32 ;
1013
- break ;
1014
1013
case Triple::x86_64:
1015
1014
MapParams = Linux_X86_MemoryMapParams.bits64 ;
1016
1015
break ;
1016
+ case Triple::x86:
1017
+ MapParams = Linux_X86_MemoryMapParams.bits32 ;
1018
+ break ;
1017
1019
case Triple::mips64:
1018
1020
case Triple::mips64el:
1019
1021
MapParams = Linux_MIPS_MemoryMapParams.bits64 ;
@@ -1246,7 +1248,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
1246
1248
Value *End =
1247
1249
IRB.CreateUDiv (RoundUp, ConstantInt::get (MS.IntptrTy , kOriginSize ));
1248
1250
auto [InsertPt, Index] =
1249
- SplitBlockAndInsertSimpleForLoop (End, &*IRB.GetInsertPoint ());
1251
+ SplitBlockAndInsertSimpleForLoop (End, &*IRB.GetInsertPoint ());
1250
1252
IRB.SetInsertPoint (InsertPt);
1251
1253
1252
1254
Value *GEP = IRB.CreateGEP (MS.OriginTy , OriginPtr, Index);
@@ -1653,7 +1655,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
1653
1655
if (isa<ScalableVectorType>(V->getType ()))
1654
1656
return convertShadowToScalar (IRB.CreateOrReduce (V), IRB);
1655
1657
unsigned BitWidth =
1656
- V->getType ()->getPrimitiveSizeInBits ().getFixedValue ();
1658
+ V->getType ()->getPrimitiveSizeInBits ().getFixedValue ();
1657
1659
return IRB.CreateBitCast (V, IntegerType::get (*MS.C , BitWidth));
1658
1660
}
1659
1661
return V;
@@ -1692,7 +1694,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
1692
1694
Constant *constToIntPtr (Type *IntPtrTy, uint64_t C) const {
1693
1695
if (VectorType *VectTy = dyn_cast<VectorType>(IntPtrTy)) {
1694
1696
return ConstantVector::getSplat (
1695
- VectTy->getElementCount (), constToIntPtr (VectTy->getElementType (), C));
1697
+ VectTy->getElementCount (),
1698
+ constToIntPtr (VectTy->getElementType (), C));
1696
1699
}
1697
1700
assert (IntPtrTy == MS.IntptrTy );
1698
1701
return ConstantInt::get (MS.IntptrTy , C);
@@ -2009,8 +2012,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
2009
2012
(void )Cpy;
2010
2013
2011
2014
if (MS.TrackOrigins ) {
2012
- Value *OriginPtr =
2013
- getOriginPtrForArgument (EntryIRB, ArgOffset);
2015
+ Value *OriginPtr = getOriginPtrForArgument (EntryIRB, ArgOffset);
2014
2016
// FIXME: OriginSize should be:
2015
2017
// alignTo(V % kMinOriginAlignment + Size, kMinOriginAlignment)
2016
2018
unsigned OriginSize = alignTo (Size, kMinOriginAlignment );
@@ -2033,8 +2035,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
2033
2035
ShadowPtr = EntryIRB.CreateAlignedLoad (getShadowTy (&FArg), Base,
2034
2036
kShadowTLSAlignment );
2035
2037
if (MS.TrackOrigins ) {
2036
- Value *OriginPtr =
2037
- getOriginPtrForArgument (EntryIRB, ArgOffset);
2038
+ Value *OriginPtr = getOriginPtrForArgument (EntryIRB, ArgOffset);
2038
2039
setOrigin (A, EntryIRB.CreateLoad (MS.OriginTy , OriginPtr));
2039
2040
}
2040
2041
}
@@ -4540,8 +4541,6 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
4540
4541
if (EagerCheck) {
4541
4542
insertShadowCheck (A, &CB);
4542
4543
Size = DL.getTypeAllocSize (A->getType ());
4543
- if (ArgOffset + Size > kParamTLSSize )
4544
- break ;
4545
4544
} else {
4546
4545
Value *Store = nullptr ;
4547
4546
// Compute the Shadow for arg even if it is ByVal, because
@@ -4654,12 +4653,11 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
4654
4653
}
4655
4654
IRBuilder<> IRBAfter (&*NextInsn);
4656
4655
Value *RetvalShadow = IRBAfter.CreateAlignedLoad (
4657
- getShadowTy (&CB), getShadowPtrForRetval (IRBAfter),
4658
- kShadowTLSAlignment , " _msret" );
4656
+ getShadowTy (&CB), getShadowPtrForRetval (IRBAfter), kShadowTLSAlignment ,
4657
+ " _msret" );
4659
4658
setShadow (&CB, RetvalShadow);
4660
4659
if (MS.TrackOrigins )
4661
- setOrigin (&CB, IRBAfter.CreateLoad (MS.OriginTy ,
4662
- getOriginPtrForRetval ()));
4660
+ setOrigin (&CB, IRBAfter.CreateLoad (MS.OriginTy , getOriginPtrForRetval ()));
4663
4661
}
4664
4662
4665
4663
bool isAMustTailRetVal (Value *RetVal) {
@@ -5596,7 +5594,7 @@ struct VarArgPowerPCHelper : public VarArgHelperBase {
5596
5594
unsigned IntptrSize = DL.getTypeStoreSize(MS.IntptrTy);
5597
5595
5598
5596
VarArgPowerPCHelper (Function &F, MemorySanitizer &MS,
5599
- MemorySanitizerVisitor &MSV, unsigned VAListTagSize)
5597
+ MemorySanitizerVisitor &MSV, unsigned VAListTagSize)
5600
5598
: VarArgHelperBase(F, MS, MSV, VAListTagSize) {}
5601
5599
5602
5600
void visitCallBase (CallBase &CB, IRBuilder<> &IRB) override {
@@ -5727,14 +5725,14 @@ struct VarArgPowerPCHelper : public VarArgHelperBase {
5727
5725
// In PPC32 va_list_tag is a struct, whereas in PPC64 it's a pointer
5728
5726
if (TargetTriple.isPPC64 ()) {
5729
5727
RegSaveAreaPtrPtr = IRB.CreateIntToPtr (
5730
- IRB.CreatePtrToInt (VAListTag, MS.IntptrTy ), MS.PtrTy );
5728
+ IRB.CreatePtrToInt (VAListTag, MS.IntptrTy ), MS.PtrTy );
5731
5729
} else {
5732
5730
RegSaveAreaPtrPtr = IRB.CreateIntToPtr (
5733
5731
IRB.CreateAdd (IRB.CreatePtrToInt (VAListTag, MS.IntptrTy ),
5734
- ConstantInt::get (MS.IntptrTy , 8 )), MS.PtrTy );
5732
+ ConstantInt::get (MS.IntptrTy , 8 )),
5733
+ MS.PtrTy );
5735
5734
}
5736
5735
5737
-
5738
5736
Value *RegSaveAreaPtr = IRB.CreateLoad (MS.PtrTy , RegSaveAreaPtrPtr);
5739
5737
Value *RegSaveAreaShadowPtr, *RegSaveAreaOriginPtr;
5740
5738
const Align Alignment = Align (IntptrSize);
@@ -6036,7 +6034,7 @@ struct VarArgI386Helper : public VarArgHelperBase {
6036
6034
unsigned IntptrSize = DL.getTypeStoreSize(MS.IntptrTy);
6037
6035
6038
6036
VarArgI386Helper (Function &F, MemorySanitizer &MS,
6039
- MemorySanitizerVisitor &MSV)
6037
+ MemorySanitizerVisitor &MSV)
6040
6038
: VarArgHelperBase(F, MS, MSV, /* VAListTagSize=*/ 4 ) {}
6041
6039
6042
6040
void visitCallBase (CallBase &CB, IRBuilder<> &IRB) override {
@@ -6071,8 +6069,8 @@ struct VarArgI386Helper : public VarArgHelperBase {
6071
6069
Align ArgAlign = Align (IntptrSize);
6072
6070
VAArgOffset = alignTo (VAArgOffset, ArgAlign);
6073
6071
if (DL.isBigEndian ()) {
6074
- // Adjusting the shadow for argument with size < IntptrSize to match the
6075
- // placement of bits in big endian system
6072
+ // Adjusting the shadow for argument with size < IntptrSize to match
6073
+ // the placement of bits in big endian system
6076
6074
if (ArgSize < IntptrSize)
6077
6075
VAArgOffset += (IntptrSize - ArgSize);
6078
6076
}
@@ -6097,9 +6095,10 @@ struct VarArgI386Helper : public VarArgHelperBase {
6097
6095
" finalizeInstrumentation called twice" );
6098
6096
6099
6097
IRBuilder<> IRB (MSV.FnPrologueEnd );
6100
- VAArgSize = IRB.CreateLoad (MS.IntptrTy , MS.VAArgOverflowSizeTLS );
6101
- Value *CopySize =
6102
- IRB.CreateAdd (ConstantInt::get (MS.IntptrTy , 0 ), VAArgSize);
6098
+ VAArgSize = IRB.CreateLoad (IRB.getInt64Ty (), MS.VAArgOverflowSizeTLS );
6099
+ Value *CopySize = IRB.CreateAdd (
6100
+ ConstantInt::get (MS.IntptrTy , 0 ),
6101
+ IRB.CreateIntCast (VAArgSize, MS.IntptrTy , /* isSigned*/ false ));
6103
6102
6104
6103
if (!VAStartInstrumentationList.empty ()) {
6105
6104
// If there is a va_start in this function, make a backup copy of
@@ -6139,38 +6138,39 @@ struct VarArgI386Helper : public VarArgHelperBase {
6139
6138
}
6140
6139
};
6141
6140
6142
- // / Implementation of VarArgHelper that is used for ARM32, MIPS, RISCV, LoongArch64.
6141
+ // / Implementation of VarArgHelper that is used for ARM32, MIPS, RISCV,
6142
+ // / LoongArch64.
6143
6143
struct VarArgGenericHelper : public VarArgHelperBase {
6144
6144
AllocaInst *VAArgTLSCopy = nullptr ;
6145
6145
Value *VAArgSize = nullptr ;
6146
6146
const DataLayout &DL = F.getDataLayout();
6147
6147
unsigned IntptrSize = DL.getTypeStoreSize(MS.IntptrTy);
6148
6148
6149
6149
VarArgGenericHelper (Function &F, MemorySanitizer &MS,
6150
- MemorySanitizerVisitor &MSV, const unsigned VAListTagSize)
6150
+ MemorySanitizerVisitor &MSV, const unsigned VAListTagSize)
6151
6151
: VarArgHelperBase(F, MS, MSV, VAListTagSize) {}
6152
6152
6153
6153
void visitCallBase (CallBase &CB, IRBuilder<> &IRB) override {
6154
6154
unsigned VAArgOffset = 0 ;
6155
- for (Value *A :
6156
- llvm::drop_begin (CB.args (), CB.getFunctionType ()->getNumParams ())) {
6157
- Triple TargetTriple (F.getParent ()->getTargetTriple ());
6158
- Value *Base;
6155
+ for (const auto &[ArgNo, A] : llvm::enumerate (CB.args ())) {
6156
+ bool IsFixed = ArgNo < CB.getFunctionType ()->getNumParams ();
6157
+ if (IsFixed) {
6158
+ continue ;
6159
+ }
6159
6160
uint64_t ArgSize = DL.getTypeAllocSize (A->getType ());
6160
6161
if (DL.isBigEndian ()) {
6161
6162
// Adjusting the shadow for argument with size < IntptrSize to match the
6162
6163
// placement of bits in big endian system
6163
6164
if (ArgSize < IntptrSize)
6164
6165
VAArgOffset += (IntptrSize - ArgSize);
6165
6166
}
6166
- Base = getShadowPtrForVAArgument (IRB, VAArgOffset, ArgSize);
6167
+ Value * Base = getShadowPtrForVAArgument (IRB, VAArgOffset, ArgSize);
6167
6168
VAArgOffset += ArgSize;
6168
6169
VAArgOffset = alignTo (VAArgOffset, IntptrSize);
6169
6170
if (!Base) {
6170
6171
continue ;
6171
6172
}
6172
- Value *Shadow = MSV.getShadow (A);
6173
- IRB.CreateAlignedStore (Shadow, Base, kShadowTLSAlignment );
6173
+ IRB.CreateAlignedStore (MSV.getShadow (A), Base, kShadowTLSAlignment );
6174
6174
}
6175
6175
6176
6176
Constant *TotalVAArgSize = ConstantInt::get (MS.IntptrTy , VAArgOffset);
@@ -6205,8 +6205,7 @@ struct VarArgGenericHelper : public VarArgHelperBase {
6205
6205
6206
6206
// Instrument va_start.
6207
6207
// Copy va_list shadow from the backup copy of the TLS contents.
6208
- for (size_t i = 0 , n = VAStartInstrumentationList.size (); i < n; i++) {
6209
- CallInst *OrigInst = VAStartInstrumentationList[i];
6208
+ for (CallInst *OrigInst : VAStartInstrumentationList) {
6210
6209
NextNodeIRBuilder IRB (OrigInst);
6211
6210
Value *VAListTag = OrigInst->getArgOperand (0 );
6212
6211
Type *RegSaveAreaPtrTy = PointerType::getUnqual (*MS.C );
@@ -6226,7 +6225,8 @@ struct VarArgGenericHelper : public VarArgHelperBase {
6226
6225
}
6227
6226
};
6228
6227
6229
- // ARM32, Loongarch64, MIPS and RISCV share the same calling conventions regarding VAArgs.
6228
+ // ARM32, Loongarch64, MIPS and RISCV share the same calling conventions
6229
+ // regarding VAArgs.
6230
6230
using VarArgARM32Helper = VarArgGenericHelper;
6231
6231
using VarArgRISCVHelper = VarArgGenericHelper;
6232
6232
using VarArgMIPSHelper = VarArgGenericHelper;
@@ -6275,7 +6275,8 @@ static VarArgHelper *CreateVarArgHelper(Function &Func, MemorySanitizer &Msan,
6275
6275
}
6276
6276
6277
6277
if (TargetTriple.isPPC32 ()) {
6278
- // On PowerPC32 VAListTag is a struct {char, char, i16 padding, char *, char *}
6278
+ // On PowerPC32 VAListTag is a struct
6279
+ // {char, char, i16 padding, char *, char *}
6279
6280
return new VarArgPowerPCHelper (Func, Msan, Visitor, /* VAListTagSize=*/ 12 );
6280
6281
}
6281
6282
@@ -6300,7 +6301,8 @@ static VarArgHelper *CreateVarArgHelper(Function &Func, MemorySanitizer &Msan,
6300
6301
}
6301
6302
6302
6303
if (TargetTriple.isLoongArch64 ()) {
6303
- return new VarArgLoongArch64Helper (Func, Msan, Visitor, /* VAListTagSize=*/ 8 );
6304
+ return new VarArgLoongArch64Helper (Func, Msan, Visitor,
6305
+ /* VAListTagSize=*/ 8 );
6304
6306
}
6305
6307
6306
6308
return new VarArgNoOpHelper (Func, Msan, Visitor);
0 commit comments