1 /*
2 * Copyright (c) 2021-2022 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 "audioadmdispatcherrendercmdid_fuzzer.h"
16 #include "hdf_log.h"
17 #include "audio_hdi_common.h"
18 #include "audio_adm_common.h"
19
20 using namespace OHOS::Audio;
21 namespace OHOS {
22 namespace Audio {
AudioAdmDispatcherRenderCmdidFuzzTest(const uint8_t * data,size_t size)23 bool AudioAdmDispatcherRenderCmdidFuzzTest(const uint8_t *data, size_t size)
24 {
25 bool result = false;
26 struct HdfIoService *admDisRenFuzzService = nullptr;
27 struct HdfSBuf *sBuf = nullptr;
28 struct HdfSBuf *reply = nullptr;
29 struct AudioPcmHwParams hwParams {
30 .streamType = AUDIO_RENDER_STREAM, .channels = AUDIO_CHANNEL_BOTH_RIGHT,
31 .rate = BROADCAST_AM_RATE, .periodSize = FRAME_DATA / 2,
32 .periodCount = 32, .format = AUDIO_FORMAT_TYPE_PCM_24_BIT, .cardServiceName = CARD_SEVICE_NAME.c_str(),
33 .isBigEndian = 0, .isSignedData = 1, .silenceThreshold = 16384
34 };
35
36 admDisRenFuzzService = HdfIoServiceBind(HDF_RENDER_SERVICE.c_str());
37 if (admDisRenFuzzService == nullptr || admDisRenFuzzService->dispatcher == nullptr) {
38 HDF_LOGE("%{public}s: HdfIoServiceBind failed \n", __func__);
39 return false;
40 }
41 sBuf = HdfSbufObtainDefaultSize();
42 if (sBuf == nullptr) {
43 HDF_LOGE("%{public}s: sBuf is NULL \n", __func__);
44 return false;
45 }
46 int32_t ret = WriteHwParamsToBuf(sBuf, hwParams);
47 if (ret < 0) {
48 HDF_LOGE("%{public}s: Write hwparams to buffer failed \n", __func__);
49 return false;
50 }
51 int32_t cmdId = *(reinterpret_cast<int32_t *>(const_cast<uint8_t *>(data)));
52 ret = admDisRenFuzzService->dispatcher->Dispatch(&admDisRenFuzzService->object, cmdId, sBuf, reply);
53 if (ret == HDF_SUCCESS) {
54 HDF_LOGE("%{public}s: Dispatch sucess \n", __func__);
55 result = true;
56 }
57 HdfSbufRecycle(sBuf);
58 HdfIoServiceRecycle(admDisRenFuzzService);
59 return result;
60 }
61 }
62 }
63
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)64 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
65 {
66 OHOS::Audio::AudioAdmDispatcherRenderCmdidFuzzTest(data, size);
67 return 0;
68 }
69