• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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[BUFFER_INDEX_ZERO] << PCM_24_BIT) | (ptr[BUFFER_INDEX_ONE] << PCM_16_BIT) |
37         (ptr[BUFFER_INDEX_TWO] << PCM_8_BIT) | (ptr[BUFFER_INDEX_THREE]);
38 }
39 
RenderFucSwitch(struct IAudioRender * & render,uint32_t cmd,const uint8_t * & rawData,size_t size)40 void RenderFucSwitch(struct IAudioRender *&render, uint32_t cmd, const uint8_t *&rawData, size_t size)
41 {
42     uint8_t *data = const_cast<uint8_t *>(rawData);
43     switch (cmd) {
44         case AUDIO_OFFLOAD_RENDER_SET_BUFFER_SIZE:
45             render->SetBufferSize(render, *(reinterpret_cast<uint32_t *>(data)));
46             break;
47         default:
48             return;
49     }
50 }
51 
DoSomethingInterestingWithMyAPI(const uint8_t * rawData,size_t size)52 bool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)
53 {
54     if (rawData == nullptr) {
55         return false;
56     }
57     struct IAudioAdapter *adapter = nullptr;
58     struct IAudioRender *render = nullptr;
59     uint32_t cmd = Convert2Uint32(rawData);
60     uint32_t renderId = 0;
61 
62     rawData = rawData + OFFSET;
63     size = size - OFFSET;
64     struct IAudioManager *manager = IAudioManagerGet(false);
65     if (manager == nullptr) {
66         return false;
67     }
68     int32_t ret = AudioOffloadCreateRender(manager, PIN_OUT_SPEAKER, ADAPTER_NAME, &adapter, &render, &renderId);
69     if (ret != HDF_SUCCESS) {
70         return false;
71     }
72     RenderFucSwitch(render, cmd, rawData, size);
73     adapter->DestroyRender(adapter, renderId);
74     manager->UnloadAdapter(manager, ADAPTER_NAME.c_str());
75     IAudioManagerRelease(manager, false);
76     return true;
77 }
78 }
79 }
80 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
82 {
83     if (size < OHOS::Audio::THRESHOLD) {
84         return 0;
85     }
86     OHOS::Audio::DoSomethingInterestingWithMyAPI(data, size);
87     return 0;
88 }