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

Branden Archer b.m.archer4 at gmail.com
Sat Jan 19 21:09:00 PST 2013


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

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()}}
}


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.


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


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

- Branden


On Fri, Jan 18, 2013 at 10:07 PM, Anna Zaks <ganna at apple.com> wrote:

>
> On Jan 18, 2013, at 7:02 PM, Jordan Rose <jordan_rose at apple.com> wrote:
>
> (see previous e-mail, "Re: Add PointerEscapeKind to checkPointerEscape
> callback")
>
> I'd definitely like to see this improvement to MallocChecker! Let's see...
>
> +
> +  const MemRegion *Rbase = R->getBaseRegion();
>
> -  const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R);
> +  const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(Rbase);
>
> I would just collapse these into one line. More importantly, I think you
> should rename SR and possibly Sym to indicate that they're the *base* region/symbol,
> which isn't the same as the region actually passed to free.
>
>
> +  // Check if the memory location being freed is the actual location
> +  // allocated, or an offset.
> +  RegionOffset offset = R->getAsOffset();
>
> LLVM style guide is Germanic; all nouns are capitalized, even local
> variables.
>
>
> +  if ( RS && RS->isAllocated() &&
>
> Conventionally we have no space after the open paren for an 'if'.
>
> +       offset.isValid() &&
> +       offset.hasSymbolicOffset() == false &&
>
> Conventionally written "!offset.hasSymbolicOffset()"
>
>
> +      // Get the offset into the memory region before
> +      // getting the super region, as retrieving the
> +      // super region will ignore the offset.
> +      RegionOffset offset = MR->getAsOffset();
>
> Capital letter, again, but...
>
>
> +
> +        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]). 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.)
>
> Also, please put the && at the end of the line, not the start.
>
> And while you're in there...
>
>        while (const ElementRegion *ER = dyn_cast<ElementRegion>(MR))
>          MR = ER->getSuperRegion();
>
> ...this should probably be replaced by either MR->getBaseRegion(), so that
> we strip off struct field layers as well, or MR->StripCasts(), so that we
> don't say that '&x[1]' is the address of 'x'.
>
>
> In one of the later patches Branden does replace the loop with MR =
> MR->getBaseRegion(). I think he submitted it separately.
>
> Branden, can you send us just the final 2 patches?
>
> Thanks!
> Anna.
>
> Okay, I guess you're *not* in there anymore. I'll file a bug.
>
>
> +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.
>
>
> Okay, I think that's it! Thanks, Branden.
> Jordan
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130120/a296e9a8/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 01-modify-checkPointerEscape.patch
Type: application/octet-stream
Size: 10205 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130120/a296e9a8/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 02-update-malloc-checker.patch
Type: application/octet-stream
Size: 8260 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130120/a296e9a8/attachment-0001.obj>


More information about the cfe-commits mailing list