4

Using clang (3.5.1) with address sanitizer on my program using boost (1.56) I got: boost/serialization/singleton.hpp:132:13: runtime error: reference binding to null pointer

The example is:

#include<boost/serialization/singleton.hpp> 
#include <string> 
#include <iostream> 
class Foo{ 
private: 
  std::string bar = "Hello World"; 
public: 
  void print() const{ 
    std::cout << bar << std::endl; 
  } 
}; 



int main(){ 
  boost::serialization::singleton<Foo> test; 
  test.get_const_instance().print(); 
} 

Then I do:

compilation

clang++ -I/boost/1_56_0/gcc-4.8.2/include/ -fsanitize=address,undefined -std=c++11 test.cpp

output

./a.out:
boost/1_56_0/gcc-4.8.2/include/boost/serialization/singleton.hpp:132:13: runtime error: reference binding to null pointer of type 'const Foo'
Hello World

Looking at the code, I am confused by the role of the reference instance in the class singleton. It looks like undefined behaviour. Do you get it?

template<class T>
bool detail::singleton_wrapper< T >::m_is_destroyed = false;

} // detail

template <class T>
class singleton : public singleton_module
{
private:
    BOOST_DLLEXPORT static T & instance;
    // include this to provoke instantiation at pre-execution time
    static void use(T const &) {}
    BOOST_DLLEXPORT static T & get_instance() {
        static detail::singleton_wrapper< T > t;
        // refer to instance, causing it to be instantiated (and
        // initialized at startup on working compilers)
        BOOST_ASSERT(! detail::singleton_wrapper< T >::m_is_destroyed);
        use(instance); // That's the line 132
        return static_cast<T &>(t);
    }
public:
    BOOST_DLLEXPORT static T & get_mutable_instance(){
        BOOST_ASSERT(! is_locked());
        return get_instance();
    }
    BOOST_DLLEXPORT static const T & get_const_instance(){
        return get_instance();
    }
    BOOST_DLLEXPORT static bool is_destroyed(){
        return detail::singleton_wrapper< T >::m_is_destroyed;
    }
};
5
  • 1
    The problem is most likely not in the Boost code, but in how you use it. Please edit your question to show your code, preferably a Minimal, Complete, and Verifiable Example. Jan 21, 2015 at 8:59
  • Fair. I prepare that. Jan 21, 2015 at 9:00
  • I'd try boiling it down further--you may be able to make a repro case without using Boost at all, which would be helpful if you need to file a bug against the tools. Jan 21, 2015 at 9:02
  • 1
    The header comments link to groups.google.com/forum/#!topic/microsoft.public.vc.language/… which appears to explain precisely the reasoning behind their current approach, (which might indeed silently rely on UB?)
    – sehe
    Jan 21, 2015 at 10:20
  • Ok thanks, I did not see that. trick to avoid linking problem with VC6. I go on digging. Jan 21, 2015 at 11:07

1 Answer 1

1

I wasn't able to get the (address?) sanitizer working in my Xcode 6 environment. But I did trace through the program with the debugger. line 132 of singleton.hpp contains the line

use(instance);

where the value of instance has the (uninitialized value of zero). This might be considered an error by the sanitizer, but use(...) is an empty function. It is only included to guarantee that the singleton is called before main. If this isn't included, compile for release may optimize away the pre-main invocation and the class may not function as intended. So I would call this an over zealous behavior of the sanitizer. Or maybe the sanitizer could be considered not smart enough to trace one more level deep. or... .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.