Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[core] Set read-only permission on compiled blob (#32403)

Pawel Raasz5ed0cd598509
diff --git a/src/inference/src/cache_manager.hpp b/src/inference/src/cache_manager.hpp
index b00c406613a..34cc5254163 100644
--- a/src/inference/src/cache_manager.hpp
+++ b/src/inference/src/cache_manager.hpp
@@ -135,8 +135,12 @@ private:
void write_cache_entry(const std::string& id, StreamWriter writer) override {
// Fix the bug caused by pugixml, which may return unexpected results if the locale is different from "C".
ScopedLocale plocal_C(LC_ALL, "C");
- std::ofstream stream(getBlobFile(id), std::ios_base::binary | std::ofstream::out);
+ const auto blob_path = getBlobFile(id);
+ std::ofstream stream(blob_path, std::ios_base::binary);
writer(stream);
+ stream.close();
+ std::filesystem::permissions(blob_path,
+ std::filesystem::perms::owner_read | std::filesystem::perms::group_read);
}
void read_cache_entry(const std::string& id, bool enable_mmap, StreamReader reader) override {
diff --git a/src/inference/tests/functional/caching_test.cpp b/src/inference/tests/functional/caching_test.cpp
index 4e48df837be..d474ee3bf86 100644
--- a/src/inference/tests/functional/caching_test.cpp
+++ b/src/inference/tests/functional/caching_test.cpp
@@ -1684,8 +1684,15 @@ TEST_P(CachingTest, TestCacheFileCorrupted) {
{
auto blobs = ov::test::utils::listFilesWithExt(m_cacheDir, "blob");
for (const auto& fileName : blobs) {
+ std::filesystem::permissions(fileName,
+ std::filesystem::perms::owner_write,
+ std::filesystem::perm_options::add);
std::ofstream stream(fileName, std::ios_base::binary);
stream << "SomeCorruptedText";
+ stream.close();
+ std::filesystem::permissions(fileName,
+ std::filesystem::perms::owner_write,
+ std::filesystem::perm_options::remove);
}
}
m_post_mock_net_callbacks.pop_back();
@@ -1764,8 +1771,16 @@ TEST_P(CachingTest, TestCacheFileOldVersion) {
} else {
return; // skip test
}
+
+ std::filesystem::permissions(fileName,
+ std::filesystem::perms::owner_write,
+ std::filesystem::perm_options::add);
std::ofstream out(fileName, std::ios_base::binary);
out.write(content.c_str(), static_cast<std::streamsize>(content.size()));
+ out.close();
+ std::filesystem::permissions(fileName,
+ std::filesystem::perms::owner_write,
+ std::filesystem::perm_options::remove);
}
}
m_post_mock_net_callbacks.pop_back();
@@ -1863,8 +1878,15 @@ TEST_P(CachingTest, TestCacheFileWithCompiledModelRuntimeProperties) {
} else {
return; // skip test
}
+ std::filesystem::permissions(fileName,
+ std::filesystem::perms::owner_write,
+ std::filesystem::perm_options::add);
std::ofstream out(fileName, std::ios_base::binary);
out.write(content.c_str(), static_cast<std::streamsize>(content.size()));
+ out.close();
+ std::filesystem::permissions(fileName,
+ std::filesystem::perms::owner_write,
+ std::filesystem::perm_options::remove);
}
}
m_post_mock_net_callbacks.pop_back();
diff --git a/src/tests/functional/base_func_tests/src/behavior/ov_plugin/caching_tests.cpp b/src/tests/functional/base_func_tests/src/behavior/ov_plugin/caching_tests.cpp
index cafd021163f..d38a62b5857 100644
--- a/src/tests/functional/base_func_tests/src/behavior/ov_plugin/caching_tests.cpp
+++ b/src/tests/functional/base_func_tests/src/behavior/ov_plugin/caching_tests.cpp
@@ -386,8 +386,8 @@ TEST_P(CompileModelLoadFromFileTestBase, CanCreateCacheDirAndDumpBinariesUnicode
// Check that directory with cached model exists after loading network
ASSERT_TRUE(ov::util::directory_exists(cache_path_w)) << "Directory with cached kernels doesn't exist";
// Check that folder contains cache files and remove them
- int removed_files_num = 0;
- removed_files_num += ov::test::utils::removeFilesWithExt(cache_path_w, ov::util::string_to_wstring("blob"));
+ auto removed_files_num =
+ ov::test::utils::removeFilesWithExt<opt::FORCE>(cache_path_w, ov::util::string_to_wstring("blob"));
removed_files_num += ov::test::utils::removeFilesWithExt(cache_path_w, ov::util::string_to_wstring("cl_cache"));
ASSERT_GT(removed_files_num, 0);
ov::test::utils::removeFile(model_xml_path_w);
@@ -399,7 +399,9 @@ TEST_P(CompileModelLoadFromFileTestBase, CanCreateCacheDirAndDumpBinariesUnicode
// Cleanup in case of any exception
if (ov::util::directory_exists(cache_path_w)) {
// Check that folder contains cache files and remove them
- ASSERT_GT(ov::test::utils::removeFilesWithExt(cache_path_w, ov::util::string_to_wstring("blob")), 0);
+ ASSERT_GT(
+ ov::test::utils::removeFilesWithExt<opt::FORCE>(cache_path_w, ov::util::string_to_wstring("blob")),
+ 0);
ov::test::utils::removeFile(model_xml_path_w);
ov::test::utils::removeFile(model_bin_path_w);
ov::test::utils::removeDir(cache_path_w);
@@ -496,8 +498,13 @@ void CompileModelCacheRuntimePropertiesTestBase::run() {
m_compiled_model_runtime_properties.replace(1, 1, "x");
}
content.replace(index, m_compiled_model_runtime_properties.size(), m_compiled_model_runtime_properties);
+ std::filesystem::permissions(fileName, std::filesystem::perms::owner_write, std::filesystem::perm_options::add);
std::ofstream out(fileName, std::ios_base::binary);
out.write(content.c_str(), static_cast<std::streamsize>(content.size()));
+ out.close();
+ std::filesystem::permissions(fileName,
+ std::filesystem::perms::owner_write,
+ std::filesystem::perm_options::remove);
}
// Third compile model to remove old cache blob and create new model cache blob file
@@ -798,7 +805,7 @@ TEST_P(CompiledKernelsCacheTest, CanCreateCacheDirAndDumpBinaries) {
int number_of_deleted_files = 0;
for (auto& ext : m_extList) {
// Check that folder contains cache files and remove them
- number_of_deleted_files += ov::test::utils::removeFilesWithExt(cache_path, ext);
+ number_of_deleted_files += ov::test::utils::removeFilesWithExt<opt::FORCE>(cache_path, ext);
}
ASSERT_GT(number_of_deleted_files, 0);
// Remove directory and check that it doesn't exist anymore
@@ -840,7 +847,7 @@ TEST_P(CompiledKernelsCacheTest, TwoNetworksWithSameModelCreatesSameCache) {
for (auto& ext : m_extList) {
// Check that folder contains cache files and remove them
n_cache_files_compare += ov::test::utils::listFilesWithExt(cache_path, ext).size();
- number_of_deleted_files += ov::test::utils::removeFilesWithExt(cache_path, ext);
+ number_of_deleted_files += ov::test::utils::removeFilesWithExt<opt::FORCE>(cache_path, ext);
}
ASSERT_GT(number_of_deleted_files, 0);
ASSERT_EQ(n_cache_files_compare, n_cache_files);
@@ -881,7 +888,9 @@ TEST_P(CompiledKernelsCacheTest, CanCreateCacheDirAndDumpBinariesUnicodePath) {
int count_of_removed_files = 0;
for (auto& ext : m_extList) {
// Check that folder contains cache files and remove them
- count_of_removed_files += ov::test::utils::removeFilesWithExt(cache_path_w, ov::test::utils::stringToWString(ext));
+ count_of_removed_files +=
+ ov::test::utils::removeFilesWithExt<opt::FORCE>(cache_path_w,
+ ov::test::utils::stringToWString(ext));
}
ASSERT_GT(count_of_removed_files, 0);
// Remove directory and check that it doesn't exist anymore
diff --git a/src/tests/test_utils/common_test_utils/include/common_test_utils/common_utils.hpp b/src/tests/test_utils/common_test_utils/include/common_test_utils/common_utils.hpp
index 6d0f0ea2fce..4fb704fb811 100644
--- a/src/tests/test_utils/common_test_utils/include/common_test_utils/common_utils.hpp
+++ b/src/tests/test_utils/common_test_utils/include/common_test_utils/common_utils.hpp
@@ -17,6 +17,10 @@
#include "openvino/core/partial_shape.hpp"
+namespace ov::test::opt {
+inline constexpr auto FORCE = true;
+}
+
namespace ov {
namespace test {
namespace utils {
diff --git a/src/tests/test_utils/common_test_utils/include/common_test_utils/file_utils.hpp b/src/tests/test_utils/common_test_utils/include/common_test_utils/file_utils.hpp
index 49e577adb2d..8704e8cdb32 100644
--- a/src/tests/test_utils/common_test_utils/include/common_test_utils/file_utils.hpp
+++ b/src/tests/test_utils/common_test_utils/include/common_test_utils/file_utils.hpp
@@ -89,6 +89,7 @@ inline void removeIRFiles(const std::string& xmlFilePath, const std::string& bin
// Return value:
// < 0 - error
// >= 0 - count of removed files
+template <bool Force = !opt::FORCE>
inline int removeFilesWithExt(std::string path, std::string ext) {
struct dirent* ent;
DIR* dir = opendir(path.c_str());
@@ -99,6 +100,11 @@ inline int removeFilesWithExt(std::string path, std::string ext) {
struct stat stat_path;
stat(file.c_str(), &stat_path);
if (!S_ISDIR(stat_path.st_mode) && endsWith(file, "." + ext)) {
+ if constexpr (Force) {
+ std::filesystem::permissions(file,
+ std::filesystem::perms::owner_write,
+ std::filesystem::perm_options::add);
+ }
auto err = std::remove(file.c_str());
if (err != 0) {
closedir(dir);
diff --git a/src/tests/test_utils/common_test_utils/include/common_test_utils/unicode_utils.hpp b/src/tests/test_utils/common_test_utils/include/common_test_utils/unicode_utils.hpp
index 2fdea374b44..ff874cc5497 100644
--- a/src/tests/test_utils/common_test_utils/include/common_test_utils/unicode_utils.hpp
+++ b/src/tests/test_utils/common_test_utils/include/common_test_utils/unicode_utils.hpp
@@ -88,6 +88,7 @@ inline bool endsWith(const std::wstring& source, const std::wstring& expectedSuf
// Return value:
// < 0 - error
// >= 0 - count of removed files
+template <bool Force = !opt::FORCE>
inline int removeFilesWithExt(std::wstring path, std::wstring ext) {
int ret = 0;
# ifdef _WIN32
@@ -99,6 +100,11 @@ inline int removeFilesWithExt(std::wstring path, std::wstring ext) {
struct _stat64i32 stat_path;
_wstat(file.c_str(), &stat_path);
if (!S_ISDIR(stat_path.st_mode) && endsWith(file, L"." + ext)) {
+ if constexpr (Force) {
+ std::filesystem::permissions(file,
+ std::filesystem::perms::owner_write,
+ std::filesystem::perm_options::add);
+ }
auto err = _wremove(file.c_str());
if (err != 0) {
_wclosedir(dir);
@@ -120,6 +126,11 @@ inline int removeFilesWithExt(std::wstring path, std::wstring ext) {
struct stat stat_path;
stat(file.c_str(), &stat_path);
if (!S_ISDIR(stat_path.st_mode) && endsWith(file, "." + ext_mb)) {
+ if constexpr (Force) {
+ std::filesystem::permissions(file,
+ std::filesystem::perms::owner_write,
+ std::filesystem::perm_options::add);
+ }
auto err = std::remove(file.c_str());
if (err != 0) {
closedir(dir);