]> granicus.if.org Git - clang/commit
[analyzer] Indirect invalidation counts as an escape for leak checkers.
authorJordan Rose <jordan_rose@apple.com>
Fri, 10 May 2013 17:07:16 +0000 (17:07 +0000)
committerJordan Rose <jordan_rose@apple.com>
Fri, 10 May 2013 17:07:16 +0000 (17:07 +0000)
commit374ae320b87c15b0262c40e5c46e8990111df5ca
tree7fac073baeaaa6d05b67f8b9e5642399b2091a3b
parent0b6d8469aa202d79e5eca116e2598a0673253ce2
[analyzer] Indirect invalidation counts as an escape for leak checkers.

Consider this example:

  char *p = malloc(sizeof(char));
  systemFunction(&p);
  free(p);

In this case, when we call systemFunction, we know (because it's a system
function) that it won't free 'p'. However, we /don't/ know whether or not
it will /change/ 'p', so the analyzer is forced to invalidate 'p', wiping
out any bindings it contains. But now the malloc'd region looks like a
leak, since there are no more bindings pointing to it, and we'll get a
spurious leak warning.

The fix for this is to notice when something is becoming inaccessible due
to invalidation (i.e. an imperfect model, as opposed to being explicitly
overwritten) and stop tracking it at that point. Currently, the best way
to determine this for a call is the "indirect escape" pointer-escape kind.

In practice, all the patch does is take the "system functions don't free
memory" special case and limit it to direct parameters, i.e. just the
arguments to a call and not other regions accessible to them. This is a
conservative change that should only cause us to escape regions more
eagerly, which means fewer leak warnings.

This isn't perfect for several reasons, the main one being that this
example is treated the same as the one above:

  char **p = malloc(sizeof(char *));
  systemFunction(p + 1);
  // leak

Currently, "addresses accessible by offsets of the starting region" and
"addresses accessible through bindings of the starting region" are both
considered "indirect" regions, hence this uniform treatment.

Another issue is our longstanding problem of not distinguishing const and
non-const bindings; if in the first example systemFunction's parameter were
a char * const *, we should know that the function will not overwrite 'p',
and thus we can safely report the leak.

<rdar://problem/13758386>

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@181607 91177308-0d34-0410-b5e6-96231b3b80d8
lib/StaticAnalyzer/Checkers/MallocChecker.cpp
lib/StaticAnalyzer/Checkers/SimpleStreamChecker.cpp
test/Analysis/Inputs/system-header-simulator.h
test/Analysis/malloc.c
test/Analysis/simple-stream-checks.c