OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[Core] Fix crash in get_id_for_file when hashing std::filesystem::path across ABI boundary (#36729)
Jatin Wadhwab787c77a8e6f
diff --git a/src/common/util/include/openvino/util/file_util.hpp b/src/common/util/include/openvino/util/file_util.hppindex 31b8d47d3dc..de715717d3f 100644--- a/src/common/util/include/openvino/util/file_util.hpp+++ b/src/common/util/include/openvino/util/file_util.hpp@@ -10,6 +10,7 @@#include <functional>#include <initializer_list>#include <string>+#include <type_traits>#include <vector>#include "openvino/util/file_path.hpp"@@ -295,7 +296,13 @@ void save_binary(const std::filesystem::path& path, const void* binary, size_t bconst char* trim_file_name(const char* const fname);inline uint64_t get_id_for_file(const std::filesystem::path& path, size_t offset, size_t size) {- return util::u64_hash_combine(std::filesystem::hash_value(path), {offset, size});+ using unsigned_value_type = std::make_unsigned_t<std::filesystem::path::value_type>;++ uint64_t path_hash = 0;+ for (const auto* value = path.c_str(); *value != 0; ++value) {+ path_hash = util::u64_hash_combine(path_hash, static_cast<uint64_t>(static_cast<unsigned_value_type>(*value)));+ }+ return util::u64_hash_combine(path_hash, {offset, size});}/**diff --git a/src/tests/test_utils/common_test_utils/tests/file_util_test.cpp b/src/tests/test_utils/common_test_utils/tests/file_util_test.cppindex 45c64db8ce7..6713c555317 100644--- a/src/tests/test_utils/common_test_utils/tests/file_util_test.cpp+++ b/src/tests/test_utils/common_test_utils/tests/file_util_test.cpp@@ -693,4 +693,41 @@ TEST_F(SanitizePathTest, empty_dir_dotdot_still_rejected) {EXPECT_THROW(ov::util::sanitize_path("", ".."), std::runtime_error);}+TEST(file_util_get_id_for_file, matches_byte_fold) {+ // Recompute the id by independently folding the raw path bytes with u64_hash_combine. The id must+ // match this fold; if get_id_for_file is switched back to std::filesystem::hash_value / std::hash+ // (ABI-sensitive, crashed across a libstdc++ ABI boundary), it will diverge and this test fails.+ const auto expected_file_id = [](const std::filesystem::path& path, size_t offset, size_t size) {+ using unsigned_value_type = std::make_unsigned_t<std::filesystem::path::value_type>;+ uint64_t path_hash = 0;+ for (const auto* value = path.c_str(); *value != 0; ++value) {+ const auto byte = static_cast<uint64_t>(static_cast<unsigned_value_type>(*value));+ path_hash = ov::util::u64_hash_combine(path_hash, byte);+ }+ return ov::util::u64_hash_combine(path_hash, {offset, size});+ };++ const std::filesystem::path path = "dir/subdir/weights.bin";+ EXPECT_EQ(ov::util::get_id_for_file(path, 0, 128), expected_file_id(path, 0, 128));+ EXPECT_EQ(ov::util::get_id_for_file(path, 64, 256), expected_file_id(path, 64, 256));+}++TEST(file_util_get_id_for_file, is_deterministic) {+ const std::filesystem::path path = "dir/subdir/model.bin";+ // Same inputs -> same id (used as a weight-sharing cache key).+ EXPECT_EQ(ov::util::get_id_for_file(path, 64, 256), ov::util::get_id_for_file(path, 64, 256));+ // Rebuilding the path from its native string must not change the id (id is over the path bytes).+ const std::filesystem::path same_path{path.native()};+ EXPECT_EQ(ov::util::get_id_for_file(path, 64, 256), ov::util::get_id_for_file(same_path, 64, 256));+}++TEST(file_util_get_id_for_file, distinguishes_path_offset_and_size) {+ const std::filesystem::path path_a = "weights_a.bin";+ const std::filesystem::path path_b = "weights_b.bin";+ const auto base = ov::util::get_id_for_file(path_a, 0, 128);+ EXPECT_NE(base, ov::util::get_id_for_file(path_b, 0, 128)); // different path+ EXPECT_NE(base, ov::util::get_id_for_file(path_a, 1, 128)); // different offset+ EXPECT_NE(base, ov::util::get_id_for_file(path_a, 0, 129)); // different size+}+} // namespace ov::test