llvm.org GIT mirror llvm / 405de2b
hwasan: Use llvm.read_register intrinsic to read the PC on aarch64 instead of taking the function's address. This shaves an instruction (and a GOT entry in PIC code) off prologues of functions with stack variables. Differential Revision: https://reviews.llvm.org/D63472 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@364608 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 3 months ago
2 changed file(s) with 22 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
209209 SmallVectorImpl &Allocas,
210210 DenseMap> &AllocaDeclareMap,
211211 SmallVectorImpl &RetVec, Value *StackTag);
212 Value *readRegister(IRBuilder<> &IRB, StringRef Name);
212213 bool instrumentLandingPads(SmallVectorImpl &RetVec);
213214 Value *getNextTagWithCall(IRBuilder<> &IRB);
214215 Value *getStackBaseTag(IRBuilder<> &IRB);
934935 StackBaseTag = IRB.CreateAShr(ThreadLong, 3);
935936
936937 // Prepare ring buffer data.
937 auto PC = IRB.CreatePtrToInt(F, IntptrTy);
938 Value *PC;
939 if (TargetTriple.getArch() == Triple::aarch64)
940 PC = readRegister(IRB, "pc");
941 else
942 PC = IRB.CreatePtrToInt(F, IntptrTy);
938943 auto GetStackPointerFn =
939944 Intrinsic::getDeclaration(F->getParent(), Intrinsic::frameaddress);
940945 Value *SP = IRB.CreatePtrToInt(
980985 LocalDynamicShadow = IRB.CreateIntToPtr(LocalDynamicShadow, Int8PtrTy);
981986 }
982987
988 Value *HWAddressSanitizer::readRegister(IRBuilder<> &IRB, StringRef Name) {
989 Module *M = IRB.GetInsertBlock()->getParent()->getParent();
990 Function *ReadRegister =
991 Intrinsic::getDeclaration(M, Intrinsic::read_register, IntptrTy);
992 MDNode *MD = MDNode::get(*C, {MDString::get(*C, Name)});
993 Value *Args[] = {MetadataAsValue::get(*C, MD)};
994 return IRB.CreateCall(ReadRegister, Args);
995 }
996
983997 bool HWAddressSanitizer::instrumentLandingPads(
984998 SmallVectorImpl &LandingPadVec) {
985 Module *M = LandingPadVec[0]->getModule();
986 Function *ReadRegister =
987 Intrinsic::getDeclaration(M, Intrinsic::read_register, IntptrTy);
988 const char *RegName =
989 (TargetTriple.getArch() == Triple::x86_64) ? "rsp" : "sp";
990 MDNode *MD = MDNode::get(*C, {MDString::get(*C, RegName)});
991 Value *Args[] = {MetadataAsValue::get(*C, MD)};
992
993999 for (auto *LP : LandingPadVec) {
9941000 IRBuilder<> IRB(LP->getNextNode());
995 IRB.CreateCall(HWAsanHandleVfork, {IRB.CreateCall(ReadRegister, Args)});
1001 IRB.CreateCall(
1002 HWAsanHandleVfork,
1003 {readRegister(IRB, (TargetTriple.getArch() == Triple::x86_64) ? "rsp"
1004 : "sp")});
9961005 }
9971006 return true;
9981007 }
5959
6060 ; CHECK-NOHISTORY-NOT: store i64
6161
62 ; CHECK-HISTORY: call i64 @llvm.read_register.i64(metadata [[MD:![0-9]*]])
6263 ; CHECK-HISTORY: %[[PTR:[^ ]*]] = inttoptr i64 %[[D]] to i64*
6364 ; CHECK-HISTORY: store i64 %{{.*}}, i64* %[[PTR]]
6465 ; CHECK-HISTORY: %[[D1:[^ ]*]] = ashr i64 %[[D]], 56
8182 call void @use(i32* %x)
8283 ret void
8384 }
85
86 ; CHECK-HISTORY: [[MD]] = !{!"pc"}