[compiler-rt] r202266 - [asan] remove UB (comparison of two unrelated pointers) from a test

Kostya Serebryany kcc at google.com
Wed Feb 26 08:40:13 PST 2014


On Wed, Feb 26, 2014 at 8:32 AM, David Blaikie <dblaikie at gmail.com> wrote:

> On Wed, Feb 26, 2014 at 5:53 AM, Kostya Serebryany <kcc at google.com> wrote:
> > Author: kcc
> > Date: Wed Feb 26 07:53:23 2014
> > New Revision: 202266
> >
> > URL: http://llvm.org/viewvc/llvm-project?rev=202266&view=rev
> > Log:
> > [asan] remove UB (comparison of two unrelated pointers) from a test
>
> I assume you're aware of this (& chose the below fix as the better one
> anyway - which is totally fine) but you can have a well-defined
> comparison of unrelated pointers using std::less, if you happen to
> need/want/prefer that.
>
I was experimenting with an implementation for a checker that find the UB
case.
https://code.google.com/p/address-sanitizer/issues/detail?id=269

The implementation is rough: it concludes the type of operand (pointer or
not pointer)
from the LLVM IR Type, which is wrong, because the types in LLVM may change
during optimizations from int to pointer and back.
The correct implementation should probably instrument the instructions in
the frontend.
Before I have that, I can not rely on std::less.
(And no, I did not know about std::less, so thanks!)


>
> >
> > Modified:
> >     compiler-rt/trunk/lib/asan/tests/asan_mem_test.cc
> >
> > Modified: compiler-rt/trunk/lib/asan/tests/asan_mem_test.cc
> > URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/tests/asan_mem_test.cc?rev=202266&r1=202265&r2=202266&view=diff
> >
> ==============================================================================
> > --- compiler-rt/trunk/lib/asan/tests/asan_mem_test.cc (original)
> > +++ compiler-rt/trunk/lib/asan/tests/asan_mem_test.cc Wed Feb 26
> 07:53:23 2014
> > @@ -76,17 +76,17 @@ TEST(AddressSanitizer, MemSetOOBTest) {
> >  // Strictly speaking we are not guaranteed to find such two pointers,
> >  // but given the structure of asan's allocator we will.
> >  static bool AllocateTwoAdjacentArrays(char **x1, char **x2, size_t
> size) {
> > -  vector<char *> v;
> > +  vector<uintptr_t> v;
> >    bool res = false;
> >    for (size_t i = 0; i < 1000U && !res; i++) {
> > -    v.push_back(new char[size]);
> > +    v.push_back(reinterpret_cast<uintptr_t>(new char[size]));
> >      if (i == 0) continue;
> >      sort(v.begin(), v.end());
> >      for (size_t j = 1; j < v.size(); j++) {
> >        assert(v[j] > v[j-1]);
> >        if ((size_t)(v[j] - v[j-1]) < size * 2) {
> > -        *x2 = v[j];
> > -        *x1 = v[j-1];
> > +        *x2 = reinterpret_cast<char*>(v[j]);
> > +        *x1 = reinterpret_cast<char*>(v[j-1]);
> >          res = true;
> >          break;
> >        }
> > @@ -94,9 +94,10 @@ static bool AllocateTwoAdjacentArrays(ch
> >    }
> >
> >    for (size_t i = 0; i < v.size(); i++) {
> > -    if (res && v[i] == *x1) continue;
> > -    if (res && v[i] == *x2) continue;
> > -    delete [] v[i];
> > +    char *p = reinterpret_cast<char *>(v[i]);
> > +    if (res && p == *x1) continue;
> > +    if (res && p == *x2) continue;
> > +    delete [] p;
> >    }
> >    return res;
> >  }
> >
> >
> > _______________________________________________
> > llvm-commits mailing list
> > llvm-commits at cs.uiuc.edu
> > http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140226/7e74913c/attachment.html>


More information about the llvm-commits mailing list