y liu via llvm-dev
2020-Apr-06 07:07 UTC
[llvm-dev] How to use llvm.addressofreturnaddress() function?
Hi, I want to check the return address of function. I use the following code to build pass. " FunctionType *func_type = FunctionType::get(ty_ptr, false); Function *func = Function::Create(func_type, GlobalValue::ExternalLinkage, "*llvm.addressofreturnaddress*", F->getParent()); IRBuilder<> Builder(inst); Builder.CreateCall(func); " When I compiled it, the following error message appeared: " Intrinsic name not mangled correctly for type arguments! Should be: llvm.addressofreturnaddress.p0i8 i8* ()* @llvm.addressofreturnaddress " So I made the following modification: " FunctionType *func_type = FunctionType::get(ty_ptr, false); Function *func = Function::Create(func_type, GlobalValue::ExternalLinkage, "l*lvm.addressofreturnaddress.p0i8*", F->getParent()); IRBuilder<> Builder(inst); Builder.CreateCall(func); " Compile through, I get the compiled so file. However, the following error occurred when I used it to optimize my code: " Intrinsic name not mangled correctly for type arguments! Should be: llvm.addressofreturnaddress.p0i8 i8* ()** @llvm.addressofreturnaddress.p0i8.1* in function main " I can't fix it. What should I do? Then, I tried another way. " CallInst *IRBuilderBase::CreateIntrinsic(Intrinsic::ID ID, 751 ArrayRef<Type *> Types, 752 ArrayRef<Value *> Args, 753 Instruction *FMFSource, 754 const Twine &Name) { 755 Module *M = BB->getModule(); 756 Function *Fn = Intrinsic::getDeclaration(M, ID, Types); 757 return createCallHelper(Fn, Args, this, Name, FMFSource); 758 } " I use the following code. " Function *Fn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::addressofreturnaddress); " But, " Stack dump: 0. Program arguments: opt -load ../build/lib/LLVMMyPass.so -hello 1. Running pass 'Function Pass Manager' on module '<stdin>'. 2. Running pass 'Hello World Pass' on function '@_Z4vulnv' #0 0x0000563d2d976bba llvm::sys::PrintStackTrace(llvm::raw_ostream&) (/opt/llvm/bin/opt+0x2458bba) #1 0x0000563d2d974954 llvm::sys::RunSignalHandlers() (/opt/llvm/bin/opt+0x2456954) #2 0x0000563d2d974a92 SignalHandler(int) (/opt/llvm/bin/opt+0x2456a92) #3 0x00007fce4a662890 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12890) #4 0x0000563d2d238bd4 DecodeFixedType(llvm::ArrayRef<llvm::Intrinsic::IITDescriptor>&, llvm::ArrayRef<llvm::Type*>, llvm::LLVMContext&) (/opt/llvm/bin/opt+0x1d1abd4) #5 0x0000563d2d23b576 llvm::Intrinsic::getType(llvm::LLVMContext&, llvm::Intrinsic::ID, llvm::ArrayRef<llvm::Type*>) (/opt/llvm/bin/opt+0x1d1d576) #6 0x0000563d2d240ab5 llvm::Intrinsic::getDeclaration(llvm::Module*, llvm::Intrinsic::ID, llvm::ArrayRef<llvm::Type*>) (/opt/llvm/bin/opt+0x1d22ab5) #7 0x00007fce492f095a (anonymous namespace)::Hello::runOnFunction(llvm::Function&) (../build/lib/LLVMMyPass.so+0x195a) #8 0x0000563d2d27db08 llvm::FPPassManager::runOnFunction(llvm::Function&) (/opt/llvm/bin/opt+0x1d5fb08) #9 0x0000563d2d27e1f1 llvm::FPPassManager::runOnModule(llvm::Module&) (/opt/llvm/bin/opt+0x1d601f1) #10 0x0000563d2d27ce61 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/opt/llvm/bin/opt+0x1d5ee61) #11 0x0000563d2bc5c193 main (/opt/llvm/bin/opt+0x73e193) #12 0x00007fce49513b97 __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:344:0 #13 0x0000563d2bce06ba _start (/opt/llvm/bin/opt+0x7c26ba) Segmentation fault (core dumped) " I can't understand it. I tried to use "returnaddress" to replace "addressofreturnaddress". It's OK! They are all Intrinsic function. Why? Plese help me out. Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200406/c16a4e60/attachment.html>
Eli Friedman via llvm-dev
2020-Apr-06 17:18 UTC
[llvm-dev] How to use llvm.addressofreturnaddress() function?
Two issues: 1. You usually want to use Module:: getOrInsertFunction to construct a function declaration, so you get the existing declaration if it already exists. This should avoid the “.1” suffix. 2. If you’re using Intrinsic::getDeclaration to get an intrinsic declaration, you have to pass the overloaded type (an i8*) as the third argument to the function. -Eli From: llvm-dev <llvm-dev-bounces at lists.llvm.org> On Behalf Of y liu via llvm-dev Sent: Monday, April 6, 2020 12:08 AM To: llvm-dev at lists.llvm.org Subject: [EXT] [llvm-dev] How to use llvm.addressofreturnaddress() function? Hi, I want to check the return address of function. I use the following code to build pass. " FunctionType *func_type = FunctionType::get(ty_ptr, false); Function *func = Function::Create(func_type, GlobalValue::ExternalLinkage, "llvm.addressofreturnaddress", F->getParent()); IRBuilder<> Builder(inst); Builder.CreateCall(func); " When I compiled it, the following error message appeared: " Intrinsic name not mangled correctly for type arguments! Should be: llvm.addressofreturnaddress.p0i8 i8* ()* @llvm.addressofreturnaddress " So I made the following modification: " FunctionType *func_type = FunctionType::get(ty_ptr, false); Function *func = Function::Create(func_type, GlobalValue::ExternalLinkage, "llvm.addressofreturnaddress.p0i8", F->getParent()); IRBuilder<> Builder(inst); Builder.CreateCall(func); " Compile through, I get the compiled so file. However, the following error occurred when I used it to optimize my code: " Intrinsic name not mangled correctly for type arguments! Should be: llvm.addressofreturnaddress.p0i8 i8* ()* @llvm.addressofreturnaddress.p0i8.1 in function main " I can't fix it. What should I do? Then, I tried another way. " CallInst *IRBuilderBase::CreateIntrinsic(Intrinsic::ID ID, 751 ArrayRef<Type *> Types, 752 ArrayRef<Value *> Args, 753 Instruction *FMFSource, 754 const Twine &Name) { 755 Module *M = BB->getModule(); 756 Function *Fn = Intrinsic::getDeclaration(M, ID, Types); 757 return createCallHelper(Fn, Args, this, Name, FMFSource); 758 } " I use the following code. " Function *Fn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::addressofreturnaddress); " But, " Stack dump: 0. Program arguments: opt -load ../build/lib/LLVMMyPass.so -hello 1. Running pass 'Function Pass Manager' on module '<stdin>'. 2. Running pass 'Hello World Pass' on function '@_Z4vulnv' #0 0x0000563d2d976bba llvm::sys::PrintStackTrace(llvm::raw_ostream&) (/opt/llvm/bin/opt+0x2458bba) #1 0x0000563d2d974954 llvm::sys::RunSignalHandlers() (/opt/llvm/bin/opt+0x2456954) #2 0x0000563d2d974a92 SignalHandler(int) (/opt/llvm/bin/opt+0x2456a92) #3 0x00007fce4a662890 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12890) #4 0x0000563d2d238bd4 DecodeFixedType(llvm::ArrayRef<llvm::Intrinsic::IITDescriptor>&, llvm::ArrayRef<llvm::Type*>, llvm::LLVMContext&) (/opt/llvm/bin/opt+0x1d1abd4) #5 0x0000563d2d23b576 llvm::Intrinsic::getType(llvm::LLVMContext&, llvm::Intrinsic::ID, llvm::ArrayRef<llvm::Type*>) (/opt/llvm/bin/opt+0x1d1d576) #6 0x0000563d2d240ab5 llvm::Intrinsic::getDeclaration(llvm::Module*, llvm::Intrinsic::ID, llvm::ArrayRef<llvm::Type*>) (/opt/llvm/bin/opt+0x1d22ab5) #7 0x00007fce492f095a (anonymous namespace)::Hello::runOnFunction(llvm::Function&) (../build/lib/LLVMMyPass.so+0x195a) #8 0x0000563d2d27db08 llvm::FPPassManager::runOnFunction(llvm::Function&) (/opt/llvm/bin/opt+0x1d5fb08) #9 0x0000563d2d27e1f1 llvm::FPPassManager::runOnModule(llvm::Module&) (/opt/llvm/bin/opt+0x1d601f1) #10 0x0000563d2d27ce61 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/opt/llvm/bin/opt+0x1d5ee61) #11 0x0000563d2bc5c193 main (/opt/llvm/bin/opt+0x73e193) #12 0x00007fce49513b97 __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:344:0 #13 0x0000563d2bce06ba _start (/opt/llvm/bin/opt+0x7c26ba) Segmentation fault (core dumped) " I can't understand it. I tried to use "returnaddress" to replace "addressofreturnaddress". It's OK! They are all Intrinsic function. Why? Plese help me out. Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200406/0dcff015/attachment.html>