1 /* 2 * Copyright (C) 2020 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 #include <LegacyUtils.h> 18 #include <nnapi/TypeUtils.h> 19 #include <src/libfuzzer/libfuzzer_macro.h> 20 21 #include <algorithm> 22 23 #include "Converter.h" 24 #include "Model.pb.h" 25 #include "TestHarness.h" 26 27 // Fuzz test logic. This function will either run to completion and return, or crash. 28 extern void nnapiFuzzTest(const ::test_helper::TestModel& testModel); 29 30 namespace { 31 32 using ::android::nn::getNonExtensionSize; 33 using ::android::nn::OperandType; 34 using ::android::nn::fuzz::convertToTestModel; 35 using ::test_helper::TestModel; 36 using ::test_helper::TestOperand; 37 operandOverflows(const TestOperand & operand)38bool operandOverflows(const TestOperand& operand) { 39 const auto operandType = static_cast<OperandType>(operand.type); 40 return getNonExtensionSize(operandType, operand.dimensions).has_value(); 41 } 42 shouldSkip(const TestModel & model)43bool shouldSkip(const TestModel& model) { 44 return std::any_of(model.main.operands.begin(), model.main.operands.end(), operandOverflows); 45 } 46 47 } // namespace 48 DEFINE_PROTO_FUZZER(const::android_nn_fuzz::Test & model)49DEFINE_PROTO_FUZZER(const ::android_nn_fuzz::Test& model) { 50 const TestModel testModel = convertToTestModel(model); 51 if (!shouldSkip(testModel)) { 52 nnapiFuzzTest(testModel); 53 } 54 } 55