1 //===-- x86ABIFixups.cpp ----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // C Includes
11 // C++ Includes
12 #include <set>
13 
14 // Other libraries and framework includes
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/IR/BasicBlock.h"
17 #include "llvm/IR/CallSite.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Instruction.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IRReader/IRReader.h"
24 #include "llvm/Pass.h"
25 
26 // Project includes
27 #include "lldb/Core/Log.h"
28 #include "lldb/Target/Process.h"
29 
30 using namespace lldb_private;
31 namespace
32 {
33 
34 bool
35 isRSAPICall(llvm::Module &module, llvm::CallInst *call_inst)
36 {
37     // TODO get the list of renderscript modules from lldb and check if
38     // this llvm::Module calls into any of them.
39     (void)module;
40     const auto func_name = call_inst->getCalledFunction()->getName();
41     if (func_name.startswith("llvm") || func_name.startswith("lldb"))
42         return false;
43 
44     if (call_inst->getCalledFunction()->isIntrinsic())
45         return false;
46 
47     return true;
48 }
49 
50 bool
51 isRSLargeReturnCall(llvm::Module &module, llvm::CallInst *call_inst)
52 {
53     // i686 and x86_64 returns for large vectors in the RenderScript API are not handled as normal
54     // register pairs, but as a hidden sret type. This is not reflected in the debug info or mangled
55     // symbol name, and the android ABI for x86 and x86_64, (as well as the emulators) specifies there is
56     // no AVX, so bcc generates an sret function because we cannot natively return 256 bit vectors.
57     // This function simply checks whether a function has a > 128bit return type. It is perhaps an
58     // unreliable heuristic, and relies on bcc not generating AVX code, so if the android ABI one day
59     // provides for AVX, this function may go out of fashion.
60     (void)module;
61     if (!call_inst || !call_inst->getCalledFunction())
62         return false;
63 
64     return call_inst->getCalledFunction()->getReturnType()->getPrimitiveSizeInBits() > 128;
65 }
66 
67 bool
68 isRSAllocationPtrTy(const llvm::Type *type)
69 {
70     if (!type->isPointerTy())
71         return false;
72     auto ptr_type = type->getPointerElementType();
73 
74     return ptr_type->isStructTy() && ptr_type->getStructName().startswith("struct.rs_allocation");
75 }
76 
77 bool
78 isRSAllocationTyCallSite(llvm::Module &module, llvm::CallInst *call_inst)
79 {
80     (void)module;
81     if (!call_inst->hasByValArgument())
82         return false;
83     for (const auto &param : call_inst->operand_values())
84         if (isRSAllocationPtrTy(param->getType()))
85             return true;
86     return false;
87 }
88 
89 llvm::FunctionType *
90 cloneToStructRetFnTy(llvm::CallInst *call_inst)
91 {
92     // on x86 StructReturn functions return a pointer to the return value, rather than the return
93     // value itself [ref](http://www.agner.org/optimize/calling_conventions.pdf section 6).
94     // We create a return type by getting the pointer type of the old return type, and inserting a new
95     // initial argument of pointer type of the original return type.
96     Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_LANGUAGE | LIBLLDB_LOG_EXPRESSIONS));
97 
98     assert(call_inst && "no CallInst");
99     llvm::Function *orig = call_inst->getCalledFunction();
100     assert(orig && "CallInst has no called function");
101     llvm::FunctionType *orig_type = orig->getFunctionType();
102     auto name = orig->getName();
103     if (log)
104         log->Printf("%s - cloning to StructRet function for '%s'", __FUNCTION__, name.str().c_str());
105 
106     unsigned num_params = orig_type->getNumParams();
107     std::vector<llvm::Type *> new_params{num_params + 1, nullptr};
108     std::vector<llvm::Type *> params{orig_type->param_begin(), orig_type->param_end()};
109 
110     // This may not work if the function is somehow declared void as llvm is strongly typed
111     // and represents void* with i8*
112     assert(!orig_type->getReturnType()->isVoidTy() && "Cannot add StructRet attribute to void function");
113     llvm::PointerType *return_type_ptr_type = llvm::PointerType::getUnqual(orig->getReturnType());
114     assert(return_type_ptr_type && "failed to get function return type PointerType");
115     if (!return_type_ptr_type)
116         return nullptr;
117 
118     if (log)
119         log->Printf("%s - return type pointer type for StructRet clone @ '0x%p':\n", __FUNCTION__,
120                     (void *)return_type_ptr_type);
121     // put the the sret pointer argument in place at the beginning of the argument list.
122     params.emplace(params.begin(), return_type_ptr_type);
123     assert(params.size() == num_params + 1);
124     return llvm::FunctionType::get(return_type_ptr_type, params, orig->isVarArg());
125 }
126 
127 bool
128 findRSCallSites(llvm::Module &module, std::set<llvm::CallInst *> &rs_callsites,
129                 bool (*predicate)(llvm::Module &, llvm::CallInst *))
130 {
131     bool found = false;
132 
133     for (auto &func : module.getFunctionList())
134         for (auto &block : func.getBasicBlockList())
135             for (auto &inst : block)
136             {
137                 llvm::CallInst *call_inst = llvm::dyn_cast_or_null<llvm::CallInst>(&inst);
138                 if (!call_inst || !call_inst->getCalledFunction())
139                     // This is not the call-site you are looking for...
140                     continue;
141                 if (isRSAPICall(module, call_inst) && predicate(module, call_inst))
142                 {
143                     rs_callsites.insert(call_inst);
144                     found = true;
145                 }
146             }
147     return found;
148 }
149 
150 bool
151 fixupX86StructRetCalls(llvm::Module &module)
152 {
153     bool changed = false;
154     // changing a basic block while iterating over it seems to have some undefined behaviour
155     // going on so we find all RS callsites first, then fix them up after consuming
156     // the iterator.
157     std::set<llvm::CallInst *> rs_callsites;
158     if (!findRSCallSites(module, rs_callsites, isRSLargeReturnCall))
159         return false;
160 
161     for (auto call_inst : rs_callsites)
162     {
163         llvm::FunctionType *new_func_type = cloneToStructRetFnTy(call_inst);
164         assert(new_func_type && "failed to clone functionType for Renderscript ABI fixup");
165 
166         llvm::CallSite call_site(call_inst);
167         llvm::Function *func = call_inst->getCalledFunction();
168         assert(func && "cannot resolve function in RenderScriptRuntime");
169         // Copy the original call arguments
170         std::vector<llvm::Value *> new_call_args(call_site.arg_begin(), call_site.arg_end());
171 
172         // Allocate enough space to store the return value of the original function
173         // we pass a pointer to this allocation as the StructRet param, and then copy its
174         // value into the lldb return value
175         llvm::AllocaInst *return_value_alloc =
176             new llvm::AllocaInst(func->getReturnType(), "var_vector_return_alloc", call_inst);
177         // use the new allocation as the new first argument
178         new_call_args.emplace(new_call_args.begin(), llvm::cast<llvm::Value>(return_value_alloc));
179         llvm::PointerType *new_func_ptr_type = llvm::PointerType::get(new_func_type, 0);
180         // Create the type cast from the old function type to the new one
181         llvm::Constant *new_func_cast =
182             llvm::ConstantExpr::getCast(llvm::Instruction::BitCast, func, new_func_ptr_type);
183         // create an allocation for a new function pointer
184         llvm::AllocaInst *new_func_ptr = new llvm::AllocaInst(new_func_ptr_type, "new_func_ptr", call_inst);
185         // store the new_func_cast to the newly allocated space
186         (void)new llvm::StoreInst(new_func_cast, new_func_ptr, "new_func_ptr_load_cast", call_inst);
187         // load the new function address ready for a jump
188         llvm::LoadInst *new_func_addr_load = new llvm::LoadInst(new_func_ptr, "load_func_pointer", call_inst);
189         // and create a callinstruction from it
190         llvm::CallInst *new_call_inst =
191             llvm::CallInst::Create(new_func_addr_load, new_call_args, "new_func_call", call_inst);
192         new_call_inst->setCallingConv(call_inst->getCallingConv());
193         new_call_inst->setTailCall(call_inst->isTailCall());
194         llvm::LoadInst *lldb_save_result_address = new llvm::LoadInst(return_value_alloc, "save_return_val", call_inst);
195 
196         // Now remove the old broken call
197         call_inst->replaceAllUsesWith(lldb_save_result_address);
198         call_inst->eraseFromParent();
199         changed = true;
200     }
201     return changed;
202 }
203 
204 bool
205 fixupRSAllocationStructByValCalls(llvm::Module &module)
206 {
207     // On x86_64, calls to functions in the RS runtime that take an `rs_allocation` type argument
208     // are actually handled as by-ref params by bcc, but appear to be passed by value by lldb (the callsite all use
209     // `struct byval`).
210     // On x86_64 Linux, struct arguments are transferred in registers if the struct size is no bigger than
211     // 128bits [ref](http://www.agner.org/optimize/calling_conventions.pdf) section 7.1 "Passing and returning objects"
212     // otherwise passed on the stack.
213     // an object of type `rs_allocation` is actually 256bits, so should be passed on the stack. However, code generated
214     // by bcc actually treats formal params of type `rs_allocation` as `rs_allocation *` so we need to convert the
215     // calling convention to pass by reference, and remove any hint of byval from formal parameters.
216     bool changed = false;
217     std::set<llvm::CallInst *> rs_callsites;
218     if (!findRSCallSites(module, rs_callsites, isRSAllocationTyCallSite))
219         return false;
220 
221     std::set<llvm::Function *> rs_functions;
222 
223     // for all call instructions
224     for (auto call_inst : rs_callsites)
225     {
226         // add the called function to a set so that we can strip its byval attributes in another pass
227         rs_functions.insert(call_inst->getCalledFunction());
228 
229         // get the function attributes
230         llvm::AttributeSet call_attribs = call_inst->getAttributes();
231 
232         // iterate over the argument attributes
233         for (size_t i = 1; i <= call_attribs.getNumSlots(); ++i)
234         {
235             // if this argument is passed by val
236             if (call_attribs.hasAttribute(i, llvm::Attribute::ByVal))
237             {
238                 // strip away the byval attribute
239                 call_inst->removeAttribute(i, llvm::Attribute::get(module.getContext(), llvm::Attribute::ByVal));
240                 changed = true;
241             }
242         }
243     }
244 
245     llvm::AttributeSet attr_byval = llvm::AttributeSet::get(module.getContext(), 1u, llvm::Attribute::ByVal);
246 
247     // for all called function decls
248     for (auto func : rs_functions)
249     {
250         // inspect all of the arguments in the call
251         llvm::SymbolTableList<llvm::Argument> &argList = func->getArgumentList();
252         for (auto &arg : argList)
253         {
254             if (arg.hasByValAttr())
255             {
256                 arg.removeAttr(attr_byval);
257                 changed = true;
258             }
259         }
260     }
261     return changed;
262 }
263 } // end anonymous namespace
264 
265 namespace lldb_private
266 {
267 namespace lldb_renderscript
268 {
269 
270 bool
271 fixupX86FunctionCalls(llvm::Module &module)
272 {
273     return fixupX86StructRetCalls(module);
274 }
275 
276 bool
277 fixupX86_64FunctionCalls(llvm::Module &module)
278 {
279     bool changed = false;
280     changed |= fixupX86StructRetCalls(module);
281     changed |= fixupRSAllocationStructByValCalls(module);
282     return changed;
283 }
284 
285 } // end namespace lldb_renderscript
286 } // end namespace lldb_private
287