1 /*
2 * Copyright (c) 2024 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 <cstddef>
17 #include <cstdint>
18
19 #include "ipc_skeleton.h"
20 #include "avsession_errors.h"
21 #include "system_ability_definition.h"
22 #include "avsession_log.h"
23 #include "audio_info.h"
24 #include "focussessionstrategy_fuzzer.h"
25 #include "focus_session_strategy.h"
26 #include "avsession_item.h"
27 #include "securec.h"
28
29 using namespace std;
30 namespace OHOS::AVSession {
31 using OHOS::AudioStandard::AudioRendererChangeInfo;
32 using OHOS::AudioStandard::RendererState;
33 static const int32_t MAX_CODE_LEN = 512;
34 static const int32_t MIN_SIZE_NUM = 4;
35
36 static const uint8_t *RAW_DATA = nullptr;
37 static size_t g_dataSize = 0;
38 static size_t g_pos;
39
40 template<class T>
GetData()41 T GetData()
42 {
43 T object {};
44 size_t objectSize = sizeof(object);
45 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
46 return object;
47 }
48 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
49 if (ret != EOK) {
50 return {};
51 }
52 g_pos += objectSize;
53 return object;
54 }
55
FocusSessionStrategyFuzzTest(uint8_t * data,size_t size)56 void FocusSessionStrategyFuzzer::FocusSessionStrategyFuzzTest(uint8_t* data, size_t size)
57 {
58 if ((data == nullptr) || (size > MAX_CODE_LEN) || (size < MIN_SIZE_NUM)) {
59 return;
60 }
61 vector<RendererState> rendererStates;
62 rendererStates.push_back(RendererState::RENDERER_INVALID);
63 rendererStates.push_back(RendererState::RENDERER_NEW);
64 rendererStates.push_back(RendererState::RENDERER_PREPARED);
65 rendererStates.push_back(RendererState::RENDERER_RUNNING);
66 rendererStates.push_back(RendererState::RENDERER_STOPPED);
67 rendererStates.push_back(RendererState::RENDERER_RELEASED);
68 rendererStates.push_back(RendererState::RENDERER_PAUSED);
69 FocusSessionStrategy focusSessionStrategy;
70
71 std::shared_ptr<AudioRendererChangeInfo> info = std::make_shared<AudioRendererChangeInfo>();
72 info->clientUID = GetData<uint32_t>();
73 info->sessionId = GetData<uint32_t>();
74 info->rendererState = rendererStates[GetData<uint8_t>() % rendererStates.size()];
75
76 AudioRendererChangeInfo info_ = {};
77 info_.clientUID = GetData<uint32_t>();
78 info_.sessionId = GetData<uint32_t>();
79 info_.clientPid = GetData<uint32_t>();
80 std::pair<int32_t, int32_t> key = std::make_pair(info_.clientUID, info_.clientPid);
81 info_.rendererState = rendererStates[GetData<uint8_t>() % rendererStates.size()];
82 focusSessionStrategy.IsFocusSession(key);
83
84 AudioRendererChangeInfos infosExpected;
85 infosExpected.push_back(std::move(info));
86 focusSessionStrategy.HandleAudioRenderStateChangeEvent(infosExpected);
87 }
88
FocusSessionStrategyRemoteRequest(uint8_t * data,size_t size)89 void FocusSessionStrategyRemoteRequest(uint8_t* data, size_t size)
90 {
91 auto focusSessionStrategy = std::make_unique<FocusSessionStrategyFuzzer>();
92 if (focusSessionStrategy == nullptr) {
93 SLOGI("focusSessionStrategy is null");
94 return;
95 }
96 focusSessionStrategy->FocusSessionStrategyFuzzTest(data, size);
97 }
98
99 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)100 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
101 {
102 /* Run your code on data */
103 RAW_DATA = data;
104 g_dataSize = size;
105 g_pos = 0;
106 FocusSessionStrategyRemoteRequest(data, size);
107 return 0;
108 }
109 }