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 #include "audiooffloadrender_fuzzer.h"
16 #include "hdi_service_common.h"
17 using namespace std;
18 namespace OHOS {
19 namespace Audio {
20 constexpr size_t THRESHOLD = 200;
21 constexpr int32_t OFFSET = 4;
22 
23 enum OffloadRenderCmdId {
24     AUDIO_OFFLOAD_RENDER_SET_BUFFER_SIZE,
25 };
26 
Convert2Uint32(const uint8_t * ptr)27 static uint32_t Convert2Uint32(const uint8_t *ptr)
28 {
29     if (ptr == nullptr) {
30         return 0;
31     }
32     /*
33      * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left,
34      * and the third digit no left
35      */
36     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
37 }
38 
RenderFucSwitch(struct IAudioRender * & render,uint32_t cmd,const uint8_t * & rawData,size_t size)39 void RenderFucSwitch(struct IAudioRender *&render, uint32_t cmd, const uint8_t *&rawData, size_t size)
40 {
41     uint8_t *data = const_cast<uint8_t *>(rawData);
42     switch (cmd) {
43         case AUDIO_OFFLOAD_RENDER_SET_BUFFER_SIZE:
44             render->SetBufferSize(render, *(reinterpret_cast<uint32_t *>(data)));
45             break;
46         default:
47             return;
48     }
49 }
50 
DoSomethingInterestingWithMyAPI(const uint8_t * rawData,size_t size)51 bool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)
52 {
53     if (rawData == nullptr) {
54         return false;
55     }
56     struct IAudioAdapter *adapter = nullptr;
57     struct IAudioRender *render = nullptr;
58     uint32_t cmd = Convert2Uint32(rawData);
59     uint32_t renderId = 0;
60 
61     rawData = rawData + OFFSET;
62     size = size - OFFSET;
63     struct IAudioManager *manager = IAudioManagerGet(false);
64     if (manager == nullptr) {
65         return false;
66     }
67     int32_t ret = AudioOffloadCreateRender(manager, PIN_OUT_SPEAKER, ADAPTER_NAME, &adapter, &render, &renderId);
68     if (ret != HDF_SUCCESS) {
69         return false;
70     }
71     RenderFucSwitch(render, cmd, rawData, size);
72     adapter->DestroyRender(adapter, renderId);
73     manager->UnloadAdapter(manager, ADAPTER_NAME.c_str());
74     IAudioManagerRelease(manager, false);
75     return true;
76 }
77 }
78 }
79 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)80 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
81 {
82     if (size < OHOS::Audio::THRESHOLD) {
83         return 0;
84     }
85     OHOS::Audio::DoSomethingInterestingWithMyAPI(data, size);
86     return 0;
87 }