Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oneTBB-2021.7.0 Asan detect memory leak #1133

Closed
rickyzhang012500 opened this issue Jun 13, 2023 · 4 comments
Closed

oneTBB-2021.7.0 Asan detect memory leak #1133

rickyzhang012500 opened this issue Jun 13, 2023 · 4 comments

Comments

@rickyzhang012500
Copy link

Direct leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x557615942dde in malloc /tmp/llvm_release/final/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:69:3
    #1 0x7fb38c1951ae in tbb::detail::r1::allocate_memory(unsigned long) /proc/self/cwd/external/oneTBB/src/tbb/allocator.cpp:251:20
    #2 0x7fb38c21dc88 in tbb::detail::d1::tbb_allocator<std::atomic<tbb::detail::d1::list_node<unsigned long>*>>::allocate(unsigned long) /proc/self/cwd/external/oneTBB/include/oneapi/tbb/tbb_allocator.h:60:32
    #3 0x7fb38c21dc88 in std::allocator_traits<tbb::detail::d1::tbb_allocator<std::atomic<tbb::detail::d1::list_node<unsigned long>*>>>::allocate(tbb::detail::d1::tbb_allocator<std::atomic<tbb::detail::d1::list_node<unsigned long>*>>&, unsigned long) /usr/lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/alloc_traits.h:306:20
    #4 0x7fb38c21dc88 in tbb::detail::d1::concurrent_unordered_base<tbb::detail::d1::concurrent_unordered_map_traits<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::hash<int>, std::equal_to<int>, tbb::detail::d1::tbb_allocator<std::pair<int const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>, false>>::unordered_segment_table::create_segment(std::atomic<std::atomic<tbb::detail::d1::list_node<unsigned long>*>*>*, unsigned long, unsigned long) /proc/self/cwd/external/oneTBB/include/oneapi/tbb/detail/_concurrent_unordered_base.h:827:40
    #5 0x7fb38c21dc88 in tbb::detail::d1::segment_table<std::atomic<tbb::detail::d1::list_node<unsigned long>*>, tbb::detail::d1::tbb_allocator<std::pair<int const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>, tbb::detail::d1::concurrent_unordered_base<tbb::detail::d1::concurrent_unordered_map_traits<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::hash<int>, std::equal_to<int>, tbb::detail::d1::tbb_allocator<std::pair<int const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>, false>>::unordered_segment_table, 63ul>::enable_segment(std::atomic<tbb::detail::d1::list_node<unsigned long>*>*&, std::atomic<std::atomic<tbb::detail::d1::list_node<unsigned long>*>*>*, unsigned long, unsigned long) /proc/self/cwd/external/oneTBB/include/oneapi/tbb/detail/_segment_table.h:170:44
    #6 0x7fb38c21dc88 in std::atomic<tbb::detail::d1::list_node<unsigned long>*>& tbb::detail::d1::segment_table<std::atomic<tbb::detail::d1::list_node<unsigned long>*>, tbb::detail::d1::tbb_allocator<std::pair<int const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>, tbb::detail::d1::concurrent_unordered_base<tbb::detail::d1::concurrent_unordered_map_traits<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::hash<int>, std::equal_to<int>, tbb::detail::d1::tbb_allocator<std::pair<int const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>, false>>::unordered_segment_table, 63ul>::internal_subscript<true>(unsigned long) /proc/self/cwd/external/oneTBB/include/oneapi/tbb/detail/_segment_table.h:511:17
    #7 0x7fb38c21dc88 in tbb::detail::d1::segment_table<std::atomic<tbb::detail::d1::list_node<unsigned long>*>, tbb::detail::d1::tbb_allocator<std::pair<int const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>, tbb::detail::d1::concurrent_unordered_base<tbb::detail::d1::concurrent_unordered_map_traits<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::hash<int>, std::equal_to<int>, tbb::detail::d1::tbb_allocator<std::pair<int const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>, false>>::unordered_segment_table, 63ul>::operator[](unsigned long) /proc/self/cwd/external/oneTBB/include/oneapi/tbb/detail/_segment_table.h:153:16
    #8 0x7fb38c21dc88 in tbb::detail::d1::concurrent_unordered_base<tbb::detail::d1::concurrent_unordered_map_traits<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::hash<int>, std::equal_to<int>, tbb::detail::d1::tbb_allocator<std::pair<int const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>, false>>::init_bucket(unsigned long) /proc/self/cwd/external/oneTBB/include/oneapi/tbb/detail/_concurrent_unordered_base.h:1099:16
    #9 0x7fb38c21d980 in tbb::detail::d1::concurrent_unordered_base<tbb::detail::d1::concurrent_unordered_map_traits<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::hash<int>, std::equal_to<int>, tbb::detail::d1::tbb_allocator<std::pair<int const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>, false>>::get_bucket(unsigned long) /proc/self/cwd/external/oneTBB/include/oneapi/tbb/detail/_concurrent_unordered_base.h:1084:13
    #10 0x7fb38c21c9eb in tbb::detail::d1::concurrent_unordered_base<tbb::detail::d1::concurrent_unordered_map_traits<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::hash<int>, std::equal_to<int>, tbb::detail::d1::tbb_allocator<std::pair<int const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>, false>>::prepare_bucket(unsigned long) /proc/self/cwd/external/oneTBB/include/oneapi/tbb/detail/_concurrent_unordered_base.h:1078:16
    #11 0x7fb38c21c9eb in tbb::detail::d1::value_node<std::pair<int const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, unsigned long>* tbb::detail::d1::concurrent_unordered_base<tbb::detail::d1::concurrent_unordered_map_traits<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::hash<int>, std::equal_to<int>, tbb::detail::d1::tbb_allocator<std::pair<int const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>, false>>::internal_find<int>(int const&) /proc/self/cwd/external/oneTBB/include/oneapi/tbb/detail/_concurrent_unordered_base.h:1257:25
    #12 0x7fb38c21c9eb in tbb::detail::d1::concurrent_unordered_base<tbb::detail::d1::concurrent_unordered_map_traits<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::hash<int>, std::equal_to<int>, tbb::detail::d1::tbb_allocator<std::pair<int const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>, false>>::find(int const&) /proc/self/cwd/external/oneTBB/include/oneapi/tbb/detail/_concurrent_unordered_base.h:549:33
    #13 0x7fb38c21c9eb in tbb::detail::d1::concurrent_unordered_map<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::hash<int>, std::equal_to<int>, tbb::detail::d1::tbb_allocator<std::pair<int const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>>::operator[](int const&) /proc/self/cwd/external/oneTBB/include/oneapi/tbb/concurrent_unordered_map.h:91:32

Compiler clang-13.1

@pavelkumbrasev
Copy link
Contributor

Hi @rickyzhang012500, could you please provide a reproducer for this problem. Our tests don't reproduce same problem.

@rickyzhang012500
Copy link
Author

//file: thread_util.h

 #ifndef EXPEREIMENTAL_USERS_THREAD_UTIL_H_
#define EXPEREIMENTAL_USERS_THREAD_UTIL_H_

#include <sys/types.h>  // for pid_t
#include <string>
#include "oneapi/tbb/concurrent_unordered_map.h"
#include "global/singleton.h"

namespace ding {
#define CONTEXT_THREAD_NAME_SET(thread_name) \
  ding::ThreadUtil::Instance()->SetThreadName(thread_name);
#define CONTEXT_THREAD_NAME_GET(pid) \
  ding::ThreadUtil::Instance()->GetThreadName(pid);
pid_t GetTid(void);
class ThreadUtil {
 public:
  void SetThreadName(const std::string& name);
  const std::string GetThreadName(int pid);
 private:
  tbb::concurrent_unordered_map<int, std::string> pid_to_name_;
  DECLARE_SINGLETON(ThreadUtil);
};
}  // namespace ding
#endif  // EXPEREIMENTAL_USERS_THREAD_UTIL_H_

// file: thread_util.cc

#include "thread_util.h"

#include <sys/syscall.h>  // for __NR_gettid
#include <unistd.h>       // for syscall

#include <utility>  // for pair

#include "absl/strings/str_cat.h"  // for StrCat
#include "oneapi/tbb/concurrent_u#include "thread_util.h"

#include <sys/syscall.h>  // for __NR_gettid
#include <unistd.h>       // for syscall

#include <utility>  // for pair

#include "absl/strings/str_cat.h"  // for StrCat
#include "oneapi/tbb/concurrent_unordered_map.h"
#include "utils/file_util.h"  // for GetFileContentByGetline

namespace ding {

pid_t GetTid(void) { return (pid_t)syscall(__NR_gettid); }

ThreadUtil::ThreadUtil() {}

void ThreadUtil::SetThreadName(const std::string& name) {
  pid_t pid = GetTid();
  pid_to_name_[pid] = name;
}

const std::string ThreadUtil::GetThreadName(int pid) {
  std::string str;
  auto it = pid_to_name_.find(pid);
  if (it != pid_to_name_.end()) {
    str = it->second;
  } else {
    std::string thread_name;
    std::string thread_file(absl::StrCat("/proc/", pid, "/comm"));
    file_util::GetFileContentByGetline(thread_file, &thread_name);
    str = thread_name.substr(0, thread_name.length() - 1);
    pid_to_name_[pid] = str;
  }
  return str;
}

}  // namespace dingnordered_map.h"
#include "utils/file_util.h"  // for GetFileContentByGetline

namespace ding {

pid_t GetTid(void) { return (pid_t)syscall(__NR_gettid); }

ThreadUtil::ThreadUtil() {}

void ThreadUtil::SetThreadName(const std::string& name) {
  pid_t pid = GetTid();
  pid_to_name_[pid] = name;
}

const std::string ThreadUtil::GetThreadName(int pid) {
  std::string str;
  auto it = pid_to_name_.find(pid);
  if (it != pid_to_name_.end()) {
    str = it->second;
  } else {
    std::string thread_name;
    std::string thread_file(absl::StrCat("/proc/", pid, "/comm"));
    file_util::GetFileContentByGetline(thread_file, &thread_name);
    str = thread_name.substr(0, thread_name.length() - 1);
    pid_to_name_[pid] = str;
  }
  return str;
}

}  // namespace ding

// caller

  write_file_future_ = std::async([this, log_type]() {
    CONTEXT_THREAD_NAME_SET("WriteFile");
    auto stop_or_full = [this]() NO_THREAD_SAFETY_ANALYSIS {
      return stop_thread_.load() ||
             (kv_queue_->size() > kMaxNonSensitiveFullSize);
    };

I guess this problem caused by calling tbb::concurrent_unordered_map method in a thread,such as CONTEXT_THREAD_NAME_SET provided by thread_util.

@nofuturre
Copy link

@rickyzhang012500 is this issue still relevant?

@nofuturre
Copy link

If anyone encounter this issue in the future please open new issue with a link to this one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants