@@ -723,6 +723,26 @@ static void markAsIgnoreThreadCheckingAtRuntime(llvm::Function *Fn) {
723
723
Fn->removeFnAttr (llvm::Attribute::SanitizeThread);
724
724
}
725
725
726
+ static bool matchesStlAllocatorFn (const Decl *D, const ASTContext &Ctx) {
727
+ auto *MD = dyn_cast_or_null<CXXMethodDecl>(D);
728
+ if (!MD || !MD->getDeclName ().getAsIdentifierInfo () ||
729
+ !MD->getDeclName ().getAsIdentifierInfo ()->isStr (" allocate" ) ||
730
+ (MD->getNumParams () != 1 && MD->getNumParams () != 2 ))
731
+ return false ;
732
+
733
+ if (MD->parameters ()[0 ]->getType ().getCanonicalType () != Ctx.getSizeType ())
734
+ return false ;
735
+
736
+ if (MD->getNumParams () == 2 ) {
737
+ auto *PT = MD->parameters ()[1 ]->getType ()->getAs <PointerType>();
738
+ if (!PT || !PT->isVoidPointerType () ||
739
+ !PT->getPointeeType ().isConstQualified ())
740
+ return false ;
741
+ }
742
+
743
+ return true ;
744
+ }
745
+
726
746
void CodeGenFunction::StartFunction (GlobalDecl GD,
727
747
QualType RetTy,
728
748
llvm::Function *Fn,
@@ -782,6 +802,14 @@ void CodeGenFunction::StartFunction(GlobalDecl GD,
782
802
}
783
803
}
784
804
805
+ // Ignore unrelated casts in STL allocate() since the allocator must cast
806
+ // from void* to T* before object initialization completes. Don't match on the
807
+ // namespace because not all allocators are in std::
808
+ if (D && SanOpts.has (SanitizerKind::CFIUnrelatedCast)) {
809
+ if (matchesStlAllocatorFn (D, getContext ()))
810
+ SanOpts.Mask &= ~SanitizerKind::CFIUnrelatedCast;
811
+ }
812
+
785
813
// Apply xray attributes to the function (as a string, for now)
786
814
if (D && ShouldXRayInstrumentFunction ()) {
787
815
if (const auto *XRayAttr = D->getAttr <XRayInstrumentAttr>()) {
0 commit comments