sam djafari via llvm-dev
2018-Sep-20 20:37 UTC
[llvm-dev] Added AllocaInsts are relocated in stack
Hi there, I am wondering how I can prevent the LLVM from re-ordering the added local variables during instrumentation? Because, during the instrumentation, I add some metadata to some local variables, exactly next to it, and the generated bitcode looks good. However, when it is executed, basically the stack is formed as all original local variables are located next to each other, and then all the metadata is inserted. In other words, I was expecting to have "data, metadata, data, metadata", however, I was seeing "data, data, metadata, metadata" in the actual stack. Investigating into this problem, I realized that when printing the instruction instance (errs() << AI << "\n"), there is a numbering for each AllocaInst. However, the newly-added AllocaInsts, during instrumentation, are having a way higher number than the original AllocaInsts', and I guess that is why all the original local variables are first located in the stack, and then the metadata that I added are inserted into the stack. I am wondering how I can prevent LLVM from doing re-ordering them, or even reset the AllocaInst numbers, so the newly added AllocaInst can be inserted between the existing and original local variables? Look forward to hearing from you. Best regards, Saman -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180920/46d50b52/attachment.html>
Tim Northover via llvm-dev
2018-Sep-21 09:28 UTC
[llvm-dev] Added AllocaInsts are relocated in stack
On Thu, 20 Sep 2018 at 21:38, sam djafari via llvm-dev <llvm-dev at lists.llvm.org> wrote:> I am wondering how I can prevent LLVM from doing re-ordering them, or even reset the AllocaInst numbers, so the newly added AllocaInst can be inserted between the existing and original local variables?As far as I know you can't. LLVM makes no guarantees about stack layout at any point. What you almost certainly need to do is replace the original alloca with one big enough to contain both the data and the metadata. You can use ReplaceAllUsesWith directly on the new alloca for the existing uses, and then your metadata handling would GEP into it before access to get to the right part. Cheers. Tim.
sam djafari via llvm-dev
2018-Sep-21 13:05 UTC
[llvm-dev] Added AllocaInsts are relocated in stack
Hi Tim, Thanks for your reply. However, I have seen that addressSanitizer has done this by placing redzones around each local variable. But i have not figured out yet how they have done it, I was wondering if there is a switch or a method by which I can reset the slotNumbering given to each instruction. By doing so, LLVM would place them in the expected order I guess. Best regards, Saman On Fri, Sep 21, 2018 at 5:29 AM Tim Northover <t.p.northover at gmail.com> wrote:> On Thu, 20 Sep 2018 at 21:38, sam djafari via llvm-dev > <llvm-dev at lists.llvm.org> wrote: > > I am wondering how I can prevent LLVM from doing re-ordering them, or > even reset the AllocaInst numbers, so the newly added AllocaInst can be > inserted between the existing and original local variables? > > As far as I know you can't. LLVM makes no guarantees about stack > layout at any point. > > What you almost certainly need to do is replace the original alloca > with one big enough to contain both the data and the metadata. You can > use ReplaceAllUsesWith directly on the new alloca for the existing > uses, and then your metadata handling would GEP into it before access > to get to the right part. > > Cheers. > > Tim. >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180921/4ed04e58/attachment.html>