mirror of
https://github.com/Evolution-X/hardware_interfaces
synced 2026-02-01 16:50:18 +00:00
Merge "Revert "Allow compilations in TOCTOU tests to fail with GENERAL_FAILURE."" into rvc-dev
This commit is contained in:
@@ -315,8 +315,7 @@ class CompilationCachingTestBase : public testing::Test {
|
|||||||
|
|
||||||
void saveModelToCache(const Model& model, const hidl_vec<hidl_handle>& modelCache,
|
void saveModelToCache(const Model& model, const hidl_vec<hidl_handle>& modelCache,
|
||||||
const hidl_vec<hidl_handle>& dataCache,
|
const hidl_vec<hidl_handle>& dataCache,
|
||||||
sp<IPreparedModel>* preparedModel = nullptr,
|
sp<IPreparedModel>* preparedModel = nullptr) {
|
||||||
bool allowGeneralFailure = false) {
|
|
||||||
if (preparedModel != nullptr) *preparedModel = nullptr;
|
if (preparedModel != nullptr) *preparedModel = nullptr;
|
||||||
|
|
||||||
// Launch prepare model.
|
// Launch prepare model.
|
||||||
@@ -330,10 +329,7 @@ class CompilationCachingTestBase : public testing::Test {
|
|||||||
|
|
||||||
// Retrieve prepared model.
|
// Retrieve prepared model.
|
||||||
preparedModelCallback->wait();
|
preparedModelCallback->wait();
|
||||||
const auto prepareCallbackStatus = preparedModelCallback->getStatus();
|
ASSERT_EQ(preparedModelCallback->getStatus(), ErrorStatus::NONE);
|
||||||
if (!allowGeneralFailure || prepareCallbackStatus != ErrorStatus::GENERAL_FAILURE) {
|
|
||||||
ASSERT_EQ(prepareCallbackStatus, ErrorStatus::NONE);
|
|
||||||
}
|
|
||||||
if (preparedModel != nullptr) {
|
if (preparedModel != nullptr) {
|
||||||
*preparedModel = IPreparedModel::castFrom(preparedModelCallback->getPreparedModel())
|
*preparedModel = IPreparedModel::castFrom(preparedModelCallback->getPreparedModel())
|
||||||
.withDefault(nullptr);
|
.withDefault(nullptr);
|
||||||
@@ -1026,8 +1022,7 @@ static void copyCacheFiles(const std::vector<std::vector<std::string>>& from,
|
|||||||
|
|
||||||
// Number of operations in the large test model.
|
// Number of operations in the large test model.
|
||||||
constexpr uint32_t kLargeModelSize = 100;
|
constexpr uint32_t kLargeModelSize = 100;
|
||||||
constexpr uint32_t kNumSuccessfulIterationsTOCTOU = 100;
|
constexpr uint32_t kNumIterationsTOCTOU = 100;
|
||||||
constexpr uint32_t kMaxNumFailedIterationsTOCTOU = 100;
|
|
||||||
|
|
||||||
TEST_P(CompilationCachingTest, SaveToCache_TOCTOU) {
|
TEST_P(CompilationCachingTest, SaveToCache_TOCTOU) {
|
||||||
if (!mIsCachingSupported) return;
|
if (!mIsCachingSupported) return;
|
||||||
@@ -1055,30 +1050,18 @@ TEST_P(CompilationCachingTest, SaveToCache_TOCTOU) {
|
|||||||
// Use a different token for modelAdd.
|
// Use a different token for modelAdd.
|
||||||
mToken[0]++;
|
mToken[0]++;
|
||||||
|
|
||||||
// This test is probabilistic, so we run it multiple times. We allow the compilation to fail
|
// This test is probabilistic, so we run it multiple times.
|
||||||
// because it is not related to the security aspect of the TOCTOU test. However, we need to have
|
for (uint32_t i = 0; i < kNumIterationsTOCTOU; i++) {
|
||||||
// enough successful iterations to ensure the test coverage.
|
|
||||||
uint32_t numSuccessfulIterations = 0, numFailedIterations = 0;
|
|
||||||
while (numSuccessfulIterations < kNumSuccessfulIterationsTOCTOU) {
|
|
||||||
// Save the modelAdd compilation to cache.
|
// Save the modelAdd compilation to cache.
|
||||||
{
|
{
|
||||||
hidl_vec<hidl_handle> modelCache, dataCache;
|
hidl_vec<hidl_handle> modelCache, dataCache;
|
||||||
createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache);
|
createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache);
|
||||||
createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
|
createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
|
||||||
|
|
||||||
sp<IPreparedModel> preparedModel = nullptr;
|
|
||||||
// Spawn a thread to copy the cache content concurrently while saving to cache.
|
// Spawn a thread to copy the cache content concurrently while saving to cache.
|
||||||
std::thread thread(copyCacheFiles, std::cref(modelCacheMul), std::cref(mModelCache));
|
std::thread thread(copyCacheFiles, std::cref(modelCacheMul), std::cref(mModelCache));
|
||||||
saveModelToCache(modelAdd, modelCache, dataCache, &preparedModel,
|
saveModelToCache(modelAdd, modelCache, dataCache);
|
||||||
/*allowGeneralFailure=*/true);
|
|
||||||
thread.join();
|
thread.join();
|
||||||
|
|
||||||
if (preparedModel == nullptr) {
|
|
||||||
numFailedIterations++;
|
|
||||||
ASSERT_LE(numFailedIterations, kMaxNumFailedIterationsTOCTOU);
|
|
||||||
} else {
|
|
||||||
numSuccessfulIterations++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve preparedModel from cache.
|
// Retrieve preparedModel from cache.
|
||||||
@@ -1129,26 +1112,14 @@ TEST_P(CompilationCachingTest, PrepareFromCache_TOCTOU) {
|
|||||||
// Use a different token for modelAdd.
|
// Use a different token for modelAdd.
|
||||||
mToken[0]++;
|
mToken[0]++;
|
||||||
|
|
||||||
// This test is probabilistic, so we run it multiple times. We allow the compilation to fail
|
// This test is probabilistic, so we run it multiple times.
|
||||||
// because it is not related to the security aspect of the TOCTOU test. However, we need to have
|
for (uint32_t i = 0; i < kNumIterationsTOCTOU; i++) {
|
||||||
// enough successful iterations to ensure the test coverage.
|
|
||||||
uint32_t numSuccessfulIterations = 0, numFailedIterations = 0;
|
|
||||||
while (numSuccessfulIterations < kNumSuccessfulIterationsTOCTOU) {
|
|
||||||
// Save the modelAdd compilation to cache.
|
// Save the modelAdd compilation to cache.
|
||||||
{
|
{
|
||||||
hidl_vec<hidl_handle> modelCache, dataCache;
|
hidl_vec<hidl_handle> modelCache, dataCache;
|
||||||
createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache);
|
createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache);
|
||||||
createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
|
createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
|
||||||
sp<IPreparedModel> preparedModel = nullptr;
|
saveModelToCache(modelAdd, modelCache, dataCache);
|
||||||
saveModelToCache(modelAdd, modelCache, dataCache, &preparedModel,
|
|
||||||
/*allowGeneralFailure=*/true);
|
|
||||||
|
|
||||||
if (preparedModel == nullptr) {
|
|
||||||
numFailedIterations++;
|
|
||||||
ASSERT_LE(numFailedIterations, kMaxNumFailedIterationsTOCTOU);
|
|
||||||
} else {
|
|
||||||
numSuccessfulIterations++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve preparedModel from cache.
|
// Retrieve preparedModel from cache.
|
||||||
|
|||||||
@@ -318,8 +318,7 @@ class CompilationCachingTestBase : public testing::Test {
|
|||||||
|
|
||||||
void saveModelToCache(const Model& model, const hidl_vec<hidl_handle>& modelCache,
|
void saveModelToCache(const Model& model, const hidl_vec<hidl_handle>& modelCache,
|
||||||
const hidl_vec<hidl_handle>& dataCache,
|
const hidl_vec<hidl_handle>& dataCache,
|
||||||
sp<IPreparedModel>* preparedModel = nullptr,
|
sp<IPreparedModel>* preparedModel = nullptr) {
|
||||||
bool allowGeneralFailure = false) {
|
|
||||||
if (preparedModel != nullptr) *preparedModel = nullptr;
|
if (preparedModel != nullptr) *preparedModel = nullptr;
|
||||||
|
|
||||||
// Launch prepare model.
|
// Launch prepare model.
|
||||||
@@ -333,10 +332,7 @@ class CompilationCachingTestBase : public testing::Test {
|
|||||||
|
|
||||||
// Retrieve prepared model.
|
// Retrieve prepared model.
|
||||||
preparedModelCallback->wait();
|
preparedModelCallback->wait();
|
||||||
const auto prepareCallbackStatus = preparedModelCallback->getStatus();
|
ASSERT_EQ(preparedModelCallback->getStatus(), ErrorStatus::NONE);
|
||||||
if (!allowGeneralFailure || prepareCallbackStatus != ErrorStatus::GENERAL_FAILURE) {
|
|
||||||
ASSERT_EQ(prepareCallbackStatus, ErrorStatus::NONE);
|
|
||||||
}
|
|
||||||
if (preparedModel != nullptr) {
|
if (preparedModel != nullptr) {
|
||||||
*preparedModel = IPreparedModel::castFrom(preparedModelCallback->getPreparedModel())
|
*preparedModel = IPreparedModel::castFrom(preparedModelCallback->getPreparedModel())
|
||||||
.withDefault(nullptr);
|
.withDefault(nullptr);
|
||||||
@@ -1017,8 +1013,7 @@ static void copyCacheFiles(const std::vector<std::vector<std::string>>& from,
|
|||||||
|
|
||||||
// Number of operations in the large test model.
|
// Number of operations in the large test model.
|
||||||
constexpr uint32_t kLargeModelSize = 100;
|
constexpr uint32_t kLargeModelSize = 100;
|
||||||
constexpr uint32_t kNumSuccessfulIterationsTOCTOU = 100;
|
constexpr uint32_t kNumIterationsTOCTOU = 100;
|
||||||
constexpr uint32_t kMaxNumFailedIterationsTOCTOU = 100;
|
|
||||||
|
|
||||||
TEST_P(CompilationCachingTest, SaveToCache_TOCTOU) {
|
TEST_P(CompilationCachingTest, SaveToCache_TOCTOU) {
|
||||||
if (!mIsCachingSupported) return;
|
if (!mIsCachingSupported) return;
|
||||||
@@ -1046,30 +1041,18 @@ TEST_P(CompilationCachingTest, SaveToCache_TOCTOU) {
|
|||||||
// Use a different token for modelAdd.
|
// Use a different token for modelAdd.
|
||||||
mToken[0]++;
|
mToken[0]++;
|
||||||
|
|
||||||
// This test is probabilistic, so we run it multiple times. We allow the compilation to fail
|
// This test is probabilistic, so we run it multiple times.
|
||||||
// because it is not related to the security aspect of the TOCTOU test. However, we need to have
|
for (uint32_t i = 0; i < kNumIterationsTOCTOU; i++) {
|
||||||
// enough successful iterations to ensure the test coverage.
|
|
||||||
uint32_t numSuccessfulIterations = 0, numFailedIterations = 0;
|
|
||||||
while (numSuccessfulIterations < kNumSuccessfulIterationsTOCTOU) {
|
|
||||||
// Save the modelAdd compilation to cache.
|
// Save the modelAdd compilation to cache.
|
||||||
{
|
{
|
||||||
hidl_vec<hidl_handle> modelCache, dataCache;
|
hidl_vec<hidl_handle> modelCache, dataCache;
|
||||||
createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache);
|
createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache);
|
||||||
createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
|
createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
|
||||||
|
|
||||||
sp<IPreparedModel> preparedModel = nullptr;
|
|
||||||
// Spawn a thread to copy the cache content concurrently while saving to cache.
|
// Spawn a thread to copy the cache content concurrently while saving to cache.
|
||||||
std::thread thread(copyCacheFiles, std::cref(modelCacheMul), std::cref(mModelCache));
|
std::thread thread(copyCacheFiles, std::cref(modelCacheMul), std::cref(mModelCache));
|
||||||
saveModelToCache(modelAdd, modelCache, dataCache, &preparedModel,
|
saveModelToCache(modelAdd, modelCache, dataCache);
|
||||||
/*allowGeneralFailure=*/true);
|
|
||||||
thread.join();
|
thread.join();
|
||||||
|
|
||||||
if (preparedModel == nullptr) {
|
|
||||||
numFailedIterations++;
|
|
||||||
ASSERT_LE(numFailedIterations, kMaxNumFailedIterationsTOCTOU);
|
|
||||||
} else {
|
|
||||||
numSuccessfulIterations++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve preparedModel from cache.
|
// Retrieve preparedModel from cache.
|
||||||
@@ -1120,26 +1103,14 @@ TEST_P(CompilationCachingTest, PrepareFromCache_TOCTOU) {
|
|||||||
// Use a different token for modelAdd.
|
// Use a different token for modelAdd.
|
||||||
mToken[0]++;
|
mToken[0]++;
|
||||||
|
|
||||||
// This test is probabilistic, so we run it multiple times. We allow the compilation to fail
|
// This test is probabilistic, so we run it multiple times.
|
||||||
// because it is not related to the security aspect of the TOCTOU test. However, we need to have
|
for (uint32_t i = 0; i < kNumIterationsTOCTOU; i++) {
|
||||||
// enough successful iterations to ensure the test coverage.
|
|
||||||
uint32_t numSuccessfulIterations = 0, numFailedIterations = 0;
|
|
||||||
while (numSuccessfulIterations < kNumSuccessfulIterationsTOCTOU) {
|
|
||||||
// Save the modelAdd compilation to cache.
|
// Save the modelAdd compilation to cache.
|
||||||
{
|
{
|
||||||
hidl_vec<hidl_handle> modelCache, dataCache;
|
hidl_vec<hidl_handle> modelCache, dataCache;
|
||||||
createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache);
|
createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache);
|
||||||
createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
|
createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
|
||||||
sp<IPreparedModel> preparedModel = nullptr;
|
saveModelToCache(modelAdd, modelCache, dataCache);
|
||||||
saveModelToCache(modelAdd, modelCache, dataCache, &preparedModel,
|
|
||||||
/*allowGeneralFailure=*/true);
|
|
||||||
|
|
||||||
if (preparedModel == nullptr) {
|
|
||||||
numFailedIterations++;
|
|
||||||
ASSERT_LE(numFailedIterations, kMaxNumFailedIterationsTOCTOU);
|
|
||||||
} else {
|
|
||||||
numSuccessfulIterations++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve preparedModel from cache.
|
// Retrieve preparedModel from cache.
|
||||||
|
|||||||
Reference in New Issue
Block a user