Skip to content

Commit

Permalink
Fix ASAN issue (#959)
Browse files Browse the repository at this point in the history
ASAN report an error if we tries to downcast a misaligned address.
To avoid it, we need to check if an address is valid before casting it.

Here is the ASAN error:

/home/runner/work/mold/mold/third-party/tbb/src/tbb/../../include/tbb/../oneapi/tbb/concurrent_hash_map.h:446:23: runtime error: downcast of misaligned address 0x000000000003 for type 'tbb::detail::d2::hash_map_iterator<tbb::detail::d2::concurrent_hash_map<std::basic_string_view<char, std::char_traits<char>>, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>, tbb::detail::d1::tbb_hash_compare<std::basic_string_view<char, std::char_traits<char>>>, tbb::detail::d1::tbb_allocator<std::pair<const std::basic_string_view<char, std::char_traits<char>>, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>>>>, std::pair<const std::basic_string_view<char, std::char_traits<char>>, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, st0x000000000003: note: pointer points here
<memory cannot be printed>
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /home/runner/work/mold/mold/third-party/tbb/src/tbb/../../include/tbb/../oneapi/tbb/concurrent_hash_map.h:446:23

Signed-off-by: Rui Ueyama <ruiu@cs.stanford.edu>

Signed-off-by: Rui Ueyama <ruiu@cs.stanford.edu>
  • Loading branch information
rui314 authored Nov 14, 2022
1 parent b14b68a commit 4146a9e
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions include/oneapi/tbb/concurrent_hash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,11 @@ class hash_map_iterator {
if( k&(k-2) ) // not the beginning of a segment
++my_bucket;
else my_bucket = my_map->get_bucket( k );
my_node = static_cast<node*>( my_bucket->node_list.load(std::memory_order_relaxed) );
if( map_base::is_valid(my_node) ) {
my_index = k; return;
node_base *n = my_bucket->node_list.load(std::memory_order_relaxed);
if( map_base::is_valid(n) ) {
my_node = static_cast<node*>(n);
my_index = k;
return;
}
++k;
}
Expand Down

0 comments on commit 4146a9e

Please sign in to comment.