<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 Jan 19, 2013, at 21:09 , Branden Archer <<a href="mailto:b.m.archer4@gmail.com">b.m.archer4@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Attached are the most recent version of the two patches. (By the way, is there a website set up for conducting code reviews for clang? If so, maybe in the future that would be more convenient and easier to use that instead of many emails).<br>
<br>Jordan, I think you were looking at an older patch. Some of your comments are probably already resolved. However, just in case...<br><br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">
<div><div>+</div><div>+ if ( offset.isValid()</div><div>+ && !offset.hasSymbolicOffset()</div><div>+ && offset.getOffset() != 0) {</div><div>+ os << "; the memory location passed is an offset of an allocated location";</div>
<div>+ }</div><div>+</div></div></blockquote><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote"><br></blockquote><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">
<div>...this test is not correct; it will fire for free(&local[1]).<br></div></blockquote><div> </div>I do not agree. My understanding is that local stack variables would fail the following test in ReportBadFree:<br><br>
<div style="margin-left:40px">const MemRegion *MR = ArgVal.getAsRegion();<br> if (MR) {<br></div></blockquote><div><br></div><div>All memory is represented by MemRegions. '&local[1]' translates to an ElementRegion whose super-region is a VarRegion.</div><div><br></div><div><br></div><br><blockquote type="cite">which protects the code you mention above. In FreeMemAux, before ReportBadFree is called the symbol is confirmed to be known malloc'ed memory (which did not appear in earlier patches). Just to be sure, I do have a unit test which tests just this:<br>
<br><div style="margin-left:40px">void testFreeNonMallocPointerWithOffset() {<br> char c;<br> char * r = &c;<br> r = r + 10;<br> free(r-10); // expected-warning {{Argument to free() is the address of the local variable 'c', which is not memory allocated by malloc()}}<br>
}</div></blockquote><div><br></div><div>This doesn't actually test the case I was talking about, which is where you pass an <i>offset</i> of a local variable:</div><div><br></div><div>char c;</div><div>char *r = &c;</div><div>free(r + 1);</div><div><br></div><div>I believe that will fire, but I admit I haven't built clang with your new warning.</div><div><br></div><br><blockquote type="cite"><br><blockquote style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex; position: static; z-index: auto; font-size: 10px; " class="gmail_quote"><font size="4" style="font-size: 12px; ">Given that the message isn't great either, and that
you've already established what kind of value is being freed, I think
you should just make a new helper function to report the error,
ReportOffsetFree or something. Then you can make a nicer error message,
like "Argument to free() is an offset of an allocation". (I don't think
this message is perfect either -- it doesn't mention malloc(), "an
offset" isn't really the correct term -- but at least it's more
concise.)</font><br></blockquote><br>The reason I added to ReportBadFree instead of making another function was I believed that most of the code would be identical. Additionally, ReportBadFree also reports if the free is performed on malloc'ed data or not, which I believed would be useful in a message. If you feel that the message should be more clear, I can add another function, ReportOffsetFree, for it.<font size="4"><br></font></blockquote><div><br></div><div>Yeah, I thought the same at the start, but since the "offset" message is only being reported for memory that is already known to be malloc()'d, I think most of the benefits of code reuse are lost.</div><div><br></div><div><br></div><br><blockquote type="cite"><blockquote style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex; position: static; z-index: auto; " class="gmail_quote"><font size="4"><div style="font-size: 12px; ">+void freeOffsetPointerPassedToFunction()</div>
<div style="font-size: 12px; ">+{</div><div style="font-size: 12px; ">+ int *p = malloc(sizeof(int)*2);</div><div style="font-size: 12px; ">+ p[1] = 0;</div><div style="font-size: 12px; ">+ p += 1;</div><div style="font-size: 12px; ">+ myfooint(*p); // not passing the pointer, only a value pointed by pointer</div><div style="font-size: 12px; ">+
free(p); // expected-warning {{Argument to free() is not memory
allocated by malloc(); the memory location passed is an offset of an
allocated location}}</div><div style="font-size: 12px; ">+}</div></font><br style="font-size: 10px; "><font size="4" style="font-size: 12px; "><div>A better test would be to pass 'p' as a <i>const</i> pointer, which means the contents of the region get invalidated but 'p' itself will not.</div>
</font></blockquote><br>I am not clear on what you mean. Pass 'p' as a const pointer to free? The const will be ignored in that case. Or maybe you mean myfooint? That accepts an integer and not a pointer. Maybe you were referring to something different, maybe passing 'p' to a function that takes a const pointer. I have added another test case that tests for this, called testOffsetPassedAsConst.<br></blockquote><div><br></div><div>Thanks, the latter is what I meant. The test for passing *p doesn't do much based on how the analyzer core works right now, but it's still a good completeness test.</div><div><br></div><div><br></div><br><blockquote type="cite"><blockquote style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex; position: static; z-index: auto; " class="gmail_quote"><span style="font-size:15px"><div style="font-size: 13px; ">- if (Call && doesNotFreeMemory(Call, State))</div>
<div style="font-size: 13px; ">+ if ((Kind == PSK_DirectEscapeOnCall ||</div><div style="font-size: 13px; ">+ Kind == PSK_IndirectEscapeOnCall) &&</div><div style="font-size: 13px; ">+ doesNotFreeMemory(Call, State)) {</div></span><br style="font-size: 10px; "><span style="font-size: 13px; ">
This
is not correct. Before, this branch was only taken if the Kind
is PSK_DirectEscapeOnCall. Indirect escapes can still possibly free
memory (although it's unlikely).</span><br style="font-size: 10px; "><span style="font-size: 13px; "><div>- if (Call && guaranteedNotToCloseFile(*Call))</div><div>+ if ((Kind == PSK_DirectEscapeOnCall ||</div><div>+ Kind == PSK_IndirectEscapeOnCall) &&</div>
<div>+ guaranteedNotToCloseFile(*Call)) {</div></span><br style="font-size: 10px; "><span style="font-size: 13px; ">Ditto.</span></blockquote><br>Anna mentioned that the behavior change is intended for the malloc checker. However, I did not consider the behavior change for the simple stream checker. In the attached patch, I have changed the condition to only check for direct escape.<br>
</blockquote><div><br></div><div>Whoops, clearly I have trouble reasoning about this part. I think I understand Anna's explanation now: it's unlikely to have a <i>known, system </i>API that will <i>not</i> free (or cause to escape) a top-level argument, but <i>will</i> free (or cause to escape) some indirectly-accessible region, and that applies to stream handles as well.</div><div><br></div><div>(The reason I was confused is because of RetainCountChecker, because the Cocoa memory conventions specify that direct arguments will not have their retain counts messed with, but stuff accessed through them may be fair game.)</div><div><br></div><div>A couple of style things are still a bit off from LLVM conventions: some &&s are still leading instead of trailing, and some nested conditions are not properly lined up with parentheses. (If you have to wrap, the next line should be indented to one character past the open paren.) Also, I thought of yet another way to write the implication assertion:</div><div><br></div><div>if (Kind == PSK_DirectEscapeOnCall || Kind == PSK_IndirectEscapeOnCall)</div><div> assert(Call);</div><div><br></div><div>Dead code cleanup will take care of this in release builds, and now it reads like a proper implication, too.</div><div><br></div><div>Jordan</div></div></body></html>