[asan] Assert in __sanitizer_ptr_{sub,cmp} if one of the pointers was freed.
authorFilipe Cabecinhas <me@filcab.net>
Fri, 29 Apr 2016 20:37:34 +0000 (20:37 +0000)
committerFilipe Cabecinhas <me@filcab.net>
Fri, 29 Apr 2016 20:37:34 +0000 (20:37 +0000)
commit04d61050ea0c064520e0952497fbc5948d908662
tree1f1ff3f4a6ca360aa02e61cadfc5930b69e5f338
parent9debb987219720b1ff880a830dbfb158c4b864bf
[asan] Assert in __sanitizer_ptr_{sub,cmp} if one of the pointers was freed.

Summary:
This (partially) implements the check mentioned at
http://kristerw.blogspot.co.uk/2016/04/dangling-pointers-and-undefined-behavior.html
(via John Regehr)

Quoting:
"That the behavior is undefined follows from C11 6.2.4 "Storage
durations of objects"
  The lifetime of an object is the portion of program execution during
  which storage is guaranteed to be reserved for it. An object exists, has
  a constant address, and retains its last-stored value throughout its
  lifetime. If an object is referred to outside of its lifetime, the
  behavior is undefined. The value of a pointer becomes indeterminate when
  the object it points to (or just past) reaches the end of its lifetime.
and 7.22.3 "Memory management functions" that says that free ends the
lifetime of objects
  The lifetime of an allocated object extends from the allocation until
  the deallocation.
"

We can probably implement this for stack variables too, but I think this
is a good start to see if there's interest in this check.
We can also hide this behind a flag, too.

Reviewers: samsonov, kcc, rsmith, regehr

Subscribers: kubabrecka, llvm-commits

Differential Revision: http://reviews.llvm.org/D19691

llvm-svn: 268097
compiler-rt/lib/asan/asan_allocator.cc
compiler-rt/lib/asan/asan_allocator.h
compiler-rt/lib/asan/asan_report.cc
compiler-rt/test/asan/TestCases/invalid-pointer-pairs.cc [new file with mode: 0644]