• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "audiointerfaceliboutputcapturecmdid_fuzzer.h"
17 #include "audio_adm_fuzzer_common.h"
18 
19 using namespace OHOS::Audio;
20 namespace OHOS {
21 namespace Audio {
AudioInterfaceliboutputcaptureCmdidFuzzTest(const uint8_t * data,size_t size)22     bool AudioInterfaceliboutputcaptureCmdidFuzzTest(const uint8_t *data, size_t size)
23     {
24         bool result = false;
25         char resolvedPath[] = HDF_LIBRARY_FULL_PATH("libaudio_capture_adapter");
26         void *outputCapPtrHandle = dlopen(resolvedPath, RTLD_LAZY);
27         if (outputCapPtrHandle == nullptr) {
28             HDF_LOGE("%{public}s: dlopen failed \n", __func__);
29             return false;
30         }
31         struct DevHandle *(*AudioBindService)(const char *) = nullptr;
32         int32_t (*InterfaceLibOutputCapture)(struct DevHandle *, int, struct AudioHwCaptureParam *) = nullptr;
33         AudioBindService = reinterpret_cast<struct DevHandle* (*)(const char *)>(dlsym(outputCapPtrHandle,
34             "AudioBindService"));
35         if (AudioBindService == nullptr) {
36             HDF_LOGE("%{public}s: dlsym AudioBindService failed \n", __func__);
37             dlclose(outputCapPtrHandle);
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(outputCapPtrHandle);
44             return false;
45         }
46         InterfaceLibOutputCapture = reinterpret_cast<int32_t (*)(struct DevHandle *, int,
47             struct AudioHwCaptureParam *)>(dlsym(outputCapPtrHandle, "AudioInterfaceLibOutputCapture"));
48         if (InterfaceLibOutputCapture == nullptr) {
49             HDF_LOGE("%{public}s: dlsym AudioInterfaceLibOutputCapture failed \n", __func__);
50             dlclose(outputCapPtrHandle);
51             return false;
52         }
53         struct AudioHwCapture *hwCapture = nullptr;
54         (void)BindServiceAndHwCapture(hwCapture);
55         if (size < sizeof(int32_t)) {
56             return false;
57         }
58         int32_t cmdId = *(reinterpret_cast<int32_t *>(const_cast<uint8_t *>(data)));
59         int32_t ret = InterfaceLibOutputCapture(handle, cmdId, &hwCapture->captureParam);
60         if (ret == HDF_SUCCESS) {
61             result = true;
62         }
63         free(hwCapture);
64         void (*CloseService)(const struct DevHandle *) = nullptr;
65         CloseService = reinterpret_cast<void (*)(const struct DevHandle *)>(dlsym(outputCapPtrHandle,
66             "AudioCloseServiceCapture"));
67         if (CloseService == nullptr) {
68             HDF_LOGE("%{public}s: dlsym AudioCloseServiceCapture failed \n", __func__);
69             dlclose(outputCapPtrHandle);
70             return false;
71         }
72         CloseService(handle);
73         dlclose(outputCapPtrHandle);
74         return result;
75     }
76 }
77 }
78 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)79 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
80 {
81     OHOS::Audio::AudioInterfaceliboutputcaptureCmdidFuzzTest(data, size);
82     return 0;
83 }
84