• 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 "session_coordinator_fuzzer.h"
17 #include "camera_log.h"
18 #include "deferred_processing_service.h"
19 #include "message_parcel.h"
20 #include <cstddef>
21 #include <cstdint>
22 #include <memory>
23 #include "accesstoken_kit.h"
24 #include "camera_error_code.h"
25 #include "securec.h"
26 
27 namespace OHOS {
28 namespace CameraStandard {
29 static constexpr int32_t NUM_TRI = 13;
30 static constexpr int32_t MAX_CODE_LEN = 512;
31 static constexpr int32_t MIN_SIZE_NUM = 4;
32 static const uint8_t* RAW_DATA = nullptr;
33 const size_t THRESHOLD = 10;
34 static size_t g_dataSize = 0;
35 static size_t g_pos;
36 
37 std::shared_ptr<DeferredProcessing::SessionCoordinator> SessionCoordinatorFuzzer::fuzz_{nullptr};
38 
39 /*
40 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
41 * tips: only support basic type
42 */
43 template<class T>
GetData()44 T GetData()
45 {
46     T object {};
47     size_t objectSize = sizeof(object);
48     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
49         return object;
50     }
51     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
52     if (ret != EOK) {
53         return {};
54     }
55     g_pos += objectSize;
56     return object;
57 }
58 
59 template<class T>
GetArrLength(T & arr)60 uint32_t GetArrLength(T& arr)
61 {
62     if (arr == nullptr) {
63         MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
64         return 0;
65     }
66     return sizeof(arr) / sizeof(arr[0]);
67 }
68 
SessionCoordinatorFuzzTest()69 void SessionCoordinatorFuzzer::SessionCoordinatorFuzzTest()
70 {
71     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
72         return;
73     }
74     fuzz_ = std::make_shared<DeferredProcessing::SessionCoordinator>();
75     CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
76     fuzz_->Initialize();
77     int32_t userId = GetData<int32_t>();
78     uint8_t randomNum = GetData<uint8_t>();
79     std::vector<std::string> testStrings = {"test1", "test2"};
80     std::string imageId(testStrings[randomNum % testStrings.size()]);
81     std::shared_ptr<DeferredProcessing::BufferInfo> bufferInfo;
82     fuzz_->imageProcCallbacks_->OnProcessDone(userId, imageId, bufferInfo);
83     std::shared_ptr<DeferredProcessing::BufferInfoExt> bufferInfoExt;
84     fuzz_->imageProcCallbacks_->OnProcessDoneExt(userId, imageId, bufferInfoExt);
85     DeferredProcessing::DpsError dpsError = static_cast<DeferredProcessing::DpsError>(GetData<int32_t>() % NUM_TRI);
86     fuzz_->imageProcCallbacks_->OnError(userId, imageId, dpsError);
87     sptr<IPCFileDescriptor> ipcFd;
88     fuzz_->videoProcCallbacks_->OnProcessDone(userId, imageId, ipcFd);
89     fuzz_->videoProcCallbacks_->OnError(userId, imageId, dpsError);
90     std::vector<DeferredProcessing::DpsStatus> dpsStatusVec = {
91         DeferredProcessing::DPS_SESSION_STATE_IDLE,
92         DeferredProcessing::DPS_SESSION_STATE_RUNNALBE,
93         DeferredProcessing::DPS_SESSION_STATE_RUNNING,
94         DeferredProcessing::DPS_SESSION_STATE_SUSPENDED,
95     };
96     uint8_t arrcodeNum = randomNum % dpsStatusVec.size();
97     DeferredProcessing::DpsStatus dpsStatus = dpsStatusVec[arrcodeNum];
98     fuzz_->imageProcCallbacks_->OnStateChanged(userId, dpsStatus);
99     fuzz_->videoProcCallbacks_->OnStateChanged(userId, dpsStatus);
100     fuzz_->Start();
101     fuzz_->OnStateChanged(userId, dpsStatus);
102     fuzz_->OnVideoStateChanged(userId, dpsStatus);
103     fuzz_->GetImageProcCallbacks();
104     fuzz_->GetVideoProcCallbacks();
105     fuzz_->OnProcessDone(userId, imageId, ipcFd, userId, GetData<bool>());
106     fuzz_->OnProcessDoneExt(userId, imageId, nullptr, GetData<bool>());
107     fuzz_->OnError(userId, imageId, dpsError);
108     fuzz_->OnVideoProcessDone(userId, imageId, ipcFd);
109     fuzz_->OnVideoError(userId, imageId, dpsError);
110     auto videoCallback = fuzz_->GetRemoteVideoCallback(userId);
111     fuzz_->ProcessVideoResults(videoCallback);
112     fuzz_->DeleteVideoSession(userId);
113     fuzz_->DeletePhotoSession(userId);
114     fuzz_->Stop();
115 }
116 
Test()117 void Test()
118 {
119     auto sessionCoordinatorFuzz = std::make_unique<SessionCoordinatorFuzzer>();
120     if (sessionCoordinatorFuzz == nullptr) {
121         MEDIA_INFO_LOG("sessionCoordinatorFuzz is null");
122         return;
123     }
124     sessionCoordinatorFuzz->SessionCoordinatorFuzzTest();
125 }
126 
127 typedef void (*TestFuncs[1])();
128 
129 TestFuncs g_testFuncs = {
130     Test,
131 };
132 
FuzzTest(const uint8_t * rawData,size_t size)133 bool FuzzTest(const uint8_t* rawData, size_t size)
134 {
135     // initialize data
136     RAW_DATA = rawData;
137     g_dataSize = size;
138     g_pos = 0;
139 
140     uint32_t code = GetData<uint32_t>();
141     uint32_t len = GetArrLength(g_testFuncs);
142     if (len > 0) {
143         g_testFuncs[code % len]();
144     } else {
145         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
146     }
147 
148     return true;
149 }
150 } // namespace CameraStandard
151 } // namespace OHOS
152 
153 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)154 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
155 {
156     if (size < OHOS::CameraStandard::THRESHOLD) {
157         return 0;
158     }
159 
160     OHOS::CameraStandard::FuzzTest(data, size);
161     return 0;
162 }