1 /*
2 * Copyright (c) 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
16 #include "audiointerfaceliboutputrendercmdid_fuzzer.h"
17 #include "audio_adm_fuzzer_common.h"
18
19 namespace OHOS {
20 namespace Audio {
AudioInterfaceliboutputrenderCmdidFuzzTest(const uint8_t * data,size_t size)21 bool AudioInterfaceliboutputrenderCmdidFuzzTest(const uint8_t *data, size_t size)
22 {
23 bool result = false;
24 char resolvedPath[] = HDF_LIBRARY_FULL_PATH("libaudio_render_adapter");
25 void *outputRenFuzzPtrHandle = dlopen(resolvedPath, RTLD_LAZY);
26 if (outputRenFuzzPtrHandle == nullptr) {
27 HDF_LOGE("%{public}s: dlopen failed \n", __func__);
28 return false;
29 }
30 struct DevHandle *(*BindServiceRender)(const char *) = nullptr;
31 int32_t (*InterfaceLibOutputRender)(struct DevHandle *, int, struct AudioHwRenderParam *) = nullptr;
32 BindServiceRender = reinterpret_cast<struct DevHandle* (*)(const char *)>
33 (dlsym(outputRenFuzzPtrHandle, "AudioBindServiceRender"));
34 if (BindServiceRender == nullptr) {
35 HDF_LOGE("%{public}s: dlsym AudioBindServiceRender failed \n", __func__);
36 dlclose(outputRenFuzzPtrHandle);
37 return false;
38 }
39 struct DevHandle *handle = nullptr;
40 handle = BindServiceRender(BIND_CONTROL.c_str());
41 if (handle == nullptr) {
42 HDF_LOGE("%{public}s: BindServiceRender failed \n", __func__);
43 dlclose(outputRenFuzzPtrHandle);
44 return false;
45 }
46 InterfaceLibOutputRender = reinterpret_cast<int32_t (*)(struct DevHandle *, int,
47 struct AudioHwRenderParam *)>(dlsym(outputRenFuzzPtrHandle, "AudioInterfaceLibOutputRender"));
48 if (InterfaceLibOutputRender == nullptr) {
49 HDF_LOGE("%{public}s: dlsym AudioInterfaceLibOutputRender failed \n", __func__);
50 dlclose(outputRenFuzzPtrHandle);
51 return false;
52 }
53 struct AudioHwRender *hwRender = nullptr;
54 int32_t ret = BindServiceAndHwRender(hwRender);
55 int32_t cmdId = *(reinterpret_cast<int32_t *>(const_cast<uint8_t *>(data)));
56 InterfaceLibOutputRender(handle, cmdId, &hwRender->renderParam);
57 if (ret == HDF_SUCCESS) {
58 result = true;
59 }
60 free(hwRender);
61 void (*CloseService)(const struct DevHandle *) = nullptr;
62 CloseService = reinterpret_cast<void (*)(const struct DevHandle *)>(dlsym(outputRenFuzzPtrHandle,
63 "AudioCloseServiceRender"));
64 if (CloseService == nullptr) {
65 HDF_LOGE("%{public}s: dlsym AudioCloseServiceRender failed \n", __func__);
66 dlclose(outputRenFuzzPtrHandle);
67 return false;
68 }
69 CloseService(handle);
70 dlclose(outputRenFuzzPtrHandle);
71 return result;
72 }
73 }
74 }
75
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)76 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
77 {
78 OHOS::Audio::AudioInterfaceliboutputrenderCmdidFuzzTest(data, size);
79 return 0;
80 }