Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "neuralnetworks_hidl_hal_test" |
| 18 | |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 19 | #include <android-base/logging.h> |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 21 | #include <ftw.h> |
| 22 | #include <gtest/gtest.h> |
| 23 | #include <hidlmemory/mapping.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include <cstdio> |
| 27 | #include <cstdlib> |
| 28 | #include <random> |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame] | 29 | #include <thread> |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 30 | |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 31 | #include "1.2/Callbacks.h" |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 32 | #include "GeneratedTestHarness.h" |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 33 | #include "MemoryUtils.h" |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 34 | #include "TestHarness.h" |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 35 | #include "VtsHalNeuralnetworks.h" |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 36 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 37 | // Forward declaration of the mobilenet generated test models in |
| 38 | // frameworks/ml/nn/runtime/test/generated/. |
Slava Shklyaev | 0da5c34 | 2019-07-17 15:50:57 +0100 | [diff] [blame] | 39 | namespace generated_tests::mobilenet_224_gender_basic_fixed { |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 40 | const test_helper::TestModel& get_test_model(); |
Slava Shklyaev | 0da5c34 | 2019-07-17 15:50:57 +0100 | [diff] [blame] | 41 | } // namespace generated_tests::mobilenet_224_gender_basic_fixed |
Slava Shklyaev | e8b2446 | 2019-07-17 15:50:57 +0100 | [diff] [blame] | 42 | |
| 43 | namespace generated_tests::mobilenet_quantized { |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 44 | const test_helper::TestModel& get_test_model(); |
Slava Shklyaev | e8b2446 | 2019-07-17 15:50:57 +0100 | [diff] [blame] | 45 | } // namespace generated_tests::mobilenet_quantized |
| 46 | |
Michael Butler | 62749b9 | 2019-08-26 23:55:47 -0700 | [diff] [blame] | 47 | namespace android::hardware::neuralnetworks::V1_2::vts::functional { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 48 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 49 | using namespace test_helper; |
Michael Butler | 62749b9 | 2019-08-26 23:55:47 -0700 | [diff] [blame] | 50 | using implementation::PreparedModelCallback; |
| 51 | using V1_0::ErrorStatus; |
| 52 | using V1_1::ExecutionPreference; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 53 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 54 | namespace float32_model { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 55 | |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 56 | constexpr auto get_test_model = generated_tests::mobilenet_224_gender_basic_fixed::get_test_model; |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 57 | |
| 58 | } // namespace float32_model |
| 59 | |
| 60 | namespace quant8_model { |
| 61 | |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 62 | constexpr auto get_test_model = generated_tests::mobilenet_quantized::get_test_model; |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 63 | |
| 64 | } // namespace quant8_model |
| 65 | |
| 66 | namespace { |
| 67 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 68 | enum class AccessMode { READ_WRITE, READ_ONLY, WRITE_ONLY }; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 69 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 70 | // Creates cache handles based on provided file groups. |
| 71 | // The outer vector corresponds to handles and the inner vector is for fds held by each handle. |
| 72 | void createCacheHandles(const std::vector<std::vector<std::string>>& fileGroups, |
| 73 | const std::vector<AccessMode>& mode, hidl_vec<hidl_handle>* handles) { |
| 74 | handles->resize(fileGroups.size()); |
| 75 | for (uint32_t i = 0; i < fileGroups.size(); i++) { |
| 76 | std::vector<int> fds; |
| 77 | for (const auto& file : fileGroups[i]) { |
| 78 | int fd; |
| 79 | if (mode[i] == AccessMode::READ_ONLY) { |
| 80 | fd = open(file.c_str(), O_RDONLY); |
| 81 | } else if (mode[i] == AccessMode::WRITE_ONLY) { |
| 82 | fd = open(file.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); |
| 83 | } else if (mode[i] == AccessMode::READ_WRITE) { |
| 84 | fd = open(file.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); |
| 85 | } else { |
| 86 | FAIL(); |
| 87 | } |
| 88 | ASSERT_GE(fd, 0); |
| 89 | fds.push_back(fd); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 90 | } |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 91 | native_handle_t* cacheNativeHandle = native_handle_create(fds.size(), 0); |
| 92 | ASSERT_NE(cacheNativeHandle, nullptr); |
| 93 | std::copy(fds.begin(), fds.end(), &cacheNativeHandle->data[0]); |
| 94 | (*handles)[i].setTo(cacheNativeHandle, /*shouldOwn=*/true); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 95 | } |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void createCacheHandles(const std::vector<std::vector<std::string>>& fileGroups, AccessMode mode, |
| 99 | hidl_vec<hidl_handle>* handles) { |
| 100 | createCacheHandles(fileGroups, std::vector<AccessMode>(fileGroups.size(), mode), handles); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 103 | // Create a chain of broadcast operations. The second operand is always constant tensor [1]. |
| 104 | // For simplicity, activation scalar is shared. The second operand is not shared |
| 105 | // in the model to let driver maintain a non-trivial size of constant data and the corresponding |
| 106 | // data locations in cache. |
| 107 | // |
| 108 | // --------- activation -------- |
| 109 | // ↓ ↓ ↓ ↓ |
| 110 | // E.g. input -> ADD -> ADD -> ADD -> ... -> ADD -> output |
| 111 | // ↑ ↑ ↑ ↑ |
| 112 | // [1] [1] [1] [1] |
| 113 | // |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 114 | // This function assumes the operation is either ADD or MUL. |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 115 | template <typename CppType, TestOperandType operandType> |
| 116 | TestModel createLargeTestModelImpl(TestOperationType op, uint32_t len) { |
| 117 | EXPECT_TRUE(op == TestOperationType::ADD || op == TestOperationType::MUL); |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 118 | |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 119 | // Model operations and operands. |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 120 | std::vector<TestOperation> operations(len); |
| 121 | std::vector<TestOperand> operands(len * 2 + 2); |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 122 | |
| 123 | // The activation scalar, value = 0. |
| 124 | operands[0] = { |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 125 | .type = TestOperandType::INT32, |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 126 | .dimensions = {}, |
| 127 | .numberOfConsumers = len, |
| 128 | .scale = 0.0f, |
| 129 | .zeroPoint = 0, |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 130 | .lifetime = TestOperandLifeTime::CONSTANT_COPY, |
| 131 | .data = TestBuffer::createFromVector<int32_t>({0}), |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 132 | }; |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 133 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 134 | // The buffer value of the constant second operand. The logical value is always 1.0f. |
| 135 | CppType bufferValue; |
| 136 | // The scale of the first and second operand. |
| 137 | float scale1, scale2; |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 138 | if (operandType == TestOperandType::TENSOR_FLOAT32) { |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 139 | bufferValue = 1.0f; |
| 140 | scale1 = 0.0f; |
| 141 | scale2 = 0.0f; |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 142 | } else if (op == TestOperationType::ADD) { |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 143 | bufferValue = 1; |
| 144 | scale1 = 1.0f; |
| 145 | scale2 = 1.0f; |
| 146 | } else { |
| 147 | // To satisfy the constraint on quant8 MUL: input0.scale * input1.scale < output.scale, |
| 148 | // set input1 to have scale = 0.5f and bufferValue = 2, i.e. 1.0f in floating point. |
| 149 | bufferValue = 2; |
| 150 | scale1 = 1.0f; |
| 151 | scale2 = 0.5f; |
| 152 | } |
| 153 | |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 154 | for (uint32_t i = 0; i < len; i++) { |
| 155 | const uint32_t firstInputIndex = i * 2 + 1; |
| 156 | const uint32_t secondInputIndex = firstInputIndex + 1; |
| 157 | const uint32_t outputIndex = secondInputIndex + 1; |
| 158 | |
| 159 | // The first operation input. |
| 160 | operands[firstInputIndex] = { |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 161 | .type = operandType, |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 162 | .dimensions = {1}, |
| 163 | .numberOfConsumers = 1, |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 164 | .scale = scale1, |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 165 | .zeroPoint = 0, |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 166 | .lifetime = (i == 0 ? TestOperandLifeTime::MODEL_INPUT |
| 167 | : TestOperandLifeTime::TEMPORARY_VARIABLE), |
| 168 | .data = (i == 0 ? TestBuffer::createFromVector<CppType>({1}) : TestBuffer()), |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | // The second operation input, value = 1. |
| 172 | operands[secondInputIndex] = { |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 173 | .type = operandType, |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 174 | .dimensions = {1}, |
| 175 | .numberOfConsumers = 1, |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 176 | .scale = scale2, |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 177 | .zeroPoint = 0, |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 178 | .lifetime = TestOperandLifeTime::CONSTANT_COPY, |
| 179 | .data = TestBuffer::createFromVector<CppType>({bufferValue}), |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 180 | }; |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 181 | |
| 182 | // The operation. All operations share the same activation scalar. |
| 183 | // The output operand is created as an input in the next iteration of the loop, in the case |
| 184 | // of all but the last member of the chain; and after the loop as a model output, in the |
| 185 | // case of the last member of the chain. |
| 186 | operations[i] = { |
| 187 | .type = op, |
| 188 | .inputs = {firstInputIndex, secondInputIndex, /*activation scalar*/ 0}, |
| 189 | .outputs = {outputIndex}, |
| 190 | }; |
| 191 | } |
| 192 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 193 | // For TestOperationType::ADD, output = 1 + 1 * len = len + 1 |
| 194 | // For TestOperationType::MUL, output = 1 * 1 ^ len = 1 |
| 195 | CppType outputResult = static_cast<CppType>(op == TestOperationType::ADD ? len + 1u : 1u); |
| 196 | |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 197 | // The model output. |
| 198 | operands.back() = { |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 199 | .type = operandType, |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 200 | .dimensions = {1}, |
| 201 | .numberOfConsumers = 0, |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 202 | .scale = scale1, |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 203 | .zeroPoint = 0, |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 204 | .lifetime = TestOperandLifeTime::MODEL_OUTPUT, |
| 205 | .data = TestBuffer::createFromVector<CppType>({outputResult}), |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 206 | }; |
| 207 | |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 208 | return { |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 209 | .main = {.operands = std::move(operands), |
| 210 | .operations = std::move(operations), |
| 211 | .inputIndexes = {1}, |
| 212 | .outputIndexes = {len * 2 + 1}}, |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 213 | .isRelaxed = false, |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 214 | }; |
| 215 | } |
| 216 | |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 217 | } // namespace |
| 218 | |
| 219 | // Tag for the compilation caching tests. |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 220 | class CompilationCachingTestBase : public testing::Test { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 221 | protected: |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 222 | CompilationCachingTestBase(sp<IDevice> device, OperandType type) |
| 223 | : kDevice(std::move(device)), kOperandType(type) {} |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 224 | |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 225 | void SetUp() override { |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 226 | testing::Test::SetUp(); |
Michael Butler | 13b0516 | 2019-08-29 22:17:24 -0700 | [diff] [blame] | 227 | ASSERT_NE(kDevice.get(), nullptr); |
Michael Butler | 9c3c864 | 2021-08-23 18:14:50 -0700 | [diff] [blame^] | 228 | const bool deviceIsResponsive = kDevice->ping().isOk(); |
| 229 | ASSERT_TRUE(deviceIsResponsive); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 230 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 231 | // Create cache directory. The cache directory and a temporary cache file is always created |
| 232 | // to test the behavior of prepareModelFromCache, even when caching is not supported. |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 233 | char cacheDirTemp[] = "/data/local/tmp/TestCompilationCachingXXXXXX"; |
| 234 | char* cacheDir = mkdtemp(cacheDirTemp); |
| 235 | ASSERT_NE(cacheDir, nullptr); |
Xusong Wang | 6824cc1 | 2019-02-12 18:00:37 -0800 | [diff] [blame] | 236 | mCacheDir = cacheDir; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 237 | mCacheDir.push_back('/'); |
Xusong Wang | 6824cc1 | 2019-02-12 18:00:37 -0800 | [diff] [blame] | 238 | |
Michael Butler | 13b0516 | 2019-08-29 22:17:24 -0700 | [diff] [blame] | 239 | Return<void> ret = kDevice->getNumberOfCacheFilesNeeded( |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 240 | [this](ErrorStatus status, uint32_t numModelCache, uint32_t numDataCache) { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 241 | EXPECT_EQ(ErrorStatus::NONE, status); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 242 | mNumModelCache = numModelCache; |
| 243 | mNumDataCache = numDataCache; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 244 | }); |
| 245 | EXPECT_TRUE(ret.isOk()); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 246 | mIsCachingSupported = mNumModelCache > 0 || mNumDataCache > 0; |
| 247 | |
| 248 | // Create empty cache files. |
| 249 | mTmpCache = mCacheDir + "tmp"; |
| 250 | for (uint32_t i = 0; i < mNumModelCache; i++) { |
| 251 | mModelCache.push_back({mCacheDir + "model" + std::to_string(i)}); |
| 252 | } |
| 253 | for (uint32_t i = 0; i < mNumDataCache; i++) { |
| 254 | mDataCache.push_back({mCacheDir + "data" + std::to_string(i)}); |
| 255 | } |
Sushil Nath | 898088d | 2020-08-18 01:07:20 +0000 | [diff] [blame] | 256 | // Sample handles, use AccessMode::WRITE_ONLY for createCacheHandles to create files. |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 257 | hidl_vec<hidl_handle> modelHandle, dataHandle, tmpHandle; |
| 258 | createCacheHandles(mModelCache, AccessMode::WRITE_ONLY, &modelHandle); |
| 259 | createCacheHandles(mDataCache, AccessMode::WRITE_ONLY, &dataHandle); |
| 260 | createCacheHandles({{mTmpCache}}, AccessMode::WRITE_ONLY, &tmpHandle); |
| 261 | |
| 262 | if (!mIsCachingSupported) { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 263 | LOG(INFO) << "NN VTS: Early termination of test because vendor service does not " |
| 264 | "support compilation caching."; |
| 265 | std::cout << "[ ] Early termination of test because vendor service does not " |
| 266 | "support compilation caching." |
| 267 | << std::endl; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 268 | } |
Xusong Wang | 6824cc1 | 2019-02-12 18:00:37 -0800 | [diff] [blame] | 269 | } |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 270 | |
Xusong Wang | 6824cc1 | 2019-02-12 18:00:37 -0800 | [diff] [blame] | 271 | void TearDown() override { |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 272 | // If the test passes, remove the tmp directory. Otherwise, keep it for debugging purposes. |
Michael Butler | 13b0516 | 2019-08-29 22:17:24 -0700 | [diff] [blame] | 273 | if (!testing::Test::HasFailure()) { |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 274 | // Recursively remove the cache directory specified by mCacheDir. |
| 275 | auto callback = [](const char* entry, const struct stat*, int, struct FTW*) { |
| 276 | return remove(entry); |
| 277 | }; |
| 278 | nftw(mCacheDir.c_str(), callback, 128, FTW_DEPTH | FTW_MOUNT | FTW_PHYS); |
Xusong Wang | 6824cc1 | 2019-02-12 18:00:37 -0800 | [diff] [blame] | 279 | } |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 280 | testing::Test::TearDown(); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 281 | } |
| 282 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 283 | // Model and examples creators. According to kOperandType, the following methods will return |
| 284 | // either float32 model/examples or the quant8 variant. |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 285 | TestModel createTestModel() { |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 286 | if (kOperandType == OperandType::TENSOR_FLOAT32) { |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 287 | return float32_model::get_test_model(); |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 288 | } else { |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 289 | return quant8_model::get_test_model(); |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 293 | TestModel createLargeTestModel(OperationType op, uint32_t len) { |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 294 | if (kOperandType == OperandType::TENSOR_FLOAT32) { |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 295 | return createLargeTestModelImpl<float, TestOperandType::TENSOR_FLOAT32>( |
| 296 | static_cast<TestOperationType>(op), len); |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 297 | } else { |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 298 | return createLargeTestModelImpl<uint8_t, TestOperandType::TENSOR_QUANT8_ASYMM>( |
| 299 | static_cast<TestOperationType>(op), len); |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 303 | // See if the service can handle the model. |
Michael Butler | 62749b9 | 2019-08-26 23:55:47 -0700 | [diff] [blame] | 304 | bool isModelFullySupported(const Model& model) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 305 | bool fullySupportsModel = false; |
Michael Butler | 13b0516 | 2019-08-29 22:17:24 -0700 | [diff] [blame] | 306 | Return<void> supportedCall = kDevice->getSupportedOperations_1_2( |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 307 | model, |
| 308 | [&fullySupportsModel, &model](ErrorStatus status, const hidl_vec<bool>& supported) { |
| 309 | ASSERT_EQ(ErrorStatus::NONE, status); |
| 310 | ASSERT_EQ(supported.size(), model.operations.size()); |
| 311 | fullySupportsModel = std::all_of(supported.begin(), supported.end(), |
| 312 | [](bool valid) { return valid; }); |
| 313 | }); |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 314 | EXPECT_TRUE(supportedCall.isOk()); |
| 315 | return fullySupportsModel; |
| 316 | } |
| 317 | |
Michael Butler | 62749b9 | 2019-08-26 23:55:47 -0700 | [diff] [blame] | 318 | void saveModelToCache(const Model& model, const hidl_vec<hidl_handle>& modelCache, |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 319 | const hidl_vec<hidl_handle>& dataCache, |
Xusong Wang | 3dca799 | 2020-06-22 18:04:33 +0000 | [diff] [blame] | 320 | sp<IPreparedModel>* preparedModel = nullptr) { |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 321 | if (preparedModel != nullptr) *preparedModel = nullptr; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 322 | |
| 323 | // Launch prepare model. |
| 324 | sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback(); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 325 | hidl_array<uint8_t, sizeof(mToken)> cacheToken(mToken); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 326 | Return<ErrorStatus> prepareLaunchStatus = |
Michael Butler | 13b0516 | 2019-08-29 22:17:24 -0700 | [diff] [blame] | 327 | kDevice->prepareModel_1_2(model, ExecutionPreference::FAST_SINGLE_ANSWER, |
| 328 | modelCache, dataCache, cacheToken, preparedModelCallback); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 329 | ASSERT_TRUE(prepareLaunchStatus.isOk()); |
| 330 | ASSERT_EQ(static_cast<ErrorStatus>(prepareLaunchStatus), ErrorStatus::NONE); |
| 331 | |
| 332 | // Retrieve prepared model. |
| 333 | preparedModelCallback->wait(); |
Xusong Wang | 3dca799 | 2020-06-22 18:04:33 +0000 | [diff] [blame] | 334 | ASSERT_EQ(preparedModelCallback->getStatus(), ErrorStatus::NONE); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 335 | if (preparedModel != nullptr) { |
Michael Butler | 62749b9 | 2019-08-26 23:55:47 -0700 | [diff] [blame] | 336 | *preparedModel = IPreparedModel::castFrom(preparedModelCallback->getPreparedModel()) |
| 337 | .withDefault(nullptr); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 338 | } |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | bool checkEarlyTermination(ErrorStatus status) { |
| 342 | if (status == ErrorStatus::GENERAL_FAILURE) { |
| 343 | LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot " |
| 344 | "save the prepared model that it does not support."; |
| 345 | std::cout << "[ ] Early termination of test because vendor service cannot " |
| 346 | "save the prepared model that it does not support." |
| 347 | << std::endl; |
| 348 | return true; |
| 349 | } |
| 350 | return false; |
| 351 | } |
| 352 | |
Michael Butler | 62749b9 | 2019-08-26 23:55:47 -0700 | [diff] [blame] | 353 | bool checkEarlyTermination(const Model& model) { |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 354 | if (!isModelFullySupported(model)) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 355 | LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot " |
| 356 | "prepare model that it does not support."; |
| 357 | std::cout << "[ ] Early termination of test because vendor service cannot " |
| 358 | "prepare model that it does not support." |
| 359 | << std::endl; |
| 360 | return true; |
| 361 | } |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | void prepareModelFromCache(const hidl_vec<hidl_handle>& modelCache, |
| 366 | const hidl_vec<hidl_handle>& dataCache, |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 367 | sp<IPreparedModel>* preparedModel, ErrorStatus* status) { |
| 368 | // Launch prepare model from cache. |
| 369 | sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback(); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 370 | hidl_array<uint8_t, sizeof(mToken)> cacheToken(mToken); |
Michael Butler | 13b0516 | 2019-08-29 22:17:24 -0700 | [diff] [blame] | 371 | Return<ErrorStatus> prepareLaunchStatus = kDevice->prepareModelFromCache( |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 372 | modelCache, dataCache, cacheToken, preparedModelCallback); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 373 | ASSERT_TRUE(prepareLaunchStatus.isOk()); |
| 374 | if (static_cast<ErrorStatus>(prepareLaunchStatus) != ErrorStatus::NONE) { |
| 375 | *preparedModel = nullptr; |
| 376 | *status = static_cast<ErrorStatus>(prepareLaunchStatus); |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | // Retrieve prepared model. |
| 381 | preparedModelCallback->wait(); |
| 382 | *status = preparedModelCallback->getStatus(); |
Michael Butler | 62749b9 | 2019-08-26 23:55:47 -0700 | [diff] [blame] | 383 | *preparedModel = IPreparedModel::castFrom(preparedModelCallback->getPreparedModel()) |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 384 | .withDefault(nullptr); |
| 385 | } |
| 386 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 387 | // Absolute path to the temporary cache directory. |
Xusong Wang | 6824cc1 | 2019-02-12 18:00:37 -0800 | [diff] [blame] | 388 | std::string mCacheDir; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 389 | |
| 390 | // Groups of file paths for model and data cache in the tmp cache directory, initialized with |
| 391 | // outer_size = mNum{Model|Data}Cache, inner_size = 1. The outer vector corresponds to handles |
| 392 | // and the inner vector is for fds held by each handle. |
| 393 | std::vector<std::vector<std::string>> mModelCache; |
| 394 | std::vector<std::vector<std::string>> mDataCache; |
| 395 | |
| 396 | // A separate temporary file path in the tmp cache directory. |
| 397 | std::string mTmpCache; |
| 398 | |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 399 | uint8_t mToken[static_cast<uint32_t>(Constant::BYTE_SIZE_OF_CACHE_TOKEN)] = {}; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 400 | uint32_t mNumModelCache; |
| 401 | uint32_t mNumDataCache; |
| 402 | uint32_t mIsCachingSupported; |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 403 | |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 404 | const sp<IDevice> kDevice; |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 405 | // The primary data type of the testModel. |
| 406 | const OperandType kOperandType; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 407 | }; |
| 408 | |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 409 | using CompilationCachingTestParam = std::tuple<NamedDevice, OperandType>; |
| 410 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 411 | // A parameterized fixture of CompilationCachingTestBase. Every test will run twice, with the first |
| 412 | // pass running with float32 models and the second pass running with quant8 models. |
| 413 | class CompilationCachingTest : public CompilationCachingTestBase, |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 414 | public testing::WithParamInterface<CompilationCachingTestParam> { |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 415 | protected: |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 416 | CompilationCachingTest() |
| 417 | : CompilationCachingTestBase(getData(std::get<NamedDevice>(GetParam())), |
| 418 | std::get<OperandType>(GetParam())) {} |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 419 | }; |
| 420 | |
| 421 | TEST_P(CompilationCachingTest, CacheSavingAndRetrieval) { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 422 | // Create test HIDL model and compile. |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 423 | const TestModel& testModel = createTestModel(); |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 424 | const Model model = createModel(testModel); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 425 | if (checkEarlyTermination(model)) return; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 426 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 427 | |
| 428 | // Save the compilation to cache. |
| 429 | { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 430 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 431 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 432 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 433 | saveModelToCache(model, modelCache, dataCache); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | // Retrieve preparedModel from cache. |
| 437 | { |
| 438 | preparedModel = nullptr; |
| 439 | ErrorStatus status; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 440 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 441 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 442 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 443 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 444 | if (!mIsCachingSupported) { |
| 445 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 446 | ASSERT_EQ(preparedModel, nullptr); |
| 447 | return; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 448 | } else if (checkEarlyTermination(status)) { |
| 449 | ASSERT_EQ(preparedModel, nullptr); |
| 450 | return; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 451 | } else { |
| 452 | ASSERT_EQ(status, ErrorStatus::NONE); |
| 453 | ASSERT_NE(preparedModel, nullptr); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | // Execute and verify results. |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 458 | EvaluatePreparedModel(preparedModel, testModel, |
| 459 | /*testDynamicOutputShape=*/false); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 462 | TEST_P(CompilationCachingTest, CacheSavingAndRetrievalNonZeroOffset) { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 463 | // Create test HIDL model and compile. |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 464 | const TestModel& testModel = createTestModel(); |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 465 | const Model model = createModel(testModel); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 466 | if (checkEarlyTermination(model)) return; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 467 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 468 | |
| 469 | // Save the compilation to cache. |
| 470 | { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 471 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 472 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 473 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Sushil Nath | 898088d | 2020-08-18 01:07:20 +0000 | [diff] [blame] | 474 | uint8_t sampleBytes[] = {0, 0}; |
| 475 | // Write a sample integer to the cache. |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 476 | // The driver should be able to handle non-empty cache and non-zero fd offset. |
| 477 | for (uint32_t i = 0; i < modelCache.size(); i++) { |
Sushil Nath | 898088d | 2020-08-18 01:07:20 +0000 | [diff] [blame] | 478 | ASSERT_EQ(write(modelCache[i].getNativeHandle()->data[0], &sampleBytes, |
| 479 | sizeof(sampleBytes)), |
| 480 | sizeof(sampleBytes)); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 481 | } |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 482 | for (uint32_t i = 0; i < dataCache.size(); i++) { |
| 483 | ASSERT_EQ( |
Sushil Nath | 898088d | 2020-08-18 01:07:20 +0000 | [diff] [blame] | 484 | write(dataCache[i].getNativeHandle()->data[0], &sampleBytes, sizeof(sampleBytes)), |
| 485 | sizeof(sampleBytes)); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 486 | } |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 487 | saveModelToCache(model, modelCache, dataCache); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | // Retrieve preparedModel from cache. |
| 491 | { |
| 492 | preparedModel = nullptr; |
| 493 | ErrorStatus status; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 494 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 495 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 496 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Sushil Nath | 898088d | 2020-08-18 01:07:20 +0000 | [diff] [blame] | 497 | uint8_t sampleByte = 0; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 498 | // Advance the offset of each handle by one byte. |
| 499 | // The driver should be able to handle non-zero fd offset. |
| 500 | for (uint32_t i = 0; i < modelCache.size(); i++) { |
Sushil Nath | 898088d | 2020-08-18 01:07:20 +0000 | [diff] [blame] | 501 | ASSERT_GE(read(modelCache[i].getNativeHandle()->data[0], &sampleByte, 1), 0); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 502 | } |
| 503 | for (uint32_t i = 0; i < dataCache.size(); i++) { |
Sushil Nath | 898088d | 2020-08-18 01:07:20 +0000 | [diff] [blame] | 504 | ASSERT_GE(read(dataCache[i].getNativeHandle()->data[0], &sampleByte, 1), 0); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 505 | } |
| 506 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 507 | if (!mIsCachingSupported) { |
| 508 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 509 | ASSERT_EQ(preparedModel, nullptr); |
| 510 | return; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 511 | } else if (checkEarlyTermination(status)) { |
| 512 | ASSERT_EQ(preparedModel, nullptr); |
| 513 | return; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 514 | } else { |
| 515 | ASSERT_EQ(status, ErrorStatus::NONE); |
| 516 | ASSERT_NE(preparedModel, nullptr); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | // Execute and verify results. |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 521 | EvaluatePreparedModel(preparedModel, testModel, |
| 522 | /*testDynamicOutputShape=*/false); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 525 | TEST_P(CompilationCachingTest, SaveToCacheInvalidNumCache) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 526 | // Create test HIDL model and compile. |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 527 | const TestModel& testModel = createTestModel(); |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 528 | const Model model = createModel(testModel); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 529 | if (checkEarlyTermination(model)) return; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 530 | |
| 531 | // Test with number of model cache files greater than mNumModelCache. |
| 532 | { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 533 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 534 | // Pass an additional cache file for model cache. |
| 535 | mModelCache.push_back({mTmpCache}); |
| 536 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 537 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 538 | mModelCache.pop_back(); |
| 539 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 540 | saveModelToCache(model, modelCache, dataCache, &preparedModel); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 541 | ASSERT_NE(preparedModel, nullptr); |
| 542 | // Execute and verify results. |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 543 | EvaluatePreparedModel(preparedModel, testModel, |
| 544 | /*testDynamicOutputShape=*/false); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 545 | // Check if prepareModelFromCache fails. |
| 546 | preparedModel = nullptr; |
| 547 | ErrorStatus status; |
| 548 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 549 | if (status != ErrorStatus::INVALID_ARGUMENT) { |
| 550 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 551 | } |
| 552 | ASSERT_EQ(preparedModel, nullptr); |
| 553 | } |
| 554 | |
| 555 | // Test with number of model cache files smaller than mNumModelCache. |
| 556 | if (mModelCache.size() > 0) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 557 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 558 | // Pop out the last cache file. |
| 559 | auto tmp = mModelCache.back(); |
| 560 | mModelCache.pop_back(); |
| 561 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 562 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 563 | mModelCache.push_back(tmp); |
| 564 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 565 | saveModelToCache(model, modelCache, dataCache, &preparedModel); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 566 | ASSERT_NE(preparedModel, nullptr); |
| 567 | // Execute and verify results. |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 568 | EvaluatePreparedModel(preparedModel, testModel, |
| 569 | /*testDynamicOutputShape=*/false); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 570 | // Check if prepareModelFromCache fails. |
| 571 | preparedModel = nullptr; |
| 572 | ErrorStatus status; |
| 573 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 574 | if (status != ErrorStatus::INVALID_ARGUMENT) { |
| 575 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 576 | } |
| 577 | ASSERT_EQ(preparedModel, nullptr); |
| 578 | } |
| 579 | |
| 580 | // Test with number of data cache files greater than mNumDataCache. |
| 581 | { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 582 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 583 | // Pass an additional cache file for data cache. |
| 584 | mDataCache.push_back({mTmpCache}); |
| 585 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 586 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 587 | mDataCache.pop_back(); |
| 588 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 589 | saveModelToCache(model, modelCache, dataCache, &preparedModel); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 590 | ASSERT_NE(preparedModel, nullptr); |
| 591 | // Execute and verify results. |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 592 | EvaluatePreparedModel(preparedModel, testModel, |
| 593 | /*testDynamicOutputShape=*/false); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 594 | // Check if prepareModelFromCache fails. |
| 595 | preparedModel = nullptr; |
| 596 | ErrorStatus status; |
| 597 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 598 | if (status != ErrorStatus::INVALID_ARGUMENT) { |
| 599 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 600 | } |
| 601 | ASSERT_EQ(preparedModel, nullptr); |
| 602 | } |
| 603 | |
| 604 | // Test with number of data cache files smaller than mNumDataCache. |
| 605 | if (mDataCache.size() > 0) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 606 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 607 | // Pop out the last cache file. |
| 608 | auto tmp = mDataCache.back(); |
| 609 | mDataCache.pop_back(); |
| 610 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 611 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 612 | mDataCache.push_back(tmp); |
| 613 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 614 | saveModelToCache(model, modelCache, dataCache, &preparedModel); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 615 | ASSERT_NE(preparedModel, nullptr); |
| 616 | // Execute and verify results. |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 617 | EvaluatePreparedModel(preparedModel, testModel, |
| 618 | /*testDynamicOutputShape=*/false); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 619 | // Check if prepareModelFromCache fails. |
| 620 | preparedModel = nullptr; |
| 621 | ErrorStatus status; |
| 622 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 623 | if (status != ErrorStatus::INVALID_ARGUMENT) { |
| 624 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 625 | } |
| 626 | ASSERT_EQ(preparedModel, nullptr); |
| 627 | } |
| 628 | } |
| 629 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 630 | TEST_P(CompilationCachingTest, PrepareModelFromCacheInvalidNumCache) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 631 | // Create test HIDL model and compile. |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 632 | const TestModel& testModel = createTestModel(); |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 633 | const Model model = createModel(testModel); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 634 | if (checkEarlyTermination(model)) return; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 635 | |
| 636 | // Save the compilation to cache. |
| 637 | { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 638 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 639 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 640 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 641 | saveModelToCache(model, modelCache, dataCache); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | // Test with number of model cache files greater than mNumModelCache. |
| 645 | { |
| 646 | sp<IPreparedModel> preparedModel = nullptr; |
| 647 | ErrorStatus status; |
| 648 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 649 | mModelCache.push_back({mTmpCache}); |
| 650 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 651 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 652 | mModelCache.pop_back(); |
| 653 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 654 | if (status != ErrorStatus::GENERAL_FAILURE) { |
| 655 | ASSERT_EQ(status, ErrorStatus::INVALID_ARGUMENT); |
| 656 | } |
| 657 | ASSERT_EQ(preparedModel, nullptr); |
| 658 | } |
| 659 | |
| 660 | // Test with number of model cache files smaller than mNumModelCache. |
| 661 | if (mModelCache.size() > 0) { |
| 662 | sp<IPreparedModel> preparedModel = nullptr; |
| 663 | ErrorStatus status; |
| 664 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 665 | auto tmp = mModelCache.back(); |
| 666 | mModelCache.pop_back(); |
| 667 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 668 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 669 | mModelCache.push_back(tmp); |
| 670 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 671 | if (status != ErrorStatus::GENERAL_FAILURE) { |
| 672 | ASSERT_EQ(status, ErrorStatus::INVALID_ARGUMENT); |
| 673 | } |
| 674 | ASSERT_EQ(preparedModel, nullptr); |
| 675 | } |
| 676 | |
| 677 | // Test with number of data cache files greater than mNumDataCache. |
| 678 | { |
| 679 | sp<IPreparedModel> preparedModel = nullptr; |
| 680 | ErrorStatus status; |
| 681 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 682 | mDataCache.push_back({mTmpCache}); |
| 683 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 684 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 685 | mDataCache.pop_back(); |
| 686 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 687 | if (status != ErrorStatus::GENERAL_FAILURE) { |
| 688 | ASSERT_EQ(status, ErrorStatus::INVALID_ARGUMENT); |
| 689 | } |
| 690 | ASSERT_EQ(preparedModel, nullptr); |
| 691 | } |
| 692 | |
| 693 | // Test with number of data cache files smaller than mNumDataCache. |
| 694 | if (mDataCache.size() > 0) { |
| 695 | sp<IPreparedModel> preparedModel = nullptr; |
| 696 | ErrorStatus status; |
| 697 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 698 | auto tmp = mDataCache.back(); |
| 699 | mDataCache.pop_back(); |
| 700 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 701 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 702 | mDataCache.push_back(tmp); |
| 703 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 704 | if (status != ErrorStatus::GENERAL_FAILURE) { |
| 705 | ASSERT_EQ(status, ErrorStatus::INVALID_ARGUMENT); |
| 706 | } |
| 707 | ASSERT_EQ(preparedModel, nullptr); |
| 708 | } |
| 709 | } |
| 710 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 711 | TEST_P(CompilationCachingTest, SaveToCacheInvalidNumFd) { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 712 | // Create test HIDL model and compile. |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 713 | const TestModel& testModel = createTestModel(); |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 714 | const Model model = createModel(testModel); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 715 | if (checkEarlyTermination(model)) return; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 716 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 717 | // Go through each handle in model cache, test with NumFd greater than 1. |
| 718 | for (uint32_t i = 0; i < mNumModelCache; i++) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 719 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 720 | // Pass an invalid number of fds for handle i. |
| 721 | mModelCache[i].push_back(mTmpCache); |
| 722 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 723 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 724 | mModelCache[i].pop_back(); |
| 725 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 726 | saveModelToCache(model, modelCache, dataCache, &preparedModel); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 727 | ASSERT_NE(preparedModel, nullptr); |
| 728 | // Execute and verify results. |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 729 | EvaluatePreparedModel(preparedModel, testModel, |
| 730 | /*testDynamicOutputShape=*/false); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 731 | // Check if prepareModelFromCache fails. |
| 732 | preparedModel = nullptr; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 733 | ErrorStatus status; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 734 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 735 | if (status != ErrorStatus::INVALID_ARGUMENT) { |
| 736 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 737 | } |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 738 | ASSERT_EQ(preparedModel, nullptr); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 739 | } |
| 740 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 741 | // Go through each handle in model cache, test with NumFd equal to 0. |
| 742 | for (uint32_t i = 0; i < mNumModelCache; i++) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 743 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 744 | // Pass an invalid number of fds for handle i. |
| 745 | auto tmp = mModelCache[i].back(); |
| 746 | mModelCache[i].pop_back(); |
| 747 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 748 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 749 | mModelCache[i].push_back(tmp); |
| 750 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 751 | saveModelToCache(model, modelCache, dataCache, &preparedModel); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 752 | ASSERT_NE(preparedModel, nullptr); |
| 753 | // Execute and verify results. |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 754 | EvaluatePreparedModel(preparedModel, testModel, |
| 755 | /*testDynamicOutputShape=*/false); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 756 | // Check if prepareModelFromCache fails. |
| 757 | preparedModel = nullptr; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 758 | ErrorStatus status; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 759 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 760 | if (status != ErrorStatus::INVALID_ARGUMENT) { |
| 761 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 762 | } |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 763 | ASSERT_EQ(preparedModel, nullptr); |
| 764 | } |
| 765 | |
| 766 | // Go through each handle in data cache, test with NumFd greater than 1. |
| 767 | for (uint32_t i = 0; i < mNumDataCache; i++) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 768 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 769 | // Pass an invalid number of fds for handle i. |
| 770 | mDataCache[i].push_back(mTmpCache); |
| 771 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 772 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 773 | mDataCache[i].pop_back(); |
| 774 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 775 | saveModelToCache(model, modelCache, dataCache, &preparedModel); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 776 | ASSERT_NE(preparedModel, nullptr); |
| 777 | // Execute and verify results. |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 778 | EvaluatePreparedModel(preparedModel, testModel, |
| 779 | /*testDynamicOutputShape=*/false); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 780 | // Check if prepareModelFromCache fails. |
| 781 | preparedModel = nullptr; |
| 782 | ErrorStatus status; |
| 783 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 784 | if (status != ErrorStatus::INVALID_ARGUMENT) { |
| 785 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 786 | } |
| 787 | ASSERT_EQ(preparedModel, nullptr); |
| 788 | } |
| 789 | |
| 790 | // Go through each handle in data cache, test with NumFd equal to 0. |
| 791 | for (uint32_t i = 0; i < mNumDataCache; i++) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 792 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 793 | // Pass an invalid number of fds for handle i. |
| 794 | auto tmp = mDataCache[i].back(); |
| 795 | mDataCache[i].pop_back(); |
| 796 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 797 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 798 | mDataCache[i].push_back(tmp); |
| 799 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 800 | saveModelToCache(model, modelCache, dataCache, &preparedModel); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 801 | ASSERT_NE(preparedModel, nullptr); |
| 802 | // Execute and verify results. |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 803 | EvaluatePreparedModel(preparedModel, testModel, |
| 804 | /*testDynamicOutputShape=*/false); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 805 | // Check if prepareModelFromCache fails. |
| 806 | preparedModel = nullptr; |
| 807 | ErrorStatus status; |
| 808 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 809 | if (status != ErrorStatus::INVALID_ARGUMENT) { |
| 810 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 811 | } |
| 812 | ASSERT_EQ(preparedModel, nullptr); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 816 | TEST_P(CompilationCachingTest, PrepareModelFromCacheInvalidNumFd) { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 817 | // Create test HIDL model and compile. |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 818 | const TestModel& testModel = createTestModel(); |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 819 | const Model model = createModel(testModel); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 820 | if (checkEarlyTermination(model)) return; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 821 | |
| 822 | // Save the compilation to cache. |
| 823 | { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 824 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 825 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 826 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 827 | saveModelToCache(model, modelCache, dataCache); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 828 | } |
| 829 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 830 | // Go through each handle in model cache, test with NumFd greater than 1. |
| 831 | for (uint32_t i = 0; i < mNumModelCache; i++) { |
| 832 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 833 | ErrorStatus status; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 834 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 835 | mModelCache[i].push_back(mTmpCache); |
| 836 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 837 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 838 | mModelCache[i].pop_back(); |
| 839 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 840 | if (status != ErrorStatus::GENERAL_FAILURE) { |
| 841 | ASSERT_EQ(status, ErrorStatus::INVALID_ARGUMENT); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 842 | } |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 843 | ASSERT_EQ(preparedModel, nullptr); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 844 | } |
| 845 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 846 | // Go through each handle in model cache, test with NumFd equal to 0. |
| 847 | for (uint32_t i = 0; i < mNumModelCache; i++) { |
| 848 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 849 | ErrorStatus status; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 850 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 851 | auto tmp = mModelCache[i].back(); |
| 852 | mModelCache[i].pop_back(); |
| 853 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 854 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 855 | mModelCache[i].push_back(tmp); |
| 856 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 857 | if (status != ErrorStatus::GENERAL_FAILURE) { |
| 858 | ASSERT_EQ(status, ErrorStatus::INVALID_ARGUMENT); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 859 | } |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 860 | ASSERT_EQ(preparedModel, nullptr); |
| 861 | } |
| 862 | |
| 863 | // Go through each handle in data cache, test with NumFd greater than 1. |
| 864 | for (uint32_t i = 0; i < mNumDataCache; i++) { |
| 865 | sp<IPreparedModel> preparedModel = nullptr; |
| 866 | ErrorStatus status; |
| 867 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 868 | mDataCache[i].push_back(mTmpCache); |
| 869 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 870 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 871 | mDataCache[i].pop_back(); |
| 872 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 873 | if (status != ErrorStatus::GENERAL_FAILURE) { |
| 874 | ASSERT_EQ(status, ErrorStatus::INVALID_ARGUMENT); |
| 875 | } |
| 876 | ASSERT_EQ(preparedModel, nullptr); |
| 877 | } |
| 878 | |
| 879 | // Go through each handle in data cache, test with NumFd equal to 0. |
| 880 | for (uint32_t i = 0; i < mNumDataCache; i++) { |
| 881 | sp<IPreparedModel> preparedModel = nullptr; |
| 882 | ErrorStatus status; |
| 883 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 884 | auto tmp = mDataCache[i].back(); |
| 885 | mDataCache[i].pop_back(); |
| 886 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 887 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 888 | mDataCache[i].push_back(tmp); |
| 889 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 890 | if (status != ErrorStatus::GENERAL_FAILURE) { |
| 891 | ASSERT_EQ(status, ErrorStatus::INVALID_ARGUMENT); |
| 892 | } |
| 893 | ASSERT_EQ(preparedModel, nullptr); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 897 | TEST_P(CompilationCachingTest, SaveToCacheInvalidAccessMode) { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 898 | // Create test HIDL model and compile. |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 899 | const TestModel& testModel = createTestModel(); |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 900 | const Model model = createModel(testModel); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 901 | if (checkEarlyTermination(model)) return; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 902 | std::vector<AccessMode> modelCacheMode(mNumModelCache, AccessMode::READ_WRITE); |
| 903 | std::vector<AccessMode> dataCacheMode(mNumDataCache, AccessMode::READ_WRITE); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 904 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 905 | // Go through each handle in model cache, test with invalid access mode. |
| 906 | for (uint32_t i = 0; i < mNumModelCache; i++) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 907 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 908 | modelCacheMode[i] = AccessMode::READ_ONLY; |
| 909 | createCacheHandles(mModelCache, modelCacheMode, &modelCache); |
| 910 | createCacheHandles(mDataCache, dataCacheMode, &dataCache); |
| 911 | modelCacheMode[i] = AccessMode::READ_WRITE; |
| 912 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 913 | saveModelToCache(model, modelCache, dataCache, &preparedModel); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 914 | ASSERT_NE(preparedModel, nullptr); |
| 915 | // Execute and verify results. |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 916 | EvaluatePreparedModel(preparedModel, testModel, |
| 917 | /*testDynamicOutputShape=*/false); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 918 | // Check if prepareModelFromCache fails. |
| 919 | preparedModel = nullptr; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 920 | ErrorStatus status; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 921 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 922 | if (status != ErrorStatus::INVALID_ARGUMENT) { |
| 923 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 924 | } |
| 925 | ASSERT_EQ(preparedModel, nullptr); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 926 | } |
| 927 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 928 | // Go through each handle in data cache, test with invalid access mode. |
| 929 | for (uint32_t i = 0; i < mNumDataCache; i++) { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 930 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 931 | dataCacheMode[i] = AccessMode::READ_ONLY; |
| 932 | createCacheHandles(mModelCache, modelCacheMode, &modelCache); |
| 933 | createCacheHandles(mDataCache, dataCacheMode, &dataCache); |
| 934 | dataCacheMode[i] = AccessMode::READ_WRITE; |
| 935 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 936 | saveModelToCache(model, modelCache, dataCache, &preparedModel); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 937 | ASSERT_NE(preparedModel, nullptr); |
| 938 | // Execute and verify results. |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 939 | EvaluatePreparedModel(preparedModel, testModel, |
| 940 | /*testDynamicOutputShape=*/false); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 941 | // Check if prepareModelFromCache fails. |
| 942 | preparedModel = nullptr; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 943 | ErrorStatus status; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 944 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 945 | if (status != ErrorStatus::INVALID_ARGUMENT) { |
| 946 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 947 | } |
| 948 | ASSERT_EQ(preparedModel, nullptr); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 952 | TEST_P(CompilationCachingTest, PrepareModelFromCacheInvalidAccessMode) { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 953 | // Create test HIDL model and compile. |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 954 | const TestModel& testModel = createTestModel(); |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 955 | const Model model = createModel(testModel); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 956 | if (checkEarlyTermination(model)) return; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 957 | std::vector<AccessMode> modelCacheMode(mNumModelCache, AccessMode::READ_WRITE); |
| 958 | std::vector<AccessMode> dataCacheMode(mNumDataCache, AccessMode::READ_WRITE); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 959 | |
| 960 | // Save the compilation to cache. |
| 961 | { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 962 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 963 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 964 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 965 | saveModelToCache(model, modelCache, dataCache); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 966 | } |
| 967 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 968 | // Go through each handle in model cache, test with invalid access mode. |
| 969 | for (uint32_t i = 0; i < mNumModelCache; i++) { |
| 970 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 971 | ErrorStatus status; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 972 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 973 | modelCacheMode[i] = AccessMode::WRITE_ONLY; |
| 974 | createCacheHandles(mModelCache, modelCacheMode, &modelCache); |
| 975 | createCacheHandles(mDataCache, dataCacheMode, &dataCache); |
| 976 | modelCacheMode[i] = AccessMode::READ_WRITE; |
| 977 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 978 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 979 | ASSERT_EQ(preparedModel, nullptr); |
| 980 | } |
| 981 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 982 | // Go through each handle in data cache, test with invalid access mode. |
| 983 | for (uint32_t i = 0; i < mNumDataCache; i++) { |
| 984 | sp<IPreparedModel> preparedModel = nullptr; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 985 | ErrorStatus status; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 986 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 987 | dataCacheMode[i] = AccessMode::WRITE_ONLY; |
| 988 | createCacheHandles(mModelCache, modelCacheMode, &modelCache); |
| 989 | createCacheHandles(mDataCache, dataCacheMode, &dataCache); |
| 990 | dataCacheMode[i] = AccessMode::READ_WRITE; |
| 991 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 992 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 993 | ASSERT_EQ(preparedModel, nullptr); |
| 994 | } |
| 995 | } |
| 996 | |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 997 | // Copy file contents between file groups. |
| 998 | // The outer vector corresponds to handles and the inner vector is for fds held by each handle. |
| 999 | // The outer vector sizes must match and the inner vectors must have size = 1. |
| 1000 | static void copyCacheFiles(const std::vector<std::vector<std::string>>& from, |
| 1001 | const std::vector<std::vector<std::string>>& to) { |
| 1002 | constexpr size_t kBufferSize = 1000000; |
| 1003 | uint8_t buffer[kBufferSize]; |
| 1004 | |
| 1005 | ASSERT_EQ(from.size(), to.size()); |
| 1006 | for (uint32_t i = 0; i < from.size(); i++) { |
| 1007 | ASSERT_EQ(from[i].size(), 1u); |
| 1008 | ASSERT_EQ(to[i].size(), 1u); |
| 1009 | int fromFd = open(from[i][0].c_str(), O_RDONLY); |
| 1010 | int toFd = open(to[i][0].c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); |
| 1011 | ASSERT_GE(fromFd, 0); |
| 1012 | ASSERT_GE(toFd, 0); |
| 1013 | |
| 1014 | ssize_t readBytes; |
| 1015 | while ((readBytes = read(fromFd, &buffer, kBufferSize)) > 0) { |
| 1016 | ASSERT_EQ(write(toFd, &buffer, readBytes), readBytes); |
| 1017 | } |
| 1018 | ASSERT_GE(readBytes, 0); |
| 1019 | |
| 1020 | close(fromFd); |
| 1021 | close(toFd); |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | // Number of operations in the large test model. |
| 1026 | constexpr uint32_t kLargeModelSize = 100; |
Xusong Wang | 3dca799 | 2020-06-22 18:04:33 +0000 | [diff] [blame] | 1027 | constexpr uint32_t kNumIterationsTOCTOU = 100; |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1028 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 1029 | TEST_P(CompilationCachingTest, SaveToCache_TOCTOU) { |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1030 | if (!mIsCachingSupported) return; |
| 1031 | |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 1032 | // Create test models and check if fully supported by the service. |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1033 | const TestModel testModelMul = createLargeTestModel(OperationType::MUL, kLargeModelSize); |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 1034 | const Model modelMul = createModel(testModelMul); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1035 | if (checkEarlyTermination(modelMul)) return; |
| 1036 | const TestModel testModelAdd = createLargeTestModel(OperationType::ADD, kLargeModelSize); |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 1037 | const Model modelAdd = createModel(testModelAdd); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1038 | if (checkEarlyTermination(modelAdd)) return; |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 1039 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1040 | // Save the modelMul compilation to cache. |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1041 | auto modelCacheMul = mModelCache; |
| 1042 | for (auto& cache : modelCacheMul) { |
| 1043 | cache[0].append("_mul"); |
| 1044 | } |
| 1045 | { |
| 1046 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1047 | createCacheHandles(modelCacheMul, AccessMode::READ_WRITE, &modelCache); |
| 1048 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1049 | saveModelToCache(modelMul, modelCache, dataCache); |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1050 | } |
| 1051 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1052 | // Use a different token for modelAdd. |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1053 | mToken[0]++; |
| 1054 | |
Xusong Wang | 3dca799 | 2020-06-22 18:04:33 +0000 | [diff] [blame] | 1055 | // This test is probabilistic, so we run it multiple times. |
| 1056 | for (uint32_t i = 0; i < kNumIterationsTOCTOU; i++) { |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1057 | // Save the modelAdd compilation to cache. |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1058 | { |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1059 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1060 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 1061 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 1062 | |
| 1063 | // Spawn a thread to copy the cache content concurrently while saving to cache. |
| 1064 | std::thread thread(copyCacheFiles, std::cref(modelCacheMul), std::cref(mModelCache)); |
Xusong Wang | 3dca799 | 2020-06-22 18:04:33 +0000 | [diff] [blame] | 1065 | saveModelToCache(modelAdd, modelCache, dataCache); |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1066 | thread.join(); |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | // Retrieve preparedModel from cache. |
| 1070 | { |
| 1071 | sp<IPreparedModel> preparedModel = nullptr; |
| 1072 | ErrorStatus status; |
| 1073 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1074 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 1075 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 1076 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 1077 | |
| 1078 | // The preparation may fail or succeed, but must not crash. If the preparation succeeds, |
| 1079 | // the prepared model must be executed with the correct result and not crash. |
| 1080 | if (status != ErrorStatus::NONE) { |
| 1081 | ASSERT_EQ(preparedModel, nullptr); |
| 1082 | } else { |
| 1083 | ASSERT_NE(preparedModel, nullptr); |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 1084 | EvaluatePreparedModel(preparedModel, testModelAdd, |
| 1085 | /*testDynamicOutputShape=*/false); |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 1091 | TEST_P(CompilationCachingTest, PrepareFromCache_TOCTOU) { |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1092 | if (!mIsCachingSupported) return; |
| 1093 | |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 1094 | // Create test models and check if fully supported by the service. |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1095 | const TestModel testModelMul = createLargeTestModel(OperationType::MUL, kLargeModelSize); |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 1096 | const Model modelMul = createModel(testModelMul); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1097 | if (checkEarlyTermination(modelMul)) return; |
| 1098 | const TestModel testModelAdd = createLargeTestModel(OperationType::ADD, kLargeModelSize); |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 1099 | const Model modelAdd = createModel(testModelAdd); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1100 | if (checkEarlyTermination(modelAdd)) return; |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 1101 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1102 | // Save the modelMul compilation to cache. |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1103 | auto modelCacheMul = mModelCache; |
| 1104 | for (auto& cache : modelCacheMul) { |
| 1105 | cache[0].append("_mul"); |
| 1106 | } |
| 1107 | { |
| 1108 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1109 | createCacheHandles(modelCacheMul, AccessMode::READ_WRITE, &modelCache); |
| 1110 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1111 | saveModelToCache(modelMul, modelCache, dataCache); |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1114 | // Use a different token for modelAdd. |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1115 | mToken[0]++; |
| 1116 | |
Xusong Wang | 3dca799 | 2020-06-22 18:04:33 +0000 | [diff] [blame] | 1117 | // This test is probabilistic, so we run it multiple times. |
| 1118 | for (uint32_t i = 0; i < kNumIterationsTOCTOU; i++) { |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1119 | // Save the modelAdd compilation to cache. |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1120 | { |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1121 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1122 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 1123 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | 3dca799 | 2020-06-22 18:04:33 +0000 | [diff] [blame] | 1124 | saveModelToCache(modelAdd, modelCache, dataCache); |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | // Retrieve preparedModel from cache. |
| 1128 | { |
| 1129 | sp<IPreparedModel> preparedModel = nullptr; |
| 1130 | ErrorStatus status; |
| 1131 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1132 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 1133 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 1134 | |
| 1135 | // Spawn a thread to copy the cache content concurrently while preparing from cache. |
| 1136 | std::thread thread(copyCacheFiles, std::cref(modelCacheMul), std::cref(mModelCache)); |
| 1137 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 1138 | thread.join(); |
| 1139 | |
| 1140 | // The preparation may fail or succeed, but must not crash. If the preparation succeeds, |
| 1141 | // the prepared model must be executed with the correct result and not crash. |
| 1142 | if (status != ErrorStatus::NONE) { |
| 1143 | ASSERT_EQ(preparedModel, nullptr); |
| 1144 | } else { |
| 1145 | ASSERT_NE(preparedModel, nullptr); |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 1146 | EvaluatePreparedModel(preparedModel, testModelAdd, |
| 1147 | /*testDynamicOutputShape=*/false); |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1148 | } |
| 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 1153 | TEST_P(CompilationCachingTest, ReplaceSecuritySensitiveCache) { |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1154 | if (!mIsCachingSupported) return; |
| 1155 | |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 1156 | // Create test models and check if fully supported by the service. |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1157 | const TestModel testModelMul = createLargeTestModel(OperationType::MUL, kLargeModelSize); |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 1158 | const Model modelMul = createModel(testModelMul); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1159 | if (checkEarlyTermination(modelMul)) return; |
| 1160 | const TestModel testModelAdd = createLargeTestModel(OperationType::ADD, kLargeModelSize); |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 1161 | const Model modelAdd = createModel(testModelAdd); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1162 | if (checkEarlyTermination(modelAdd)) return; |
Xusong Wang | 4f71afc | 2019-04-26 15:33:38 -0700 | [diff] [blame] | 1163 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1164 | // Save the modelMul compilation to cache. |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1165 | auto modelCacheMul = mModelCache; |
| 1166 | for (auto& cache : modelCacheMul) { |
| 1167 | cache[0].append("_mul"); |
| 1168 | } |
| 1169 | { |
| 1170 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1171 | createCacheHandles(modelCacheMul, AccessMode::READ_WRITE, &modelCache); |
| 1172 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1173 | saveModelToCache(modelMul, modelCache, dataCache); |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1176 | // Use a different token for modelAdd. |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1177 | mToken[0]++; |
| 1178 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1179 | // Save the modelAdd compilation to cache. |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1180 | { |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1181 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1182 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 1183 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1184 | saveModelToCache(modelAdd, modelCache, dataCache); |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1187 | // Replace the model cache of modelAdd with modelMul. |
Xusong Wang | 7cc0ccc | 2019-04-23 14:28:17 -0700 | [diff] [blame] | 1188 | copyCacheFiles(modelCacheMul, mModelCache); |
| 1189 | |
| 1190 | // Retrieve the preparedModel from cache, expect failure. |
| 1191 | { |
| 1192 | sp<IPreparedModel> preparedModel = nullptr; |
| 1193 | ErrorStatus status; |
| 1194 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1195 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 1196 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 1197 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
| 1198 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 1199 | ASSERT_EQ(preparedModel, nullptr); |
| 1200 | } |
| 1201 | } |
| 1202 | |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 1203 | static const auto kNamedDeviceChoices = testing::ValuesIn(getNamedDevices()); |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 1204 | static const auto kOperandTypeChoices = |
Michael Butler | 13b0516 | 2019-08-29 22:17:24 -0700 | [diff] [blame] | 1205 | testing::Values(OperandType::TENSOR_FLOAT32, OperandType::TENSOR_QUANT8_ASYMM); |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 1206 | |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 1207 | std::string printCompilationCachingTest( |
| 1208 | const testing::TestParamInfo<CompilationCachingTestParam>& info) { |
| 1209 | const auto& [namedDevice, operandType] = info.param; |
| 1210 | const std::string type = (operandType == OperandType::TENSOR_FLOAT32 ? "float32" : "quant8"); |
| 1211 | return gtestCompliantName(getName(namedDevice) + "_" + type); |
| 1212 | } |
| 1213 | |
Xusong Wang | 7f40613 | 2020-09-11 10:47:03 -0700 | [diff] [blame] | 1214 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(CompilationCachingTest); |
Jim Pollock | 9df50c6 | 2020-08-07 13:19:32 +0100 | [diff] [blame] | 1215 | INSTANTIATE_TEST_SUITE_P(TestCompilationCaching, CompilationCachingTest, |
| 1216 | testing::Combine(kNamedDeviceChoices, kOperandTypeChoices), |
| 1217 | printCompilationCachingTest); |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 1218 | |
| 1219 | using CompilationCachingSecurityTestParam = std::tuple<NamedDevice, OperandType, uint32_t>; |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 1220 | |
| 1221 | class CompilationCachingSecurityTest |
| 1222 | : public CompilationCachingTestBase, |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 1223 | public testing::WithParamInterface<CompilationCachingSecurityTestParam> { |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1224 | protected: |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 1225 | CompilationCachingSecurityTest() |
| 1226 | : CompilationCachingTestBase(getData(std::get<NamedDevice>(GetParam())), |
| 1227 | std::get<OperandType>(GetParam())) {} |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 1228 | |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1229 | void SetUp() { |
Xusong Wang | 0e0721f | 2019-05-07 12:57:49 -0700 | [diff] [blame] | 1230 | CompilationCachingTestBase::SetUp(); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1231 | generator.seed(kSeed); |
| 1232 | } |
| 1233 | |
| 1234 | // Get a random integer within a closed range [lower, upper]. |
| 1235 | template <typename T> |
| 1236 | T getRandomInt(T lower, T upper) { |
| 1237 | std::uniform_int_distribution<T> dis(lower, upper); |
| 1238 | return dis(generator); |
| 1239 | } |
| 1240 | |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1241 | // Randomly flip one single bit of the cache entry. |
| 1242 | void flipOneBitOfCache(const std::string& filename, bool* skip) { |
| 1243 | FILE* pFile = fopen(filename.c_str(), "r+"); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1244 | ASSERT_EQ(fseek(pFile, 0, SEEK_END), 0); |
| 1245 | long int fileSize = ftell(pFile); |
| 1246 | if (fileSize == 0) { |
| 1247 | fclose(pFile); |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1248 | *skip = true; |
| 1249 | return; |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1250 | } |
| 1251 | ASSERT_EQ(fseek(pFile, getRandomInt(0l, fileSize - 1), SEEK_SET), 0); |
| 1252 | int readByte = fgetc(pFile); |
| 1253 | ASSERT_NE(readByte, EOF); |
| 1254 | ASSERT_EQ(fseek(pFile, -1, SEEK_CUR), 0); |
| 1255 | ASSERT_NE(fputc(static_cast<uint8_t>(readByte) ^ (1U << getRandomInt(0, 7)), pFile), EOF); |
| 1256 | fclose(pFile); |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1257 | *skip = false; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1258 | } |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1259 | |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1260 | // Randomly append bytes to the cache entry. |
| 1261 | void appendBytesToCache(const std::string& filename, bool* skip) { |
| 1262 | FILE* pFile = fopen(filename.c_str(), "a"); |
| 1263 | uint32_t appendLength = getRandomInt(1, 256); |
| 1264 | for (uint32_t i = 0; i < appendLength; i++) { |
| 1265 | ASSERT_NE(fputc(getRandomInt<uint8_t>(0, 255), pFile), EOF); |
| 1266 | } |
| 1267 | fclose(pFile); |
| 1268 | *skip = false; |
| 1269 | } |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1270 | |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1271 | enum class ExpectedResult { GENERAL_FAILURE, NOT_CRASH }; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1272 | |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1273 | // Test if the driver behaves as expected when given corrupted cache or token. |
| 1274 | // The modifier will be invoked after save to cache but before prepare from cache. |
| 1275 | // The modifier accepts one pointer argument "skip" as the returning value, indicating |
| 1276 | // whether the test should be skipped or not. |
| 1277 | void testCorruptedCache(ExpectedResult expected, std::function<void(bool*)> modifier) { |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1278 | const TestModel& testModel = createTestModel(); |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 1279 | const Model model = createModel(testModel); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1280 | if (checkEarlyTermination(model)) return; |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1281 | |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1282 | // Save the compilation to cache. |
| 1283 | { |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1284 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1285 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 1286 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 1287 | saveModelToCache(model, modelCache, dataCache); |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1288 | } |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1289 | |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1290 | bool skip = false; |
| 1291 | modifier(&skip); |
| 1292 | if (skip) return; |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1293 | |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1294 | // Retrieve preparedModel from cache. |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1295 | { |
| 1296 | sp<IPreparedModel> preparedModel = nullptr; |
| 1297 | ErrorStatus status; |
| 1298 | hidl_vec<hidl_handle> modelCache, dataCache; |
| 1299 | createCacheHandles(mModelCache, AccessMode::READ_WRITE, &modelCache); |
| 1300 | createCacheHandles(mDataCache, AccessMode::READ_WRITE, &dataCache); |
| 1301 | prepareModelFromCache(modelCache, dataCache, &preparedModel, &status); |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1302 | |
| 1303 | switch (expected) { |
| 1304 | case ExpectedResult::GENERAL_FAILURE: |
| 1305 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 1306 | ASSERT_EQ(preparedModel, nullptr); |
| 1307 | break; |
| 1308 | case ExpectedResult::NOT_CRASH: |
| 1309 | ASSERT_EQ(preparedModel == nullptr, status != ErrorStatus::NONE); |
| 1310 | break; |
| 1311 | default: |
| 1312 | FAIL(); |
| 1313 | } |
Xusong Wang | ed0822b | 2019-02-25 16:58:58 -0800 | [diff] [blame] | 1314 | } |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1315 | } |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1316 | |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 1317 | const uint32_t kSeed = std::get<uint32_t>(GetParam()); |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1318 | std::mt19937 generator; |
| 1319 | }; |
| 1320 | |
| 1321 | TEST_P(CompilationCachingSecurityTest, CorruptedModelCache) { |
| 1322 | if (!mIsCachingSupported) return; |
| 1323 | for (uint32_t i = 0; i < mNumModelCache; i++) { |
| 1324 | testCorruptedCache(ExpectedResult::GENERAL_FAILURE, |
| 1325 | [this, i](bool* skip) { flipOneBitOfCache(mModelCache[i][0], skip); }); |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | TEST_P(CompilationCachingSecurityTest, WrongLengthModelCache) { |
| 1330 | if (!mIsCachingSupported) return; |
| 1331 | for (uint32_t i = 0; i < mNumModelCache; i++) { |
| 1332 | testCorruptedCache(ExpectedResult::GENERAL_FAILURE, |
| 1333 | [this, i](bool* skip) { appendBytesToCache(mModelCache[i][0], skip); }); |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | TEST_P(CompilationCachingSecurityTest, CorruptedDataCache) { |
| 1338 | if (!mIsCachingSupported) return; |
| 1339 | for (uint32_t i = 0; i < mNumDataCache; i++) { |
| 1340 | testCorruptedCache(ExpectedResult::NOT_CRASH, |
| 1341 | [this, i](bool* skip) { flipOneBitOfCache(mDataCache[i][0], skip); }); |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | TEST_P(CompilationCachingSecurityTest, WrongLengthDataCache) { |
| 1346 | if (!mIsCachingSupported) return; |
| 1347 | for (uint32_t i = 0; i < mNumDataCache; i++) { |
| 1348 | testCorruptedCache(ExpectedResult::NOT_CRASH, |
| 1349 | [this, i](bool* skip) { appendBytesToCache(mDataCache[i][0], skip); }); |
| 1350 | } |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | TEST_P(CompilationCachingSecurityTest, WrongToken) { |
| 1354 | if (!mIsCachingSupported) return; |
Xusong Wang | e371f6f | 2019-04-23 14:51:50 -0700 | [diff] [blame] | 1355 | testCorruptedCache(ExpectedResult::GENERAL_FAILURE, [this](bool* skip) { |
| 1356 | // Randomly flip one single bit in mToken. |
| 1357 | uint32_t ind = |
| 1358 | getRandomInt(0u, static_cast<uint32_t>(Constant::BYTE_SIZE_OF_CACHE_TOKEN) - 1); |
| 1359 | mToken[ind] ^= (1U << getRandomInt(0, 7)); |
| 1360 | *skip = false; |
| 1361 | }); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1362 | } |
| 1363 | |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 1364 | std::string printCompilationCachingSecurityTest( |
| 1365 | const testing::TestParamInfo<CompilationCachingSecurityTestParam>& info) { |
| 1366 | const auto& [namedDevice, operandType, seed] = info.param; |
| 1367 | const std::string type = (operandType == OperandType::TENSOR_FLOAT32 ? "float32" : "quant8"); |
| 1368 | return gtestCompliantName(getName(namedDevice) + "_" + type + "_" + std::to_string(seed)); |
| 1369 | } |
| 1370 | |
Xusong Wang | 7f40613 | 2020-09-11 10:47:03 -0700 | [diff] [blame] | 1371 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(CompilationCachingSecurityTest); |
Jim Pollock | 9df50c6 | 2020-08-07 13:19:32 +0100 | [diff] [blame] | 1372 | INSTANTIATE_TEST_SUITE_P(TestCompilationCaching, CompilationCachingSecurityTest, |
| 1373 | testing::Combine(kNamedDeviceChoices, kOperandTypeChoices, |
| 1374 | testing::Range(0U, 10U)), |
| 1375 | printCompilationCachingSecurityTest); |
Xusong Wang | 96e68dc | 2019-01-18 17:28:26 -0800 | [diff] [blame] | 1376 | |
Michael Butler | 62749b9 | 2019-08-26 23:55:47 -0700 | [diff] [blame] | 1377 | } // namespace android::hardware::neuralnetworks::V1_2::vts::functional |