• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "audiointerfacelibctlrendercmdid_fuzzer.h"
17 #include "audio_adm_fuzzer_common.h"
18 
19 using namespace OHOS::Audio;
20 namespace OHOS {
21 namespace Audio {
AudioInterfacelibctlrenderCmdidFuzzTest(const uint8_t * data,size_t size)22     bool AudioInterfacelibctlrenderCmdidFuzzTest(const uint8_t *data, size_t size)
23     {
24         bool result = false;
25         char resolvedPath[] = HDF_LIBRARY_FULL_PATH("libaudio_render_adapter");
26         void *ctlRenFuzzPtrHandle = dlopen(resolvedPath, RTLD_LAZY);
27         if (ctlRenFuzzPtrHandle == nullptr) {
28             HDF_LOGE("%{public}s: dlopen failed \n", __func__);
29             return false;
30         }
31         struct DevHandle *(*BindServiceRender)(const char *) = nullptr;
32         int32_t (*InterfaceLibCtlRender)(struct DevHandle *, int32_t, struct AudioHwRenderParam *) = nullptr;
33         BindServiceRender = reinterpret_cast<struct DevHandle *(*)(const char *)>(dlsym(ctlRenFuzzPtrHandle,
34             "AudioBindServiceRender"));
35         if (BindServiceRender == nullptr) {
36             HDF_LOGE("%{public}s: dlsym AudioBindServiceRender failed \n", __func__);
37             dlclose(ctlRenFuzzPtrHandle);
38             return false;
39         }
40         struct DevHandle *handle = BindServiceRender(BIND_CONTROL.c_str());
41         if (handle == nullptr) {
42             HDF_LOGE("%{public}s: BindServiceRender failed \n", __func__);
43             dlclose(ctlRenFuzzPtrHandle);
44             return false;
45         }
46         InterfaceLibCtlRender = reinterpret_cast<int32_t (*)(struct DevHandle *, int,
47             struct AudioHwRenderParam *)>(dlsym(ctlRenFuzzPtrHandle, "AudioInterfaceLibCtlRender"));
48         if (InterfaceLibCtlRender == nullptr) {
49             HDF_LOGE("%{public}s: dlsym AudioInterfaceLibCtlRender failed \n", __func__);
50             dlclose(ctlRenFuzzPtrHandle);
51             return false;
52         }
53         struct AudioHwRender *hwRender = nullptr;
54         (void)BindServiceAndHwRender(hwRender);
55         hwRender->renderParam.renderMode.hwInfo.card = AUDIO_SERVICE_IN;
56         if (size < sizeof(int32_t)) {
57             return false;
58         }
59         int32_t cmdId = *(reinterpret_cast<int32_t *>(const_cast<uint8_t *>(data)));
60         int32_t ret = InterfaceLibCtlRender(handle, cmdId, &hwRender->renderParam);
61         if (ret == HDF_SUCCESS) {
62             result = true;
63         }
64         free(hwRender);
65         void (*CloseService)(const struct DevHandle *) = nullptr;
66         CloseService = reinterpret_cast<void (*)(const struct DevHandle *)>(dlsym(ctlRenFuzzPtrHandle,
67             "AudioCloseServiceRender"));
68         if (CloseService == nullptr) {
69             HDF_LOGE("%{public}s:dlsym AudioCloseServiceRender failed \n", __func__);
70             dlclose(ctlRenFuzzPtrHandle);
71             return false;
72         }
73         CloseService(handle);
74         dlclose(ctlRenFuzzPtrHandle);
75         return result;
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     OHOS::Audio::AudioInterfacelibctlrenderCmdidFuzzTest(data, size);
83     return 0;
84 }