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_impl_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<CommandServerImpl> CommandServerImplFuzzer::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
CommandServerImplFuzzTest()62 void CommandServerImplFuzzer::CommandServerImplFuzzTest()
63 {
64 if ((RAW_DATA == nullptr) || (g_dataSize > MAX_CODE_LEN) || (g_dataSize < MIN_SIZE_NUM)) {
65 return;
66 }
67
68 uint8_t randomNum = GetData<uint8_t>();
69 std::vector<std::string> testStrings = {"test1", "test2"};
70 std::string cmdServerName(testStrings[randomNum % testStrings.size()]);
71 fuzz_ = std::make_shared<CommandServerImpl>(cmdServerName);
72 CHECK_ERROR_RETURN_LOG(!fuzz_, "Create fuzz_ Error");
73 int32_t threadPriority = GetData<int32_t>();
74 fuzz_->SetThreadPriority(threadPriority);
75 fuzz_->GetThreadPriority();
76 }
77
Test()78 void Test()
79 {
80 auto commandServerImpl = std::make_unique<CommandServerImplFuzzer>();
81 if (commandServerImpl == nullptr) {
82 MEDIA_INFO_LOG("commandServerImpl is null");
83 return;
84 }
85 commandServerImpl->CommandServerImplFuzzTest();
86 }
87
88 typedef void (*TestFuncs[1])();
89
90 TestFuncs g_testFuncs = {
91 Test,
92 };
93
FuzzTest(const uint8_t * rawData,size_t size)94 bool FuzzTest(const uint8_t* rawData, size_t size)
95 {
96 // initialize data
97 RAW_DATA = rawData;
98 g_dataSize = size;
99 g_pos = 0;
100
101 uint32_t code = GetData<uint32_t>();
102 uint32_t len = GetArrLength(g_testFuncs);
103 if (len > 0) {
104 g_testFuncs[code % len]();
105 } else {
106 MEDIA_INFO_LOG("%{public}s: The len length is equal to 0", __func__);
107 }
108
109 return true;
110 }
111 } // namespace CameraStandard
112 } // namespace OHOS
113
114 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)115 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
116 {
117 if (size < OHOS::CameraStandard::THRESHOLD) {
118 return 0;
119 }
120
121 OHOS::CameraStandard::FuzzTest(data, size);
122 return 0;
123 }