<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 Sep 17, 2013, at 1:39 PM, Gábor Kozár <<a href="mailto:kozargabor@gmail.com">kozargabor@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">
<title></title>
<div><div>Hi Anna,<br></div>
<div> </div>
<blockquote><div><span class="highlight" style="background-color: rgb(255, 255, 255)"><span class="colour" style="color:rgb(31, 31, 31)"> Work on extending the analyzer core infrastructure to track value objects. We currently do not have time to work on this, but we could schedule an internal meeting to figure out a design for this if you are interested in pursuing this direction.</span></span><br></div>
</blockquote><div> </div>
<div>How difficult a job would this be?</div></div></blockquote><div><br></div><div>We don't really know, but it is unlikely to be very easy.</div><br><blockquote type="cite"><div><div> I'm absolutely open to this idea, but it's doubtful that my boss would authorize this (we're low on manpower), and I simply don't have the energy to work on this outside my working hours (I'm also an undergraduate university student with a full timetable).<br></div>
<div> </div></div></blockquote><blockquote type="cite"><div>
<blockquote type="cite"><div>Some hacky solution based on pattern matching the expression in the checker. For example, you'll need to identify the region created by v.begin() and deal with the fact that it is a temporary by pattern matching on the MaterializeTemporaryExpr. As with any pattern matching solutions, this will be brittle and might not work for all code. Something like this could work (I did not think it through very well):<br></div>
</blockquote><div> </div>
<div>This is similar to what I was considering attempting, but it just doesn't feel robust enough - i.e. there probably would be many corner-cases when this simply would not work - to be worth the effort.<br></div>
<div> </div>
<div>Thanks!</div>
<div> </div>
<div id="sig19426269"><div class="signature">-- <br></div>
<div class="signature"> Gábor Kozár -- ShdNx<br></div>
<div class="signature"> <a href="mailto:kozargabor@gmail.com">kozargabor@gmail.com</a><br></div>
<div class="signature"> </div>
</div>
<div> </div>
<div> </div>
<div>On Tue, Sep 17, 2013, at 18:25, Anna Zaks wrote:<br></div>
<blockquote type="cite"><div> </div>
<div><div>On Sep 14, 2013, at 7:44 AM, Gábor Kozár <<a href="mailto:kozargabor@gmail.com">kozargabor@gmail.com</a>> wrote:<br></div>
<div> </div>
<blockquote type="cite"><div><div>Hi Anna,<br></div>
<div> </div>
<blockquote><div>The last line is probably wrong.. You have "checkPostStmt on v.begin()" twice.<br></div>
</blockquote></div>
</blockquote><div>This is strange. Would be good to figure out what's going on.<br></div>
<blockquote type="cite"><div><div> </div>
<div>I get it every time I re-run the checker, so I'm positive this is what happens. It did look weird for me too, that's why I mentioned it. Note though that I'm using Clang 3.3, so it <i>might</i> be something that's been fixed since then.<br></div>
<div> </div>
<div>This is what I get for the bind:<br></div>
<div> </div>
<blockquote><div>Bind: val lazyCompoundVal{0x4cca6b0,temp_object{iterator,0x4bd1df0}} => loc &it<br></div>
<div> - in stmt: v.begin()<br></div>
<div> - stmt ast: CXXConstructExpr 0x4bd5d18 'std::vector<int>::iterator':'class __gnu_cxx::__normal_iterator<int *, class std::vector<int, class std::allocator<int> > >' 'void (class __gnu_cxx::__normal_iterator<int *, class std::vector<int, class std::allocator<int> > > &&) noexcept' elidable<br></div>
<div>`-MaterializeTemporaryExpr 0x4bd5bb8 'class __gnu_cxx::__normal_iterator<int *, class std::vector<int, class std::allocator<int> > >' xvalue<br></div>
<div> `-CXXMemberCallExpr 0x4bd1df0 'iterator':'class __gnu_cxx::__normal_iterator<int *, class std::vector<int, class std::allocator<int> > >'<br></div>
<div> `-MemberExpr 0x4bd1dc0 '<bound member function type>' .begin 0x4bc7600<br></div>
<div> `-DeclRefExpr 0x4bd1d28 'std::vector<int>':'class std::vector<int, class std::allocator<int> >' lvalue Var 0x4b96ee0 'v' 'std::vector<int>':'class std::vector<int, class std::allocator<int> >'<br></div>
</blockquote></div>
</blockquote><div>We expect the MaterializeTemporaryExpr to create a new region - it is "materializing" a temporary value into memory by creating a region to host it.<br></div>
</div>
<div><div> </div>
<blockquote type="cite"><div><div> </div>
<blockquote><div><div>The issue here is that the iterators are value objects, represented by
LazyCompoundVals and SVals, and we do not have any way of
persisting/tracking values right now.<br></div>
</div>
</blockquote><div><div> </div>
<div>The only case I'm concerning myself with right now is when iterators are saved into local variables, like so:<br></div>
<div> </div>
</div>
<blockquote><div><div>std::vector<int> v = { 1, 2, 3 };<br></div>
<div>auto it = v.begin();<br></div>
<div>v.push_back(10);<br></div>
<div>it++; // possibly invalid!<br></div>
</div>
</blockquote><div><div> </div>
<div>The variable 'it' will be represented by a memory region, and that it what I'm tracking, instead of the actual iterator value. I realize that this is limiting, but my understanding of the Clang Static Analyzer is rather limited and this is the best I could come up with. :)<br></div>
</div>
</div>
</blockquote><div> </div>
<div>Again, none of the checkers are trying to track value objects. They track regions (pointers). This checker is different.<br></div>
<div> </div>
<div>There are two approaches one could take:<br></div>
<div> </div>
<div> 1) Work on extending the analyzer core infrastructure to track value objects. We currently do not have time to work on this, but we could schedule an internal meeting to figure out a design for this if you are interested in pursuing this direction.<br></div>
<div> </div>
<div> 2) Some hacky solution based on pattern matching the expression in the checker. For example, you'll need to identify the region created by v.begin() and deal with the fact that it is a temporary by pattern matching on the MaterializeTemporaryExpr. As with any pattern matching solutions, this will be brittle and might not work for all code. Something like this could work (I did not think it through very well):<br></div>
<div> - You register a callback to intercept PostCall on v.begin(). Then check if the parent expr is a MaterializeTemporaryExpr. If yes, you store (callExpr, region) pair into a map. (You probably have another map that maps this region to vector state.)<br></div>
<div> - You register another callback for MaterializeTemporaryExpr. Here, you check if the child expression is one of the callExprs you have in the map, and if yes, you remove the pair from the map and associate the new region with the vector info.<br></div>
<div>The trickiest part here is ensuring that you clear out the state after you don't use the info anymore. For example, if you store info for every v.begin regardless weather it is wrapped in MaterializeTemporaryExpr or not, you might never clean out that info from the map.<br></div>
</div>
<div> </div>
<div>Jordan, I think this is similar to what we've discussed, but I might be missing/added some details..<br></div>
<div>Anna.<br></div>
<div><blockquote type="cite"><div><div><div> </div>
<div>Basically the only problem seems to be that I don't get consistent values representing the iterator: the result SVal from checkPostCall is different than the value that is actually bound to the variable. I guess I can try to work-around by implementing a kind of a state machine, and instead of starting to track iterator values in checkPostCall, I just record that a given Expr yields an iterator value, and look for that Expr in checkBind, but that sounds hackish and I don't see any reason why what I'm trying now shouldn't work.<br></div>
<div> </div>
<div>Thanks!<br></div>
<div> </div>
<div>-- <br></div>
<div> Gábor Kozár -- ShdNx<br></div>
<div> <a href="mailto:kozargabor@gmail.com">kozargabor@gmail.com</a><br></div>
<div> </div>
<div> </div>
</div>
<div> </div>
<div>On Thu, Sep 12, 2013, at 19:48, Anna Zaks wrote:<br></div>
<blockquote type="cite"><div> </div>
<div><div>On Sep 11, 2013, at 2:03 PM, Gábor Kozár <<a href="mailto:kozargabor@gmail.com">kozargabor@gmail.com</a>> wrote:<br></div>
<div> </div>
<blockquote type="cite"><div><div>I'm analyzing the following source line:<br></div>
<div> </div>
<div>auto it = v.begin(); // v is an std::vector<int><br></div>
<div> </div>
<div>As far as I'm able to reconstruct what happens using the checker callbacks, it goes somehow like this:<br></div>
<div> - checkPostCall on v.begin(): the call has been interpreted, the result is a lazyCompoundVal with a temp_object within it<br></div>
<div> - checkPostStmt on v.begin(): ProgramState::getSVal() yields the same result as above (the same lazyCompoundVal with the same temp_object within it)<br></div>
<div> - bind: some lazyCompoundVal with a temp_object within it to loc '&it' -- but this lazyCompoundVal and temp_object are different than above!!!<br></div>
<div> - checkPostStmt on v.begin() again: ProgramState::getSVal() yields &it, getting the SVal inside the region gives a lazyCompoundVal with 'it' in it<br></div>
</div>
</blockquote><div> </div>
<div>The last line is probably wrong.. You have "checkPostStmt on v.begin()" twice. Also, can you figure out which AST node is responsible for the bind?<br></div>
<div> </div>
<div>Basically, to understand what is going on better, it would be valuable to match the AST of the statements to the callbacks (you can get the AST with <span class="font" style="font-family:Menlo"><span class="size" style="font-size:11px">clang -cc1 -ast-dump</span></span>):<br></div>
<div><div style="margin: 0px; font-size: 11px; font-family: Menlo;"> `-DeclStmt 0x1023e7f00 <line:6:3, col:22><br></div>
<div style="margin: 0px; font-size: 11px; font-family: Menlo;"> `-VarDecl 0x1023cd780 <col:3, col:21> it 'class __gnu_cxx::__normal_iterator<int *, class std::vector<int, class std::allocator<int> > >':'class __gnu_cxx::__normal_iterator<int *, class std::vector<int, class std::allocator<int> > >'<br></div>
<div style="margin: 0px; font-size: 11px; font-family: Menlo;"> `-CXXConstructExpr 0x1023e7ec8 <col:13, col:21> 'class __gnu_cxx::__normal_iterator<int *, class std::vector<int, class std::allocator<int> > >':'class __gnu_cxx::__normal_iterator<int *, class std::vector<int, class std::allocator<int> > >' 'void (const class __gnu_cxx::__normal_iterator<int *, class std::vector<int, class std::allocator<int> > > &) throw()' elidable<br></div>
<div style="margin: 0px; font-size: 11px; font-family: Menlo;"> `-MaterializeTemporaryExpr 0x1023e7da8 <col:13, col:21> 'const class __gnu_cxx::__normal_iterator<int *, class std::vector<int, class std::allocator<int> > >' lvalue<br></div>
<div style="margin: 0px; font-size: 11px; font-family: Menlo;"> `-ImplicitCastExpr 0x1023e7d90 <col:13, col:21> 'const class __gnu_cxx::__normal_iterator<int *, class std::vector<int, class std::allocator<int> > >' <NoOp><br></div>
<div style="margin: 0px; font-size: 11px; font-family: Menlo;"> `-CXXMemberCallExpr 0x1023cd8a0 <col:13, col:21> 'iterator':'class __gnu_cxx::__normal_iterator<int *, class std::vector<int, class std::allocator<int> > >'<br></div>
<div style="margin: 0px; font-size: 11px; font-family: Menlo;"> `-MemberExpr 0x1023cd870 <col:13, col:15> '<bound member function type>' .begin 0x1023c66d0<br></div>
<div style="margin: 0px; font-size: 11px; font-family: Menlo;"> `-DeclRefExpr 0x1023cd7d8 <col:13> 'std::vector<int>':'class std::vector<int, class std::allocator<int> >' lvalue Var 0x1023bebc0 'v' 'std::vector<int>':'class std::vector<int, class std::allocator<int> >'<br></div>
<div> </div>
</div>
<div> </div>
<div>We've looked at implementing these checks before and it is not a simple problem. The issue here is that the iterators are value objects, represented by LazyCompoundVals and SVals, and we do not have any way of persisting/tracking values right now.<br></div>
</div>
<div> </div>
<div>Let's start with mapping the results of callbacks to the AST nodes to see if the modeling that we have now makes sense. <br></div>
<div> </div>
<div>Thanks,<br></div>
<div>Anna.<br></div>
<div> </div>
<div><blockquote type="cite"><div><div> </div>
<div>My problem is that I'm unable to implement a checker that would need to track iterator values, as temp_objects just seem to pop and disappear without leaving a trace and without having any followable connection between them.<br></div>
<div> </div>
<div>Is this working as intended, and if so, how should I approach this? I recall there being an open project for better modelling of C++ temporary objects, is that whose effect I'm seeing here?<br></div>
<div> </div>
<div>Thanks!<br></div>
<div> </div>
<div><div>-- <br></div>
<div> Gábor Kozár -- ShdNx<br></div>
<div> <a href="mailto:kozargabor@gmail.com">kozargabor@gmail.com</a><br></div>
<div> </div>
</div>
</div>
<div>
_______________________________________________<br></div>
<div>cfe-dev mailing list<br></div>
<div><a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br></div>
<div><a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br></div>
</blockquote></div>
<div> </div>
<div> </div>
</blockquote></div>
</blockquote></div>
<div> </div>
<div> </div>
</blockquote></div>
</blockquote></div><br></body></html>