• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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<uint8_t>();
73     info->sessionId = GetData<uint8_t>();
74     info->rendererState = rendererStates[GetData<uint8_t>() % rendererStates.size()];
75 
76     AudioRendererChangeInfo info_ = {};
77     info_.clientUID = GetData<uint8_t>();
78     info_.sessionId = GetData<uint8_t>();
79     info_.rendererState = rendererStates[GetData<uint8_t>() % rendererStates.size()];
80     focusSessionStrategy.IsFocusSession(info_.clientUID);
81 
82     AudioRendererChangeInfos infosExpected;
83     infosExpected.push_back(std::move(info));
84     focusSessionStrategy.HandleAudioRenderStateChangeEvent(infosExpected);
85 }
86 
FocusSessionStrategyRemoteRequest(uint8_t * data,size_t size)87 void FocusSessionStrategyRemoteRequest(uint8_t* data, size_t size)
88 {
89     auto focusSessionStrategy = std::make_unique<FocusSessionStrategyFuzzer>();
90     if (focusSessionStrategy == nullptr) {
91         SLOGI("focusSessionStrategy is null");
92         return;
93     }
94     focusSessionStrategy->FocusSessionStrategyFuzzTest(data, size);
95 }
96 
97 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)98 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
99 {
100     RAW_DATA = data;
101     g_dataSize = size;
102     g_pos = 0;
103     /* Run your code on data */
104     FocusSessionStrategyRemoteRequest(data, size);
105     return 0;
106 }
107 }