1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#include "effectmodel_fuzzer.h" 17 18#include <cstdlib> 19#include "v1_0/effect_types.h" 20#include "v1_0/ieffect_control.h" 21#include "v1_0/ieffect_model.h" 22 23using namespace std; 24 25namespace OHOS { 26namespace Audio { 27constexpr int32_t OFFSET = 4; 28constexpr size_t THRESHOLD = 10; 29constexpr uint32_t MAX_DESCRIPTOR_NUM = 20; 30 31enum EffectModelCmdId { 32 EFFECT_MODEL_IS_SUPPLY_LIBS, 33 EFFECT_MODEL_GET_ALL_DESCRIPTORS, 34 EFFECT_MODEL_CREATE_EFFECT_CONTROLLER, 35 EFFECT_MODEL_DESTROY_EFFECT_CONTROLLER, 36 EFFECT_MODEL_GET_EFFECT_DESCRIPTOR, 37}; 38 39static uint32_t Convert2Uint32(const uint8_t *ptr) 40{ 41 if (ptr == nullptr) { 42 return 0; 43 } 44 /* 45 * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left, 46 * and the third digit no left 47 */ 48 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]); 49} 50 51void EffectModelFucSwitch(struct IEffectModel *&model, uint32_t cmd, const uint8_t *&rawData, size_t size) 52{ 53 uint8_t *data = const_cast<uint8_t *>(rawData); 54 switch (cmd) { 55 case EFFECT_MODEL_IS_SUPPLY_LIBS: { 56 bool isSupport = false; 57 model->IsSupplyEffectLibs(model, &isSupport); 58 break; 59 } 60 case EFFECT_MODEL_GET_ALL_DESCRIPTORS: { 61 uint32_t descsLen = MAX_DESCRIPTOR_NUM; 62 struct EffectControllerDescriptor descs[MAX_DESCRIPTOR_NUM]; 63 model->GetAllEffectDescriptors(model, descs, &descsLen); 64 break; 65 } 66 case EFFECT_MODEL_CREATE_EFFECT_CONTROLLER: { 67 struct IEffectControl *contoller = nullptr; 68 struct ControllerId contollerId; 69 struct EffectInfo info = { 70 .libName = reinterpret_cast<char *>(data), 71 .effectId = reinterpret_cast<char *>(data), 72 .ioDirection = 1, 73 }; 74 model->CreateEffectController(model, &info, &contoller, &contollerId); 75 break; 76 } 77 case EFFECT_MODEL_DESTROY_EFFECT_CONTROLLER: { 78 struct ControllerId contollerId{ 79 .libName = reinterpret_cast<char *>(data), 80 .effectId = reinterpret_cast<char *>(data), 81 }; 82 model->DestroyEffectController(model, &contollerId); 83 break; 84 } 85 case EFFECT_MODEL_GET_EFFECT_DESCRIPTOR: { 86 struct EffectControllerDescriptor desc; 87 model->GetEffectDescriptor(model, reinterpret_cast<const char *>(data), &desc); 88 break; 89 } 90 default: 91 return; 92 } 93} 94 95bool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size) 96{ 97 if (rawData == nullptr) { 98 return false; 99 } 100 uint32_t cmd = Convert2Uint32(rawData) % EFFECT_MODEL_GET_EFFECT_DESCRIPTOR; 101 rawData = rawData + OFFSET; 102 103 struct IEffectModel *model = IEffectModelGet(true); 104 if (model == nullptr) { 105 return false; 106 } 107 108 EffectModelFucSwitch(model, cmd, rawData, size); 109 110 if (model != nullptr) { 111 IEffectModelRelease(model, true); 112 } 113 return true; 114} 115} 116} 117 118extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) 119{ 120 if (size < OHOS::Audio::THRESHOLD) { 121 return 0; 122 } 123 124 if(data == nullptr) { 125 return 0; 126 } 127 128 for (int i = 0; i < size - 1; i++) { 129 if (data[i] == '\0') { 130 return 0; 131 } 132 } 133 134 if (data[size -1] != '\0') { 135 return 0; 136 } 137 OHOS::Audio::DoSomethingInterestingWithMyAPI(data, size); 138 return 0; 139}