[cfe-commits] unix.Malloc static checker improvement: memory.LeakPtrValChanged

Anna Zaks ganna at apple.com
Mon Jan 21 10:31:52 PST 2013


On Jan 21, 2013, at 9:40 AM, Jordan Rose <jordan_rose at apple.com> wrote:

> 
> On Jan 19, 2013, at 21:09 , Branden Archer <b.m.archer4 at gmail.com> wrote:
> 
>> 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).
>> 
>> Jordan, I think you were looking at an older patch. Some of your comments are probably already resolved. However, just in case...
>> 
>> +
>> +        if (  offset.isValid()
>> +           && !offset.hasSymbolicOffset()
>> +           && offset.getOffset() != 0) {
>> +          os << "; the memory location passed is an offset of an allocated location";
>> +        }
>> +
>> 
>> ...this test is not correct; it will fire for free(&local[1]).
>>  
>> I do not agree. My understanding is that local stack variables would fail the following test in ReportBadFree:
>> 
>> const MemRegion *MR = ArgVal.getAsRegion();
>>     if (MR) {
> 
> All memory is represented by MemRegions. '&local[1]' translates to an ElementRegion whose super-region is a VarRegion.
> 
> 
> 
>> 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:
>> 
>> void testFreeNonMallocPointerWithOffset() {
>>   char c;
>>   char * r = &c;
>>   r = r + 10;
>>   free(r-10); // expected-warning {{Argument to free() is the address of the local variable 'c', which is not memory allocated by malloc()}}
>> }
> 
> This doesn't actually test the case I was talking about, which is where you pass an offset of a local variable:
> 
> char c;
> char *r = &c;
> free(r + 1);
> 
> I believe that will fire, but I admit I haven't built clang with your new warning.
> 
> 
>> 
>> 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.)
>> 
>> 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.
> 
> 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.
> 
> 
> 
>> +void freeOffsetPointerPassedToFunction()
>> +{
>> +  int *p = malloc(sizeof(int)*2);
>> +  p[1] = 0;
>> +  p += 1;
>> +  myfooint(*p); // not passing the pointer, only a value pointed by pointer
>> +  free(p); // expected-warning {{Argument to free() is not memory allocated by malloc(); the memory location passed is an offset of an allocated location}}
>> +}
>> 
>> A better test would be to pass 'p' as a const pointer, which means the contents of the region get invalidated but 'p' itself will not.
>> 
>> 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.
> 
> 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.
> 
> 
> 
>> -  if (Call && doesNotFreeMemory(Call, State))
>> +  if ((Kind == PSK_DirectEscapeOnCall ||
>> +      Kind == PSK_IndirectEscapeOnCall) &&
>> +      doesNotFreeMemory(Call, State)) {
>> 
>> 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).
>> -  if (Call && guaranteedNotToCloseFile(*Call))
>> +  if ((Kind == PSK_DirectEscapeOnCall ||
>> +      Kind == PSK_IndirectEscapeOnCall) &&
>> +      guaranteedNotToCloseFile(*Call)) {
>> 
>> Ditto.
>> 
>> 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.
> 
> Whoops, clearly I have trouble reasoning about this part. I think I understand Anna's explanation now: it's unlikely to have a known, system API that will not free (or cause to escape) a top-level argument, but will free (or cause to escape) some indirectly-accessible region, and that applies to stream handles as well.
> 
> (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.)
> 
> 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:
> 
> if (Kind == PSK_DirectEscapeOnCall || Kind == PSK_IndirectEscapeOnCall)
>   assert(Call);
> 

I prefer to keep everything inside an assert. For example, the one below might be more readable.

  assert((!(Kind == PSK_DirectEscapeOnCall || 
            Kind == PSK_IndirectEscapeOnCall) ||
          Call) && "Call must not be NULL when escaping on call");


> Dead code cleanup will take care of this in release builds, and now it reads like a proper implication, too.
> 
> Jordan

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130121/d79919ae/attachment.html>


More information about the cfe-commits mailing list