Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 35afac97 authored by Xusong Wang's avatar Xusong Wang Committed by android-build-merger
Browse files

Merge "Move getSupportedOperations out of saveToCache." into qt-dev am: 3ca195bf

am: d49c50c2

Change-Id: I995d3ac2a416fb38483cd931bfd0489f2fa745d8
parents 11d0212b d49c50c2
Loading
Loading
Loading
Loading
+70 −92
Original line number Original line Diff line number Diff line
@@ -263,12 +263,8 @@ class CompilationCachingTest : public NeuralnetworksHidlTest {
        NeuralnetworksHidlTest::TearDown();
        NeuralnetworksHidlTest::TearDown();
    }
    }


    void saveModelToCache(const V1_2::Model& model, const hidl_vec<hidl_handle>& modelCache,
    // See if the service can handle the model.
                          const hidl_vec<hidl_handle>& dataCache, bool* supported,
    bool isModelFullySupported(const V1_2::Model& model) {
                          sp<IPreparedModel>* preparedModel = nullptr) {
        if (preparedModel != nullptr) *preparedModel = nullptr;

        // See if service can handle model.
        bool fullySupportsModel = false;
        bool fullySupportsModel = false;
        Return<void> supportedCall = device->getSupportedOperations_1_2(
        Return<void> supportedCall = device->getSupportedOperations_1_2(
                model,
                model,
@@ -278,9 +274,14 @@ class CompilationCachingTest : public NeuralnetworksHidlTest {
                    fullySupportsModel = std::all_of(supported.begin(), supported.end(),
                    fullySupportsModel = std::all_of(supported.begin(), supported.end(),
                                                     [](bool valid) { return valid; });
                                                     [](bool valid) { return valid; });
                });
                });
        ASSERT_TRUE(supportedCall.isOk());
        EXPECT_TRUE(supportedCall.isOk());
        *supported = fullySupportsModel;
        return fullySupportsModel;
        if (!fullySupportsModel) return;
    }

    void saveModelToCache(const V1_2::Model& model, const hidl_vec<hidl_handle>& modelCache,
                          const hidl_vec<hidl_handle>& dataCache,
                          sp<IPreparedModel>* preparedModel = nullptr) {
        if (preparedModel != nullptr) *preparedModel = nullptr;


        // Launch prepare model.
        // Launch prepare model.
        sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
        sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
@@ -314,8 +315,8 @@ class CompilationCachingTest : public NeuralnetworksHidlTest {
        return false;
        return false;
    }
    }


    bool checkEarlyTermination(bool supported) {
    bool checkEarlyTermination(const V1_2::Model& model) {
        if (!supported) {
        if (!isModelFullySupported(model)) {
            LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
            LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
                         "prepare model that it does not support.";
                         "prepare model that it does not support.";
            std::cout << "[          ]   Early termination of test because vendor service cannot "
            std::cout << "[          ]   Early termination of test because vendor service cannot "
@@ -369,17 +370,16 @@ class CompilationCachingTest : public NeuralnetworksHidlTest {


TEST_F(CompilationCachingTest, CacheSavingAndRetrieval) {
TEST_F(CompilationCachingTest, CacheSavingAndRetrieval) {
    // Create test HIDL model and compile.
    // Create test HIDL model and compile.
    Model testModel = createTestModel();
    const Model testModel = createTestModel();
    if (checkEarlyTermination(testModel)) return;
    sp<IPreparedModel> preparedModel = nullptr;
    sp<IPreparedModel> preparedModel = nullptr;


    // Save the compilation to cache.
    // Save the compilation to cache.
    {
    {
        bool supported;
        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);
        saveModelToCache(testModel, modelCache, dataCache, &supported);
        saveModelToCache(testModel, modelCache, dataCache);
        if (checkEarlyTermination(supported)) return;
    }
    }


    // Retrieve preparedModel from cache.
    // Retrieve preparedModel from cache.
@@ -411,12 +411,12 @@ TEST_F(CompilationCachingTest, CacheSavingAndRetrieval) {


TEST_F(CompilationCachingTest, CacheSavingAndRetrievalNonZeroOffset) {
TEST_F(CompilationCachingTest, CacheSavingAndRetrievalNonZeroOffset) {
    // Create test HIDL model and compile.
    // Create test HIDL model and compile.
    Model testModel = createTestModel();
    const Model testModel = createTestModel();
    if (checkEarlyTermination(testModel)) return;
    sp<IPreparedModel> preparedModel = nullptr;
    sp<IPreparedModel> preparedModel = nullptr;


    // Save the compilation to cache.
    // Save the compilation to cache.
    {
    {
        bool supported;
        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);
@@ -433,8 +433,7 @@ TEST_F(CompilationCachingTest, CacheSavingAndRetrievalNonZeroOffset) {
                    write(dataCache[i].getNativeHandle()->data[0], &dummyBytes, sizeof(dummyBytes)),
                    write(dataCache[i].getNativeHandle()->data[0], &dummyBytes, sizeof(dummyBytes)),
                    sizeof(dummyBytes));
                    sizeof(dummyBytes));
        }
        }
        saveModelToCache(testModel, modelCache, dataCache, &supported);
        saveModelToCache(testModel, modelCache, dataCache);
        if (checkEarlyTermination(supported)) return;
    }
    }


    // Retrieve preparedModel from cache.
    // Retrieve preparedModel from cache.
@@ -475,11 +474,11 @@ TEST_F(CompilationCachingTest, CacheSavingAndRetrievalNonZeroOffset) {


TEST_F(CompilationCachingTest, SaveToCacheInvalidNumCache) {
TEST_F(CompilationCachingTest, SaveToCacheInvalidNumCache) {
    // Create test HIDL model and compile.
    // Create test HIDL model and compile.
    Model testModel = createTestModel();
    const Model testModel = createTestModel();
    if (checkEarlyTermination(testModel)) return;


    // Test with number of model cache files greater than mNumModelCache.
    // Test with number of model cache files greater than mNumModelCache.
    {
    {
        bool supported;
        hidl_vec<hidl_handle> modelCache, dataCache;
        hidl_vec<hidl_handle> modelCache, dataCache;
        // Pass an additional cache file for model cache.
        // Pass an additional cache file for model cache.
        mModelCache.push_back({mTmpCache});
        mModelCache.push_back({mTmpCache});
@@ -487,8 +486,7 @@ TEST_F(CompilationCachingTest, SaveToCacheInvalidNumCache) {
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        mModelCache.pop_back();
        mModelCache.pop_back();
        sp<IPreparedModel> preparedModel = nullptr;
        sp<IPreparedModel> preparedModel = nullptr;
        saveModelToCache(testModel, modelCache, dataCache, &supported, &preparedModel);
        saveModelToCache(testModel, modelCache, dataCache, &preparedModel);
        if (checkEarlyTermination(supported)) return;
        ASSERT_NE(preparedModel, nullptr);
        ASSERT_NE(preparedModel, nullptr);
        // Execute and verify results.
        // Execute and verify results.
        generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; },
        generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; },
@@ -507,7 +505,6 @@ TEST_F(CompilationCachingTest, SaveToCacheInvalidNumCache) {


    // Test with number of model cache files smaller than mNumModelCache.
    // Test with number of model cache files smaller than mNumModelCache.
    if (mModelCache.size() > 0) {
    if (mModelCache.size() > 0) {
        bool supported;
        hidl_vec<hidl_handle> modelCache, dataCache;
        hidl_vec<hidl_handle> modelCache, dataCache;
        // Pop out the last cache file.
        // Pop out the last cache file.
        auto tmp = mModelCache.back();
        auto tmp = mModelCache.back();
@@ -516,8 +513,7 @@ TEST_F(CompilationCachingTest, SaveToCacheInvalidNumCache) {
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        mModelCache.push_back(tmp);
        mModelCache.push_back(tmp);
        sp<IPreparedModel> preparedModel = nullptr;
        sp<IPreparedModel> preparedModel = nullptr;
        saveModelToCache(testModel, modelCache, dataCache, &supported, &preparedModel);
        saveModelToCache(testModel, modelCache, dataCache, &preparedModel);
        if (checkEarlyTermination(supported)) return;
        ASSERT_NE(preparedModel, nullptr);
        ASSERT_NE(preparedModel, nullptr);
        // Execute and verify results.
        // Execute and verify results.
        generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; },
        generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; },
@@ -536,7 +532,6 @@ TEST_F(CompilationCachingTest, SaveToCacheInvalidNumCache) {


    // Test with number of data cache files greater than mNumDataCache.
    // Test with number of data cache files greater than mNumDataCache.
    {
    {
        bool supported;
        hidl_vec<hidl_handle> modelCache, dataCache;
        hidl_vec<hidl_handle> modelCache, dataCache;
        // Pass an additional cache file for data cache.
        // Pass an additional cache file for data cache.
        mDataCache.push_back({mTmpCache});
        mDataCache.push_back({mTmpCache});
@@ -544,8 +539,7 @@ TEST_F(CompilationCachingTest, SaveToCacheInvalidNumCache) {
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        mDataCache.pop_back();
        mDataCache.pop_back();
        sp<IPreparedModel> preparedModel = nullptr;
        sp<IPreparedModel> preparedModel = nullptr;
        saveModelToCache(testModel, modelCache, dataCache, &supported, &preparedModel);
        saveModelToCache(testModel, modelCache, dataCache, &preparedModel);
        if (checkEarlyTermination(supported)) return;
        ASSERT_NE(preparedModel, nullptr);
        ASSERT_NE(preparedModel, nullptr);
        // Execute and verify results.
        // Execute and verify results.
        generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; },
        generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; },
@@ -564,7 +558,6 @@ TEST_F(CompilationCachingTest, SaveToCacheInvalidNumCache) {


    // Test with number of data cache files smaller than mNumDataCache.
    // Test with number of data cache files smaller than mNumDataCache.
    if (mDataCache.size() > 0) {
    if (mDataCache.size() > 0) {
        bool supported;
        hidl_vec<hidl_handle> modelCache, dataCache;
        hidl_vec<hidl_handle> modelCache, dataCache;
        // Pop out the last cache file.
        // Pop out the last cache file.
        auto tmp = mDataCache.back();
        auto tmp = mDataCache.back();
@@ -573,8 +566,7 @@ TEST_F(CompilationCachingTest, SaveToCacheInvalidNumCache) {
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        mDataCache.push_back(tmp);
        mDataCache.push_back(tmp);
        sp<IPreparedModel> preparedModel = nullptr;
        sp<IPreparedModel> preparedModel = nullptr;
        saveModelToCache(testModel, modelCache, dataCache, &supported, &preparedModel);
        saveModelToCache(testModel, modelCache, dataCache, &preparedModel);
        if (checkEarlyTermination(supported)) return;
        ASSERT_NE(preparedModel, nullptr);
        ASSERT_NE(preparedModel, nullptr);
        // Execute and verify results.
        // Execute and verify results.
        generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; },
        generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; },
@@ -594,16 +586,15 @@ TEST_F(CompilationCachingTest, SaveToCacheInvalidNumCache) {


TEST_F(CompilationCachingTest, PrepareModelFromCacheInvalidNumCache) {
TEST_F(CompilationCachingTest, PrepareModelFromCacheInvalidNumCache) {
    // Create test HIDL model and compile.
    // Create test HIDL model and compile.
    Model testModel = createTestModel();
    const Model testModel = createTestModel();
    if (checkEarlyTermination(testModel)) return;


    // Save the compilation to cache.
    // Save the compilation to cache.
    {
    {
        bool supported;
        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);
        saveModelToCache(testModel, modelCache, dataCache, &supported);
        saveModelToCache(testModel, modelCache, dataCache);
        if (checkEarlyTermination(supported)) return;
    }
    }


    // Test with number of model cache files greater than mNumModelCache.
    // Test with number of model cache files greater than mNumModelCache.
@@ -675,11 +666,11 @@ TEST_F(CompilationCachingTest, PrepareModelFromCacheInvalidNumCache) {


TEST_F(CompilationCachingTest, SaveToCacheInvalidNumFd) {
TEST_F(CompilationCachingTest, SaveToCacheInvalidNumFd) {
    // Create test HIDL model and compile.
    // Create test HIDL model and compile.
    Model testModel = createTestModel();
    const Model testModel = createTestModel();
    if (checkEarlyTermination(testModel)) return;


    // Go through each handle in model cache, test with NumFd greater than 1.
    // Go through each handle in model cache, test with NumFd greater than 1.
    for (uint32_t i = 0; i < mNumModelCache; i++) {
    for (uint32_t i = 0; i < mNumModelCache; i++) {
        bool supported;
        hidl_vec<hidl_handle> modelCache, dataCache;
        hidl_vec<hidl_handle> modelCache, dataCache;
        // Pass an invalid number of fds for handle i.
        // Pass an invalid number of fds for handle i.
        mModelCache[i].push_back(mTmpCache);
        mModelCache[i].push_back(mTmpCache);
@@ -687,8 +678,7 @@ TEST_F(CompilationCachingTest, SaveToCacheInvalidNumFd) {
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        mModelCache[i].pop_back();
        mModelCache[i].pop_back();
        sp<IPreparedModel> preparedModel = nullptr;
        sp<IPreparedModel> preparedModel = nullptr;
        saveModelToCache(testModel, modelCache, dataCache, &supported, &preparedModel);
        saveModelToCache(testModel, modelCache, dataCache, &preparedModel);
        if (checkEarlyTermination(supported)) return;
        ASSERT_NE(preparedModel, nullptr);
        ASSERT_NE(preparedModel, nullptr);
        // Execute and verify results.
        // Execute and verify results.
        generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; },
        generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; },
@@ -707,7 +697,6 @@ TEST_F(CompilationCachingTest, SaveToCacheInvalidNumFd) {


    // Go through each handle in model cache, test with NumFd equal to 0.
    // Go through each handle in model cache, test with NumFd equal to 0.
    for (uint32_t i = 0; i < mNumModelCache; i++) {
    for (uint32_t i = 0; i < mNumModelCache; i++) {
        bool supported;
        hidl_vec<hidl_handle> modelCache, dataCache;
        hidl_vec<hidl_handle> modelCache, dataCache;
        // Pass an invalid number of fds for handle i.
        // Pass an invalid number of fds for handle i.
        auto tmp = mModelCache[i].back();
        auto tmp = mModelCache[i].back();
@@ -716,8 +705,7 @@ TEST_F(CompilationCachingTest, SaveToCacheInvalidNumFd) {
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        mModelCache[i].push_back(tmp);
        mModelCache[i].push_back(tmp);
        sp<IPreparedModel> preparedModel = nullptr;
        sp<IPreparedModel> preparedModel = nullptr;
        saveModelToCache(testModel, modelCache, dataCache, &supported, &preparedModel);
        saveModelToCache(testModel, modelCache, dataCache, &preparedModel);
        if (checkEarlyTermination(supported)) return;
        ASSERT_NE(preparedModel, nullptr);
        ASSERT_NE(preparedModel, nullptr);
        // Execute and verify results.
        // Execute and verify results.
        generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; },
        generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; },
@@ -736,7 +724,6 @@ TEST_F(CompilationCachingTest, SaveToCacheInvalidNumFd) {


    // Go through each handle in data cache, test with NumFd greater than 1.
    // Go through each handle in data cache, test with NumFd greater than 1.
    for (uint32_t i = 0; i < mNumDataCache; i++) {
    for (uint32_t i = 0; i < mNumDataCache; i++) {
        bool supported;
        hidl_vec<hidl_handle> modelCache, dataCache;
        hidl_vec<hidl_handle> modelCache, dataCache;
        // Pass an invalid number of fds for handle i.
        // Pass an invalid number of fds for handle i.
        mDataCache[i].push_back(mTmpCache);
        mDataCache[i].push_back(mTmpCache);
@@ -744,8 +731,7 @@ TEST_F(CompilationCachingTest, SaveToCacheInvalidNumFd) {
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        mDataCache[i].pop_back();
        mDataCache[i].pop_back();
        sp<IPreparedModel> preparedModel = nullptr;
        sp<IPreparedModel> preparedModel = nullptr;
        saveModelToCache(testModel, modelCache, dataCache, &supported, &preparedModel);
        saveModelToCache(testModel, modelCache, dataCache, &preparedModel);
        if (checkEarlyTermination(supported)) return;
        ASSERT_NE(preparedModel, nullptr);
        ASSERT_NE(preparedModel, nullptr);
        // Execute and verify results.
        // Execute and verify results.
        generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; },
        generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; },
@@ -764,7 +750,6 @@ TEST_F(CompilationCachingTest, SaveToCacheInvalidNumFd) {


    // Go through each handle in data cache, test with NumFd equal to 0.
    // Go through each handle in data cache, test with NumFd equal to 0.
    for (uint32_t i = 0; i < mNumDataCache; i++) {
    for (uint32_t i = 0; i < mNumDataCache; i++) {
        bool supported;
        hidl_vec<hidl_handle> modelCache, dataCache;
        hidl_vec<hidl_handle> modelCache, dataCache;
        // Pass an invalid number of fds for handle i.
        // Pass an invalid number of fds for handle i.
        auto tmp = mDataCache[i].back();
        auto tmp = mDataCache[i].back();
@@ -773,8 +758,7 @@ TEST_F(CompilationCachingTest, SaveToCacheInvalidNumFd) {
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        mDataCache[i].push_back(tmp);
        mDataCache[i].push_back(tmp);
        sp<IPreparedModel> preparedModel = nullptr;
        sp<IPreparedModel> preparedModel = nullptr;
        saveModelToCache(testModel, modelCache, dataCache, &supported, &preparedModel);
        saveModelToCache(testModel, modelCache, dataCache, &preparedModel);
        if (checkEarlyTermination(supported)) return;
        ASSERT_NE(preparedModel, nullptr);
        ASSERT_NE(preparedModel, nullptr);
        // Execute and verify results.
        // Execute and verify results.
        generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; },
        generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; },
@@ -794,16 +778,15 @@ TEST_F(CompilationCachingTest, SaveToCacheInvalidNumFd) {


TEST_F(CompilationCachingTest, PrepareModelFromCacheInvalidNumFd) {
TEST_F(CompilationCachingTest, PrepareModelFromCacheInvalidNumFd) {
    // Create test HIDL model and compile.
    // Create test HIDL model and compile.
    Model testModel = createTestModel();
    const Model testModel = createTestModel();
    if (checkEarlyTermination(testModel)) return;


    // Save the compilation to cache.
    // Save the compilation to cache.
    {
    {
        bool supported;
        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);
        saveModelToCache(testModel, modelCache, dataCache, &supported);
        saveModelToCache(testModel, modelCache, dataCache);
        if (checkEarlyTermination(supported)) return;
    }
    }


    // Go through each handle in model cache, test with NumFd greater than 1.
    // Go through each handle in model cache, test with NumFd greater than 1.
@@ -875,21 +858,20 @@ TEST_F(CompilationCachingTest, PrepareModelFromCacheInvalidNumFd) {


TEST_F(CompilationCachingTest, SaveToCacheInvalidAccessMode) {
TEST_F(CompilationCachingTest, SaveToCacheInvalidAccessMode) {
    // Create test HIDL model and compile.
    // Create test HIDL model and compile.
    Model testModel = createTestModel();
    const Model testModel = createTestModel();
    if (checkEarlyTermination(testModel)) return;
    std::vector<AccessMode> modelCacheMode(mNumModelCache, AccessMode::READ_WRITE);
    std::vector<AccessMode> modelCacheMode(mNumModelCache, AccessMode::READ_WRITE);
    std::vector<AccessMode> dataCacheMode(mNumDataCache, AccessMode::READ_WRITE);
    std::vector<AccessMode> dataCacheMode(mNumDataCache, AccessMode::READ_WRITE);


    // Go through each handle in model cache, test with invalid access mode.
    // Go through each handle in model cache, test with invalid access mode.
    for (uint32_t i = 0; i < mNumModelCache; i++) {
    for (uint32_t i = 0; i < mNumModelCache; i++) {
        bool supported;
        hidl_vec<hidl_handle> modelCache, dataCache;
        hidl_vec<hidl_handle> modelCache, dataCache;
        modelCacheMode[i] = AccessMode::READ_ONLY;
        modelCacheMode[i] = AccessMode::READ_ONLY;
        createCacheHandles(mModelCache, modelCacheMode, &modelCache);
        createCacheHandles(mModelCache, modelCacheMode, &modelCache);
        createCacheHandles(mDataCache, dataCacheMode, &dataCache);
        createCacheHandles(mDataCache, dataCacheMode, &dataCache);
        modelCacheMode[i] = AccessMode::READ_WRITE;
        modelCacheMode[i] = AccessMode::READ_WRITE;
        sp<IPreparedModel> preparedModel = nullptr;
        sp<IPreparedModel> preparedModel = nullptr;
        saveModelToCache(testModel, modelCache, dataCache, &supported, &preparedModel);
        saveModelToCache(testModel, modelCache, dataCache, &preparedModel);
        if (checkEarlyTermination(supported)) return;
        ASSERT_NE(preparedModel, nullptr);
        ASSERT_NE(preparedModel, nullptr);
        // Execute and verify results.
        // Execute and verify results.
        generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; },
        generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; },
@@ -908,15 +890,13 @@ TEST_F(CompilationCachingTest, SaveToCacheInvalidAccessMode) {


    // Go through each handle in data cache, test with invalid access mode.
    // Go through each handle in data cache, test with invalid access mode.
    for (uint32_t i = 0; i < mNumDataCache; i++) {
    for (uint32_t i = 0; i < mNumDataCache; i++) {
        bool supported;
        hidl_vec<hidl_handle> modelCache, dataCache;
        hidl_vec<hidl_handle> modelCache, dataCache;
        dataCacheMode[i] = AccessMode::READ_ONLY;
        dataCacheMode[i] = AccessMode::READ_ONLY;
        createCacheHandles(mModelCache, modelCacheMode, &modelCache);
        createCacheHandles(mModelCache, modelCacheMode, &modelCache);
        createCacheHandles(mDataCache, dataCacheMode, &dataCache);
        createCacheHandles(mDataCache, dataCacheMode, &dataCache);
        dataCacheMode[i] = AccessMode::READ_WRITE;
        dataCacheMode[i] = AccessMode::READ_WRITE;
        sp<IPreparedModel> preparedModel = nullptr;
        sp<IPreparedModel> preparedModel = nullptr;
        saveModelToCache(testModel, modelCache, dataCache, &supported, &preparedModel);
        saveModelToCache(testModel, modelCache, dataCache, &preparedModel);
        if (checkEarlyTermination(supported)) return;
        ASSERT_NE(preparedModel, nullptr);
        ASSERT_NE(preparedModel, nullptr);
        // Execute and verify results.
        // Execute and verify results.
        generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; },
        generated_tests::EvaluatePreparedModel(preparedModel, [](int) { return false; },
@@ -936,18 +916,17 @@ TEST_F(CompilationCachingTest, SaveToCacheInvalidAccessMode) {


TEST_F(CompilationCachingTest, PrepareModelFromCacheInvalidAccessMode) {
TEST_F(CompilationCachingTest, PrepareModelFromCacheInvalidAccessMode) {
    // Create test HIDL model and compile.
    // Create test HIDL model and compile.
    Model testModel = createTestModel();
    const Model testModel = createTestModel();
    if (checkEarlyTermination(testModel)) return;
    std::vector<AccessMode> modelCacheMode(mNumModelCache, AccessMode::READ_WRITE);
    std::vector<AccessMode> modelCacheMode(mNumModelCache, AccessMode::READ_WRITE);
    std::vector<AccessMode> dataCacheMode(mNumDataCache, AccessMode::READ_WRITE);
    std::vector<AccessMode> dataCacheMode(mNumDataCache, AccessMode::READ_WRITE);


    // Save the compilation to cache.
    // Save the compilation to cache.
    {
    {
        bool supported;
        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);
        saveModelToCache(testModel, modelCache, dataCache, &supported);
        saveModelToCache(testModel, modelCache, dataCache);
        if (checkEarlyTermination(supported)) return;
    }
    }


    // Go through each handle in model cache, test with invalid access mode.
    // Go through each handle in model cache, test with invalid access mode.
@@ -1014,8 +993,13 @@ constexpr uint32_t kNumIterationsTOCTOU = 100;
TEST_F(CompilationCachingTest, SaveToCache_TOCTOU) {
TEST_F(CompilationCachingTest, SaveToCache_TOCTOU) {
    if (!mIsCachingSupported) return;
    if (!mIsCachingSupported) return;


    // Create test models and check if fully supported by the service.
    const Model testModelMul = createLargeTestModel(OperationType::MUL, kLargeModelSize);
    if (checkEarlyTermination(testModelMul)) return;
    const Model testModelAdd = createLargeTestModel(OperationType::ADD, kLargeModelSize);
    if (checkEarlyTermination(testModelAdd)) return;

    // Save the testModelMul compilation to cache.
    // Save the testModelMul compilation to cache.
    Model testModelMul = createLargeTestModel(OperationType::MUL, kLargeModelSize);
    auto modelCacheMul = mModelCache;
    auto modelCacheMul = mModelCache;
    for (auto& cache : modelCacheMul) {
    for (auto& cache : modelCacheMul) {
        cache[0].append("_mul");
        cache[0].append("_mul");
@@ -1024,29 +1008,24 @@ TEST_F(CompilationCachingTest, SaveToCache_TOCTOU) {
        hidl_vec<hidl_handle> modelCache, dataCache;
        hidl_vec<hidl_handle> modelCache, dataCache;
        createCacheHandles(modelCacheMul, AccessMode::READ_WRITE, &modelCache);
        createCacheHandles(modelCacheMul, AccessMode::READ_WRITE, &modelCache);
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        bool supported;
        saveModelToCache(testModelMul, modelCache, dataCache);
        saveModelToCache(testModelMul, modelCache, dataCache, &supported);
        if (checkEarlyTermination(supported)) return;
    }
    }


    // Use a different token for testModelAdd.
    // Use a different token for testModelAdd.
    mToken[0]++;
    mToken[0]++;


    // This test is probabilistic, so we run it multiple times.
    // This test is probabilistic, so we run it multiple times.
    Model testModelAdd = createLargeTestModel(OperationType::ADD, kLargeModelSize);
    for (uint32_t i = 0; i < kNumIterationsTOCTOU; i++) {
    for (uint32_t i = 0; i < kNumIterationsTOCTOU; i++) {
        // Save the testModelAdd compilation to cache.
        // Save the testModelAdd compilation to cache.
        {
        {
            bool supported;
            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);


            // 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(testModelAdd, modelCache, dataCache, &supported);
            saveModelToCache(testModelAdd, modelCache, dataCache);
            thread.join();
            thread.join();
            if (checkEarlyTermination(supported)) return;
        }
        }


        // Retrieve preparedModel from cache.
        // Retrieve preparedModel from cache.
@@ -1077,8 +1056,13 @@ TEST_F(CompilationCachingTest, SaveToCache_TOCTOU) {
TEST_F(CompilationCachingTest, PrepareFromCache_TOCTOU) {
TEST_F(CompilationCachingTest, PrepareFromCache_TOCTOU) {
    if (!mIsCachingSupported) return;
    if (!mIsCachingSupported) return;


    // Create test models and check if fully supported by the service.
    const Model testModelMul = createLargeTestModel(OperationType::MUL, kLargeModelSize);
    if (checkEarlyTermination(testModelMul)) return;
    const Model testModelAdd = createLargeTestModel(OperationType::ADD, kLargeModelSize);
    if (checkEarlyTermination(testModelAdd)) return;

    // Save the testModelMul compilation to cache.
    // Save the testModelMul compilation to cache.
    Model testModelMul = createLargeTestModel(OperationType::MUL, kLargeModelSize);
    auto modelCacheMul = mModelCache;
    auto modelCacheMul = mModelCache;
    for (auto& cache : modelCacheMul) {
    for (auto& cache : modelCacheMul) {
        cache[0].append("_mul");
        cache[0].append("_mul");
@@ -1087,25 +1071,20 @@ TEST_F(CompilationCachingTest, PrepareFromCache_TOCTOU) {
        hidl_vec<hidl_handle> modelCache, dataCache;
        hidl_vec<hidl_handle> modelCache, dataCache;
        createCacheHandles(modelCacheMul, AccessMode::READ_WRITE, &modelCache);
        createCacheHandles(modelCacheMul, AccessMode::READ_WRITE, &modelCache);
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        bool supported;
        saveModelToCache(testModelMul, modelCache, dataCache);
        saveModelToCache(testModelMul, modelCache, dataCache, &supported);
        if (checkEarlyTermination(supported)) return;
    }
    }


    // Use a different token for testModelAdd.
    // Use a different token for testModelAdd.
    mToken[0]++;
    mToken[0]++;


    // This test is probabilistic, so we run it multiple times.
    // This test is probabilistic, so we run it multiple times.
    Model testModelAdd = createLargeTestModel(OperationType::ADD, kLargeModelSize);
    for (uint32_t i = 0; i < kNumIterationsTOCTOU; i++) {
    for (uint32_t i = 0; i < kNumIterationsTOCTOU; i++) {
        // Save the testModelAdd compilation to cache.
        // Save the testModelAdd compilation to cache.
        {
        {
            bool supported;
            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);
            saveModelToCache(testModelAdd, modelCache, dataCache, &supported);
            saveModelToCache(testModelAdd, modelCache, dataCache);
            if (checkEarlyTermination(supported)) return;
        }
        }


        // Retrieve preparedModel from cache.
        // Retrieve preparedModel from cache.
@@ -1140,8 +1119,13 @@ TEST_F(CompilationCachingTest, PrepareFromCache_TOCTOU) {
TEST_F(CompilationCachingTest, ReplaceSecuritySensitiveCache) {
TEST_F(CompilationCachingTest, ReplaceSecuritySensitiveCache) {
    if (!mIsCachingSupported) return;
    if (!mIsCachingSupported) return;


    // Create test models and check if fully supported by the service.
    const Model testModelMul = createLargeTestModel(OperationType::MUL, kLargeModelSize);
    if (checkEarlyTermination(testModelMul)) return;
    const Model testModelAdd = createLargeTestModel(OperationType::ADD, kLargeModelSize);
    if (checkEarlyTermination(testModelAdd)) return;

    // Save the testModelMul compilation to cache.
    // Save the testModelMul compilation to cache.
    Model testModelMul = createLargeTestModel(OperationType::MUL, kLargeModelSize);
    auto modelCacheMul = mModelCache;
    auto modelCacheMul = mModelCache;
    for (auto& cache : modelCacheMul) {
    for (auto& cache : modelCacheMul) {
        cache[0].append("_mul");
        cache[0].append("_mul");
@@ -1150,23 +1134,18 @@ TEST_F(CompilationCachingTest, ReplaceSecuritySensitiveCache) {
        hidl_vec<hidl_handle> modelCache, dataCache;
        hidl_vec<hidl_handle> modelCache, dataCache;
        createCacheHandles(modelCacheMul, AccessMode::READ_WRITE, &modelCache);
        createCacheHandles(modelCacheMul, AccessMode::READ_WRITE, &modelCache);
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache);
        bool supported;
        saveModelToCache(testModelMul, modelCache, dataCache);
        saveModelToCache(testModelMul, modelCache, dataCache, &supported);
        if (checkEarlyTermination(supported)) return;
    }
    }


    // Use a different token for testModelAdd.
    // Use a different token for testModelAdd.
    mToken[0]++;
    mToken[0]++;


    // Save the testModelAdd compilation to cache.
    // Save the testModelAdd compilation to cache.
    Model testModelAdd = createLargeTestModel(OperationType::ADD, kLargeModelSize);
    {
    {
        bool supported;
        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);
        saveModelToCache(testModelAdd, modelCache, dataCache, &supported);
        saveModelToCache(testModelAdd, modelCache, dataCache);
        if (checkEarlyTermination(supported)) return;
    }
    }


    // Replace the model cache of testModelAdd with testModelMul.
    // Replace the model cache of testModelAdd with testModelMul.
@@ -1237,16 +1216,15 @@ class CompilationCachingSecurityTest : public CompilationCachingTest,
    // The modifier accepts one pointer argument "skip" as the returning value, indicating
    // The modifier accepts one pointer argument "skip" as the returning value, indicating
    // whether the test should be skipped or not.
    // whether the test should be skipped or not.
    void testCorruptedCache(ExpectedResult expected, std::function<void(bool*)> modifier) {
    void testCorruptedCache(ExpectedResult expected, std::function<void(bool*)> modifier) {
        Model testModel = createTestModel();
        const Model testModel = createTestModel();
        if (checkEarlyTermination(testModel)) return;


        // Save the compilation to cache.
        // Save the compilation to cache.
        {
        {
            bool supported;
            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);
            saveModelToCache(testModel, modelCache, dataCache, &supported);
            saveModelToCache(testModel, modelCache, dataCache);
            if (checkEarlyTermination(supported)) return;
        }
        }


        bool skip = false;
        bool skip = false;