[libcxx] Consolidate new/delete replacement in tests and disable it when using sanitizers.

Summary:
MSAN and ASAN also replace new/delete which leads to a link error in these tests. Currently they are unsupported but I think it would be useful if these tests could run with sanitizers.

This patch creates a support header that consolidates the new/delete replacement functionality and checking.
When we are using sanitizers new and delete are no longer replaced and the checks always return true.

Reviewers: mclow.lists, danalbert, jroelofs, EricWF

Reviewed By: EricWF

Subscribers: cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@224741 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Eric Fiselier
2014-12-22 22:38:59 +00:00
parent 71dc14e864
commit 4eb5b6d5ee
19 changed files with 372 additions and 449 deletions

View File

@@ -13,45 +13,28 @@
// ~ctype();
// UNSUPPORTED: asan, msan
#include <locale>
#include <cassert>
#include <new>
unsigned delete_called = 0;
void* operator new[](size_t sz) throw(std::bad_alloc)
{
return operator new(sz);
}
void operator delete[](void* p) throw()
{
operator delete(p);
++delete_called;
}
#include "count_new.hpp"
int main()
{
{
delete_called = 0;
std::locale l(std::locale::classic(), new std::ctype<char>);
assert(delete_called == 0);
assert(globalMemCounter.checkDeleteArrayCalledEq(0));
}
assert(delete_called == 0);
assert(globalMemCounter.checkDeleteArrayCalledEq(0));
{
std::ctype<char>::mask table[256];
delete_called = 0;
std::locale l(std::locale::classic(), new std::ctype<char>(table));
assert(delete_called == 0);
assert(globalMemCounter.checkDeleteArrayCalledEq(0));
}
assert(delete_called == 0);
assert(globalMemCounter.checkDeleteArrayCalledEq(0));
{
delete_called = 0;
std::locale l(std::locale::classic(),
new std::ctype<char>(new std::ctype<char>::mask[256], true));
assert(delete_called == 0);
assert(globalMemCounter.checkDeleteArrayCalledEq(0));
}
assert(delete_called == 1);
assert(globalMemCounter.checkDeleteArrayCalledEq(1));
}

View File

@@ -17,43 +17,26 @@
// // unspecified
// };
// UNSUPPORTED: asan, msan
// Not a portable test
#include <codecvt>
#include <cstdlib>
#include <cassert>
int outstanding_news = 0;
void* operator new(std::size_t s) throw(std::bad_alloc)
{
++outstanding_news;
return std::malloc(s);
}
void operator delete(void* p) throw()
{
if (p)
{
--outstanding_news;
std::free(p);
}
}
#include "count_new.hpp"
int main()
{
assert(outstanding_news == 0);
assert(globalMemCounter.checkOutstandingNewEq(0));
{
typedef std::codecvt_utf16<wchar_t> C;
C c;
assert(outstanding_news == 0);
assert(globalMemCounter.checkOutstandingNewEq(0));
}
{
typedef std::codecvt_utf16<wchar_t> C;
std::locale loc(std::locale::classic(), new C);
assert(outstanding_news != 0);
assert(globalMemCounter.checkOutstandingNewNotEq(0));
}
assert(outstanding_news == 0);
assert(globalMemCounter.checkOutstandingNewEq(0));
}

View File

@@ -17,43 +17,26 @@
// // unspecified
// };
// UNSUPPORTED: asan, msan
// Not a portable test
#include <codecvt>
#include <cstdlib>
#include <cassert>
int outstanding_news = 0;
void* operator new(std::size_t s) throw(std::bad_alloc)
{
++outstanding_news;
return std::malloc(s);
}
void operator delete(void* p) throw()
{
if (p)
{
--outstanding_news;
std::free(p);
}
}
#include "count_new.hpp"
int main()
{
assert(outstanding_news == 0);
assert(globalMemCounter.checkOutstandingNewEq(0));
{
typedef std::codecvt_utf8<wchar_t> C;
C c;
assert(outstanding_news == 0);
assert(globalMemCounter.checkOutstandingNewEq(0));
}
{
typedef std::codecvt_utf8<wchar_t> C;
std::locale loc(std::locale::classic(), new C);
assert(outstanding_news != 0);
assert(globalMemCounter.checkOutstandingNewNotEq(0));
}
assert(outstanding_news == 0);
assert(globalMemCounter.checkOutstandingNewEq(0));
}

View File

@@ -14,27 +14,12 @@
// wbuffer_convert(streambuf *bytebuf = 0, Codecvt *pcvt = new Codecvt,
// state_type state = state_type());
// UNSUPPORTED: asan, msan
#include <locale>
#include <codecvt>
#include <sstream>
#include <cassert>
#include <new>
int new_called = 0;
void* operator new(std::size_t s) throw(std::bad_alloc)
{
++new_called;
return std::malloc(s);
}
void operator delete(void* p) throw()
{
--new_called;
std::free(p);
}
#include "count_new.hpp"
int main()
{
@@ -46,28 +31,28 @@ int main()
{
B b;
assert(b.rdbuf() == nullptr);
assert(new_called != 0);
assert(globalMemCounter.checkOutstandingNewNotEq(0));
}
assert(new_called == 0);
assert(globalMemCounter.checkOutstandingNewEq(0));
{
std::stringstream s;
B b(s.rdbuf());
assert(b.rdbuf() == s.rdbuf());
assert(new_called != 0);
assert(globalMemCounter.checkOutstandingNewNotEq(0));
}
assert(new_called == 0);
assert(globalMemCounter.checkOutstandingNewEq(0));
{
std::stringstream s;
B b(s.rdbuf(), new std::codecvt_utf8<wchar_t>);
assert(b.rdbuf() == s.rdbuf());
assert(new_called != 0);
assert(globalMemCounter.checkOutstandingNewNotEq(0));
}
assert(new_called == 0);
assert(globalMemCounter.checkOutstandingNewEq(0));
{
std::stringstream s;
B b(s.rdbuf(), new std::codecvt_utf8<wchar_t>, std::mbstate_t());
assert(b.rdbuf() == s.rdbuf());
assert(new_called != 0);
assert(globalMemCounter.checkOutstandingNewNotEq(0));
}
assert(new_called == 0);
assert(globalMemCounter.checkOutstandingNewEq(0));
}

View File

@@ -11,27 +11,11 @@
// locale() throw();
// UNSUPPORTED: asan, msan
#include <locale>
#include <new>
#include <cassert>
#include "platform_support.h" // locale name macros
int new_called = 0;
void* operator new(std::size_t s) throw(std::bad_alloc)
{
++new_called;
return std::malloc(s);
}
void operator delete(void* p) throw()
{
--new_called;
std::free(p);
}
#include "count_new.hpp"
void check(const std::locale& loc)
{
@@ -73,19 +57,19 @@ int main()
int ok;
{
std::locale loc;
assert(new_called == 0);
assert(globalMemCounter.checkOutstandingNewEq(0));
assert(loc.name() == "C");
assert(new_called == 0);
assert(globalMemCounter.checkOutstandingNewEq(0));
check(loc);
assert(new_called == 0);
assert(globalMemCounter.checkOutstandingNewEq(0));
assert(std::locale::global(std::locale(LOCALE_en_US_UTF_8)) == loc);
ok = new_called;
ok = globalMemCounter.outstanding_new;
std::locale loc2;
assert(new_called == ok);
assert(globalMemCounter.checkOutstandingNewEq(ok));
check(loc2);
assert(new_called == ok);
assert(globalMemCounter.checkOutstandingNewEq(ok));
assert(loc2 == std::locale(LOCALE_en_US_UTF_8));
assert(new_called == ok);
assert(globalMemCounter.checkOutstandingNewEq(ok));
}
assert(new_called == ok);
assert(globalMemCounter.checkOutstandingNewEq(ok));
}

View File

@@ -11,25 +11,10 @@
// template <class Facet> locale combine(const locale& other) const;
// UNSUPPORTED: asan, msan
#include <locale>
#include <new>
#include <cassert>
int new_called = 0;
void* operator new(std::size_t s) throw(std::bad_alloc)
{
++new_called;
return std::malloc(s);
}
void operator delete(void* p) throw()
{
--new_called;
std::free(p);
}
#include "count_new.hpp"
void check(const std::locale& loc)
{
@@ -89,7 +74,7 @@ int main()
const my_facet& f = std::use_facet<my_facet>(loc3);
assert(f.test() == 5);
}
assert(new_called == 0);
assert(globalMemCounter.checkOutstandingNewEq(0));
}
{
{
@@ -104,6 +89,6 @@ int main()
{
}
}
assert(new_called == 0);
assert(globalMemCounter.checkOutstandingNewEq(0));
}
}