[cfe-dev] source-to-source transformation to insert instrumentation calls

Jan Smets jan.smets at alcatel-lucent.com
Mon Sep 24 08:28:15 PDT 2012

On 24/09/2012 16:48, John Criswell wrote:
> On 9/23/12 12:34 PM, Jan Smets wrote:
>> Hi
>> I'm trying to use CLANG to build a standalone source-to-source
>> transformation tool. My first goal is to insert instrumentation calls
>> for every memory access (wherever possible).
>> <snip>
>> How would you advice me to find the right location where I can insert
>> the call ?  Are there any other ways I can do this? Perhaps using the
>> MatchFinder is not the best way to do this?
>> Ultimately I would like to create a source-to-source transformation
>> tool that applies the StackGuard principle and can move stack
>> variables to the heap. (similar to what CIL does.)
> First, why do you want to do a source-to-source level transformation
> instead of an LLVM IR transformation?  If it's because you need to feed
> the transformed source into a C compiler for a special hardware target,
> it may be easier to write an LLVM IR transform and to get the C backend
> up and running again.
Our main platform is MIPS and we're not convinced that the LLVM (mips) 
code generation is mature and well tested enough. Unfortunately I don't 
have the resources to get that done.
I think a source-to-source transformation tool with CLANG is the only 
short term solution.

> If you can explain why you need a source-to-source transform, someone on
> the list may be able to provide ideas for a workable solution.

I have changed my test code to a RecursiveASTVisitor and I have 
DeclRefExpr Visitor. Now I'm still looking for a way to determine a good 
location where I can insert my call.
Should I walk up the AST tree until I find some sort of Statement and 
then use that location to insert the call ?  Would that be a good place?
How do I walk up the tree ?

> Second, as an FYI, SAFECode has a pass that will promote potentially
> escaping stack allocations into heap allocations.  The transform hasn't
> been updated to LLVM mainline yet, but doing so should be relatively easy.
> -- John T.
>> I'm aware of other tools like Asan but it's not the implementation I'm
>> looking for.
>> Thank you
>>   - Jan

More information about the cfe-dev mailing list