HWAddressSanitizer.cpp 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  1. //===- HWAddressSanitizer.cpp - detector of uninitialized reads -------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. /// \file
  10. /// This file is a part of HWAddressSanitizer, an address basic correctness
  11. /// checker based on tagged addressing.
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h"
  14. #include "llvm/ADT/MapVector.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/ADT/StringExtras.h"
  18. #include "llvm/ADT/StringRef.h"
  19. #include "llvm/ADT/Triple.h"
  20. #include "llvm/Analysis/GlobalsModRef.h"
  21. #include "llvm/Analysis/PostDominators.h"
  22. #include "llvm/Analysis/StackSafetyAnalysis.h"
  23. #include "llvm/Analysis/ValueTracking.h"
  24. #include "llvm/BinaryFormat/Dwarf.h"
  25. #include "llvm/BinaryFormat/ELF.h"
  26. #include "llvm/IR/Attributes.h"
  27. #include "llvm/IR/BasicBlock.h"
  28. #include "llvm/IR/Constant.h"
  29. #include "llvm/IR/Constants.h"
  30. #include "llvm/IR/DataLayout.h"
  31. #include "llvm/IR/DebugInfoMetadata.h"
  32. #include "llvm/IR/DerivedTypes.h"
  33. #include "llvm/IR/Dominators.h"
  34. #include "llvm/IR/Function.h"
  35. #include "llvm/IR/IRBuilder.h"
  36. #include "llvm/IR/InlineAsm.h"
  37. #include "llvm/IR/InstIterator.h"
  38. #include "llvm/IR/Instruction.h"
  39. #include "llvm/IR/Instructions.h"
  40. #include "llvm/IR/IntrinsicInst.h"
  41. #include "llvm/IR/Intrinsics.h"
  42. #include "llvm/IR/LLVMContext.h"
  43. #include "llvm/IR/MDBuilder.h"
  44. #include "llvm/IR/Module.h"
  45. #include "llvm/IR/NoFolder.h"
  46. #include "llvm/IR/Type.h"
  47. #include "llvm/IR/Value.h"
  48. #include "llvm/Support/Casting.h"
  49. #include "llvm/Support/CommandLine.h"
  50. #include "llvm/Support/Debug.h"
  51. #include "llvm/Support/raw_ostream.h"
  52. #include "llvm/Transforms/Instrumentation/AddressSanitizerCommon.h"
  53. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  54. #include "llvm/Transforms/Utils/MemoryTaggingSupport.h"
  55. #include "llvm/Transforms/Utils/ModuleUtils.h"
  56. #include "llvm/Transforms/Utils/PromoteMemToReg.h"
  57. #include <optional>
  58. using namespace llvm;
  59. #define DEBUG_TYPE "hwasan"
  60. const char kHwasanModuleCtorName[] = "hwasan.module_ctor";
  61. const char kHwasanNoteName[] = "hwasan.note";
  62. const char kHwasanInitName[] = "__hwasan_init";
  63. const char kHwasanPersonalityThunkName[] = "__hwasan_personality_thunk";
  64. const char kHwasanShadowMemoryDynamicAddress[] =
  65. "__hwasan_shadow_memory_dynamic_address";
  66. // Accesses sizes are powers of two: 1, 2, 4, 8, 16.
  67. static const size_t kNumberOfAccessSizes = 5;
  68. static const size_t kDefaultShadowScale = 4;
  69. static const uint64_t kDynamicShadowSentinel =
  70. std::numeric_limits<uint64_t>::max();
  71. static const unsigned kShadowBaseAlignment = 32;
  72. static cl::opt<std::string>
  73. ClMemoryAccessCallbackPrefix("hwasan-memory-access-callback-prefix",
  74. cl::desc("Prefix for memory access callbacks"),
  75. cl::Hidden, cl::init("__hwasan_"));
  76. static cl::opt<bool> ClKasanMemIntrinCallbackPrefix(
  77. "hwasan-kernel-mem-intrinsic-prefix",
  78. cl::desc("Use prefix for memory intrinsics in KASAN mode"), cl::Hidden,
  79. cl::init(false));
  80. static cl::opt<bool> ClInstrumentWithCalls(
  81. "hwasan-instrument-with-calls",
  82. cl::desc("instrument reads and writes with callbacks"), cl::Hidden,
  83. cl::init(false));
  84. static cl::opt<bool> ClInstrumentReads("hwasan-instrument-reads",
  85. cl::desc("instrument read instructions"),
  86. cl::Hidden, cl::init(true));
  87. static cl::opt<bool>
  88. ClInstrumentWrites("hwasan-instrument-writes",
  89. cl::desc("instrument write instructions"), cl::Hidden,
  90. cl::init(true));
  91. static cl::opt<bool> ClInstrumentAtomics(
  92. "hwasan-instrument-atomics",
  93. cl::desc("instrument atomic instructions (rmw, cmpxchg)"), cl::Hidden,
  94. cl::init(true));
  95. static cl::opt<bool> ClInstrumentByval("hwasan-instrument-byval",
  96. cl::desc("instrument byval arguments"),
  97. cl::Hidden, cl::init(true));
  98. static cl::opt<bool>
  99. ClRecover("hwasan-recover",
  100. cl::desc("Enable recovery mode (continue-after-error)."),
  101. cl::Hidden, cl::init(false));
  102. static cl::opt<bool> ClInstrumentStack("hwasan-instrument-stack",
  103. cl::desc("instrument stack (allocas)"),
  104. cl::Hidden, cl::init(true));
  105. static cl::opt<bool>
  106. ClUseStackSafety("hwasan-use-stack-safety", cl::Hidden, cl::init(true),
  107. cl::Hidden, cl::desc("Use Stack Safety analysis results"),
  108. cl::Optional);
  109. static cl::opt<size_t> ClMaxLifetimes(
  110. "hwasan-max-lifetimes-for-alloca", cl::Hidden, cl::init(3),
  111. cl::ReallyHidden,
  112. cl::desc("How many lifetime ends to handle for a single alloca."),
  113. cl::Optional);
  114. static cl::opt<bool>
  115. ClUseAfterScope("hwasan-use-after-scope",
  116. cl::desc("detect use after scope within function"),
  117. cl::Hidden, cl::init(false));
  118. static cl::opt<bool> ClUARRetagToZero(
  119. "hwasan-uar-retag-to-zero",
  120. cl::desc("Clear alloca tags before returning from the function to allow "
  121. "non-instrumented and instrumented function calls mix. When set "
  122. "to false, allocas are retagged before returning from the "
  123. "function to detect use after return."),
  124. cl::Hidden, cl::init(true));
  125. static cl::opt<bool> ClGenerateTagsWithCalls(
  126. "hwasan-generate-tags-with-calls",
  127. cl::desc("generate new tags with runtime library calls"), cl::Hidden,
  128. cl::init(false));
  129. static cl::opt<bool> ClGlobals("hwasan-globals", cl::desc("Instrument globals"),
  130. cl::Hidden, cl::init(false));
  131. static cl::opt<int> ClMatchAllTag(
  132. "hwasan-match-all-tag",
  133. cl::desc("don't report bad accesses via pointers with this tag"),
  134. cl::Hidden, cl::init(-1));
  135. static cl::opt<bool>
  136. ClEnableKhwasan("hwasan-kernel",
  137. cl::desc("Enable KernelHWAddressSanitizer instrumentation"),
  138. cl::Hidden, cl::init(false));
  139. // These flags allow to change the shadow mapping and control how shadow memory
  140. // is accessed. The shadow mapping looks like:
  141. // Shadow = (Mem >> scale) + offset
  142. static cl::opt<uint64_t>
  143. ClMappingOffset("hwasan-mapping-offset",
  144. cl::desc("HWASan shadow mapping offset [EXPERIMENTAL]"),
  145. cl::Hidden, cl::init(0));
  146. static cl::opt<bool>
  147. ClWithIfunc("hwasan-with-ifunc",
  148. cl::desc("Access dynamic shadow through an ifunc global on "
  149. "platforms that support this"),
  150. cl::Hidden, cl::init(false));
  151. static cl::opt<bool> ClWithTls(
  152. "hwasan-with-tls",
  153. cl::desc("Access dynamic shadow through an thread-local pointer on "
  154. "platforms that support this"),
  155. cl::Hidden, cl::init(true));
  156. // Mode for selecting how to insert frame record info into the stack ring
  157. // buffer.
  158. enum RecordStackHistoryMode {
  159. // Do not record frame record info.
  160. none,
  161. // Insert instructions into the prologue for storing into the stack ring
  162. // buffer directly.
  163. instr,
  164. // Add a call to __hwasan_add_frame_record in the runtime.
  165. libcall,
  166. };
  167. static cl::opt<RecordStackHistoryMode> ClRecordStackHistory(
  168. "hwasan-record-stack-history",
  169. cl::desc("Record stack frames with tagged allocations in a thread-local "
  170. "ring buffer"),
  171. cl::values(clEnumVal(none, "Do not record stack ring history"),
  172. clEnumVal(instr, "Insert instructions into the prologue for "
  173. "storing into the stack ring buffer directly"),
  174. clEnumVal(libcall, "Add a call to __hwasan_add_frame_record for "
  175. "storing into the stack ring buffer")),
  176. cl::Hidden, cl::init(instr));
  177. static cl::opt<bool>
  178. ClInstrumentMemIntrinsics("hwasan-instrument-mem-intrinsics",
  179. cl::desc("instrument memory intrinsics"),
  180. cl::Hidden, cl::init(true));
  181. static cl::opt<bool>
  182. ClInstrumentLandingPads("hwasan-instrument-landing-pads",
  183. cl::desc("instrument landing pads"), cl::Hidden,
  184. cl::init(false));
  185. static cl::opt<bool> ClUseShortGranules(
  186. "hwasan-use-short-granules",
  187. cl::desc("use short granules in allocas and outlined checks"), cl::Hidden,
  188. cl::init(false));
  189. static cl::opt<bool> ClInstrumentPersonalityFunctions(
  190. "hwasan-instrument-personality-functions",
  191. cl::desc("instrument personality functions"), cl::Hidden);
  192. static cl::opt<bool> ClInlineAllChecks("hwasan-inline-all-checks",
  193. cl::desc("inline all checks"),
  194. cl::Hidden, cl::init(false));
  195. // Enabled from clang by "-fsanitize-hwaddress-experimental-aliasing".
  196. static cl::opt<bool> ClUsePageAliases("hwasan-experimental-use-page-aliases",
  197. cl::desc("Use page aliasing in HWASan"),
  198. cl::Hidden, cl::init(false));
  199. namespace {
  200. bool shouldUsePageAliases(const Triple &TargetTriple) {
  201. return ClUsePageAliases && TargetTriple.getArch() == Triple::x86_64;
  202. }
  203. bool shouldInstrumentStack(const Triple &TargetTriple) {
  204. return !shouldUsePageAliases(TargetTriple) && ClInstrumentStack;
  205. }
  206. bool shouldInstrumentWithCalls(const Triple &TargetTriple) {
  207. return ClInstrumentWithCalls || TargetTriple.getArch() == Triple::x86_64;
  208. }
  209. bool mightUseStackSafetyAnalysis(bool DisableOptimization) {
  210. return ClUseStackSafety.getNumOccurrences() ? ClUseStackSafety
  211. : !DisableOptimization;
  212. }
  213. bool shouldUseStackSafetyAnalysis(const Triple &TargetTriple,
  214. bool DisableOptimization) {
  215. return shouldInstrumentStack(TargetTriple) &&
  216. mightUseStackSafetyAnalysis(DisableOptimization);
  217. }
  218. bool shouldDetectUseAfterScope(const Triple &TargetTriple) {
  219. return ClUseAfterScope && shouldInstrumentStack(TargetTriple);
  220. }
  221. /// An instrumentation pass implementing detection of addressability bugs
  222. /// using tagged pointers.
  223. class HWAddressSanitizer {
  224. public:
  225. HWAddressSanitizer(Module &M, bool CompileKernel, bool Recover,
  226. const StackSafetyGlobalInfo *SSI)
  227. : M(M), SSI(SSI) {
  228. this->Recover = ClRecover.getNumOccurrences() > 0 ? ClRecover : Recover;
  229. this->CompileKernel = ClEnableKhwasan.getNumOccurrences() > 0
  230. ? ClEnableKhwasan
  231. : CompileKernel;
  232. initializeModule();
  233. }
  234. void setSSI(const StackSafetyGlobalInfo *S) { SSI = S; }
  235. bool sanitizeFunction(Function &F, FunctionAnalysisManager &FAM);
  236. void initializeModule();
  237. void createHwasanCtorComdat();
  238. void initializeCallbacks(Module &M);
  239. Value *getOpaqueNoopCast(IRBuilder<> &IRB, Value *Val);
  240. Value *getDynamicShadowIfunc(IRBuilder<> &IRB);
  241. Value *getShadowNonTls(IRBuilder<> &IRB);
  242. void untagPointerOperand(Instruction *I, Value *Addr);
  243. Value *memToShadow(Value *Shadow, IRBuilder<> &IRB);
  244. int64_t getAccessInfo(bool IsWrite, unsigned AccessSizeIndex);
  245. void instrumentMemAccessOutline(Value *Ptr, bool IsWrite,
  246. unsigned AccessSizeIndex,
  247. Instruction *InsertBefore);
  248. void instrumentMemAccessInline(Value *Ptr, bool IsWrite,
  249. unsigned AccessSizeIndex,
  250. Instruction *InsertBefore);
  251. bool ignoreMemIntrinsic(MemIntrinsic *MI);
  252. void instrumentMemIntrinsic(MemIntrinsic *MI);
  253. bool instrumentMemAccess(InterestingMemoryOperand &O);
  254. bool ignoreAccess(Instruction *Inst, Value *Ptr);
  255. void getInterestingMemoryOperands(
  256. Instruction *I, SmallVectorImpl<InterestingMemoryOperand> &Interesting);
  257. void tagAlloca(IRBuilder<> &IRB, AllocaInst *AI, Value *Tag, size_t Size);
  258. Value *tagPointer(IRBuilder<> &IRB, Type *Ty, Value *PtrLong, Value *Tag);
  259. Value *untagPointer(IRBuilder<> &IRB, Value *PtrLong);
  260. bool instrumentStack(memtag::StackInfo &Info, Value *StackTag,
  261. const DominatorTree &DT, const PostDominatorTree &PDT,
  262. const LoopInfo &LI);
  263. Value *readRegister(IRBuilder<> &IRB, StringRef Name);
  264. bool instrumentLandingPads(SmallVectorImpl<Instruction *> &RetVec);
  265. Value *getNextTagWithCall(IRBuilder<> &IRB);
  266. Value *getStackBaseTag(IRBuilder<> &IRB);
  267. Value *getAllocaTag(IRBuilder<> &IRB, Value *StackTag, AllocaInst *AI,
  268. unsigned AllocaNo);
  269. Value *getUARTag(IRBuilder<> &IRB, Value *StackTag);
  270. Value *getHwasanThreadSlotPtr(IRBuilder<> &IRB, Type *Ty);
  271. Value *applyTagMask(IRBuilder<> &IRB, Value *OldTag);
  272. unsigned retagMask(unsigned AllocaNo);
  273. void emitPrologue(IRBuilder<> &IRB, bool WithFrameRecord);
  274. void instrumentGlobal(GlobalVariable *GV, uint8_t Tag);
  275. void instrumentGlobals();
  276. Value *getPC(IRBuilder<> &IRB);
  277. Value *getSP(IRBuilder<> &IRB);
  278. Value *getFrameRecordInfo(IRBuilder<> &IRB);
  279. void instrumentPersonalityFunctions();
  280. private:
  281. LLVMContext *C;
  282. Module &M;
  283. const StackSafetyGlobalInfo *SSI;
  284. Triple TargetTriple;
  285. FunctionCallee HWAsanMemmove, HWAsanMemcpy, HWAsanMemset;
  286. FunctionCallee HWAsanHandleVfork;
  287. /// This struct defines the shadow mapping using the rule:
  288. /// shadow = (mem >> Scale) + Offset.
  289. /// If InGlobal is true, then
  290. /// extern char __hwasan_shadow[];
  291. /// shadow = (mem >> Scale) + &__hwasan_shadow
  292. /// If InTls is true, then
  293. /// extern char *__hwasan_tls;
  294. /// shadow = (mem>>Scale) + align_up(__hwasan_shadow, kShadowBaseAlignment)
  295. ///
  296. /// If WithFrameRecord is true, then __hwasan_tls will be used to access the
  297. /// ring buffer for storing stack allocations on targets that support it.
  298. struct ShadowMapping {
  299. uint8_t Scale;
  300. uint64_t Offset;
  301. bool InGlobal;
  302. bool InTls;
  303. bool WithFrameRecord;
  304. void init(Triple &TargetTriple, bool InstrumentWithCalls);
  305. Align getObjectAlignment() const { return Align(1ULL << Scale); }
  306. };
  307. ShadowMapping Mapping;
  308. Type *VoidTy = Type::getVoidTy(M.getContext());
  309. Type *IntptrTy;
  310. Type *Int8PtrTy;
  311. Type *Int8Ty;
  312. Type *Int32Ty;
  313. Type *Int64Ty = Type::getInt64Ty(M.getContext());
  314. bool CompileKernel;
  315. bool Recover;
  316. bool OutlinedChecks;
  317. bool UseShortGranules;
  318. bool InstrumentLandingPads;
  319. bool InstrumentWithCalls;
  320. bool InstrumentStack;
  321. bool DetectUseAfterScope;
  322. bool UsePageAliases;
  323. std::optional<uint8_t> MatchAllTag;
  324. unsigned PointerTagShift;
  325. uint64_t TagMaskByte;
  326. Function *HwasanCtorFunction;
  327. FunctionCallee HwasanMemoryAccessCallback[2][kNumberOfAccessSizes];
  328. FunctionCallee HwasanMemoryAccessCallbackSized[2];
  329. FunctionCallee HwasanTagMemoryFunc;
  330. FunctionCallee HwasanGenerateTagFunc;
  331. FunctionCallee HwasanRecordFrameRecordFunc;
  332. Constant *ShadowGlobal;
  333. Value *ShadowBase = nullptr;
  334. Value *StackBaseTag = nullptr;
  335. Value *CachedSP = nullptr;
  336. GlobalValue *ThreadPtrGlobal = nullptr;
  337. };
  338. } // end anonymous namespace
  339. PreservedAnalyses HWAddressSanitizerPass::run(Module &M,
  340. ModuleAnalysisManager &MAM) {
  341. const StackSafetyGlobalInfo *SSI = nullptr;
  342. auto TargetTriple = llvm::Triple(M.getTargetTriple());
  343. if (shouldUseStackSafetyAnalysis(TargetTriple, Options.DisableOptimization))
  344. SSI = &MAM.getResult<StackSafetyGlobalAnalysis>(M);
  345. HWAddressSanitizer HWASan(M, Options.CompileKernel, Options.Recover, SSI);
  346. bool Modified = false;
  347. auto &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
  348. for (Function &F : M)
  349. Modified |= HWASan.sanitizeFunction(F, FAM);
  350. if (!Modified)
  351. return PreservedAnalyses::all();
  352. PreservedAnalyses PA = PreservedAnalyses::none();
  353. // GlobalsAA is considered stateless and does not get invalidated unless
  354. // explicitly invalidated; PreservedAnalyses::none() is not enough. Sanitizers
  355. // make changes that require GlobalsAA to be invalidated.
  356. PA.abandon<GlobalsAA>();
  357. return PA;
  358. }
  359. void HWAddressSanitizerPass::printPipeline(
  360. raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
  361. static_cast<PassInfoMixin<HWAddressSanitizerPass> *>(this)->printPipeline(
  362. OS, MapClassName2PassName);
  363. OS << "<";
  364. if (Options.CompileKernel)
  365. OS << "kernel;";
  366. if (Options.Recover)
  367. OS << "recover";
  368. OS << ">";
  369. }
  370. void HWAddressSanitizer::createHwasanCtorComdat() {
  371. std::tie(HwasanCtorFunction, std::ignore) =
  372. getOrCreateSanitizerCtorAndInitFunctions(
  373. M, kHwasanModuleCtorName, kHwasanInitName,
  374. /*InitArgTypes=*/{},
  375. /*InitArgs=*/{},
  376. // This callback is invoked when the functions are created the first
  377. // time. Hook them into the global ctors list in that case:
  378. [&](Function *Ctor, FunctionCallee) {
  379. Comdat *CtorComdat = M.getOrInsertComdat(kHwasanModuleCtorName);
  380. Ctor->setComdat(CtorComdat);
  381. appendToGlobalCtors(M, Ctor, 0, Ctor);
  382. });
  383. // Create a note that contains pointers to the list of global
  384. // descriptors. Adding a note to the output file will cause the linker to
  385. // create a PT_NOTE program header pointing to the note that we can use to
  386. // find the descriptor list starting from the program headers. A function
  387. // provided by the runtime initializes the shadow memory for the globals by
  388. // accessing the descriptor list via the note. The dynamic loader needs to
  389. // call this function whenever a library is loaded.
  390. //
  391. // The reason why we use a note for this instead of a more conventional
  392. // approach of having a global constructor pass a descriptor list pointer to
  393. // the runtime is because of an order of initialization problem. With
  394. // constructors we can encounter the following problematic scenario:
  395. //
  396. // 1) library A depends on library B and also interposes one of B's symbols
  397. // 2) B's constructors are called before A's (as required for correctness)
  398. // 3) during construction, B accesses one of its "own" globals (actually
  399. // interposed by A) and triggers a HWASAN failure due to the initialization
  400. // for A not having happened yet
  401. //
  402. // Even without interposition it is possible to run into similar situations in
  403. // cases where two libraries mutually depend on each other.
  404. //
  405. // We only need one note per binary, so put everything for the note in a
  406. // comdat. This needs to be a comdat with an .init_array section to prevent
  407. // newer versions of lld from discarding the note.
  408. //
  409. // Create the note even if we aren't instrumenting globals. This ensures that
  410. // binaries linked from object files with both instrumented and
  411. // non-instrumented globals will end up with a note, even if a comdat from an
  412. // object file with non-instrumented globals is selected. The note is harmless
  413. // if the runtime doesn't support it, since it will just be ignored.
  414. Comdat *NoteComdat = M.getOrInsertComdat(kHwasanModuleCtorName);
  415. Type *Int8Arr0Ty = ArrayType::get(Int8Ty, 0);
  416. auto *Start =
  417. new GlobalVariable(M, Int8Arr0Ty, true, GlobalVariable::ExternalLinkage,
  418. nullptr, "__start_hwasan_globals");
  419. Start->setVisibility(GlobalValue::HiddenVisibility);
  420. auto *Stop =
  421. new GlobalVariable(M, Int8Arr0Ty, true, GlobalVariable::ExternalLinkage,
  422. nullptr, "__stop_hwasan_globals");
  423. Stop->setVisibility(GlobalValue::HiddenVisibility);
  424. // Null-terminated so actually 8 bytes, which are required in order to align
  425. // the note properly.
  426. auto *Name = ConstantDataArray::get(*C, "LLVM\0\0\0");
  427. auto *NoteTy = StructType::get(Int32Ty, Int32Ty, Int32Ty, Name->getType(),
  428. Int32Ty, Int32Ty);
  429. auto *Note =
  430. new GlobalVariable(M, NoteTy, /*isConstant=*/true,
  431. GlobalValue::PrivateLinkage, nullptr, kHwasanNoteName);
  432. Note->setSection(".note.hwasan.globals");
  433. Note->setComdat(NoteComdat);
  434. Note->setAlignment(Align(4));
  435. // The pointers in the note need to be relative so that the note ends up being
  436. // placed in rodata, which is the standard location for notes.
  437. auto CreateRelPtr = [&](Constant *Ptr) {
  438. return ConstantExpr::getTrunc(
  439. ConstantExpr::getSub(ConstantExpr::getPtrToInt(Ptr, Int64Ty),
  440. ConstantExpr::getPtrToInt(Note, Int64Ty)),
  441. Int32Ty);
  442. };
  443. Note->setInitializer(ConstantStruct::getAnon(
  444. {ConstantInt::get(Int32Ty, 8), // n_namesz
  445. ConstantInt::get(Int32Ty, 8), // n_descsz
  446. ConstantInt::get(Int32Ty, ELF::NT_LLVM_HWASAN_GLOBALS), // n_type
  447. Name, CreateRelPtr(Start), CreateRelPtr(Stop)}));
  448. appendToCompilerUsed(M, Note);
  449. // Create a zero-length global in hwasan_globals so that the linker will
  450. // always create start and stop symbols.
  451. auto *Dummy = new GlobalVariable(
  452. M, Int8Arr0Ty, /*isConstantGlobal*/ true, GlobalVariable::PrivateLinkage,
  453. Constant::getNullValue(Int8Arr0Ty), "hwasan.dummy.global");
  454. Dummy->setSection("hwasan_globals");
  455. Dummy->setComdat(NoteComdat);
  456. Dummy->setMetadata(LLVMContext::MD_associated,
  457. MDNode::get(*C, ValueAsMetadata::get(Note)));
  458. appendToCompilerUsed(M, Dummy);
  459. }
  460. /// Module-level initialization.
  461. ///
  462. /// inserts a call to __hwasan_init to the module's constructor list.
  463. void HWAddressSanitizer::initializeModule() {
  464. LLVM_DEBUG(dbgs() << "Init " << M.getName() << "\n");
  465. auto &DL = M.getDataLayout();
  466. TargetTriple = Triple(M.getTargetTriple());
  467. // x86_64 currently has two modes:
  468. // - Intel LAM (default)
  469. // - pointer aliasing (heap only)
  470. bool IsX86_64 = TargetTriple.getArch() == Triple::x86_64;
  471. UsePageAliases = shouldUsePageAliases(TargetTriple);
  472. InstrumentWithCalls = shouldInstrumentWithCalls(TargetTriple);
  473. InstrumentStack = shouldInstrumentStack(TargetTriple);
  474. DetectUseAfterScope = shouldDetectUseAfterScope(TargetTriple);
  475. PointerTagShift = IsX86_64 ? 57 : 56;
  476. TagMaskByte = IsX86_64 ? 0x3F : 0xFF;
  477. Mapping.init(TargetTriple, InstrumentWithCalls);
  478. C = &(M.getContext());
  479. IRBuilder<> IRB(*C);
  480. IntptrTy = IRB.getIntPtrTy(DL);
  481. Int8PtrTy = IRB.getInt8PtrTy();
  482. Int8Ty = IRB.getInt8Ty();
  483. Int32Ty = IRB.getInt32Ty();
  484. HwasanCtorFunction = nullptr;
  485. // Older versions of Android do not have the required runtime support for
  486. // short granules, global or personality function instrumentation. On other
  487. // platforms we currently require using the latest version of the runtime.
  488. bool NewRuntime =
  489. !TargetTriple.isAndroid() || !TargetTriple.isAndroidVersionLT(30);
  490. UseShortGranules =
  491. ClUseShortGranules.getNumOccurrences() ? ClUseShortGranules : NewRuntime;
  492. OutlinedChecks =
  493. (TargetTriple.isAArch64() || TargetTriple.isRISCV64()) &&
  494. TargetTriple.isOSBinFormatELF() &&
  495. (ClInlineAllChecks.getNumOccurrences() ? !ClInlineAllChecks : !Recover);
  496. if (ClMatchAllTag.getNumOccurrences()) {
  497. if (ClMatchAllTag != -1) {
  498. MatchAllTag = ClMatchAllTag & 0xFF;
  499. }
  500. } else if (CompileKernel) {
  501. MatchAllTag = 0xFF;
  502. }
  503. // If we don't have personality function support, fall back to landing pads.
  504. InstrumentLandingPads = ClInstrumentLandingPads.getNumOccurrences()
  505. ? ClInstrumentLandingPads
  506. : !NewRuntime;
  507. if (!CompileKernel) {
  508. createHwasanCtorComdat();
  509. bool InstrumentGlobals =
  510. ClGlobals.getNumOccurrences() ? ClGlobals : NewRuntime;
  511. if (InstrumentGlobals && !UsePageAliases)
  512. instrumentGlobals();
  513. bool InstrumentPersonalityFunctions =
  514. ClInstrumentPersonalityFunctions.getNumOccurrences()
  515. ? ClInstrumentPersonalityFunctions
  516. : NewRuntime;
  517. if (InstrumentPersonalityFunctions)
  518. instrumentPersonalityFunctions();
  519. }
  520. if (!TargetTriple.isAndroid()) {
  521. Constant *C = M.getOrInsertGlobal("__hwasan_tls", IntptrTy, [&] {
  522. auto *GV = new GlobalVariable(M, IntptrTy, /*isConstant=*/false,
  523. GlobalValue::ExternalLinkage, nullptr,
  524. "__hwasan_tls", nullptr,
  525. GlobalVariable::InitialExecTLSModel);
  526. appendToCompilerUsed(M, GV);
  527. return GV;
  528. });
  529. ThreadPtrGlobal = cast<GlobalVariable>(C);
  530. }
  531. }
  532. void HWAddressSanitizer::initializeCallbacks(Module &M) {
  533. IRBuilder<> IRB(*C);
  534. for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) {
  535. const std::string TypeStr = AccessIsWrite ? "store" : "load";
  536. const std::string EndingStr = Recover ? "_noabort" : "";
  537. HwasanMemoryAccessCallbackSized[AccessIsWrite] = M.getOrInsertFunction(
  538. ClMemoryAccessCallbackPrefix + TypeStr + "N" + EndingStr,
  539. FunctionType::get(IRB.getVoidTy(), {IntptrTy, IntptrTy}, false));
  540. for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
  541. AccessSizeIndex++) {
  542. HwasanMemoryAccessCallback[AccessIsWrite][AccessSizeIndex] =
  543. M.getOrInsertFunction(
  544. ClMemoryAccessCallbackPrefix + TypeStr +
  545. itostr(1ULL << AccessSizeIndex) + EndingStr,
  546. FunctionType::get(IRB.getVoidTy(), {IntptrTy}, false));
  547. }
  548. }
  549. HwasanTagMemoryFunc = M.getOrInsertFunction(
  550. "__hwasan_tag_memory", IRB.getVoidTy(), Int8PtrTy, Int8Ty, IntptrTy);
  551. HwasanGenerateTagFunc =
  552. M.getOrInsertFunction("__hwasan_generate_tag", Int8Ty);
  553. HwasanRecordFrameRecordFunc = M.getOrInsertFunction(
  554. "__hwasan_add_frame_record", IRB.getVoidTy(), Int64Ty);
  555. ShadowGlobal = M.getOrInsertGlobal("__hwasan_shadow",
  556. ArrayType::get(IRB.getInt8Ty(), 0));
  557. const std::string MemIntrinCallbackPrefix =
  558. (CompileKernel && !ClKasanMemIntrinCallbackPrefix)
  559. ? std::string("")
  560. : ClMemoryAccessCallbackPrefix;
  561. HWAsanMemmove = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memmove",
  562. IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
  563. IRB.getInt8PtrTy(), IntptrTy);
  564. HWAsanMemcpy = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memcpy",
  565. IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
  566. IRB.getInt8PtrTy(), IntptrTy);
  567. HWAsanMemset = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memset",
  568. IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
  569. IRB.getInt32Ty(), IntptrTy);
  570. HWAsanHandleVfork =
  571. M.getOrInsertFunction("__hwasan_handle_vfork", IRB.getVoidTy(), IntptrTy);
  572. }
  573. Value *HWAddressSanitizer::getOpaqueNoopCast(IRBuilder<> &IRB, Value *Val) {
  574. // An empty inline asm with input reg == output reg.
  575. // An opaque no-op cast, basically.
  576. // This prevents code bloat as a result of rematerializing trivial definitions
  577. // such as constants or global addresses at every load and store.
  578. InlineAsm *Asm =
  579. InlineAsm::get(FunctionType::get(Int8PtrTy, {Val->getType()}, false),
  580. StringRef(""), StringRef("=r,0"),
  581. /*hasSideEffects=*/false);
  582. return IRB.CreateCall(Asm, {Val}, ".hwasan.shadow");
  583. }
  584. Value *HWAddressSanitizer::getDynamicShadowIfunc(IRBuilder<> &IRB) {
  585. return getOpaqueNoopCast(IRB, ShadowGlobal);
  586. }
  587. Value *HWAddressSanitizer::getShadowNonTls(IRBuilder<> &IRB) {
  588. if (Mapping.Offset != kDynamicShadowSentinel)
  589. return getOpaqueNoopCast(
  590. IRB, ConstantExpr::getIntToPtr(
  591. ConstantInt::get(IntptrTy, Mapping.Offset), Int8PtrTy));
  592. if (Mapping.InGlobal)
  593. return getDynamicShadowIfunc(IRB);
  594. Value *GlobalDynamicAddress =
  595. IRB.GetInsertBlock()->getParent()->getParent()->getOrInsertGlobal(
  596. kHwasanShadowMemoryDynamicAddress, Int8PtrTy);
  597. return IRB.CreateLoad(Int8PtrTy, GlobalDynamicAddress);
  598. }
  599. bool HWAddressSanitizer::ignoreAccess(Instruction *Inst, Value *Ptr) {
  600. // Do not instrument accesses from different address spaces; we cannot deal
  601. // with them.
  602. Type *PtrTy = cast<PointerType>(Ptr->getType()->getScalarType());
  603. if (PtrTy->getPointerAddressSpace() != 0)
  604. return true;
  605. // Ignore swifterror addresses.
  606. // swifterror memory addresses are mem2reg promoted by instruction
  607. // selection. As such they cannot have regular uses like an instrumentation
  608. // function and it makes no sense to track them as memory.
  609. if (Ptr->isSwiftError())
  610. return true;
  611. if (findAllocaForValue(Ptr)) {
  612. if (!InstrumentStack)
  613. return true;
  614. if (SSI && SSI->stackAccessIsSafe(*Inst))
  615. return true;
  616. }
  617. return false;
  618. }
  619. void HWAddressSanitizer::getInterestingMemoryOperands(
  620. Instruction *I, SmallVectorImpl<InterestingMemoryOperand> &Interesting) {
  621. // Skip memory accesses inserted by another instrumentation.
  622. if (I->hasMetadata(LLVMContext::MD_nosanitize))
  623. return;
  624. // Do not instrument the load fetching the dynamic shadow address.
  625. if (ShadowBase == I)
  626. return;
  627. if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
  628. if (!ClInstrumentReads || ignoreAccess(I, LI->getPointerOperand()))
  629. return;
  630. Interesting.emplace_back(I, LI->getPointerOperandIndex(), false,
  631. LI->getType(), LI->getAlign());
  632. } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
  633. if (!ClInstrumentWrites || ignoreAccess(I, SI->getPointerOperand()))
  634. return;
  635. Interesting.emplace_back(I, SI->getPointerOperandIndex(), true,
  636. SI->getValueOperand()->getType(), SI->getAlign());
  637. } else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) {
  638. if (!ClInstrumentAtomics || ignoreAccess(I, RMW->getPointerOperand()))
  639. return;
  640. Interesting.emplace_back(I, RMW->getPointerOperandIndex(), true,
  641. RMW->getValOperand()->getType(), std::nullopt);
  642. } else if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) {
  643. if (!ClInstrumentAtomics || ignoreAccess(I, XCHG->getPointerOperand()))
  644. return;
  645. Interesting.emplace_back(I, XCHG->getPointerOperandIndex(), true,
  646. XCHG->getCompareOperand()->getType(),
  647. std::nullopt);
  648. } else if (auto *CI = dyn_cast<CallInst>(I)) {
  649. for (unsigned ArgNo = 0; ArgNo < CI->arg_size(); ArgNo++) {
  650. if (!ClInstrumentByval || !CI->isByValArgument(ArgNo) ||
  651. ignoreAccess(I, CI->getArgOperand(ArgNo)))
  652. continue;
  653. Type *Ty = CI->getParamByValType(ArgNo);
  654. Interesting.emplace_back(I, ArgNo, false, Ty, Align(1));
  655. }
  656. }
  657. }
  658. static unsigned getPointerOperandIndex(Instruction *I) {
  659. if (LoadInst *LI = dyn_cast<LoadInst>(I))
  660. return LI->getPointerOperandIndex();
  661. if (StoreInst *SI = dyn_cast<StoreInst>(I))
  662. return SI->getPointerOperandIndex();
  663. if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I))
  664. return RMW->getPointerOperandIndex();
  665. if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I))
  666. return XCHG->getPointerOperandIndex();
  667. report_fatal_error("Unexpected instruction");
  668. return -1;
  669. }
  670. static size_t TypeSizeToSizeIndex(uint32_t TypeSize) {
  671. size_t Res = countTrailingZeros(TypeSize / 8);
  672. assert(Res < kNumberOfAccessSizes);
  673. return Res;
  674. }
  675. void HWAddressSanitizer::untagPointerOperand(Instruction *I, Value *Addr) {
  676. if (TargetTriple.isAArch64() || TargetTriple.getArch() == Triple::x86_64 ||
  677. TargetTriple.isRISCV64())
  678. return;
  679. IRBuilder<> IRB(I);
  680. Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
  681. Value *UntaggedPtr =
  682. IRB.CreateIntToPtr(untagPointer(IRB, AddrLong), Addr->getType());
  683. I->setOperand(getPointerOperandIndex(I), UntaggedPtr);
  684. }
  685. Value *HWAddressSanitizer::memToShadow(Value *Mem, IRBuilder<> &IRB) {
  686. // Mem >> Scale
  687. Value *Shadow = IRB.CreateLShr(Mem, Mapping.Scale);
  688. if (Mapping.Offset == 0)
  689. return IRB.CreateIntToPtr(Shadow, Int8PtrTy);
  690. // (Mem >> Scale) + Offset
  691. return IRB.CreateGEP(Int8Ty, ShadowBase, Shadow);
  692. }
  693. int64_t HWAddressSanitizer::getAccessInfo(bool IsWrite,
  694. unsigned AccessSizeIndex) {
  695. return (CompileKernel << HWASanAccessInfo::CompileKernelShift) |
  696. (MatchAllTag.has_value() << HWASanAccessInfo::HasMatchAllShift) |
  697. (MatchAllTag.value_or(0) << HWASanAccessInfo::MatchAllShift) |
  698. (Recover << HWASanAccessInfo::RecoverShift) |
  699. (IsWrite << HWASanAccessInfo::IsWriteShift) |
  700. (AccessSizeIndex << HWASanAccessInfo::AccessSizeShift);
  701. }
  702. void HWAddressSanitizer::instrumentMemAccessOutline(Value *Ptr, bool IsWrite,
  703. unsigned AccessSizeIndex,
  704. Instruction *InsertBefore) {
  705. assert(!UsePageAliases);
  706. const int64_t AccessInfo = getAccessInfo(IsWrite, AccessSizeIndex);
  707. IRBuilder<> IRB(InsertBefore);
  708. Module *M = IRB.GetInsertBlock()->getParent()->getParent();
  709. Ptr = IRB.CreateBitCast(Ptr, Int8PtrTy);
  710. IRB.CreateCall(Intrinsic::getDeclaration(
  711. M, UseShortGranules
  712. ? Intrinsic::hwasan_check_memaccess_shortgranules
  713. : Intrinsic::hwasan_check_memaccess),
  714. {ShadowBase, Ptr, ConstantInt::get(Int32Ty, AccessInfo)});
  715. }
  716. void HWAddressSanitizer::instrumentMemAccessInline(Value *Ptr, bool IsWrite,
  717. unsigned AccessSizeIndex,
  718. Instruction *InsertBefore) {
  719. assert(!UsePageAliases);
  720. const int64_t AccessInfo = getAccessInfo(IsWrite, AccessSizeIndex);
  721. IRBuilder<> IRB(InsertBefore);
  722. Value *PtrLong = IRB.CreatePointerCast(Ptr, IntptrTy);
  723. Value *PtrTag = IRB.CreateTrunc(IRB.CreateLShr(PtrLong, PointerTagShift),
  724. IRB.getInt8Ty());
  725. Value *AddrLong = untagPointer(IRB, PtrLong);
  726. Value *Shadow = memToShadow(AddrLong, IRB);
  727. Value *MemTag = IRB.CreateLoad(Int8Ty, Shadow);
  728. Value *TagMismatch = IRB.CreateICmpNE(PtrTag, MemTag);
  729. if (MatchAllTag.has_value()) {
  730. Value *TagNotIgnored = IRB.CreateICmpNE(
  731. PtrTag, ConstantInt::get(PtrTag->getType(), *MatchAllTag));
  732. TagMismatch = IRB.CreateAnd(TagMismatch, TagNotIgnored);
  733. }
  734. Instruction *CheckTerm =
  735. SplitBlockAndInsertIfThen(TagMismatch, InsertBefore, false,
  736. MDBuilder(*C).createBranchWeights(1, 100000));
  737. IRB.SetInsertPoint(CheckTerm);
  738. Value *OutOfShortGranuleTagRange =
  739. IRB.CreateICmpUGT(MemTag, ConstantInt::get(Int8Ty, 15));
  740. Instruction *CheckFailTerm =
  741. SplitBlockAndInsertIfThen(OutOfShortGranuleTagRange, CheckTerm, !Recover,
  742. MDBuilder(*C).createBranchWeights(1, 100000));
  743. IRB.SetInsertPoint(CheckTerm);
  744. Value *PtrLowBits = IRB.CreateTrunc(IRB.CreateAnd(PtrLong, 15), Int8Ty);
  745. PtrLowBits = IRB.CreateAdd(
  746. PtrLowBits, ConstantInt::get(Int8Ty, (1 << AccessSizeIndex) - 1));
  747. Value *PtrLowBitsOOB = IRB.CreateICmpUGE(PtrLowBits, MemTag);
  748. SplitBlockAndInsertIfThen(PtrLowBitsOOB, CheckTerm, false,
  749. MDBuilder(*C).createBranchWeights(1, 100000),
  750. (DomTreeUpdater *)nullptr, nullptr,
  751. CheckFailTerm->getParent());
  752. IRB.SetInsertPoint(CheckTerm);
  753. Value *InlineTagAddr = IRB.CreateOr(AddrLong, 15);
  754. InlineTagAddr = IRB.CreateIntToPtr(InlineTagAddr, Int8PtrTy);
  755. Value *InlineTag = IRB.CreateLoad(Int8Ty, InlineTagAddr);
  756. Value *InlineTagMismatch = IRB.CreateICmpNE(PtrTag, InlineTag);
  757. SplitBlockAndInsertIfThen(InlineTagMismatch, CheckTerm, false,
  758. MDBuilder(*C).createBranchWeights(1, 100000),
  759. (DomTreeUpdater *)nullptr, nullptr,
  760. CheckFailTerm->getParent());
  761. IRB.SetInsertPoint(CheckFailTerm);
  762. InlineAsm *Asm;
  763. switch (TargetTriple.getArch()) {
  764. case Triple::x86_64:
  765. // The signal handler will find the data address in rdi.
  766. Asm = InlineAsm::get(
  767. FunctionType::get(IRB.getVoidTy(), {PtrLong->getType()}, false),
  768. "int3\nnopl " +
  769. itostr(0x40 + (AccessInfo & HWASanAccessInfo::RuntimeMask)) +
  770. "(%rax)",
  771. "{rdi}",
  772. /*hasSideEffects=*/true);
  773. break;
  774. case Triple::aarch64:
  775. case Triple::aarch64_be:
  776. // The signal handler will find the data address in x0.
  777. Asm = InlineAsm::get(
  778. FunctionType::get(IRB.getVoidTy(), {PtrLong->getType()}, false),
  779. "brk #" + itostr(0x900 + (AccessInfo & HWASanAccessInfo::RuntimeMask)),
  780. "{x0}",
  781. /*hasSideEffects=*/true);
  782. break;
  783. case Triple::riscv64:
  784. // The signal handler will find the data address in x10.
  785. Asm = InlineAsm::get(
  786. FunctionType::get(IRB.getVoidTy(), {PtrLong->getType()}, false),
  787. "ebreak\naddiw x0, x11, " +
  788. itostr(0x40 + (AccessInfo & HWASanAccessInfo::RuntimeMask)),
  789. "{x10}",
  790. /*hasSideEffects=*/true);
  791. break;
  792. default:
  793. report_fatal_error("unsupported architecture");
  794. }
  795. IRB.CreateCall(Asm, PtrLong);
  796. if (Recover)
  797. cast<BranchInst>(CheckFailTerm)->setSuccessor(0, CheckTerm->getParent());
  798. }
  799. bool HWAddressSanitizer::ignoreMemIntrinsic(MemIntrinsic *MI) {
  800. if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) {
  801. return (!ClInstrumentWrites || ignoreAccess(MTI, MTI->getDest())) &&
  802. (!ClInstrumentReads || ignoreAccess(MTI, MTI->getSource()));
  803. }
  804. if (isa<MemSetInst>(MI))
  805. return !ClInstrumentWrites || ignoreAccess(MI, MI->getDest());
  806. return false;
  807. }
  808. void HWAddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) {
  809. IRBuilder<> IRB(MI);
  810. if (isa<MemTransferInst>(MI)) {
  811. IRB.CreateCall(
  812. isa<MemMoveInst>(MI) ? HWAsanMemmove : HWAsanMemcpy,
  813. {IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()),
  814. IRB.CreatePointerCast(MI->getOperand(1), IRB.getInt8PtrTy()),
  815. IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)});
  816. } else if (isa<MemSetInst>(MI)) {
  817. IRB.CreateCall(
  818. HWAsanMemset,
  819. {IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()),
  820. IRB.CreateIntCast(MI->getOperand(1), IRB.getInt32Ty(), false),
  821. IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)});
  822. }
  823. MI->eraseFromParent();
  824. }
  825. bool HWAddressSanitizer::instrumentMemAccess(InterestingMemoryOperand &O) {
  826. Value *Addr = O.getPtr();
  827. LLVM_DEBUG(dbgs() << "Instrumenting: " << O.getInsn() << "\n");
  828. if (O.MaybeMask)
  829. return false; // FIXME
  830. IRBuilder<> IRB(O.getInsn());
  831. if (isPowerOf2_64(O.TypeSize) &&
  832. (O.TypeSize / 8 <= (1ULL << (kNumberOfAccessSizes - 1))) &&
  833. (!O.Alignment || *O.Alignment >= Mapping.getObjectAlignment() ||
  834. *O.Alignment >= O.TypeSize / 8)) {
  835. size_t AccessSizeIndex = TypeSizeToSizeIndex(O.TypeSize);
  836. if (InstrumentWithCalls) {
  837. IRB.CreateCall(HwasanMemoryAccessCallback[O.IsWrite][AccessSizeIndex],
  838. IRB.CreatePointerCast(Addr, IntptrTy));
  839. } else if (OutlinedChecks) {
  840. instrumentMemAccessOutline(Addr, O.IsWrite, AccessSizeIndex, O.getInsn());
  841. } else {
  842. instrumentMemAccessInline(Addr, O.IsWrite, AccessSizeIndex, O.getInsn());
  843. }
  844. } else {
  845. IRB.CreateCall(HwasanMemoryAccessCallbackSized[O.IsWrite],
  846. {IRB.CreatePointerCast(Addr, IntptrTy),
  847. ConstantInt::get(IntptrTy, O.TypeSize / 8)});
  848. }
  849. untagPointerOperand(O.getInsn(), Addr);
  850. return true;
  851. }
  852. void HWAddressSanitizer::tagAlloca(IRBuilder<> &IRB, AllocaInst *AI, Value *Tag,
  853. size_t Size) {
  854. size_t AlignedSize = alignTo(Size, Mapping.getObjectAlignment());
  855. if (!UseShortGranules)
  856. Size = AlignedSize;
  857. Value *JustTag = IRB.CreateTrunc(Tag, IRB.getInt8Ty());
  858. if (InstrumentWithCalls) {
  859. IRB.CreateCall(HwasanTagMemoryFunc,
  860. {IRB.CreatePointerCast(AI, Int8PtrTy), JustTag,
  861. ConstantInt::get(IntptrTy, AlignedSize)});
  862. } else {
  863. size_t ShadowSize = Size >> Mapping.Scale;
  864. Value *ShadowPtr = memToShadow(IRB.CreatePointerCast(AI, IntptrTy), IRB);
  865. // If this memset is not inlined, it will be intercepted in the hwasan
  866. // runtime library. That's OK, because the interceptor skips the checks if
  867. // the address is in the shadow region.
  868. // FIXME: the interceptor is not as fast as real memset. Consider lowering
  869. // llvm.memset right here into either a sequence of stores, or a call to
  870. // hwasan_tag_memory.
  871. if (ShadowSize)
  872. IRB.CreateMemSet(ShadowPtr, JustTag, ShadowSize, Align(1));
  873. if (Size != AlignedSize) {
  874. const uint8_t SizeRemainder = Size % Mapping.getObjectAlignment().value();
  875. IRB.CreateStore(ConstantInt::get(Int8Ty, SizeRemainder),
  876. IRB.CreateConstGEP1_32(Int8Ty, ShadowPtr, ShadowSize));
  877. IRB.CreateStore(JustTag, IRB.CreateConstGEP1_32(
  878. Int8Ty, IRB.CreateBitCast(AI, Int8PtrTy),
  879. AlignedSize - 1));
  880. }
  881. }
  882. }
  883. unsigned HWAddressSanitizer::retagMask(unsigned AllocaNo) {
  884. if (TargetTriple.getArch() == Triple::x86_64)
  885. return AllocaNo & TagMaskByte;
  886. // A list of 8-bit numbers that have at most one run of non-zero bits.
  887. // x = x ^ (mask << 56) can be encoded as a single armv8 instruction for these
  888. // masks.
  889. // The list does not include the value 255, which is used for UAR.
  890. //
  891. // Because we are more likely to use earlier elements of this list than later
  892. // ones, it is sorted in increasing order of probability of collision with a
  893. // mask allocated (temporally) nearby. The program that generated this list
  894. // can be found at:
  895. // https://github.com/google/sanitizers/blob/master/hwaddress-sanitizer/sort_masks.py
  896. static unsigned FastMasks[] = {0, 128, 64, 192, 32, 96, 224, 112, 240,
  897. 48, 16, 120, 248, 56, 24, 8, 124, 252,
  898. 60, 28, 12, 4, 126, 254, 62, 30, 14,
  899. 6, 2, 127, 63, 31, 15, 7, 3, 1};
  900. return FastMasks[AllocaNo % std::size(FastMasks)];
  901. }
  902. Value *HWAddressSanitizer::applyTagMask(IRBuilder<> &IRB, Value *OldTag) {
  903. if (TargetTriple.getArch() == Triple::x86_64) {
  904. Constant *TagMask = ConstantInt::get(IntptrTy, TagMaskByte);
  905. Value *NewTag = IRB.CreateAnd(OldTag, TagMask);
  906. return NewTag;
  907. }
  908. // aarch64 uses 8-bit tags, so no mask is needed.
  909. return OldTag;
  910. }
  911. Value *HWAddressSanitizer::getNextTagWithCall(IRBuilder<> &IRB) {
  912. return IRB.CreateZExt(IRB.CreateCall(HwasanGenerateTagFunc), IntptrTy);
  913. }
  914. Value *HWAddressSanitizer::getStackBaseTag(IRBuilder<> &IRB) {
  915. if (ClGenerateTagsWithCalls)
  916. return getNextTagWithCall(IRB);
  917. if (StackBaseTag)
  918. return StackBaseTag;
  919. // Extract some entropy from the stack pointer for the tags.
  920. // Take bits 20..28 (ASLR entropy) and xor with bits 0..8 (these differ
  921. // between functions).
  922. Value *StackPointerLong = getSP(IRB);
  923. Value *StackTag =
  924. applyTagMask(IRB, IRB.CreateXor(StackPointerLong,
  925. IRB.CreateLShr(StackPointerLong, 20)));
  926. StackTag->setName("hwasan.stack.base.tag");
  927. return StackTag;
  928. }
  929. Value *HWAddressSanitizer::getAllocaTag(IRBuilder<> &IRB, Value *StackTag,
  930. AllocaInst *AI, unsigned AllocaNo) {
  931. if (ClGenerateTagsWithCalls)
  932. return getNextTagWithCall(IRB);
  933. return IRB.CreateXor(StackTag,
  934. ConstantInt::get(IntptrTy, retagMask(AllocaNo)));
  935. }
  936. Value *HWAddressSanitizer::getUARTag(IRBuilder<> &IRB, Value *StackTag) {
  937. if (ClUARRetagToZero)
  938. return ConstantInt::get(IntptrTy, 0);
  939. if (ClGenerateTagsWithCalls)
  940. return getNextTagWithCall(IRB);
  941. return IRB.CreateXor(StackTag, ConstantInt::get(IntptrTy, TagMaskByte));
  942. }
  943. // Add a tag to an address.
  944. Value *HWAddressSanitizer::tagPointer(IRBuilder<> &IRB, Type *Ty,
  945. Value *PtrLong, Value *Tag) {
  946. assert(!UsePageAliases);
  947. Value *TaggedPtrLong;
  948. if (CompileKernel) {
  949. // Kernel addresses have 0xFF in the most significant byte.
  950. Value *ShiftedTag =
  951. IRB.CreateOr(IRB.CreateShl(Tag, PointerTagShift),
  952. ConstantInt::get(IntptrTy, (1ULL << PointerTagShift) - 1));
  953. TaggedPtrLong = IRB.CreateAnd(PtrLong, ShiftedTag);
  954. } else {
  955. // Userspace can simply do OR (tag << PointerTagShift);
  956. Value *ShiftedTag = IRB.CreateShl(Tag, PointerTagShift);
  957. TaggedPtrLong = IRB.CreateOr(PtrLong, ShiftedTag);
  958. }
  959. return IRB.CreateIntToPtr(TaggedPtrLong, Ty);
  960. }
  961. // Remove tag from an address.
  962. Value *HWAddressSanitizer::untagPointer(IRBuilder<> &IRB, Value *PtrLong) {
  963. assert(!UsePageAliases);
  964. Value *UntaggedPtrLong;
  965. if (CompileKernel) {
  966. // Kernel addresses have 0xFF in the most significant byte.
  967. UntaggedPtrLong =
  968. IRB.CreateOr(PtrLong, ConstantInt::get(PtrLong->getType(),
  969. 0xFFULL << PointerTagShift));
  970. } else {
  971. // Userspace addresses have 0x00.
  972. UntaggedPtrLong =
  973. IRB.CreateAnd(PtrLong, ConstantInt::get(PtrLong->getType(),
  974. ~(0xFFULL << PointerTagShift)));
  975. }
  976. return UntaggedPtrLong;
  977. }
  978. Value *HWAddressSanitizer::getHwasanThreadSlotPtr(IRBuilder<> &IRB, Type *Ty) {
  979. Module *M = IRB.GetInsertBlock()->getParent()->getParent();
  980. if (TargetTriple.isAArch64() && TargetTriple.isAndroid()) {
  981. // Android provides a fixed TLS slot for sanitizers. See TLS_SLOT_SANITIZER
  982. // in Bionic's libc/private/bionic_tls.h.
  983. Function *ThreadPointerFunc =
  984. Intrinsic::getDeclaration(M, Intrinsic::thread_pointer);
  985. Value *SlotPtr = IRB.CreatePointerCast(
  986. IRB.CreateConstGEP1_32(IRB.getInt8Ty(),
  987. IRB.CreateCall(ThreadPointerFunc), 0x30),
  988. Ty->getPointerTo(0));
  989. return SlotPtr;
  990. }
  991. if (ThreadPtrGlobal)
  992. return ThreadPtrGlobal;
  993. return nullptr;
  994. }
  995. Value *HWAddressSanitizer::getPC(IRBuilder<> &IRB) {
  996. if (TargetTriple.getArch() == Triple::aarch64)
  997. return readRegister(IRB, "pc");
  998. return IRB.CreatePtrToInt(IRB.GetInsertBlock()->getParent(), IntptrTy);
  999. }
  1000. Value *HWAddressSanitizer::getSP(IRBuilder<> &IRB) {
  1001. if (!CachedSP) {
  1002. // FIXME: use addressofreturnaddress (but implement it in aarch64 backend
  1003. // first).
  1004. Function *F = IRB.GetInsertBlock()->getParent();
  1005. Module *M = F->getParent();
  1006. auto *GetStackPointerFn = Intrinsic::getDeclaration(
  1007. M, Intrinsic::frameaddress,
  1008. IRB.getInt8PtrTy(M->getDataLayout().getAllocaAddrSpace()));
  1009. CachedSP = IRB.CreatePtrToInt(
  1010. IRB.CreateCall(GetStackPointerFn,
  1011. {Constant::getNullValue(IRB.getInt32Ty())}),
  1012. IntptrTy);
  1013. }
  1014. return CachedSP;
  1015. }
  1016. Value *HWAddressSanitizer::getFrameRecordInfo(IRBuilder<> &IRB) {
  1017. // Prepare ring buffer data.
  1018. Value *PC = getPC(IRB);
  1019. Value *SP = getSP(IRB);
  1020. // Mix SP and PC.
  1021. // Assumptions:
  1022. // PC is 0x0000PPPPPPPPPPPP (48 bits are meaningful, others are zero)
  1023. // SP is 0xsssssssssssSSSS0 (4 lower bits are zero)
  1024. // We only really need ~20 lower non-zero bits (SSSS), so we mix like this:
  1025. // 0xSSSSPPPPPPPPPPPP
  1026. SP = IRB.CreateShl(SP, 44);
  1027. return IRB.CreateOr(PC, SP);
  1028. }
  1029. void HWAddressSanitizer::emitPrologue(IRBuilder<> &IRB, bool WithFrameRecord) {
  1030. if (!Mapping.InTls)
  1031. ShadowBase = getShadowNonTls(IRB);
  1032. else if (!WithFrameRecord && TargetTriple.isAndroid())
  1033. ShadowBase = getDynamicShadowIfunc(IRB);
  1034. if (!WithFrameRecord && ShadowBase)
  1035. return;
  1036. Value *SlotPtr = nullptr;
  1037. Value *ThreadLong = nullptr;
  1038. Value *ThreadLongMaybeUntagged = nullptr;
  1039. auto getThreadLongMaybeUntagged = [&]() {
  1040. if (!SlotPtr)
  1041. SlotPtr = getHwasanThreadSlotPtr(IRB, IntptrTy);
  1042. if (!ThreadLong)
  1043. ThreadLong = IRB.CreateLoad(IntptrTy, SlotPtr);
  1044. // Extract the address field from ThreadLong. Unnecessary on AArch64 with
  1045. // TBI.
  1046. return TargetTriple.isAArch64() ? ThreadLong
  1047. : untagPointer(IRB, ThreadLong);
  1048. };
  1049. if (WithFrameRecord) {
  1050. switch (ClRecordStackHistory) {
  1051. case libcall: {
  1052. // Emit a runtime call into hwasan rather than emitting instructions for
  1053. // recording stack history.
  1054. Value *FrameRecordInfo = getFrameRecordInfo(IRB);
  1055. IRB.CreateCall(HwasanRecordFrameRecordFunc, {FrameRecordInfo});
  1056. break;
  1057. }
  1058. case instr: {
  1059. ThreadLongMaybeUntagged = getThreadLongMaybeUntagged();
  1060. StackBaseTag = IRB.CreateAShr(ThreadLong, 3);
  1061. // Store data to ring buffer.
  1062. Value *FrameRecordInfo = getFrameRecordInfo(IRB);
  1063. Value *RecordPtr = IRB.CreateIntToPtr(ThreadLongMaybeUntagged,
  1064. IntptrTy->getPointerTo(0));
  1065. IRB.CreateStore(FrameRecordInfo, RecordPtr);
  1066. // Update the ring buffer. Top byte of ThreadLong defines the size of the
  1067. // buffer in pages, it must be a power of two, and the start of the buffer
  1068. // must be aligned by twice that much. Therefore wrap around of the ring
  1069. // buffer is simply Addr &= ~((ThreadLong >> 56) << 12).
  1070. // The use of AShr instead of LShr is due to
  1071. // https://bugs.llvm.org/show_bug.cgi?id=39030
  1072. // Runtime library makes sure not to use the highest bit.
  1073. Value *WrapMask = IRB.CreateXor(
  1074. IRB.CreateShl(IRB.CreateAShr(ThreadLong, 56), 12, "", true, true),
  1075. ConstantInt::get(IntptrTy, (uint64_t)-1));
  1076. Value *ThreadLongNew = IRB.CreateAnd(
  1077. IRB.CreateAdd(ThreadLong, ConstantInt::get(IntptrTy, 8)), WrapMask);
  1078. IRB.CreateStore(ThreadLongNew, SlotPtr);
  1079. break;
  1080. }
  1081. case none: {
  1082. llvm_unreachable(
  1083. "A stack history recording mode should've been selected.");
  1084. }
  1085. }
  1086. }
  1087. if (!ShadowBase) {
  1088. if (!ThreadLongMaybeUntagged)
  1089. ThreadLongMaybeUntagged = getThreadLongMaybeUntagged();
  1090. // Get shadow base address by aligning RecordPtr up.
  1091. // Note: this is not correct if the pointer is already aligned.
  1092. // Runtime library will make sure this never happens.
  1093. ShadowBase = IRB.CreateAdd(
  1094. IRB.CreateOr(
  1095. ThreadLongMaybeUntagged,
  1096. ConstantInt::get(IntptrTy, (1ULL << kShadowBaseAlignment) - 1)),
  1097. ConstantInt::get(IntptrTy, 1), "hwasan.shadow");
  1098. ShadowBase = IRB.CreateIntToPtr(ShadowBase, Int8PtrTy);
  1099. }
  1100. }
  1101. Value *HWAddressSanitizer::readRegister(IRBuilder<> &IRB, StringRef Name) {
  1102. Module *M = IRB.GetInsertBlock()->getParent()->getParent();
  1103. Function *ReadRegister =
  1104. Intrinsic::getDeclaration(M, Intrinsic::read_register, IntptrTy);
  1105. MDNode *MD = MDNode::get(*C, {MDString::get(*C, Name)});
  1106. Value *Args[] = {MetadataAsValue::get(*C, MD)};
  1107. return IRB.CreateCall(ReadRegister, Args);
  1108. }
  1109. bool HWAddressSanitizer::instrumentLandingPads(
  1110. SmallVectorImpl<Instruction *> &LandingPadVec) {
  1111. for (auto *LP : LandingPadVec) {
  1112. IRBuilder<> IRB(LP->getNextNode());
  1113. IRB.CreateCall(
  1114. HWAsanHandleVfork,
  1115. {readRegister(IRB, (TargetTriple.getArch() == Triple::x86_64) ? "rsp"
  1116. : "sp")});
  1117. }
  1118. return true;
  1119. }
  1120. static bool isLifetimeIntrinsic(Value *V) {
  1121. auto *II = dyn_cast<IntrinsicInst>(V);
  1122. return II && II->isLifetimeStartOrEnd();
  1123. }
  1124. bool HWAddressSanitizer::instrumentStack(memtag::StackInfo &SInfo,
  1125. Value *StackTag,
  1126. const DominatorTree &DT,
  1127. const PostDominatorTree &PDT,
  1128. const LoopInfo &LI) {
  1129. // Ideally, we want to calculate tagged stack base pointer, and rewrite all
  1130. // alloca addresses using that. Unfortunately, offsets are not known yet
  1131. // (unless we use ASan-style mega-alloca). Instead we keep the base tag in a
  1132. // temp, shift-OR it into each alloca address and xor with the retag mask.
  1133. // This generates one extra instruction per alloca use.
  1134. unsigned int I = 0;
  1135. for (auto &KV : SInfo.AllocasToInstrument) {
  1136. auto N = I++;
  1137. auto *AI = KV.first;
  1138. memtag::AllocaInfo &Info = KV.second;
  1139. IRBuilder<> IRB(AI->getNextNode());
  1140. // Replace uses of the alloca with tagged address.
  1141. Value *Tag = getAllocaTag(IRB, StackTag, AI, N);
  1142. Value *AILong = IRB.CreatePointerCast(AI, IntptrTy);
  1143. Value *Replacement = tagPointer(IRB, AI->getType(), AILong, Tag);
  1144. std::string Name =
  1145. AI->hasName() ? AI->getName().str() : "alloca." + itostr(N);
  1146. Replacement->setName(Name + ".hwasan");
  1147. size_t Size = memtag::getAllocaSizeInBytes(*AI);
  1148. size_t AlignedSize = alignTo(Size, Mapping.getObjectAlignment());
  1149. Value *AICast = IRB.CreatePointerCast(AI, Int8PtrTy);
  1150. auto HandleLifetime = [&](IntrinsicInst *II) {
  1151. // Set the lifetime intrinsic to cover the whole alloca. This reduces the
  1152. // set of assumptions we need to make about the lifetime. Without this we
  1153. // would need to ensure that we can track the lifetime pointer to a
  1154. // constant offset from the alloca, and would still need to change the
  1155. // size to include the extra alignment we use for the untagging to make
  1156. // the size consistent.
  1157. //
  1158. // The check for standard lifetime below makes sure that we have exactly
  1159. // one set of start / end in any execution (i.e. the ends are not
  1160. // reachable from each other), so this will not cause any problems.
  1161. II->setArgOperand(0, ConstantInt::get(Int64Ty, AlignedSize));
  1162. II->setArgOperand(1, AICast);
  1163. };
  1164. llvm::for_each(Info.LifetimeStart, HandleLifetime);
  1165. llvm::for_each(Info.LifetimeEnd, HandleLifetime);
  1166. AI->replaceUsesWithIf(Replacement, [AICast, AILong](Use &U) {
  1167. auto *User = U.getUser();
  1168. return User != AILong && User != AICast && !isLifetimeIntrinsic(User);
  1169. });
  1170. for (auto *DDI : Info.DbgVariableIntrinsics) {
  1171. // Prepend "tag_offset, N" to the dwarf expression.
  1172. // Tag offset logically applies to the alloca pointer, and it makes sense
  1173. // to put it at the beginning of the expression.
  1174. SmallVector<uint64_t, 8> NewOps = {dwarf::DW_OP_LLVM_tag_offset,
  1175. retagMask(N)};
  1176. for (size_t LocNo = 0; LocNo < DDI->getNumVariableLocationOps(); ++LocNo)
  1177. if (DDI->getVariableLocationOp(LocNo) == AI)
  1178. DDI->setExpression(DIExpression::appendOpsToArg(DDI->getExpression(),
  1179. NewOps, LocNo));
  1180. }
  1181. auto TagEnd = [&](Instruction *Node) {
  1182. IRB.SetInsertPoint(Node);
  1183. Value *UARTag = getUARTag(IRB, StackTag);
  1184. // When untagging, use the `AlignedSize` because we need to set the tags
  1185. // for the entire alloca to zero. If we used `Size` here, we would
  1186. // keep the last granule tagged, and store zero in the last byte of the
  1187. // last granule, due to how short granules are implemented.
  1188. tagAlloca(IRB, AI, UARTag, AlignedSize);
  1189. };
  1190. // Calls to functions that may return twice (e.g. setjmp) confuse the
  1191. // postdominator analysis, and will leave us to keep memory tagged after
  1192. // function return. Work around this by always untagging at every return
  1193. // statement if return_twice functions are called.
  1194. bool StandardLifetime =
  1195. SInfo.UnrecognizedLifetimes.empty() &&
  1196. memtag::isStandardLifetime(Info.LifetimeStart, Info.LifetimeEnd, &DT,
  1197. &LI, ClMaxLifetimes) &&
  1198. !SInfo.CallsReturnTwice;
  1199. if (DetectUseAfterScope && StandardLifetime) {
  1200. IntrinsicInst *Start = Info.LifetimeStart[0];
  1201. IRB.SetInsertPoint(Start->getNextNode());
  1202. tagAlloca(IRB, AI, Tag, Size);
  1203. if (!memtag::forAllReachableExits(DT, PDT, LI, Start, Info.LifetimeEnd,
  1204. SInfo.RetVec, TagEnd)) {
  1205. for (auto *End : Info.LifetimeEnd)
  1206. End->eraseFromParent();
  1207. }
  1208. } else {
  1209. tagAlloca(IRB, AI, Tag, Size);
  1210. for (auto *RI : SInfo.RetVec)
  1211. TagEnd(RI);
  1212. // We inserted tagging outside of the lifetimes, so we have to remove
  1213. // them.
  1214. for (auto &II : Info.LifetimeStart)
  1215. II->eraseFromParent();
  1216. for (auto &II : Info.LifetimeEnd)
  1217. II->eraseFromParent();
  1218. }
  1219. memtag::alignAndPadAlloca(Info, Mapping.getObjectAlignment());
  1220. }
  1221. for (auto &I : SInfo.UnrecognizedLifetimes)
  1222. I->eraseFromParent();
  1223. return true;
  1224. }
  1225. bool HWAddressSanitizer::sanitizeFunction(Function &F,
  1226. FunctionAnalysisManager &FAM) {
  1227. if (&F == HwasanCtorFunction)
  1228. return false;
  1229. if (!F.hasFnAttribute(Attribute::SanitizeHWAddress))
  1230. return false;
  1231. LLVM_DEBUG(dbgs() << "Function: " << F.getName() << "\n");
  1232. SmallVector<InterestingMemoryOperand, 16> OperandsToInstrument;
  1233. SmallVector<MemIntrinsic *, 16> IntrinToInstrument;
  1234. SmallVector<Instruction *, 8> LandingPadVec;
  1235. memtag::StackInfoBuilder SIB(SSI);
  1236. for (auto &Inst : instructions(F)) {
  1237. if (InstrumentStack) {
  1238. SIB.visit(Inst);
  1239. }
  1240. if (InstrumentLandingPads && isa<LandingPadInst>(Inst))
  1241. LandingPadVec.push_back(&Inst);
  1242. getInterestingMemoryOperands(&Inst, OperandsToInstrument);
  1243. if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(&Inst))
  1244. if (!ignoreMemIntrinsic(MI))
  1245. IntrinToInstrument.push_back(MI);
  1246. }
  1247. memtag::StackInfo &SInfo = SIB.get();
  1248. initializeCallbacks(*F.getParent());
  1249. bool Changed = false;
  1250. if (!LandingPadVec.empty())
  1251. Changed |= instrumentLandingPads(LandingPadVec);
  1252. if (SInfo.AllocasToInstrument.empty() && F.hasPersonalityFn() &&
  1253. F.getPersonalityFn()->getName() == kHwasanPersonalityThunkName) {
  1254. // __hwasan_personality_thunk is a no-op for functions without an
  1255. // instrumented stack, so we can drop it.
  1256. F.setPersonalityFn(nullptr);
  1257. Changed = true;
  1258. }
  1259. if (SInfo.AllocasToInstrument.empty() && OperandsToInstrument.empty() &&
  1260. IntrinToInstrument.empty())
  1261. return Changed;
  1262. assert(!ShadowBase);
  1263. Instruction *InsertPt = &*F.getEntryBlock().begin();
  1264. IRBuilder<> EntryIRB(InsertPt);
  1265. emitPrologue(EntryIRB,
  1266. /*WithFrameRecord*/ ClRecordStackHistory != none &&
  1267. Mapping.WithFrameRecord &&
  1268. !SInfo.AllocasToInstrument.empty());
  1269. if (!SInfo.AllocasToInstrument.empty()) {
  1270. const DominatorTree &DT = FAM.getResult<DominatorTreeAnalysis>(F);
  1271. const PostDominatorTree &PDT = FAM.getResult<PostDominatorTreeAnalysis>(F);
  1272. const LoopInfo &LI = FAM.getResult<LoopAnalysis>(F);
  1273. Value *StackTag =
  1274. ClGenerateTagsWithCalls ? nullptr : getStackBaseTag(EntryIRB);
  1275. instrumentStack(SInfo, StackTag, DT, PDT, LI);
  1276. }
  1277. // If we split the entry block, move any allocas that were originally in the
  1278. // entry block back into the entry block so that they aren't treated as
  1279. // dynamic allocas.
  1280. if (EntryIRB.GetInsertBlock() != &F.getEntryBlock()) {
  1281. InsertPt = &*F.getEntryBlock().begin();
  1282. for (Instruction &I :
  1283. llvm::make_early_inc_range(*EntryIRB.GetInsertBlock())) {
  1284. if (auto *AI = dyn_cast<AllocaInst>(&I))
  1285. if (isa<ConstantInt>(AI->getArraySize()))
  1286. I.moveBefore(InsertPt);
  1287. }
  1288. }
  1289. for (auto &Operand : OperandsToInstrument)
  1290. instrumentMemAccess(Operand);
  1291. if (ClInstrumentMemIntrinsics && !IntrinToInstrument.empty()) {
  1292. for (auto *Inst : IntrinToInstrument)
  1293. instrumentMemIntrinsic(Inst);
  1294. }
  1295. ShadowBase = nullptr;
  1296. StackBaseTag = nullptr;
  1297. CachedSP = nullptr;
  1298. return true;
  1299. }
  1300. void HWAddressSanitizer::instrumentGlobal(GlobalVariable *GV, uint8_t Tag) {
  1301. assert(!UsePageAliases);
  1302. Constant *Initializer = GV->getInitializer();
  1303. uint64_t SizeInBytes =
  1304. M.getDataLayout().getTypeAllocSize(Initializer->getType());
  1305. uint64_t NewSize = alignTo(SizeInBytes, Mapping.getObjectAlignment());
  1306. if (SizeInBytes != NewSize) {
  1307. // Pad the initializer out to the next multiple of 16 bytes and add the
  1308. // required short granule tag.
  1309. std::vector<uint8_t> Init(NewSize - SizeInBytes, 0);
  1310. Init.back() = Tag;
  1311. Constant *Padding = ConstantDataArray::get(*C, Init);
  1312. Initializer = ConstantStruct::getAnon({Initializer, Padding});
  1313. }
  1314. auto *NewGV = new GlobalVariable(M, Initializer->getType(), GV->isConstant(),
  1315. GlobalValue::ExternalLinkage, Initializer,
  1316. GV->getName() + ".hwasan");
  1317. NewGV->copyAttributesFrom(GV);
  1318. NewGV->setLinkage(GlobalValue::PrivateLinkage);
  1319. NewGV->copyMetadata(GV, 0);
  1320. NewGV->setAlignment(
  1321. std::max(GV->getAlign().valueOrOne(), Mapping.getObjectAlignment()));
  1322. // It is invalid to ICF two globals that have different tags. In the case
  1323. // where the size of the global is a multiple of the tag granularity the
  1324. // contents of the globals may be the same but the tags (i.e. symbol values)
  1325. // may be different, and the symbols are not considered during ICF. In the
  1326. // case where the size is not a multiple of the granularity, the short granule
  1327. // tags would discriminate two globals with different tags, but there would
  1328. // otherwise be nothing stopping such a global from being incorrectly ICF'd
  1329. // with an uninstrumented (i.e. tag 0) global that happened to have the short
  1330. // granule tag in the last byte.
  1331. NewGV->setUnnamedAddr(GlobalValue::UnnamedAddr::None);
  1332. // Descriptor format (assuming little-endian):
  1333. // bytes 0-3: relative address of global
  1334. // bytes 4-6: size of global (16MB ought to be enough for anyone, but in case
  1335. // it isn't, we create multiple descriptors)
  1336. // byte 7: tag
  1337. auto *DescriptorTy = StructType::get(Int32Ty, Int32Ty);
  1338. const uint64_t MaxDescriptorSize = 0xfffff0;
  1339. for (uint64_t DescriptorPos = 0; DescriptorPos < SizeInBytes;
  1340. DescriptorPos += MaxDescriptorSize) {
  1341. auto *Descriptor =
  1342. new GlobalVariable(M, DescriptorTy, true, GlobalValue::PrivateLinkage,
  1343. nullptr, GV->getName() + ".hwasan.descriptor");
  1344. auto *GVRelPtr = ConstantExpr::getTrunc(
  1345. ConstantExpr::getAdd(
  1346. ConstantExpr::getSub(
  1347. ConstantExpr::getPtrToInt(NewGV, Int64Ty),
  1348. ConstantExpr::getPtrToInt(Descriptor, Int64Ty)),
  1349. ConstantInt::get(Int64Ty, DescriptorPos)),
  1350. Int32Ty);
  1351. uint32_t Size = std::min(SizeInBytes - DescriptorPos, MaxDescriptorSize);
  1352. auto *SizeAndTag = ConstantInt::get(Int32Ty, Size | (uint32_t(Tag) << 24));
  1353. Descriptor->setComdat(NewGV->getComdat());
  1354. Descriptor->setInitializer(ConstantStruct::getAnon({GVRelPtr, SizeAndTag}));
  1355. Descriptor->setSection("hwasan_globals");
  1356. Descriptor->setMetadata(LLVMContext::MD_associated,
  1357. MDNode::get(*C, ValueAsMetadata::get(NewGV)));
  1358. appendToCompilerUsed(M, Descriptor);
  1359. }
  1360. Constant *Aliasee = ConstantExpr::getIntToPtr(
  1361. ConstantExpr::getAdd(
  1362. ConstantExpr::getPtrToInt(NewGV, Int64Ty),
  1363. ConstantInt::get(Int64Ty, uint64_t(Tag) << PointerTagShift)),
  1364. GV->getType());
  1365. auto *Alias = GlobalAlias::create(GV->getValueType(), GV->getAddressSpace(),
  1366. GV->getLinkage(), "", Aliasee, &M);
  1367. Alias->setVisibility(GV->getVisibility());
  1368. Alias->takeName(GV);
  1369. GV->replaceAllUsesWith(Alias);
  1370. GV->eraseFromParent();
  1371. }
  1372. void HWAddressSanitizer::instrumentGlobals() {
  1373. std::vector<GlobalVariable *> Globals;
  1374. for (GlobalVariable &GV : M.globals()) {
  1375. if (GV.hasSanitizerMetadata() && GV.getSanitizerMetadata().NoHWAddress)
  1376. continue;
  1377. if (GV.isDeclarationForLinker() || GV.getName().startswith("llvm.") ||
  1378. GV.isThreadLocal())
  1379. continue;
  1380. // Common symbols can't have aliases point to them, so they can't be tagged.
  1381. if (GV.hasCommonLinkage())
  1382. continue;
  1383. // Globals with custom sections may be used in __start_/__stop_ enumeration,
  1384. // which would be broken both by adding tags and potentially by the extra
  1385. // padding/alignment that we insert.
  1386. if (GV.hasSection())
  1387. continue;
  1388. Globals.push_back(&GV);
  1389. }
  1390. MD5 Hasher;
  1391. Hasher.update(M.getSourceFileName());
  1392. MD5::MD5Result Hash;
  1393. Hasher.final(Hash);
  1394. uint8_t Tag = Hash[0];
  1395. for (GlobalVariable *GV : Globals) {
  1396. Tag &= TagMaskByte;
  1397. // Skip tag 0 in order to avoid collisions with untagged memory.
  1398. if (Tag == 0)
  1399. Tag = 1;
  1400. instrumentGlobal(GV, Tag++);
  1401. }
  1402. }
  1403. void HWAddressSanitizer::instrumentPersonalityFunctions() {
  1404. // We need to untag stack frames as we unwind past them. That is the job of
  1405. // the personality function wrapper, which either wraps an existing
  1406. // personality function or acts as a personality function on its own. Each
  1407. // function that has a personality function or that can be unwound past has
  1408. // its personality function changed to a thunk that calls the personality
  1409. // function wrapper in the runtime.
  1410. MapVector<Constant *, std::vector<Function *>> PersonalityFns;
  1411. for (Function &F : M) {
  1412. if (F.isDeclaration() || !F.hasFnAttribute(Attribute::SanitizeHWAddress))
  1413. continue;
  1414. if (F.hasPersonalityFn()) {
  1415. PersonalityFns[F.getPersonalityFn()->stripPointerCasts()].push_back(&F);
  1416. } else if (!F.hasFnAttribute(Attribute::NoUnwind)) {
  1417. PersonalityFns[nullptr].push_back(&F);
  1418. }
  1419. }
  1420. if (PersonalityFns.empty())
  1421. return;
  1422. FunctionCallee HwasanPersonalityWrapper = M.getOrInsertFunction(
  1423. "__hwasan_personality_wrapper", Int32Ty, Int32Ty, Int32Ty, Int64Ty,
  1424. Int8PtrTy, Int8PtrTy, Int8PtrTy, Int8PtrTy, Int8PtrTy);
  1425. FunctionCallee UnwindGetGR = M.getOrInsertFunction("_Unwind_GetGR", VoidTy);
  1426. FunctionCallee UnwindGetCFA = M.getOrInsertFunction("_Unwind_GetCFA", VoidTy);
  1427. for (auto &P : PersonalityFns) {
  1428. std::string ThunkName = kHwasanPersonalityThunkName;
  1429. if (P.first)
  1430. ThunkName += ("." + P.first->getName()).str();
  1431. FunctionType *ThunkFnTy = FunctionType::get(
  1432. Int32Ty, {Int32Ty, Int32Ty, Int64Ty, Int8PtrTy, Int8PtrTy}, false);
  1433. bool IsLocal = P.first && (!isa<GlobalValue>(P.first) ||
  1434. cast<GlobalValue>(P.first)->hasLocalLinkage());
  1435. auto *ThunkFn = Function::Create(ThunkFnTy,
  1436. IsLocal ? GlobalValue::InternalLinkage
  1437. : GlobalValue::LinkOnceODRLinkage,
  1438. ThunkName, &M);
  1439. if (!IsLocal) {
  1440. ThunkFn->setVisibility(GlobalValue::HiddenVisibility);
  1441. ThunkFn->setComdat(M.getOrInsertComdat(ThunkName));
  1442. }
  1443. auto *BB = BasicBlock::Create(*C, "entry", ThunkFn);
  1444. IRBuilder<> IRB(BB);
  1445. CallInst *WrapperCall = IRB.CreateCall(
  1446. HwasanPersonalityWrapper,
  1447. {ThunkFn->getArg(0), ThunkFn->getArg(1), ThunkFn->getArg(2),
  1448. ThunkFn->getArg(3), ThunkFn->getArg(4),
  1449. P.first ? IRB.CreateBitCast(P.first, Int8PtrTy)
  1450. : Constant::getNullValue(Int8PtrTy),
  1451. IRB.CreateBitCast(UnwindGetGR.getCallee(), Int8PtrTy),
  1452. IRB.CreateBitCast(UnwindGetCFA.getCallee(), Int8PtrTy)});
  1453. WrapperCall->setTailCall();
  1454. IRB.CreateRet(WrapperCall);
  1455. for (Function *F : P.second)
  1456. F->setPersonalityFn(ThunkFn);
  1457. }
  1458. }
  1459. void HWAddressSanitizer::ShadowMapping::init(Triple &TargetTriple,
  1460. bool InstrumentWithCalls) {
  1461. Scale = kDefaultShadowScale;
  1462. if (TargetTriple.isOSFuchsia()) {
  1463. // Fuchsia is always PIE, which means that the beginning of the address
  1464. // space is always available.
  1465. InGlobal = false;
  1466. InTls = false;
  1467. Offset = 0;
  1468. WithFrameRecord = true;
  1469. } else if (ClMappingOffset.getNumOccurrences() > 0) {
  1470. InGlobal = false;
  1471. InTls = false;
  1472. Offset = ClMappingOffset;
  1473. WithFrameRecord = false;
  1474. } else if (ClEnableKhwasan || InstrumentWithCalls) {
  1475. InGlobal = false;
  1476. InTls = false;
  1477. Offset = 0;
  1478. WithFrameRecord = false;
  1479. } else if (ClWithIfunc) {
  1480. InGlobal = true;
  1481. InTls = false;
  1482. Offset = kDynamicShadowSentinel;
  1483. WithFrameRecord = false;
  1484. } else if (ClWithTls) {
  1485. InGlobal = false;
  1486. InTls = true;
  1487. Offset = kDynamicShadowSentinel;
  1488. WithFrameRecord = true;
  1489. } else {
  1490. InGlobal = false;
  1491. InTls = false;
  1492. Offset = kDynamicShadowSentinel;
  1493. WithFrameRecord = false;
  1494. }
  1495. }