1 /*
2  * Copyright (c) 2022-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 
16 #include "audiointerfacelibctlcapturecmdid_fuzzer.h"
17 #include "audio_adm_fuzzer_common.h"
18 
19 using namespace OHOS::Audio;
20 namespace OHOS {
21 namespace Audio {
AudioInterfacelibctlcaptureCmdidFuzzTest(const uint8_t * data,size_t size)22     bool AudioInterfacelibctlcaptureCmdidFuzzTest(const uint8_t *data, size_t size)
23     {
24         bool result = false;
25         char resolvedPath[] = HDF_LIBRARY_FULL_PATH("libaudio_capture_adapter");
26         void *ctlcapFuzzPtrHandle = dlopen(resolvedPath, RTLD_LAZY);
27         if (ctlcapFuzzPtrHandle == nullptr) {
28             HDF_LOGE("%{public}s: dlopen failed\n", __func__);
29             return false;
30         }
31         struct DevHandle *(*AudioBindService)(const char *) = nullptr;
32         int32_t (*InterfaceLibCtlCapture)(struct DevHandle *, int, struct AudioHwCaptureParam *) = nullptr;
33         AudioBindService = reinterpret_cast<struct DevHandle *(*)(const char *)>
34            (dlsym(ctlcapFuzzPtrHandle, "AudioBindService"));
35         if (AudioBindService == nullptr) {
36             HDF_LOGE("%{public}s: dlsym AudioBindService failed \n", __func__);
37             dlclose(ctlcapFuzzPtrHandle);
38             return false;
39         }
40         struct DevHandle *handle = AudioBindService(BIND_CONTROL.c_str());
41         if (handle == nullptr) {
42             HDF_LOGE("%{public}s: AudioBindService failed \n", __func__);
43             dlclose(ctlcapFuzzPtrHandle);
44             return false;
45         }
46         InterfaceLibCtlCapture = reinterpret_cast<int32_t (*)(struct DevHandle *, int,
47             struct AudioHwCaptureParam *)>(dlsym(ctlcapFuzzPtrHandle, "AudioInterfaceLibCtlCapture"));
48         if (InterfaceLibCtlCapture == nullptr) {
49             HDF_LOGE("%{public}s: dlsym AudioInterfaceLibCtlCapture failed \n", __func__);
50             dlclose(ctlcapFuzzPtrHandle);
51             return false;
52         }
53         struct AudioHwCapture *hwCapture = nullptr;
54         (void)BindServiceAndHwCapture(hwCapture);
55         int32_t cmdId = *(reinterpret_cast<int32_t *>(const_cast<uint8_t *>(data)));
56         int32_t ret = InterfaceLibCtlCapture(handle, cmdId, &hwCapture->captureParam);
57         if (ret == HDF_SUCCESS) {
58             result = true;
59         }
60         free(hwCapture);
61         void (*CloseService)(const struct DevHandle *) = nullptr;
62         CloseService = reinterpret_cast<void (*)(const struct DevHandle *)>(dlsym(ctlcapFuzzPtrHandle,
63             "AudioCloseServiceCapture"));
64         if (CloseService == nullptr) {
65             HDF_LOGE("%{public}s: dlsym AudioCloseServiceCapture failed \n", __func__);
66             dlclose(ctlcapFuzzPtrHandle);
67             return false;
68         }
69         CloseService(handle);
70         dlclose(ctlcapFuzzPtrHandle);
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::AudioInterfacelibctlcaptureCmdidFuzzTest(data, size);
79     return 0;
80 }
81