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