1515// / We associate a few shadow bits with every byte of the application memory,
1616// / poison the shadow of the malloc-ed or alloca-ed memory, load the shadow,
1717// / 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
2020// / associated shadow is poisoned.
2121// /
2222// / But there are differences too. The first and the major one:
@@ -580,7 +580,7 @@ class MemorySanitizer {
580580
581581 Triple TargetTriple;
582582 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.
584584 Type *OriginTy;
585585 PointerType *PtrTy; // /< Integer type with the size of a ptr in default AS.
586586
@@ -841,7 +841,8 @@ static Constant *getOrInsertGlobal(Module &M, StringRef Name, Type *Ty) {
841841}
842842
843843// / 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) {
845846 IRBuilder<> IRB (*C);
846847
847848 // Create the callback.
@@ -911,7 +912,8 @@ void MemorySanitizer::createUserspaceApi(Module &M, const TargetLibraryInfo &TLI
911912}
912913
913914// / 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) {
915917 // Only do this once.
916918 if (CallbacksInitialized)
917919 return ;
@@ -1008,12 +1010,12 @@ void MemorySanitizer::initializeModule(Module &M) {
10081010 break ;
10091011 case Triple::Linux:
10101012 switch (TargetTriple.getArch ()) {
1011- case Triple::x86:
1012- MapParams = Linux_X86_MemoryMapParams.bits32 ;
1013- break ;
10141013 case Triple::x86_64:
10151014 MapParams = Linux_X86_MemoryMapParams.bits64 ;
10161015 break ;
1016+ case Triple::x86:
1017+ MapParams = Linux_X86_MemoryMapParams.bits32 ;
1018+ break ;
10171019 case Triple::mips64:
10181020 case Triple::mips64el:
10191021 MapParams = Linux_MIPS_MemoryMapParams.bits64 ;
@@ -1246,7 +1248,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
12461248 Value *End =
12471249 IRB.CreateUDiv (RoundUp, ConstantInt::get (MS.IntptrTy , kOriginSize ));
12481250 auto [InsertPt, Index] =
1249- SplitBlockAndInsertSimpleForLoop (End, &*IRB.GetInsertPoint ());
1251+ SplitBlockAndInsertSimpleForLoop (End, &*IRB.GetInsertPoint ());
12501252 IRB.SetInsertPoint (InsertPt);
12511253
12521254 Value *GEP = IRB.CreateGEP (MS.OriginTy , OriginPtr, Index);
@@ -1653,7 +1655,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
16531655 if (isa<ScalableVectorType>(V->getType ()))
16541656 return convertShadowToScalar (IRB.CreateOrReduce (V), IRB);
16551657 unsigned BitWidth =
1656- V->getType ()->getPrimitiveSizeInBits ().getFixedValue ();
1658+ V->getType ()->getPrimitiveSizeInBits ().getFixedValue ();
16571659 return IRB.CreateBitCast (V, IntegerType::get (*MS.C , BitWidth));
16581660 }
16591661 return V;
@@ -1692,7 +1694,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
16921694 Constant *constToIntPtr (Type *IntPtrTy, uint64_t C) const {
16931695 if (VectorType *VectTy = dyn_cast<VectorType>(IntPtrTy)) {
16941696 return ConstantVector::getSplat (
1695- VectTy->getElementCount (), constToIntPtr (VectTy->getElementType (), C));
1697+ VectTy->getElementCount (),
1698+ constToIntPtr (VectTy->getElementType (), C));
16961699 }
16971700 assert (IntPtrTy == MS.IntptrTy );
16981701 return ConstantInt::get (MS.IntptrTy , C);
@@ -2009,8 +2012,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
20092012 (void )Cpy;
20102013
20112014 if (MS.TrackOrigins ) {
2012- Value *OriginPtr =
2013- getOriginPtrForArgument (EntryIRB, ArgOffset);
2015+ Value *OriginPtr = getOriginPtrForArgument (EntryIRB, ArgOffset);
20142016 // FIXME: OriginSize should be:
20152017 // alignTo(V % kMinOriginAlignment + Size, kMinOriginAlignment)
20162018 unsigned OriginSize = alignTo (Size, kMinOriginAlignment );
@@ -2033,8 +2035,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
20332035 ShadowPtr = EntryIRB.CreateAlignedLoad (getShadowTy (&FArg), Base,
20342036 kShadowTLSAlignment );
20352037 if (MS.TrackOrigins ) {
2036- Value *OriginPtr =
2037- getOriginPtrForArgument (EntryIRB, ArgOffset);
2038+ Value *OriginPtr = getOriginPtrForArgument (EntryIRB, ArgOffset);
20382039 setOrigin (A, EntryIRB.CreateLoad (MS.OriginTy , OriginPtr));
20392040 }
20402041 }
@@ -4540,8 +4541,6 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
45404541 if (EagerCheck) {
45414542 insertShadowCheck (A, &CB);
45424543 Size = DL.getTypeAllocSize (A->getType ());
4543- if (ArgOffset + Size > kParamTLSSize )
4544- break ;
45454544 } else {
45464545 Value *Store = nullptr ;
45474546 // Compute the Shadow for arg even if it is ByVal, because
@@ -4654,12 +4653,11 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
46544653 }
46554654 IRBuilder<> IRBAfter (&*NextInsn);
46564655 Value *RetvalShadow = IRBAfter.CreateAlignedLoad (
4657- getShadowTy (&CB), getShadowPtrForRetval (IRBAfter),
4658- kShadowTLSAlignment , " _msret" );
4656+ getShadowTy (&CB), getShadowPtrForRetval (IRBAfter), kShadowTLSAlignment ,
4657+ " _msret" );
46594658 setShadow (&CB, RetvalShadow);
46604659 if (MS.TrackOrigins )
4661- setOrigin (&CB, IRBAfter.CreateLoad (MS.OriginTy ,
4662- getOriginPtrForRetval ()));
4660+ setOrigin (&CB, IRBAfter.CreateLoad (MS.OriginTy , getOriginPtrForRetval ()));
46634661 }
46644662
46654663 bool isAMustTailRetVal (Value *RetVal) {
@@ -5596,7 +5594,7 @@ struct VarArgPowerPCHelper : public VarArgHelperBase {
55965594 unsigned IntptrSize = DL.getTypeStoreSize(MS.IntptrTy);
55975595
55985596 VarArgPowerPCHelper (Function &F, MemorySanitizer &MS,
5599- MemorySanitizerVisitor &MSV, unsigned VAListTagSize)
5597+ MemorySanitizerVisitor &MSV, unsigned VAListTagSize)
56005598 : VarArgHelperBase(F, MS, MSV, VAListTagSize) {}
56015599
56025600 void visitCallBase (CallBase &CB, IRBuilder<> &IRB) override {
@@ -5727,14 +5725,14 @@ struct VarArgPowerPCHelper : public VarArgHelperBase {
57275725 // In PPC32 va_list_tag is a struct, whereas in PPC64 it's a pointer
57285726 if (TargetTriple.isPPC64 ()) {
57295727 RegSaveAreaPtrPtr = IRB.CreateIntToPtr (
5730- IRB.CreatePtrToInt (VAListTag, MS.IntptrTy ), MS.PtrTy );
5728+ IRB.CreatePtrToInt (VAListTag, MS.IntptrTy ), MS.PtrTy );
57315729 } else {
57325730 RegSaveAreaPtrPtr = IRB.CreateIntToPtr (
57335731 IRB.CreateAdd (IRB.CreatePtrToInt (VAListTag, MS.IntptrTy ),
5734- ConstantInt::get (MS.IntptrTy , 8 )), MS.PtrTy );
5732+ ConstantInt::get (MS.IntptrTy , 8 )),
5733+ MS.PtrTy );
57355734 }
57365735
5737-
57385736 Value *RegSaveAreaPtr = IRB.CreateLoad (MS.PtrTy , RegSaveAreaPtrPtr);
57395737 Value *RegSaveAreaShadowPtr, *RegSaveAreaOriginPtr;
57405738 const Align Alignment = Align (IntptrSize);
@@ -6036,7 +6034,7 @@ struct VarArgI386Helper : public VarArgHelperBase {
60366034 unsigned IntptrSize = DL.getTypeStoreSize(MS.IntptrTy);
60376035
60386036 VarArgI386Helper (Function &F, MemorySanitizer &MS,
6039- MemorySanitizerVisitor &MSV)
6037+ MemorySanitizerVisitor &MSV)
60406038 : VarArgHelperBase(F, MS, MSV, /* VAListTagSize=*/ 4 ) {}
60416039
60426040 void visitCallBase (CallBase &CB, IRBuilder<> &IRB) override {
@@ -6071,8 +6069,8 @@ struct VarArgI386Helper : public VarArgHelperBase {
60716069 Align ArgAlign = Align (IntptrSize);
60726070 VAArgOffset = alignTo (VAArgOffset, ArgAlign);
60736071 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
60766074 if (ArgSize < IntptrSize)
60776075 VAArgOffset += (IntptrSize - ArgSize);
60786076 }
@@ -6097,9 +6095,10 @@ struct VarArgI386Helper : public VarArgHelperBase {
60976095 " finalizeInstrumentation called twice" );
60986096
60996097 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 ));
61036102
61046103 if (!VAStartInstrumentationList.empty ()) {
61056104 // If there is a va_start in this function, make a backup copy of
@@ -6139,38 +6138,39 @@ struct VarArgI386Helper : public VarArgHelperBase {
61396138 }
61406139};
61416140
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.
61436143struct VarArgGenericHelper : public VarArgHelperBase {
61446144 AllocaInst *VAArgTLSCopy = nullptr ;
61456145 Value *VAArgSize = nullptr ;
61466146 const DataLayout &DL = F.getDataLayout();
61476147 unsigned IntptrSize = DL.getTypeStoreSize(MS.IntptrTy);
61486148
61496149 VarArgGenericHelper (Function &F, MemorySanitizer &MS,
6150- MemorySanitizerVisitor &MSV, const unsigned VAListTagSize)
6150+ MemorySanitizerVisitor &MSV, const unsigned VAListTagSize)
61516151 : VarArgHelperBase(F, MS, MSV, VAListTagSize) {}
61526152
61536153 void visitCallBase (CallBase &CB, IRBuilder<> &IRB) override {
61546154 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+ }
61596160 uint64_t ArgSize = DL.getTypeAllocSize (A->getType ());
61606161 if (DL.isBigEndian ()) {
61616162 // Adjusting the shadow for argument with size < IntptrSize to match the
61626163 // placement of bits in big endian system
61636164 if (ArgSize < IntptrSize)
61646165 VAArgOffset += (IntptrSize - ArgSize);
61656166 }
6166- Base = getShadowPtrForVAArgument (IRB, VAArgOffset, ArgSize);
6167+ Value * Base = getShadowPtrForVAArgument (IRB, VAArgOffset, ArgSize);
61676168 VAArgOffset += ArgSize;
61686169 VAArgOffset = alignTo (VAArgOffset, IntptrSize);
61696170 if (!Base) {
61706171 continue ;
61716172 }
6172- Value *Shadow = MSV.getShadow (A);
6173- IRB.CreateAlignedStore (Shadow, Base, kShadowTLSAlignment );
6173+ IRB.CreateAlignedStore (MSV.getShadow (A), Base, kShadowTLSAlignment );
61746174 }
61756175
61766176 Constant *TotalVAArgSize = ConstantInt::get (MS.IntptrTy , VAArgOffset);
@@ -6205,8 +6205,7 @@ struct VarArgGenericHelper : public VarArgHelperBase {
62056205
62066206 // Instrument va_start.
62076207 // 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) {
62106209 NextNodeIRBuilder IRB (OrigInst);
62116210 Value *VAListTag = OrigInst->getArgOperand (0 );
62126211 Type *RegSaveAreaPtrTy = PointerType::getUnqual (*MS.C );
@@ -6226,7 +6225,8 @@ struct VarArgGenericHelper : public VarArgHelperBase {
62266225 }
62276226};
62286227
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.
62306230using VarArgARM32Helper = VarArgGenericHelper;
62316231using VarArgRISCVHelper = VarArgGenericHelper;
62326232using VarArgMIPSHelper = VarArgGenericHelper;
@@ -6275,7 +6275,8 @@ static VarArgHelper *CreateVarArgHelper(Function &Func, MemorySanitizer &Msan,
62756275 }
62766276
62776277 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 *}
62796280 return new VarArgPowerPCHelper (Func, Msan, Visitor, /* VAListTagSize=*/ 12 );
62806281 }
62816282
@@ -6300,7 +6301,8 @@ static VarArgHelper *CreateVarArgHelper(Function &Func, MemorySanitizer &Msan,
63006301 }
63016302
63026303 if (TargetTriple.isLoongArch64 ()) {
6303- return new VarArgLoongArch64Helper (Func, Msan, Visitor, /* VAListTagSize=*/ 8 );
6304+ return new VarArgLoongArch64Helper (Func, Msan, Visitor,
6305+ /* VAListTagSize=*/ 8 );
63046306 }
63056307
63066308 return new VarArgNoOpHelper (Func, Msan, Visitor);
0 commit comments