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

Jonathan Roelofs jonathan at codesourcery.com
Mon Dec 8 06:40:18 PST 2014


================
Comment at: test/support/count_new.hpp:8
@@ +7,3 @@
+
+int new_called = 0;
+int delete_called = 0;
----------------
For avoidance of macros, how about something like this:

    class MemCounter {
    private:
      int new_called;
    public:
      bool checkNewCallsEq(int N) {
    #if !__has_feature(address_sanitizer) && !__has_feature(memory_sanitizer)
        return new_called == N;
    #else
        return true;
    #endif
      }
    };

    MemCounter globalMemCounter; 

    #if !__has_feature(address_sanitizer) && !__has_feature(memory_sanitizer)
    void* operator new(std::size_t s) throw(std::bad_alloc) {
        ...
    }
    #endif

And then in the individual tests:

    assert(globalMemCounter.checkNewCallsEq(0));

And then if we want to be even more fancy later on, the MemCounter objects could be set up to count only within a scope by allowing them to register with each other to listen for these events. That seems like it'd be beneficial for at least `utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/make_shared.pass.cpp`, and possibly others.

================
Comment at: test/support/count_new.hpp:54
@@ +53,3 @@
+
+#define COUNT_NEW_ASSERT(Pred) ((void)0)
+
----------------
Do MSan and ASan have hooks that we could use to implement these checks when they're enabled?

http://reviews.llvm.org/D6562






More information about the cfe-commits mailing list