From c4faf26527e665b046899627cfbc4b920460ca8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Thu, 24 Dec 2015 10:39:24 +0100 Subject: [PATCH] Make memcpy volatile to make sure bytes are copied and not optimized out by the compiler. --- test/unique_ptr_functions.cpp | 67 +++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 10 deletions(-) diff --git a/test/unique_ptr_functions.cpp b/test/unique_ptr_functions.cpp index c68d085..26f6b5b 100644 --- a/test/unique_ptr_functions.cpp +++ b/test/unique_ptr_functions.cpp @@ -47,33 +47,79 @@ static const unsigned char ee_patternbuf[PatternSize] = { 0xEE, 0xEE, 0xEE, 0xEE static const unsigned char dd_patternbuf[PatternSize] = { 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD }; static const unsigned char cc_patternbuf[PatternSize] = { 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC }; +void volatile_memset(volatile void *p, int ch, std::size_t len) +{ + volatile unsigned char *puch = static_cast(p); + for(std::size_t i = 0; i != len; ++i){ + *puch = (unsigned char)ch; + ++puch; + } +} + +int volatile_memcmp(const volatile void *p1, const volatile void *p2, std::size_t len) +{ + const volatile unsigned char *s1 = static_cast(p1); + const volatile unsigned char *s2 = static_cast(p2); + unsigned char u1, u2; + + for ( ; len-- ; s1++, s2++) { + u1 = *s1; + u2 = *s2; + if (u1 != u2) { + return (u1-u2); + } + } + return 0; +} + +void volatile_memcmp(volatile void *p, int ch, std::size_t len) +{ + volatile unsigned char *puch = static_cast(p); + for(std::size_t i = 0; i != len; ++i){ + *puch = (unsigned char)ch; + ++puch; + } +} + +#include + struct default_init { static void* operator new(std::size_t sz) { void *const p = ::operator new(sz); - return std::memset(p, 0xFF, sz); + //Make sure they are not optimized out + volatile_memset(p, 0xFF, sz); + std::cout << "0xFF" << '\n'; + return p; } static void* operator new[](std::size_t sz) { void *const p = ::operator new[](sz); - return std::memset(p, 0xEE, sz); + //Make sure they are not optimized out + volatile_memset(p, 0xEE, sz); + std::cout << "0xEE" << '\n'; + return p; } static void* operator new(std::size_t sz, const std::nothrow_t &) { void *const p = ::operator new(sz); - return std::memset(p, 0xDD, sz); + //Make sure they are not optimized out + volatile_memset(p, 0xDD, sz); + std::cout << "0xDD" << '\n'; + return p; } static void* operator new[](std::size_t sz, const std::nothrow_t &) { void *const p = ::operator new[](sz); - return std::memset(p, 0xCC, sz); + //Make sure they are not optimized out + volatile_memset(p, 0xCC, sz); + std::cout << "0xCC" << '\n'; + return p; } unsigned char buf[PatternSize]; }; - - namespace bml = ::boost::movelib; //////////////////////////////// @@ -88,11 +134,12 @@ void test() reset_counters(); { bml::unique_ptr p(bml::make_unique_definit()); - BOOST_TEST(0 == std::memcmp(p.get(), ff_patternbuf, sizeof(ff_patternbuf))); + BOOST_TEST(0 == volatile_memcmp(p.get(), ff_patternbuf, sizeof(ff_patternbuf))); } { bml::unique_ptr p(bml::make_unique_nothrow_definit()); - BOOST_TEST(0 == std::memcmp(p.get(), dd_patternbuf, sizeof(dd_patternbuf))); + + BOOST_TEST(0 == volatile_memcmp(p.get(), dd_patternbuf, sizeof(dd_patternbuf))); } BOOST_TEST(A::count == 0); @@ -167,14 +214,14 @@ void test() { bml::unique_ptr p(bml::make_unique_definit(10)); for(unsigned i = 0; i != 10; ++i){ - BOOST_TEST(0 == std::memcmp(&p[i], ee_patternbuf, sizeof(ee_patternbuf))); + BOOST_TEST(0 == volatile_memcmp(&p[i], ee_patternbuf, sizeof(ee_patternbuf))); } } reset_counters(); { bml::unique_ptr p(bml::make_unique_nothrow_definit(10)); for(unsigned i = 0; i != 10; ++i){ - BOOST_TEST(0 == std::memcmp(&p[i], cc_patternbuf, sizeof(cc_patternbuf))); + BOOST_TEST(0 == volatile_memcmp(&p[i], cc_patternbuf, sizeof(cc_patternbuf))); } } }