c++address-sanitizersanitizer

Are there anyways to work around errors given by ndefinedBehaviorSanitizer


I have the following code:

#include <memory>
#include <functional>
#include <boost/lockfree/queue.hpp>

#define _ThreadPoolLength_  100

class thread_pool {
public:
    thread_pool() : q(_ThreadPoolLength_) {}

private:
    mutable boost::lockfree::queue<std::function<void(int id)> *> q;
};

class Worker
{
    thread_pool workerPool;
};

Worker* worker;

int main() {
    worker = new Worker();
    delete worker;
    return 0;
}

If compile it with clang++ -fsanitize=address,undefined code.cpp, then during running it will produce something like:

constructor call on misaligned address 0x6060000025a0 for type 'boost::lockfree::queue *>::node', which requires 64 byte alignment 0x6060000025a0: note: pointer points here

01 00 00 3c 40 25 00 00 60 60 be be be be be be be be be be be be be be be be be be be be be be ^

#0 0x519fc5 in boost::lockfree::queue<std::function<void (int)>*>::node* boost::lockfree::detail::freelist_stack<boost::lockfree::queue<std::function<void (int)>*>::node, std::allocator<boost::lockfree::queue<std::function<void (int)>*>::node> >::construct<true, false, boost::lockfree::queue<std::function<void (int)>*>::node*>(boost::lockfree::queue<std::function<void (int)>*>::node* const&) 
#1 0x517e77 in boost::lockfree::queue<std::function<void (int)>*>::initialize() 
#2 0x51743c in boost::lockfree::queue<std::function<void (int)>*>::queue(unsigned long) 
#3 0x51713f in thread_pool::thread_pool() 
#4 0x517048 in Worker::Worker() 
#5 0x516ed9 in main 
#6 0x7f6c3cb6bb96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
#7 0x41a5f9 in _start

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ...

I suspect the errors were caused by boost::lockfree::queue<std::function<void(int id)> *>, but why? Are there any ways to work around it?

clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)

Finally, I replaced the use of boost::lockfree::queue<std::function<void(int id)> *> with a similar class by referring to a blog post.


Solution

  • You're leaking worker because you used new to construct it and never use delete to destruct it. The other ASan messages are there because as part of constructing worker, its member queue is also constructed.