0

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.

shapeare
  • 4,133
  • 7
  • 28
  • 39
  • Why there is no `delete`? If I add it, no leak is reported. As for misalignment, there are some reported issues such as [this](https://github.com/boostorg/lockfree/issues/11) and [this](https://svn.boost.org/trac10/ticket/11968) and [this](https://github.com/boostorg/lockfree/issues/52). – Daniel Langr Apr 21 '20 at 06:25
  • Sorry, I was stupid. I tried to strip down a large project and missed the delete operation. Now I have added delete, but the problem with UndefinedBehaviorSanitizer still reminds. I will update the question now. – shapeare Apr 21 '20 at 06:30
  • @DanielLangr I see, thanks. it is a bug from Boost – shapeare Apr 21 '20 at 06:39

1 Answers1

0

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.

Zuodian Hu
  • 979
  • 4
  • 9
  • Sorry, my fault. I tried to make the sampling code as small as possible to reproduce the problem from a larger project. Now I added back delete, but UndefinedBehaviorSanitizer still remains. – shapeare Apr 21 '20 at 06:35
  • Alright, then yeah this is a Boost problem. This may be something that can be [suppressed](https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#issue-suppression). – Zuodian Hu Apr 21 '20 at 06:47
  • Putting __attribute__((no_sanitize("undefined"))) in front of thread_pool's constructor or even the main function doesn't seem to help. – shapeare Apr 21 '20 at 10:00
  • Since it's somebody else's file, maybe try the `.supp` suppression file – Zuodian Hu Apr 21 '20 at 13:04