<html><head><meta http-equiv="Content-Type" content="text/html charset=iso-8859-1"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Mar 14, 2013, at 23:08 , Karthik Bhat <<a href="mailto:blitz.opensource@gmail.com">blitz.opensource@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Thanks Jordan. I will try to upload my changes so that you can access the same.<div><br></div><div>I have a small doubt. Please excuse me if it sounds a bit silly as i'm new to clang static analyzer.</div><div><br></div>
<div>How do i load the external AST file in the same ASTContext as the current compiler instance ASTContext?</div><div><br></div><div>I'm currently using LoadFromASTFile to get the ASTUnit. Did you mean i need to use ASTImporter class to import the Decl in this AST into current AST( i.e. CI.getASTContext()) ?</div></blockquote><div><br></div><div>I'm not entirely familiar with it myself; I mentioned it to Doug Gregor yesterday and he said the usual way would be to use ASTReader directly, rather than going through LoadFromASTFile. I'm not sure what happens if two files have the same static variable, though; they might get merged, and that would be Bad.</div><div><br></div><div>Several years ago there was an experimental project (efforts led by either Ted Kremenek or Zhongxing Xu) to do something similar to what you're doing, with multiple ASTContexts. That tended not to work very well because (1) it turns out much of the analyzer depends on the invariants that only hold within a single ASTContext (e.g. canonical types can be compared by pointer identity), and (2) the cost of marshalling when going between AST contexts was a significant performance hit.</div><div><br></div><div>For your actual example, I'm guessing you're handling arguments but not return values. I'm not exactly sure what would be wrong here, but return value processing is handled in ExprEngine::processCallExit, so that might be a place to start looking.</div><div><br></div><div>Jordan</div><div><br></div><br><blockquote type="cite"><div>Currently the code flow looks like-<br><div><br></div><div>1. In getRuntimeDefinition() if a function body is null i'm using LoadFromASTFile to get the ASTUnit (Unit).</div><div>    ( e.g. ASTUnit* Unit1 =  ASTUnit::LoadFromASTFile(funcDeclFile,Diags1,FileSystemOpt1); )</div>
<div><br></div><div>2. Get the TranslationUnitDecl from ASTUnit and traverse it to get the appropriate FunctionDecl.</div><div><br></div><div>3. Use this FunctionDecl to create and return the RuntimeDefinition object.</div>
<div><br></div><div>4. I'm also storing the corresponding SourceManager and propagating it  so that the Diagnostic Engine can use this SourceManager to decode the SourceLocation correctly which might be present in the loaded file.</div>
<div><br><br>P.S. I'm able to detect and report double free properly now across TU. I had made a small mistake in code. But i'm getting a lot of false alarm in case 1 mentioned above were it is reporting memory leak even though the allocated memory is freed by the called function. i.e.-</div>
<div><br></div><div><div><b>File2.c has -</b></div><div><br></div><div>char* mymalloc()</div><div>{</div><div>  char* kar = (char*) malloc(sizeof(char));</div><div>  return kar;</div><div>}</div></div><div><br></div><div>
<b>File1.c has-</b></div><div><br></div><div>extern char* mymalloc();</div><div><div>int main()</div><div>{</div><div><br></div><div>  char* q = mymalloc();</div><div>  free(q);</div><div>  return 0;</div><div>}</div><div>
<br></div><div>In the above code during processCallExit in ExprEngine Core marks symbol for variable kar in mymalloc as dead although the MemoryRegion is referred in File1.c  after aliasing it to q.</div><div><br></div><div>
Any inputs appreciated and thanks for your prompt replies.</div><div><br></div><div>Regards</div><div>Karthik</div><div><br></div><br><div class="gmail_quote">On Thu, Mar 14, 2013 at 11:17 PM, Jordan Rose <span dir="ltr"><<a href="mailto:jordan_rose@apple.com" target="_blank">jordan_rose@apple.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>I think I'd have to see your code to be sure. One thing that's very important is that you load the external AST file in the same ASTContext. Other than that, though, your approach does seem reasonable.</div>
<div><br></div><div>Can you post your modifications somewhere? I'll try to look over them when I have time and see if I see anything obviously strange.</div><span class="HOEnZb"><font color="#888888"><div>Jordan</div>
</font></span><div><div class="h5"><div><br></div><br><div><div>On Mar 13, 2013, at 3:51 , Karthik Bhat <<a href="mailto:blitz.opensource@gmail.com" target="_blank">blitz.opensource@gmail.com</a>> wrote:</div><br><blockquote type="cite">
Thanks Jordan. That was useful information. I also found  <b>"</b><span style="font-family:Arial,Helvetica,sans-serif;font-size:1em">A memory model for static analysis of C programs<b>" </b>by Ted also very useful.</span><div>

<br></div><div>I'm trying to implement IPA across TU for my internal project. The approach i'm using is i'm dumping .ast of all files using -emit-ast option. </div><div>In case a function body is not found while analyzing i load the corresponding ast file having function definition and use this FunctionDecl with the current in-lining approach.</div>

<div><br></div><div>For example in the above code myFree()  is implemented in another file and i get the FunctionDecl by loading the ast and use this to inline. </div><div><br></div><div>In this case though unfortunately region for 'q' does not contain a binding with the symbolic value returned from malloc. </div>

<div><br></div><div>Am i doing something wrong here or missing out something?</div><div><br></div><div>Any inputs greatly appreciated.</div><div><br></div><div>Thanks</div><div><br></div><div><div class="gmail_quote">On Tue, Mar 12, 2013 at 10:09 PM, Jordan Rose <span dir="ltr"><<a href="mailto:jordan_rose@apple.com" target="_blank">jordan_rose@apple.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello, Karthik. The analyzer does certain deliberate passes to clean up dead symbols and dead bindings in the state, generally before processing each statement. The top-level function for this is ExprEngine::ExprEngine::removeDead, and most of the high-level implementation is in the SymbolReaper class in ProgramState.h. The basic algorithm is pretty simple, though:<br>


<br>
(1) Find out which expressions and variables are still live (LiveVariables). This is cached, per-function, context-insensitive information.<br>
(2) Ask checkers which symbols are known to be in use, though potentially not live (checkLiveSymbols).<br>
(3) Mark live any values associated with live expressions in the Environment. Remove all other bindings.<br>
(4) Mark live any values accessible via the live regions in the Store. Remove all other bindings.<br>
(5) Remove any constraints on dead symbols.<br>
(6) Report dead symbols to the checkers, so that they can stop tracking information dependent on those symbols (checkDeadSymbols).<br>
<br>
<br>
Your second question is easier: parameter-passing is modeled as a bind to the region for the parameter (a VarRegion whose associated declaration is a ParmVarDecl). So your myFree() example is essentially the same as this:<br>


<br>
char *q = (char *)malloc(sizeof(char);<br>
char *p = q;<br>
free(p);<br>
free(q); // warning<br>
<br>
This happens in the 'enterStackFrame' method on the StoreManager, which uses CallEvent::getInitialStackFrameContents to figure out the initial bindings. You are correct that both the region for 'q' and the region for 'p' will contain a binding with the symbolic value returned from 'malloc'.<br>


<br>
Does this help?<br>
Jordan<br>
<br>
P.S. For the record, this model of parameter passing will not be entirely correct once we model the destructors of C++ temporary regions; we will need to be more careful about non-POD objects being passed by value.<br>
<div><br>
<br>
On Mar 11, 2013, at 1:09 , Karthik Bhat <<a href="mailto:blitz.opensource@gmail.com" target="_blank">blitz.opensource@gmail.com</a>> wrote:<br>
<br>
> Hi All,<br>
> I was going through Malloc checker in clang Static analyzer. I had a few doubts-<br>
><br>
> 1) How is a symbol marked as dead( How does clang static analyzer detect that a symbol is dead) ?<br>
> E.g.<br>
><br>
> char* myMalloc()<br>
> {<br>
>   char* p = (char*) malloc(sizeof(char));<br>
>   return p;<br>
> }<br>
><br>
> int main()<br>
> {<br>
>   char* q = myMalloc();<br>
>   return 0;<br>
> }<br>
><br>
> In the above example symbol assigned for p in myMalloc is alive till main(caller) exits right?<br>
><br>
><br>
> 2) In case of IPA how are symbol propagated form one function to another in case it is passed as a parameter.<br>
> E.g.<br>
><br>
> void myFree(char* p)<br>
> {<br>
>    free(p);<br>
> }<br>
><br>
> int main()<br>
> {<br>
><br>
>   char* q = (char*) malloc(sizeof(char));<br>
>   myFree(q);<br>
>   free(q);<br>
> }<br>
><br>
> In the above example is it true that the parameter p in myFree is assigned the same symbol as that of q being passed to the function? If yes could someone guide me were this assignment of symbol happens?<br>
><br>
><br>
> Thanks<br>
> Karthik<br>
><br>
><br>
</div>> _______________________________________________<br>
> cfe-dev mailing list<br>
> <a href="mailto:cfe-dev@cs.uiuc.edu" target="_blank">cfe-dev@cs.uiuc.edu</a><br>
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
<br>
</blockquote></div><br></div>
</blockquote></div><br></div></div></div></blockquote></div><br></div></div>
</blockquote></div><br></body></html>