1 /*
2  * Copyright (c) 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 <cmath>
17 #include <iostream>
18 #include "aw_common.h"
19 #include "string_ex.h"
20 #include "media_errors.h"
21 #include "directory_ex.h"
22 #include "isoundpool.h"
23 #include "soundpoolcreate_fuzzer.h"
24 
25 using namespace std;
26 using namespace OHOS;
27 using namespace Media;
28 using namespace AudioStandard;
29 
30 namespace OHOS {
31 namespace Media {
SoundPoolCreateFuzzer()32 SoundPoolCreateFuzzer::SoundPoolCreateFuzzer()
33 {
34 }
35 
~SoundPoolCreateFuzzer()36 SoundPoolCreateFuzzer::~SoundPoolCreateFuzzer()
37 {
38 }
39 
40 constexpr int32_t CONTENT_TYPE_LIST = 10;
41 const ContentType contentType_[CONTENT_TYPE_LIST] {
42     CONTENT_TYPE_UNKNOWN,
43     CONTENT_TYPE_SPEECH,
44     CONTENT_TYPE_MUSIC,
45     CONTENT_TYPE_MOVIE,
46     CONTENT_TYPE_SONIFICATION,
47     CONTENT_TYPE_RINGTONE,
48     CONTENT_TYPE_PROMPT,
49     CONTENT_TYPE_GAME,
50     CONTENT_TYPE_DTMF,
51     CONTENT_TYPE_ULTRASONIC
52 };
53 
54 constexpr int32_t STREAM_USAGE_LIST = 21;
55 const StreamUsage streamUsage_[STREAM_USAGE_LIST] {
56     STREAM_USAGE_UNKNOWN,
57     STREAM_USAGE_MEDIA,
58     STREAM_USAGE_MUSIC,
59     STREAM_USAGE_VOICE_COMMUNICATION,
60     STREAM_USAGE_VOICE_ASSISTANT,
61     STREAM_USAGE_ALARM,
62     STREAM_USAGE_VOICE_MESSAGE,
63     STREAM_USAGE_NOTIFICATION_RINGTONE,
64     STREAM_USAGE_RINGTONE,
65     STREAM_USAGE_NOTIFICATION,
66     STREAM_USAGE_ACCESSIBILITY,
67     STREAM_USAGE_SYSTEM,
68     STREAM_USAGE_MOVIE,
69     STREAM_USAGE_GAME,
70     STREAM_USAGE_AUDIOBOOK,
71     STREAM_USAGE_NAVIGATION,
72     STREAM_USAGE_DTMF,
73     STREAM_USAGE_ENFORCED_TONE,
74     STREAM_USAGE_ULTRASONIC,
75     STREAM_USAGE_RANGING,
76     STREAM_USAGE_VOICE_MODEM_COMMUNICATION
77 };
78 
FuzzSoundPoolCreate(uint8_t * data,size_t size)79 bool SoundPoolCreateFuzzer::FuzzSoundPoolCreate(uint8_t *data, size_t size)
80 {
81     int32_t maxStreams = *reinterpret_cast<int32_t *>(data);
82     AudioStandard::AudioRendererInfo audioRenderInfo;
83 
84     int32_t contenttypesubscript = *reinterpret_cast<int32_t *>(data) % (CONTENT_TYPE_LIST);
85     audioRenderInfo.contentType = contentType_[contenttypesubscript];
86 
87     int32_t streamusagesubscript = *reinterpret_cast<int32_t *>(data) % (STREAM_USAGE_LIST);
88     audioRenderInfo.streamUsage = streamUsage_[streamusagesubscript];
89 
90     audioRenderInfo.rendererFlags = 0;
91 
92     TestSoundPool::CreateSoundPool(maxStreams, audioRenderInfo);
93     TestSoundPool::Release();
94     return true;
95 }
96 
FuzzSoundPoolCreateFlags(uint8_t * data,size_t size)97 bool SoundPoolCreateFuzzer::FuzzSoundPoolCreateFlags(uint8_t *data, size_t size)
98 {
99     int maxStreams = 3;
100     AudioStandard::AudioRendererInfo audioRenderInfo;
101 
102     int32_t contenttypesubscript = *reinterpret_cast<int32_t *>(data) % (CONTENT_TYPE_LIST);
103     audioRenderInfo.contentType = contentType_[contenttypesubscript];
104 
105     int32_t streamusagesubscript = *reinterpret_cast<int32_t *>(data) % (STREAM_USAGE_LIST);
106     audioRenderInfo.streamUsage = streamUsage_[streamusagesubscript];
107 
108     audioRenderInfo.rendererFlags = *reinterpret_cast<int32_t *>(data);
109 
110     TestSoundPool::CreateSoundPool(maxStreams, audioRenderInfo);
111     TestSoundPool::Release();
112     return true;
113 }
114 } // namespace Media
115 
FuzzTestSoundPoolCreate(uint8_t * data,size_t size)116 bool FuzzTestSoundPoolCreate(uint8_t *data, size_t size)
117 {
118     auto soundPool_ = std::make_unique<SoundPoolCreateFuzzer>();
119     return soundPool_->FuzzSoundPoolCreate(data, size);
120 }
121 
FuzzTestSoundPoolCreateFlags(uint8_t * data,size_t size)122 bool FuzzTestSoundPoolCreateFlags(uint8_t *data, size_t size)
123 {
124     auto soundPool_ = std::make_unique<SoundPoolCreateFuzzer>();
125     return soundPool_->FuzzSoundPoolCreateFlags(data, size);
126 }
127 } // namespace OHOS
128 
129 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)130 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
131 {
132     /* Run your code on data */
133     FuzzTestSoundPoolCreateFlags(data, size);
134     FuzzTestSoundPoolCreate(data, size);
135     return 0;
136 }