• 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 "moving_photo_surface_wrapper_fuzzer.h"
17 #include "camera_input.h"
18 #include "camera_log.h"
19 #include "camera_photo_proxy.h"
20 #include "capture_input.h"
21 #include "capture_output.h"
22 #include "capture_scene_const.h"
23 #include "input/camera_manager.h"
24 #include "message_parcel.h"
25 #include "refbase.h"
26 #include <cstddef>
27 #include <cstdint>
28 #include <memory>
29 #include "token_setproc.h"
30 #include "nativetoken_kit.h"
31 #include "accesstoken_kit.h"
32 #include "securec.h"
33 #include <mutex>
34 
35 namespace OHOS {
36 namespace CameraStandard {
37 static constexpr int32_t MAX_CODE_LEN  = 512;
38 static constexpr int32_t MIN_SIZE_NUM = 4;
39 static const uint8_t* RAW_DATA = nullptr;
40 const size_t THRESHOLD = 10;
41 static size_t g_dataSize = 0;
42 static size_t g_pos;
43 
44 std::shared_ptr<MovingPhotoSurfaceWrapper> MovingPhotoSurfaceWrapperFuzzer::fuzz_{nullptr};
45 std::shared_ptr<MovingPhotoSurfaceWrapper::BufferConsumerListener>
46     MovingPhotoSurfaceWrapperFuzzer::listener_{nullptr};
47 
48 /*
49 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
50 * tips: only support basic type
51 */
52 template<class T>
GetData()53 T GetData()
54 {
55     T object {};
56     size_t objectSize = sizeof(object);
57     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
58         return object;
59     }
60     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
61     if (ret != EOK) {
62         return {};
63     }
64     g_pos += objectSize;
65     return object;
66 }
67 
68 template<class T>
GetArrLength(T & arr)69 uint32_t GetArrLength(T& arr)
70 {
71     if (arr == nullptr) {
72         MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
73         return 0;
74     }
75     return sizeof(arr) / sizeof(arr[0]);
76 }
77 
MovingPhotoSurfaceWrapperFuzzTest()78 void MovingPhotoSurfaceWrapperFuzzer::MovingPhotoSurfaceWrapperFuzzTest()
79 {
80     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
81         return;
82     }
83     fuzz_ = std::make_shared<MovingPhotoSurfaceWrapper>();
84     CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
85     fuzz_->GetProducer();
86     int32_t width = GetData<int32_t>();
87     int32_t height = GetData<int32_t>();
88     fuzz_->CreateMovingPhotoSurfaceWrapper(width, height);
89     fuzz_->OnBufferArrival();
90     if (listener_ == nullptr) {
91         sptr<MovingPhotoSurfaceWrapper> surfaceWrapper = nullptr;
92         listener_ = std::make_shared<MovingPhotoSurfaceWrapper::BufferConsumerListener>(surfaceWrapper);
93     }
94     listener_->OnBufferAvailable();
95 }
96 
Test()97 void Test()
98 {
99     auto movingPhotoSurfaceWrapper = std::make_unique<MovingPhotoSurfaceWrapperFuzzer>();
100     if (movingPhotoSurfaceWrapper == nullptr) {
101         MEDIA_INFO_LOG("movingPhotoSurfaceWrapper is null");
102         return;
103     }
104     movingPhotoSurfaceWrapper->MovingPhotoSurfaceWrapperFuzzTest();
105 }
106 
107 typedef void (*TestFuncs[1])();
108 
109 TestFuncs g_testFuncs = {
110     Test,
111 };
112 
FuzzTest(const uint8_t * rawData,size_t size)113 bool FuzzTest(const uint8_t* rawData, size_t size)
114 {
115     // initialize data
116     RAW_DATA = rawData;
117     g_dataSize = size;
118     g_pos = 0;
119 
120     uint32_t code = GetData<uint32_t>();
121     uint32_t len = GetArrLength(g_testFuncs);
122     if (len > 0) {
123         g_testFuncs[code % len]();
124     } else {
125         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
126     }
127 
128     return true;
129 }
130 } // namespace CameraStandard
131 } // namespace OHOS
132 
133 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)134 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
135 {
136     if (size < OHOS::CameraStandard::THRESHOLD) {
137         return 0;
138     }
139 
140     OHOS::CameraStandard::FuzzTest(data, size);
141     return 0;
142 }