1 /*
2 * Copyright (c) 2023 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 "hstream_depth_data_fuzzer.h"
17 #include "foundation/multimedia/camera_framework/common/utils/camera_log.h"
18 #include "message_parcel.h"
19 #include "iconsumer_surface.h"
20 #include "securec.h"
21
22 namespace OHOS {
23 namespace CameraStandard {
24 using namespace OHOS::HDI::Camera::V1_0;
25 static constexpr int32_t MAX_CODE_LEN = 512;
26 static constexpr int32_t MIN_SIZE_NUM = 4;
27 static const uint8_t* RAW_DATA = nullptr;
28 const size_t THRESHOLD = 10;
29 static size_t g_dataSize = 0;
30 static size_t g_pos;
31 const int32_t PHOTO_WIDTH = 1280;
32 const int32_t PHOTO_HEIGHT = 960;
33 const int32_t PHOTO_FORMAT = 2000;
34 std::shared_ptr<HStreamDepthData> HStreamDepthDataFuzzer::fuzz_{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
HStreamDepthDataFuzzTest()66 void HStreamDepthDataFuzzer::HStreamDepthDataFuzzTest()
67 {
68 if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
69 return;
70 }
71 sptr<IConsumerSurface> photoSurface = IConsumerSurface::Create();
72 sptr<IBufferProducer> producer = photoSurface->GetProducer();
73
74 fuzz_ = std::make_shared<HStreamDepthData>(producer, PHOTO_FORMAT, PHOTO_WIDTH, PHOTO_HEIGHT);
75 CHECK_RETURN_ELOG(!fuzz_, "Create fuzz_ Error");
76 fuzz_->Start();
77 fuzz_->Stop();
78 fuzz_->Release();
79 }
80
Test()81 void Test()
82 {
83 auto hstreamDepthData = std::make_unique<HStreamDepthDataFuzzer>();
84 if (hstreamDepthData == nullptr) {
85 MEDIA_INFO_LOG("hstreamDepthData is null");
86 return;
87 }
88 hstreamDepthData->HStreamDepthDataFuzzTest();
89 }
90
91 typedef void (*TestFuncs[1])();
92
93 TestFuncs g_testFuncs = {
94 Test,
95 };
96
FuzzTest(const uint8_t * rawData,size_t size)97 bool FuzzTest(const uint8_t* rawData, size_t size)
98 {
99 // initialize data
100 RAW_DATA = rawData;
101 g_dataSize = size;
102 g_pos = 0;
103
104 uint32_t code = GetData<uint32_t>();
105 uint32_t len = GetArrLength(g_testFuncs);
106 if (len > 0) {
107 g_testFuncs[code % len]();
108 } else {
109 MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
110 }
111
112 return true;
113 }
114 } // namespace CameraStandard
115 } // namespace OHOS
116
117 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)118 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
119 {
120 if (size < OHOS::CameraStandard::THRESHOLD) {
121 return 0;
122 }
123
124 OHOS::CameraStandard::FuzzTest(data, size);
125 return 0;
126 }