• 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 "hcamera_preconfig_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <memory>
21 #include "camera_log.h"
22 #include "message_parcel.h"
23 #include "hcamera_host_manager.h"
24 #include "securec.h"
25 
26 namespace OHOS {
27 namespace CameraStandard {
28 using namespace OHOS::HDI::Camera::V1_0;
29 static constexpr int32_t MAX_CODE_LEN = 512;
30 static constexpr int32_t MIN_SIZE_NUM = 4;
31 static const uint8_t* RAW_DATA = nullptr;
32 const size_t THRESHOLD = 10;
33 static size_t g_dataSize = 0;
34 static size_t g_pos;
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 class StatusCallbackDemo : public HCameraHostManager::StatusCallback {
67     public:
68         virtual ~StatusCallbackDemo() = default;
OnCameraStatus(const std::string & cameraId,CameraStatus status,CallbackInvoker invoker=CallbackInvoker::CAMERA_HOST)69         virtual void OnCameraStatus(const std::string& cameraId, CameraStatus status,
70             CallbackInvoker invoker = CallbackInvoker::CAMERA_HOST) {}
OnFlashlightStatus(const std::string & cameraId,FlashStatus status)71         virtual void OnFlashlightStatus(const std::string& cameraId, FlashStatus status) {}
OnTorchStatus(TorchStatus status)72         virtual void OnTorchStatus(TorchStatus status) {}
73 };
74 
HCameraPreconfigFuzzTest1()75 void HCameraPreconfigFuzzer::HCameraPreconfigFuzzTest1()
76 {
77     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
78         return;
79     }
80 
81     int outFd = GetData<int32_t>();
82     CameraInfoDumper infoDumper(outFd);
83     std::shared_ptr<HCameraHostManager::StatusCallback> statusCallback = std::make_shared<StatusCallbackDemo>();
84     sptr<HCameraHostManager> hostManager = new HCameraHostManager(statusCallback);
85     DumpPreconfigInfo(infoDumper, hostManager);
86 }
87 
Test()88 void Test()
89 {
90     HCameraPreconfigFuzzer::HCameraPreconfigFuzzTest1();
91 }
92 
93 typedef void (*TestFuncs[1])();
94 
95 TestFuncs g_testFuncs = {
96     Test,
97 };
98 
FuzzTest(const uint8_t * rawData,size_t size)99 bool FuzzTest(const uint8_t* rawData, size_t size)
100 {
101     // initialize data
102     RAW_DATA = rawData;
103     g_dataSize = size;
104     g_pos = 0;
105 
106     uint32_t code = GetData<uint32_t>();
107     uint32_t len = GetArrLength(g_testFuncs);
108     if (len > 0) {
109         g_testFuncs[code % len]();
110     } else {
111         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
112     }
113 
114     return true;
115 }
116 } // namespace CameraStandard
117 } // namespace OHOS
118 
119 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)120 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
121 {
122     if (size < OHOS::CameraStandard::THRESHOLD) {
123         return 0;
124     }
125 
126     OHOS::CameraStandard::FuzzTest(data, size);
127     return 0;
128 }