This is an archive of the discontinued LLVM Phabricator instance.

[asan] Assert in __sanitizer_ptr_{sub,cmp} if one of the pointers was freed.
ClosedPublic

Authored by filcab on Apr 28 2016, 2:36 PM.

Details

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.

Diff Detail

Event Timeline

filcab updated this revision to Diff 55491.Apr 28 2016, 2:36 PM
filcab retitled this revision from to [asan] Assert in __sanitizer_ptr_{sub,cmp} if one of the pointers was freed..
filcab updated this object.
filcab added reviewers: samsonov, kcc, rsmith, regehr.
filcab added a subscriber: llvm-commits.
kcc added inline comments.Apr 28 2016, 5:46 PM
test/asan/TestCases/invalid-pointer-pairs.cc
1

there is no CHECK in this file, is that right?

filcab updated this revision to Diff 55548.Apr 29 2016, 1:53 AM
filcab marked an inline comment as done.

Updated with a proper test case.

test/asan/TestCases/invalid-pointer-pairs.cc
1

Errr, no. Sorry.
I forgot to add. I'm submitting a new version with the proper test.

kcc accepted this revision.Apr 29 2016, 10:42 AM
kcc edited edge metadata.

LGTM

This revision is now accepted and ready to land.Apr 29 2016, 10:42 AM
This revision was automatically updated to reflect the committed changes.