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

Branden Archer b.m.archer4 at gmail.com
Thu Jan 24 18:23:50 PST 2013


Thanks for the comments so far!

Both malloc and simple stream checker should be the same - if we pass p+1
> to a function, which is known not to close files / free memory, we should
> still track the symbol.
>

Got it. Corrected.

which is where you pass an *offset* of a local variable:
>
> char c;
> char *r = &c;
> free(r + 1);
>

Oh, my mistake. For whatever reason I though
testFreeNonMallocPointerWithOffset was testing that. I was not looking
carefully enough. I added your test and gave it a whirl. Indeed, you are
correct; the wrong message is being printed. The attached patch has a
separate ReportOffsetFree function for reporting this error, and I have
added your test case.

Side question. I noticed that the tests do not always strictly obey the
expected-warning tags. For example, when I was running the test case above,
I tried the following for the free() line:
free(r+1); // expected-warning {{Argument to free() is the address of the
local variable 'c', which is not memory allocated by malloc()}}
or
free(r+1); // expected-warning {{Argument to free() is the address of the
local variable 'c', which is not memory allocated by malloc(); the memory
location passed is an offset of an allocated location}}
The unit tests will pass both of these, which does not make any sense. I
finally tried the following, which also passed:
free(r+1); // expected-warning {{}}
However, no expected-warning comment resulted in a failure. Is there a
special format for the expected-warning comment that I am missing, or is
the comparison that is taking place not quite right?

The message emitted from ReportOffsetFree is a little different from the
one that was emitted in ReportBadFree. Take a look and see if you agree
with its phrasing and the information it is providing.

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.)
>

I think I have all the && and spacing issues now. If I am missing them now,
I probably do not see them.


> Also, I thought of yet another way to write the implication assertion:
>
> if (Kind == PSK_DirectEscapeOnCall || Kind == PSK_IndirectEscapeOnCall)
>   assert(Call);
>


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

 I sort of agree with part of both of the possibilities. I would rather the
assert be its own contained unit, but I do not like inverting the logic in
the assert, as it may make it a little harder to parse. How about this?

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

I will probably be called on spacing rules though, as I tried to indent
pass the ( only if within the same set of (). That is, the Kinds should
line up, but the Call and "Call should not, as the first Call is within an
extra ().

- Branden

On Mon, Jan 21, 2013 at 1:31 PM, Anna Zaks <ganna at apple.com> wrote:

>
> 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/20130124/9997002f/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 01-modify-checkPointerEscape.patch
Type: application/octet-stream
Size: 10316 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130124/9997002f/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 02-update-malloc-checker.patch
Type: application/octet-stream
Size: 9967 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130124/9997002f/attachment-0001.obj>


More information about the cfe-commits mailing list