|
66 | 66 | #include "llvm/Transforms/InstCombine/InstCombine.h" |
67 | 67 | #include "llvm/Transforms/Instrumentation/AddressSanitizer.h" |
68 | 68 | #include "llvm/Transforms/Instrumentation/AddressSanitizerOptions.h" |
69 | | -#include "llvm/Transforms/Instrumentation/AllocToken.h" |
70 | 69 | #include "llvm/Transforms/Instrumentation/BoundsChecking.h" |
71 | 70 | #include "llvm/Transforms/Instrumentation/DataFlowSanitizer.h" |
72 | 71 | #include "llvm/Transforms/Instrumentation/GCOVProfiler.h" |
@@ -234,18 +233,6 @@ class EmitAssemblyHelper { |
234 | 233 | }; |
235 | 234 | } // namespace |
236 | 235 |
|
237 | | -static AllocTokenOptions getAllocTokenOptions(const LangOptions &LangOpts, |
238 | | - const CodeGenOptions &CGOpts) { |
239 | | - AllocTokenOptions Opts; |
240 | | - if (LangOpts.AllocTokenMode) |
241 | | - Opts.Mode = *LangOpts.AllocTokenMode; |
242 | | - if (LangOpts.AllocTokenMax) |
243 | | - Opts.MaxTokens = *LangOpts.AllocTokenMax; |
244 | | - Opts.Extended = CGOpts.SanitizeAllocTokenExtended; |
245 | | - Opts.FastABI = CGOpts.SanitizeAllocTokenFastABI; |
246 | | - return Opts; |
247 | | -} |
248 | | - |
249 | 236 | static SanitizerCoverageOptions |
250 | 237 | getSancovOptsFromCGOpts(const CodeGenOptions &CGOpts) { |
251 | 238 | SanitizerCoverageOptions Opts; |
@@ -874,23 +861,6 @@ static void addSanitizers(const Triple &TargetTriple, |
874 | 861 | } |
875 | 862 | } |
876 | 863 |
|
877 | | -static void addAllocTokenPass(const Triple &TargetTriple, |
878 | | - const CodeGenOptions &CodeGenOpts, |
879 | | - const LangOptions &LangOpts, PassBuilder &PB) { |
880 | | - PB.registerOptimizerLastEPCallback([&](ModulePassManager &MPM, |
881 | | - OptimizationLevel Level, |
882 | | - ThinOrFullLTOPhase) { |
883 | | - if (Level == OptimizationLevel::O0 && |
884 | | - LangOpts.Sanitize.has(SanitizerKind::AllocToken)) { |
885 | | - // The default pass builder only infers libcall function attrs when |
886 | | - // optimizing, so we insert it here because we need it for accurate |
887 | | - // memory allocation function detection with -fsanitize=alloc-token. |
888 | | - MPM.addPass(InferFunctionAttrsPass()); |
889 | | - } |
890 | | - MPM.addPass(AllocTokenPass(getAllocTokenOptions(LangOpts, CodeGenOpts))); |
891 | | - }); |
892 | | -} |
893 | | - |
894 | 864 | void EmitAssemblyHelper::RunOptimizationPipeline( |
895 | 865 | BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS, |
896 | 866 | std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS, BackendConsumer *BC) { |
@@ -1142,12 +1112,23 @@ void EmitAssemblyHelper::RunOptimizationPipeline( |
1142 | 1112 | FPM.addPass(BoundsCheckingPass(Options)); |
1143 | 1113 | }); |
1144 | 1114 |
|
1145 | | - // Don't add sanitizers if we are here from ThinLTO PostLink. That already |
1146 | | - // done on PreLink stage. |
1147 | 1115 | if (!IsThinLTOPostLink) { |
| 1116 | + // Most sanitizers only run during PreLink stage. |
1148 | 1117 | addSanitizers(TargetTriple, CodeGenOpts, LangOpts, PB); |
1149 | 1118 | addKCFIPass(TargetTriple, LangOpts, PB); |
1150 | | - addAllocTokenPass(TargetTriple, CodeGenOpts, LangOpts, PB); |
| 1119 | + |
| 1120 | + PB.registerPipelineStartEPCallback( |
| 1121 | + [&](ModulePassManager &MPM, OptimizationLevel Level) { |
| 1122 | + if (Level == OptimizationLevel::O0 && |
| 1123 | + LangOpts.Sanitize.has(SanitizerKind::AllocToken)) { |
| 1124 | + // With the default O0 pipeline, LibFunc attrs are not inferred, |
| 1125 | + // so we insert it here because we need it for accurate memory |
| 1126 | + // allocation function detection with -fsanitize=alloc-token. |
| 1127 | + // Note: This could also be added to the default O0 pipeline, but |
| 1128 | + // has a non-trivial effect on generated IR size (attributes). |
| 1129 | + MPM.addPass(InferFunctionAttrsPass()); |
| 1130 | + } |
| 1131 | + }); |
1151 | 1132 | } |
1152 | 1133 |
|
1153 | 1134 | if (std::optional<GCOVOptions> Options = |
|
0 commit comments