[llvm-dev] Possible to query type information from a malloc in optimized codes

Stephen Kell via llvm-dev llvm-dev at lists.llvm.org
Mon Jul 23 03:54:28 PDT 2018


> I am working on a pass that tries to extract type information from,
> say, all malloc statements in LLVM-IR (source language is C).
> 
> For debug code, this can be achieved by looking up the respective
> bitcast instruction and extracting the type from it.

> However, in optimized code, the LLVM-IR omits these direct bitcasts
> in different scenarios (see example after the question).
> 
> My question now, is there any way to use, e.g., debug data or some
> use-def search to reliably extract the correct type information for
> such a malloc?

Hi Alexander. Not an LLVM-flavoured answer, but in case it's useful,
this is something that the tooling from my liballocs project can do for
C source code. <https://github.com/stephenrkell/liballocs>

Looking at bitcasts is at best heuristic since even in debug code there
need not be a bitcast in all circumstances. My approach -- also
heuristic, I admit -- has been to analyse the use of "sizeof" in C
source code. This works pretty well, with the caveat that if you have
malloc wrappers in the mix, since the sizeof occurs at the wrapper
call, not the malloc call, you have to declare such wrappers to the
tool.

(I agree with you that allocation sites could usefully be described in
debugging information; at present I'm not aware of any toolchains that
do this.)

Feel free to mail me off-list if you have questions about
building/using liballocs... it's not mega-friendly as yet, though I am
interested in improving that. 

Stephen


More information about the llvm-dev mailing list