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 #include "audiomanager_fuzzer.h"
16 #include "v4_0/iaudio_manager.h"
17 #include "v4_0/audio_types.h"
18 using namespace std;
19 namespace OHOS {
20 namespace Audio {
21 constexpr size_t THRESHOLD = 200;
22 constexpr int32_t OFFSET = 4;
23 enum ManagerCmdId {
24 AUDIO_MANAGER_LOAD_ADAPTER,
25 AUDIO_MANAGER_UNLOAD_ADAPTER,
26 };
Convert2Uint32(const uint8_t * ptr)27 static uint32_t Convert2Uint32(const uint8_t *ptr)
28 {
29 if (ptr == nullptr) {
30 return 0;
31 }
32 /*
33 * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left,
34 * and the third digit no left
35 */
36 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
37 }
38
DoSomethingInterestingWithMyAPI(const uint8_t * rawData,size_t size)39 bool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)
40 {
41 if (rawData == nullptr) {
42 return false;
43 }
44 uint32_t cmd = Convert2Uint32(rawData);
45 rawData = rawData + OFFSET;
46 struct IAudioManager *manager = IAudioManagerGet(false);
47 if (manager == nullptr) {
48 return false;
49 }
50 uint8_t *data = const_cast<uint8_t *>(rawData);
51 switch (cmd) {
52 case AUDIO_MANAGER_LOAD_ADAPTER: {
53 struct AudioPort port = {
54 .dir = *(reinterpret_cast<AudioPortDirection *>(data)),
55 .portId = *(reinterpret_cast<uint32_t *>(data)),
56 .portName = reinterpret_cast<char *>(data),
57 };
58 struct AudioAdapterDescriptor desc = {
59 .adapterName = reinterpret_cast<char *>(data),
60 .ports = &port,
61 };
62 struct IAudioAdapter *adapter = nullptr;
63 manager->LoadAdapter(manager, &desc, &adapter);
64 break;
65 }
66 case AUDIO_MANAGER_UNLOAD_ADAPTER:
67 manager->UnloadAdapter(manager, reinterpret_cast<char *>(data));
68 break;
69 default:
70 break;
71 }
72 IAudioManagerRelease(manager, false);
73 return true;
74 }
75 }
76 }
77
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)78 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
79 {
80 if (size < OHOS::Audio::THRESHOLD) {
81 return 0;
82 }
83 OHOS::Audio::DoSomethingInterestingWithMyAPI(data, size);
84 return 0;
85 }