I have a memory alignment problem in a library, which I want to use via wasm. To get more information I enabled USAN which reported the error, but it makes no sense to me, because the error is within the STL, not my code. The class I use is std::unordered_set<> with a custom hasher and a custom comparer. The pointer given to the `insert` method (the error happens already on first insertion) is aligned, I triple checked that. Here's the full stack trace:
proposed: 0x3bebe8
/opt/homebrew/Cellar/emscripten/3.1.43/libexec/cache/sysroot/include/c++/v1/__memory/unique_ptr.h:469:12: runtime error: reference binding to misaligned address 0x3307474f for type 'std::__hash_node_base<std::__hash_node<antlr4::dfa::DFAState *, void *> *> *', which requires 4 byte alignment
0x3307474f: note: pointer points here
RuntimeError: memory access out of bounds
at __ubsan::Diag::~Diag() (wasm://wasm/0a18e74a:wasm-function[23303]:0xa1fae2)
at handleTypeMismatchImpl(__ubsan::TypeMismatchData*, unsigned long, __ubsan::ReportOptions) (wasm://wasm/0a18e74a:wasm-function[23315]:0xa20b40)
at __ubsan_handle_type_mismatch_v1 (wasm://wasm/0a18e74a:wasm-function[23314]:0xa20695)
at std::__2::unique_ptr<std::__2::__hash_node_base<std::__2::__hash_node<antlr4::dfa::DFAState*, void*>*>* [], std::__2::__bucket_list_deallocator<std::__2::allocator<std::__2::__hash_node_base<std::__2::__hash_node<antlr4::dfa::DFAState*, void*>*>*>>>::operator[][abi:v160004](unsigned long) const (wasm://wasm/0a18e74a:wasm-function[16520]:0x6dde0a)
at std::__2::pair<std::__2::__hash_iterator<std::__2::__hash_node<antlr4::dfa::DFAState*, void*>*>, bool> std::__2::__hash_table<antlr4::dfa::DFAState*, antlr4::dfa::DFA::DFAStateHasher, antlr4::dfa::DFA::DFAStateComparer, std::__2::allocator<antlr4::dfa::DFAState*>>::__emplace_unique_key_args<antlr4::dfa::DFAState*, antlr4::dfa::DFAState* const&>(antlr4::dfa::DFAState* const&, antlr4::dfa::DFAState* const&) (wasm://wasm/0a18e74a:wasm-function[16515]:0x6dbbfb)
at std::__2::__hash_table<antlr4::dfa::DFAState*, antlr4::dfa::DFA::DFAStateHasher, antlr4::dfa::DFA::DFAStateComparer, std::__2::allocator<antlr4::dfa::DFAState*>>::__insert_unique[abi:v160004](antlr4::dfa::DFAState* const&) (wasm://wasm/0a18e74a:wasm-function[16387]:0x6cb146)
at std::__2::unordered_set<antlr4::dfa::DFAState*, antlr4::dfa::DFA::DFAStateHasher, antlr4::dfa::DFA::DFAStateComparer, std::__2::allocator<antlr4::dfa::DFAState*>>::insert[abi:v160004](antlr4::dfa::DFAState* const&) (wasm://wasm/0a18e74a:wasm-function[16382]:0x6ca8f2)
at antlr4::atn::LexerATNSimulator::addDFAState(antlr4::atn::ATNConfigSet*, bool) (wasm://wasm/0a18e74a:wasm-function[16379]:0x6c9deb)
at antlr4::atn::LexerATNSimulator::matchATN(antlr4::CharStream*) (wasm://wasm/0a18e74a:wasm-function[16306]:0x6a4858)
at antlr4::atn::LexerATNSimulator::match(antlr4::CharStream*, unsigned long) (wasm://wasm/0a18e74a:wasm-function[16291]:0x6a095c)
To me it looks like the set tries to insert a random memory address. I added console logging in the entire path, including the hasher, to see what's going on (feeling like debugging in the 90s :-( ) and everything looks good. The hash is created and looks valid, but after that the crash happens. So what else could I do to debug further?
No unique pointers involved here, so this must be something internal to the set.