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