From edcaaa68d802f2c58eedee94d494737b12c11acc Mon Sep 17 00:00:00 2001 From: Tim King Date: Mon, 7 Nov 2016 13:38:53 -0800 Subject: [PATCH] Disabling out of memory tests unit tests when ASAN is enabled. ASAN failures are too hard for the unit testing framework. --- test/unit/context/cdlist_black.h | 60 ++++++------- .../context/cdlist_context_memory_black.h | 78 ++++++++--------- test/unit/context/cdvector_black.h | 3 - test/unit/memory.h | 84 ++++++++++++------- 4 files changed, 119 insertions(+), 106 deletions(-) diff --git a/test/unit/context/cdlist_black.h b/test/unit/context/cdlist_black.h index 8929e8fb4..f4231ac91 100644 --- a/test/unit/context/cdlist_black.h +++ b/test/unit/context/cdlist_black.h @@ -16,18 +16,17 @@ #include -#include #include +#include #include #include "base/exception.h" -#include "context/context.h" #include "context/cdlist.h" +#include "context/context.h" #include "memory.h" using namespace std; using namespace CVC4::context; -using namespace CVC4::test; using namespace CVC4; struct DtorSensitiveObject { @@ -37,19 +36,13 @@ struct DtorSensitiveObject { }; class CDListBlack : public CxxTest::TestSuite { -private: - + private: Context* d_context; -public: + public: + void setUp() { d_context = new Context(); } - void setUp() { - d_context = new Context(); - } - - void tearDown() { - delete d_context; - } + void tearDown() { delete d_context; } // test at different sizes. this triggers grow() behavior differently. // grow() was completely broken in revision 256 @@ -69,21 +62,19 @@ public: CDList list(d_context, callDestructor); TS_ASSERT(list.empty()); - for(int i = 0; i < N; ++i) { + for (int i = 0; i < N; ++i) { TS_ASSERT_EQUALS(list.size(), unsigned(i)); list.push_back(i); TS_ASSERT(!list.empty()); TS_ASSERT_EQUALS(list.back(), i); int i2 = 0; - for(CDList::const_iterator j = list.begin(); - j != list.end(); - ++j) { + for (CDList::const_iterator j = list.begin(); j != list.end(); ++j) { TS_ASSERT_EQUALS(*j, i2++); } } TS_ASSERT_EQUALS(list.size(), unsigned(N)); - for(int i = 0; i < N; ++i) { + for (int i = 0; i < N; ++i) { TS_ASSERT_EQUALS(list[i], i); } } @@ -132,30 +123,31 @@ public: } void testEmptyIterator() { - CDList* list = new(true) CDList(d_context); + CDList* list = new (true) CDList(d_context); TS_ASSERT_EQUALS(list->begin(), list->end()); list->deleteSelf(); } - /* setrlimit() totally broken on Mac OS X */ void testOutOfMemory() { -#ifdef __APPLE__ +#ifdef CVC4_MEMORY_LIMITING_DISABLED - TS_WARN("can't run memory tests on Mac OS X"); + test::WarnWithLimitedMemoryDisabledReason(); -#else /* __APPLE__ */ +#else /* CVC4_MEMORY_LIMITING_DISABLED */ CDList list(d_context); - WithLimitedMemory wlm(1); - - TS_ASSERT_THROWS({ - // We cap it at UINT_MAX, preferring to terminate with a - // failure than run indefinitely. - for(unsigned i = 0; i < UINT_MAX; ++i) { - list.push_back(i); - } - }, bad_alloc); - -#endif /* __APPLE__ */ + test::WithLimitedMemory wlm(1); + + TS_ASSERT_THROWS( + { + // We cap it at UINT_MAX, preferring to terminate with a + // failure than run indefinitely. + for (unsigned i = 0; i < UINT_MAX; ++i) { + list.push_back(i); + } + }, + bad_alloc); + +#endif /* CVC4_MEMORY_LIMITING_DISABLED */ } }; diff --git a/test/unit/context/cdlist_context_memory_black.h b/test/unit/context/cdlist_context_memory_black.h index a43d34b00..dccdd528e 100644 --- a/test/unit/context/cdlist_context_memory_black.h +++ b/test/unit/context/cdlist_context_memory_black.h @@ -16,19 +16,18 @@ #include -#include #include +#include #include #include "memory.h" -#include "context/context.h" #include "context/cdchunk_list.h" +#include "context/context.h" using namespace std; using namespace CVC4::context; -using namespace CVC4::test; struct DtorSensitiveObject { bool& d_dtorCalled; @@ -37,19 +36,13 @@ struct DtorSensitiveObject { }; class CDListContextMemoryBlack : public CxxTest::TestSuite { -private: - + private: Context* d_context; -public: + public: + void setUp() { d_context = new Context(); } - void setUp() { - d_context = new Context(); - } - - void tearDown() { - delete d_context; - } + void tearDown() { delete d_context; } // test at different sizes. this triggers grow() behavior differently. // grow() was completely broken in revision 256 @@ -66,34 +59,32 @@ public: } void listTest(int N, bool callDestructor) { - CDChunkList - list(d_context, callDestructor, ContextMemoryAllocator(d_context->getCMM())); + CDChunkList list(d_context, callDestructor, + ContextMemoryAllocator(d_context->getCMM())); TS_ASSERT(list.empty()); - for(int i = 0; i < N; ++i) { + for (int i = 0; i < N; ++i) { TS_ASSERT_EQUALS(list.size(), unsigned(i)); list.push_back(i); TS_ASSERT(!list.empty()); TS_ASSERT_EQUALS(list.back(), i); int i2 = 0; - for(CDChunkList::const_iterator j = list.begin(); - j != list.end(); - ++j) { + for (CDChunkList::const_iterator j = list.begin(); j != list.end(); + ++j) { TS_ASSERT_EQUALS(*j, i2++); } } TS_ASSERT_EQUALS(list.size(), unsigned(N)); - for(int i = 0; i < N; ++i) { + for (int i = 0; i < N; ++i) { TS_ASSERT_EQUALS(list[i], i); } } void testEmptyIterator() { - CDChunkList< int>* list = - new(d_context->getCMM()) - CDChunkList< int >(true, d_context, false, - ContextMemoryAllocator(d_context->getCMM())); + CDChunkList* list = new (d_context->getCMM()) + CDChunkList(true, d_context, false, + ContextMemoryAllocator(d_context->getCMM())); TS_ASSERT_EQUALS(list->begin(), list->end()); } @@ -104,8 +95,12 @@ public: bool shouldAlsoRemainFalse = false; bool aThirdFalse = false; - CDChunkList listT(d_context, true, ContextMemoryAllocator(d_context->getCMM())); - CDChunkList listF(d_context, false, ContextMemoryAllocator(d_context->getCMM())); + CDChunkList listT( + d_context, true, + ContextMemoryAllocator(d_context->getCMM())); + CDChunkList listF( + d_context, false, + ContextMemoryAllocator(d_context->getCMM())); DtorSensitiveObject shouldRemainFalseDSO(shouldRemainFalse); DtorSensitiveObject shouldFlipToTrueDSO(shouldFlipToTrue); @@ -140,25 +135,26 @@ public: TS_ASSERT_EQUALS(aThirdFalse, false); } - /* setrlimit() totally broken on Mac OS X */ void testOutOfMemory() { -#ifdef __APPLE__ +#ifdef CVC4_MEMORY_LIMITING_DISABLED - TS_WARN("can't run memory tests on Mac OS X"); + CVC4::test::WarnWithLimitedMemoryDisabledReason(); -#else /* __APPLE__ */ +#else /* CVC4_MEMORY_LIMITING_DISABLED */ CDChunkList list(d_context); - WithLimitedMemory wlm(1); - - TS_ASSERT_THROWS({ - // We cap it at UINT_MAX, preferring to terminate with a - // failure than run indefinitely. - for(unsigned i = 0; i < UINT_MAX; ++i) { - list.push_back(i); - } - }, bad_alloc); - -#endif /* __APPLE__ */ + CVC4::test::WithLimitedMemory wlm(1); + + TS_ASSERT_THROWS( + { + // We cap it at UINT_MAX, preferring to terminate with a + // failure than run indefinitely. + for (unsigned i = 0; i < UINT_MAX; ++i) { + list.push_back(i); + } + }, + bad_alloc); + +#endif /* CVC4_MEMORY_LIMITING_DISABLED */ } }; diff --git a/test/unit/context/cdvector_black.h b/test/unit/context/cdvector_black.h index 4b47c0660..e806ca712 100644 --- a/test/unit/context/cdvector_black.h +++ b/test/unit/context/cdvector_black.h @@ -22,14 +22,11 @@ #include -#include "memory.h" - #include "context/context.h" #include "context/cdvector.h" using namespace std; using namespace CVC4::context; -using namespace CVC4::test; struct DtorSensitiveObject { bool& d_dtorCalled; diff --git a/test/unit/memory.h b/test/unit/memory.h index 73e4814e7..d15d1353f 100644 --- a/test/unit/memory.h +++ b/test/unit/memory.h @@ -19,6 +19,12 @@ ** TS_ASSERT_THROWS( foo(), bad_alloc ); ** ** The WithLimitedMemory destructor will re-establish the previous limit. + ** + ** This class does not exist in CVC4_MEMORY_LIMITING_DISABLED is defined. + ** This can be disabled for a variety of reasons. + ** If this is disabled, there will be a function: + ** void WarnWithLimitedMemoryDisabledReason() + ** uses TS_WARN to alert users that these tests are disabled. **/ #include @@ -26,47 +32,52 @@ #ifndef __CVC4__TEST__MEMORY_H #define __CVC4__TEST__MEMORY_H -#include +#include #include +#include #include "base/cvc4_assert.h" +// Conditionally define CVC4_MEMORY_LIMITING_DISABLED. +#ifdef __APPLE__ +# define CVC4_MEMORY_LIMITING_DISABLED 1 +# define CVC4_MEMORY_LIMITING_DISABLED_REASON "setrlimit() is broken on Mac." +#else /* __APPLE__ */ +# if defined(__has_feature) +# if __has_feature(address_sanitizer) +// Tests cannot expect bad_alloc to be thrown due to limit memory using +// setrlimit when ASAN is enable. ASAN instead aborts on mmap failures. +# define CVC4_MEMORY_LIMITING_DISABLED 1 +# define CVC4_MEMORY_LIMITING_DISABLED_REASON "ASAN's mmap failures abort." +# endif /* __has_feature(address_sanitizer) */ +# endif /* defined(__has_feature) */ +#endif + + + namespace CVC4 { namespace test { -class WithLimitedMemory { - rlim_t d_prevAmount; +#ifdef CVC4_MEMORY_LIMITING_DISABLED - void remember() { - struct rlimit rlim; - TS_ASSERT_EQUALS(getrlimit(RLIMIT_AS, &rlim), 0); - d_prevAmount = rlim.rlim_cur; - } +inline void WarnWithLimitedMemoryDisabledReason() { + const std::string reason = std::string("WithLimitedMemory is disabled: ") + + std::string(CVC4_MEMORY_LIMITING_DISABLED_REASON); + TS_WARN(reason.c_str()); +} -public: +#else /* CVC4_MEMORY_LIMITING_DISABLED */ - WithLimitedMemory() { -#ifdef __APPLE__ - TS_FAIL("setrlimit() is broken on Mac, can't run memory tests."); - AlwaysAssert(false, - "setrlimit() is broken on Mac, can't run memory tests."); -#endif /* __APPLE__ */ - remember(); - } +class WithLimitedMemory { + public: + WithLimitedMemory() { remember(); } WithLimitedMemory(rlim_t amount) { -#ifdef __APPLE__ - TS_FAIL("setrlimit() is broken on Mac, can't run memory tests."); - AlwaysAssert(false, - "setrlimit() is broken on Mac, can't run memory tests."); -#endif /* __APPLE__ */ remember(); set(amount); } - ~WithLimitedMemory() { - set(d_prevAmount); - } + ~WithLimitedMemory() { set(d_prevAmount); } void set(rlim_t amount) { struct rlimit rlim; @@ -74,9 +85,26 @@ public: rlim.rlim_max = RLIM_INFINITY; TS_ASSERT_EQUALS(setrlimit(RLIMIT_AS, &rlim), 0); } -}; -}/* CVC4::test namespace */ -}/* CVC4 namespace */ + private: + void remember() { + struct rlimit rlim; + TS_ASSERT_EQUALS(getrlimit(RLIMIT_AS, &rlim), 0); + d_prevAmount = rlim.rlim_cur; + } + + rlim_t d_prevAmount; +}; /* class WithLimitedMemory */ + +#endif /* CVC4_MEMORY_LIMITING_DISABLED */ + +} /* CVC4::test namespace */ +} /* CVC4 namespace */ + + +// Remove CVC4_MEMORY_LIMITING_DISABLED_REASON if it is defined. +#ifdef CVC4_MEMORY_LIMITING_DISABLED_REASON +#undef CVC4_MEMORY_LIMITING_DISABLED_REASON +#endif /* CVC4_MEMORY_LIMITING_DISABLED_REASON */ #endif /* __CVC4__TEST__MEMORY_H */ -- 2.30.2