• 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 "camera_window_manager_client_fuzzer.h"
17 #include "camera_log.h"
18 #include "securec.h"
19 
20 namespace OHOS {
21 namespace CameraStandard {
22 static constexpr int32_t MAX_CODE_LEN = 512;
23 static constexpr int32_t MIN_SIZE_NUM = 4;
24 static const uint8_t* RAW_DATA = nullptr;
25 const size_t THRESHOLD = 10;
26 static size_t g_dataSize = 0;
27 static size_t g_pos;
28 
29 sptr<CameraWindowManagerClient> CameraWindowManagerClientFuzzer::fuzz_{nullptr};
30 std::shared_ptr<CameraWindowManagerClient::WMSSaStatusChangeCallback>
31     CameraWindowManagerClientFuzzer::callback_{nullptr};
32 
33 /*
34 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
35 * tips: only support basic type
36 */
37 template<class T>
GetData()38 T GetData()
39 {
40     T object {};
41     size_t objectSize = sizeof(object);
42     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
43         return object;
44     }
45     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
46     if (ret != EOK) {
47         return {};
48     }
49     g_pos += objectSize;
50     return object;
51 }
52 
53 template<class T>
GetArrLength(T & arr)54 uint32_t GetArrLength(T& arr)
55 {
56     if (arr == nullptr) {
57         MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
58         return 0;
59     }
60     return sizeof(arr) / sizeof(arr[0]);
61 }
62 
CameraWindowManagerClientFuzzTest()63 void CameraWindowManagerClientFuzzer::CameraWindowManagerClientFuzzTest()
64 {
65     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
66         return;
67     }
68 
69     fuzz_ = CameraWindowManagerClient::GetInstance();
70     CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
71     pid_t pid;
72     int32_t systemAbilityId = GetData<int32_t>();
73     uint8_t randomNum = GetData<uint8_t>();
74     std::vector<std::string> testStrings = {"test1", "test2"};
75     std::string deviceId(testStrings[randomNum % testStrings.size()]);
76     fuzz_->InitWindowProxy();
77     fuzz_->GetFocusWindowInfo(pid);
78     fuzz_->GetWindowManagerAgent();
79     if (callback_ == nullptr) {
80         callback_ = std::make_shared<CameraWindowManagerClient::WMSSaStatusChangeCallback>();
81     }
82     callback_->OnRemoveSystemAbility(systemAbilityId, deviceId);
83     fuzz_->UnregisterWindowManagerAgent();
84 }
85 
Test()86 void Test()
87 {
88     auto cameraWindowManagerClient = std::make_unique<CameraWindowManagerClientFuzzer>();
89     if (cameraWindowManagerClient == nullptr) {
90         MEDIA_INFO_LOG("cameraWindowManagerClient is null");
91         return;
92     }
93     cameraWindowManagerClient->CameraWindowManagerClientFuzzTest();
94 }
95 
96 typedef void (*TestFuncs[1])();
97 
98 TestFuncs g_testFuncs = {
99     Test,
100 };
101 
FuzzTest(const uint8_t * rawData,size_t size)102 bool FuzzTest(const uint8_t* rawData, size_t size)
103 {
104     // initialize data
105     RAW_DATA = rawData;
106     g_dataSize = size;
107     g_pos = 0;
108 
109     uint32_t code = GetData<uint32_t>();
110     uint32_t len = GetArrLength(g_testFuncs);
111     if (len > 0) {
112         g_testFuncs[code % len]();
113     } else {
114         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
115     }
116 
117     return true;
118 }
119 
120 } // namespace CameraStandard
121 } // namespace OHOS
122 
123 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)124 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
125 {
126     if (size < OHOS::CameraStandard::THRESHOLD) {
127         return 0;
128     }
129 
130     OHOS::CameraStandard::FuzzTest(data, size);
131     return 0;
132 }