• 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 "time_broker_fuzzer.h"
17 #include "camera_log.h"
18 #include "message_parcel.h"
19 #include "securec.h"
20 #include <memory>
21 #include "timer.h"
22 
23 namespace OHOS {
24 namespace CameraStandard {
25 using namespace DeferredProcessing;
26 static constexpr int32_t MAX_CODE_LEN = 512;
27 static constexpr int32_t MIN_SIZE_NUM = 4;
28 static const uint8_t* RAW_DATA = nullptr;
29 const size_t THRESHOLD = 10;
30 static size_t g_dataSize = 0;
31 static size_t g_pos;
32 std::shared_ptr<TimeBroker> TimeBrokerFuzzer::fuzz_{nullptr};
33 
34 /*
35 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
36 * tips: only support basic type
37 */
38 template<class T>
GetData()39 T GetData()
40 {
41     T object {};
42     size_t objectSize = sizeof(object);
43     if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
44         return object;
45     }
46     errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
47     if (ret != EOK) {
48         return {};
49     }
50     g_pos += objectSize;
51     return object;
52 }
53 
54 template<class T>
GetArrLength(T & arr)55 uint32_t GetArrLength(T& arr)
56 {
57     if (arr == nullptr) {
58         MEDIA_INFO_LOG("%{public}s: The array length is equal to 0", __func__);
59         return 0;
60     }
61     return sizeof(arr) / sizeof(arr[0]);
62 }
63 
TimeBrokerFuzzTest()64 void TimeBrokerFuzzer::TimeBrokerFuzzTest()
65 {
66     if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
67         return;
68     }
69 
70     fuzz_ = TimeBroker::Create("camera_deferred_base");
71     CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
72     fuzz_->Initialize();
73     uint32_t handle = fuzz_->GenerateHandle();
74     std::function<void(uint32_t handle)> timerCallback = fuzz_->GetExpiredFunc(handle);
75     fuzz_->GetNextHandle(handle);
76     uint32_t delayTimeMs = 1;
77     bool force = GetData<bool>();
78     fuzz_->RegisterCallback(delayTimeMs, timerCallback, handle);
79     fuzz_->RestartTimer(force);
80     fuzz_->TimerExpired();
81     fuzz_->DeregisterCallback(handle);
82 }
83 
Test()84 void Test()
85 {
86     auto timeBroker = std::make_unique<TimeBrokerFuzzer>();
87     if (timeBroker == nullptr) {
88         MEDIA_INFO_LOG("TimeBroker is null");
89         return;
90     }
91     timeBroker->TimeBrokerFuzzTest();
92 }
93 
94 typedef void (*TestFuncs[1])();
95 
96 TestFuncs g_testFuncs = {
97     Test,
98 };
99 
FuzzTest(const uint8_t * rawData,size_t size)100 bool FuzzTest(const uint8_t* rawData, size_t size)
101 {
102     // initialize data
103     RAW_DATA = rawData;
104     g_dataSize = size;
105     g_pos = 0;
106 
107     uint32_t code = GetData<uint32_t>();
108     uint32_t len = GetArrLength(g_testFuncs);
109     if (len > 0) {
110         g_testFuncs[code % len]();
111     } else {
112         MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
113     }
114 
115     return true;
116 }
117 } // namespace CameraStandard
118 } // namespace OHOS
119 
120 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)121 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
122 {
123     if (size < OHOS::CameraStandard::THRESHOLD) {
124         return 0;
125     }
126 
127     OHOS::CameraStandard::FuzzTest(data, size);
128     return 0;
129 }