Skip to content

Address undefined behavior related to hashing enums#1045

Merged
dsuponitskiy merged 1 commit intoopenfheorg:devfrom
j2kun:ubsan-enum
Sep 24, 2025
Merged

Address undefined behavior related to hashing enums#1045
dsuponitskiy merged 1 commit intoopenfheorg:devfrom
j2kun:ubsan-enum

Conversation

@j2kun
Copy link
Collaborator

@j2kun j2kun commented Aug 18, 2025

The hashing of this untyped enum was flagged as undefined behavior in v1.4.0 by ubsan. The compiler is allowed to pick the type of the enum, and static_cast from an int to the enum (as performed by unordered_map) is UB.

@j2kun j2kun requested review from dsuponitskiy and pascoec August 18, 2025 19:00
@j2kun
Copy link
Collaborator Author

j2kun commented Aug 18, 2025

If you'll excuse the noise of a Google-internal stack trace, here's the ubsan error:

third_party/crosstool/v18/stable/src/libcxx/include/unordered_map:658:108: runtime error: load of value 4, which is not a valid value for type 'const TEST_CASE_TYPE'
    #0 0x7f0106474487 in operator() third_party/crosstool/v18/stable/src/libcxx/include/unordered_map:658:108
    #1 0x7f0106474487 in std::__u::pair<std::__u::__hash_iterator<std::__u::__hash_node<std::__u::__hash_value_type<TEST_CASE_TYPE, std::__u::basic_string<char, std::__u::char_traits<char>, std::__u::allocator<char>>>, void*>*>, bool> std::__u::__hash_table<std::__u::__hash_value_type<TEST_CASE_TYPE, std::__u::basic_string<char, std::__u::char_traits<char>, std::__u::allocator<char>>>, std::__u::__unordered_map_hasher<TEST_CASE_TYPE, std::__u::pair<TEST_CASE_TYPE const, std::__u::basic_string<char, std::__u::char_traits<char>, std::__u::allocator<char>>>, std::__u::hash<TEST_CASE_TYPE>, std::__u::equal_to<TEST_CASE_TYPE>, true>, std::__u::__unordered_map_equal<TEST_CASE_TYPE, std::__u::pair<TEST_CASE_TYPE const, std::__u::basic_string<char, std::__u::char_traits<char>, std::__u::allocator<char>>>, std::__u::equal_to<TEST_CASE_TYPE>, std::__u::hash<TEST_CASE_TYPE>, true>, std::__u::allocator<std::__u::pair<TEST_CASE_TYPE const, std::__u::basic_string<char, std::__u::char_traits<char>, std::__u::allocator<char>>>>>::__emplace_unique_key_args<TEST_CASE_TYPE, std::__u::pair<TEST_CASE_TYPE const, std::__u::basic_string<char, std::__u::char_traits<char>, std::__u::allocator<char>>> const&>(TEST_CASE_TYPE const&, std::__u::pair<TEST_CASE_TYPE const, std::__u::basic_string<char, std::__u::char_traits<char>, std::__u::allocator<char>>> const&) third_party/crosstool/v18/stable/src/libcxx/include/__hash_table:1513:21
    #2 0x7f01066effce in __emplace_unique_extract_key third_party/crosstool/v18/stable/src/libcxx/include/__hash_table:800:12
    #3 0x7f01066effce in __emplace_unique third_party/crosstool/v18/stable/src/libcxx/include/__hash_table:775:12
    #4 0x7f01066effce in insert third_party/crosstool/v18/stable/src/libcxx/include/unordered_map:1642:14
    #5 0x7f01066effce in unordered_map third_party/crosstool/v18/stable/src/libcxx/include/unordered_map:1598:3
    #6 0x7f01066effce in operator<<(std::__u::basic_ostream<char, std::__u::char_traits<char>>&, TEST_CASE_TYPE const&) third_party/openfhe/src/pke/unittest/utckksrns/UnitTestInteractiveBootstrap.cpp:71:78
    #7 0x7f01066f38da in TEST_CASE_UTCKKSRNS_INTERACTIVE_BOOT::buildTestName() const third_party/openfhe/src/pke/unittest/utckksrns/UnitTestInteractiveBootstrap.cpp:96:12
    #8 0x7f01066efcc2 in operator() third_party/openfhe/src/pke/unittest/utckksrns/UnitTestInteractiveBootstrap.cpp:112:32
    #9 0x7f01066efcc2 in gtest_UnitTestsUTCKKSRNS_INTERACTIVE_BOOT_EvalGenerateName_(testing::TestParamInfo<TEST_CASE_UTCKKSRNS_INTERACTIVE_BOOT> const&) third_party/openfhe/src/pke/unittest/utckksrns/UnitTestInteractiveBootstrap.cpp:668:1
    #10 0x7f010670a8ae in testing::internal::ParameterizedTestSuiteInfo<UTCKKSRNS_INTERACTIVE_BOOT>::RegisterTests() third_party/googletest/googletest/include/gtest/internal/gtest-param-util.h:581:15
    #11 0x7f0103b0fc69 in RegisterTests third_party/googletest/googletest/include/gtest/internal/gtest-param-util.h:736:24
    #12 0x7f0103b0fc69 in RegisterParameterizedTests third_party/googletest/googletest/src/gtest.cc:2864:34
    #13 0x7f0103b0fc69 in testing::internal::UnitTestImpl::PostFlagParsingInit() third_party/googletest/googletest/src/gtest.cc:5820:5
    #14 0x7f0103b1db32 in void testing::internal::InitGoogleTestImpl<char>(int*, char**) third_party/googletest/googletest/src/gtest.cc:6994:22
    #15 0x7f0103b12b08 in testing::InitGoogleTest(int*, char**) third_party/googletest/googletest/src/gtest.cc:7012:3
    #16 0x7f0104772ea7 in main third_party/openfhe/test/Main_TestAll.cpp:164:5
    #17 0x7f00b34c33d3 in __libc_start_main (/usr/grte/v5/lib64/libc.so.6+0x613d3) (BuildId: 9a996398ce14a94560b0c642eb4f6e94)
    #18 0x5648aceea9a9 in _start /usr/grte/v5/debug-src/src/csu/../sysdeps/x86_64/start.S:120

SUMMARY: UndefinedBehaviorSanitizer: invalid-enum-load third_party/crosstool/v18/stable/src/libcxx/include/unordered_map:658:108  (//third_party/openfhe/src/pke:pke_tests)

@pascoec pascoec self-assigned this Aug 27, 2025
@yspolyakov yspolyakov added this to the Release 1.4.1 milestone Sep 23, 2025
@yspolyakov
Copy link
Contributor

@dsuponitskiy Please add an issue for this PR and also review the changes.

@dsuponitskiy dsuponitskiy merged commit cd96012 into openfheorg:dev Sep 24, 2025
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

Successfully merging this pull request may close these issues.

Undefined behavior related to creating an invalid enum via an int->enum conversion (make enum underlying type explicit to avoid UB)

4 participants