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

Clang UBSan alignment error in tbb::concurrent_hash_map::hash_map_iterator constructor #1601

Open
stephen-yee-adsk opened this issue Jan 13, 2025 · 2 comments · May be fixed by #1615
Open
Assignees
Labels

Comments

@stephen-yee-adsk
Copy link

Summary

UBSan reports an error in the tbb::concurrent_hash_map::hash_map_iterator constructor. It looks like the same error as the one reported for the hash_map_iterator::advance_to_next_bucket UBSan failure here.

    template<typename Container, typename Value>
    hash_map_iterator<Container,Value>::hash_map_iterator( const Container &map, size_t index, const bucket *b, node_base *n ) :
        my_map(&map),
        my_index(index),
        my_bucket(b),
        my_node( static_cast<node*>(n) )          // <-- UB static_cast to node*, as n is 0x3. It is not an 8-byte aligned address.
    {
        if( b && !hash_map_base::is_valid(n) )
            advance_to_next_bucket();
    }

Version

TBB version 2020.3

Environment

Rocky8
Clang 14.0.3

Observed Behavior

When using tbb::concurrent_hash_map::range() undefined behavior sanitizer generates this error:

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /path_to_tbb/tbb/2020.3/include/tbb/concurrent_hash_map.h:442:18 in 
/path_to_tbb/tbb/2020.3/include/tbb/concurrent_hash_map.h:377:27: runtime error: downcast of misaligned address 0x000000000003 for type 'tbb::interface5::internal::hash_map_iterator<tbb::interface5::concurrent_hash_map<int, int>, std::pair<const int, int>>::node' (aka 'tbb::interface5::concurrent_hash_map<int, int>::node'), which requires 8 byte alignment
0x000000000003: note: pointer points here

Expected Behavior

Undefined behavior sanitizer should not generate any errors when using tbb::concurrent_hash_map::range()

Steps To Reproduce

Paste into tbb-concurrent_hash_map-ubsan.cpp:

#include <tbb/concurrent_hash_map.h>

int
main()
{
  tbb::concurrent_hash_map<int, int> map;

  map.insert({0, 0});
  auto r = map.range();

  return 0;
}

Compile with: clang++ -g -fsanitize=undefined tbb-concurrent_hash_map-ubsan.cpp -o tbb-concurrent_hash_map-ubsan -ltbb

I fixed this locally by changing the hash_map_iterator constructor to this:


hash_map_iterator(const HashMapType& map,
                                                                    size_t             index,
                                                                    const bucket*      b,
                                                                    node_base*         n)
    : my_map(&map), my_index(index), my_bucket(b), my_node(nullptr) {
    if (b) {
        if (!hash_map_base::is_valid(n))
            advance_to_next_bucket();
        else
            my_node = static_cast<node*>(n);
    }
}
@omalyshe
Copy link
Contributor

@stephen-yee-adsk Note that TBB 2020.3 is a very old TBB version and is not supported anymore. We recommend using the latest oneTBB version if possible.

@stephen-yee-adsk
Copy link
Author

@omalyshe thanks, I just re-tested, linking against oneTBB 2021.12 and I get the same error:

/tbb/2021.12/include/tbb/../oneapi/tbb/concurrent_hash_map.h:470:62: runtime error: downcast of misaligned address 0x000000000003 for type 'tbb::detail::d2::hash_map_iterator<tbb::detail::d2::concurrent_hash_map<int, int>, std::pair<const int, int>>::node' (aka 'tbb::detail::d2::concurrent_hash_map<int, int>::node'), which requires 8 byte alignment
0x000000000003: note: pointer points here
<memory cannot be printed>
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /tbb/2021.12/include/tbb/../oneapi/tbb/concurrent_hash_map.h:470:62 in 

@dnmokhov dnmokhov linked a pull request Jan 25, 2025 that will close this issue
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants