• 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 #include "light_scan_session_fuzzer.h"
16 #include "camera_log.h"
17 #include "message_parcel.h"
18 #include "securec.h"
19 #include <memory>
20 #include "timer.h"
21 #include "input/camera_manager.h"
22 #include "system_ability_definition.h"
23 #include "iservice_registry.h"
24 
25 namespace OHOS {
26 namespace CameraStandard {
27 static constexpr int32_t MAX_CODE_LEN = 512;
28 static constexpr int32_t MIN_SIZE_NUM = 4;
29 static const uint8_t* RAW_DATA = nullptr;
30 const size_t THRESHOLD = 10;
31 static size_t g_dataSize = 0;
32 static size_t g_pos;
33 sptr<LightPaintingSession> LightScanSessionFuzzer::fuzzLight_{nullptr};
34 sptr<ScanSession> LightScanSessionFuzzer::fuzzScan_{nullptr};
35 
36 /*
37 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
38 * tips: only support basic type
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 
56 template<class T>
GetArrLength(T & arr)57 uint32_t GetArrLength(T& arr)
58 {
59     if (arr == nullptr) {
60         MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
61         return 0;
62     }
63     return sizeof(arr) / sizeof(arr[0]);
64 }
65 
66 sptr<CameraManager> cameraManager = CameraManager::GetInstance();
LightPaintingSessionFuzzTest()67 void LightScanSessionFuzzer::LightPaintingSessionFuzzTest()
68 {
69     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
70         return;
71     }
72     sptr<CameraManager> cameraManager = CameraManager::GetInstance();
73     sptr<CaptureSession> captureSession = cameraManager->CreateCaptureSession(SceneMode::SLOW_MOTION);
74     auto lightPaintingSession = static_cast<LightPaintingSession*>(captureSession.GetRefPtr());
75     fuzzLight_ = lightPaintingSession;
76     CHECK_ERROR_RETURN_LOG(!fuzzLight_, "Create fuzzLight_ Error");
77 
78     std::vector<LightPaintingType> supportedType;
79     fuzzLight_->GetSupportedLightPaintings(supportedType);
80     LightPaintingType setType = LightPaintingType::LIGHT;
81     fuzzLight_->GetLightPainting(setType);
82     fuzzLight_->SetLightPainting(setType);
83     fuzzLight_->TriggerLighting();
84 }
ScanSessionFuzzTest()85 void LightScanSessionFuzzer::ScanSessionFuzzTest()
86 {
87     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
88         return;
89     }
90     sptr<CameraManager> cameraManager = CameraManager::GetInstance();
91     sptr<CaptureSession> captureSession = cameraManager->CreateCaptureSession(SceneMode::SLOW_MOTION);
92     sptr<ScanSession> scanSession = static_cast<ScanSession*>(captureSession.GetRefPtr());
93     fuzzScan_ = scanSession;
94     CHECK_ERROR_RETURN_LOG(!fuzzScan_, "Create fuzzScan_ Error");
95     fuzzScan_->BeginConfig();
96     sptr<CaptureOutput> preview = nullptr;
97     fuzzScan_->AddOutput(preview);
98     fuzzScan_->IsBrightnessStatusSupported();
99     fuzzScan_->UnRegisterBrightnessStatusCallback();
100 }
101 
Test()102 void Test()
103 {
104     auto lightscansession = std::make_unique<LightScanSessionFuzzer>();
105     if (lightscansession == nullptr) {
106         MEDIA_INFO_LOG("lightscansession is null");
107         return;
108     }
109     MEDIA_INFO_LOG("yuanwp_fuzz 001");
110     lightscansession->LightPaintingSessionFuzzTest();
111     lightscansession->ScanSessionFuzzTest();
112 }
113 
114 typedef void (*TestFuncs[1])();
115 
116 TestFuncs g_testFuncs = {
117     Test,
118 };
119 
FuzzTest(const uint8_t * rawData,size_t size)120 bool FuzzTest(const uint8_t* rawData, size_t size)
121 {
122     // initialize data
123     RAW_DATA = rawData;
124     g_dataSize = size;
125     g_pos = 0;
126 
127     uint32_t code = GetData<uint32_t>();
128     uint32_t len = GetArrLength(g_testFuncs);
129     if (len > 0) {
130         g_testFuncs[code % len]();
131     } else {
132         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
133     }
134 
135     return true;
136 }
137 } // namespace CameraStandard
138 } // namespace OHOS
139 
140 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)141 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
142 {
143     if (size < OHOS::CameraStandard::THRESHOLD) {
144         return 0;
145     }
146     OHOS::CameraStandard::FuzzTest(data, size);
147     return 0;
148 }