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 "quickfixbootscanner_fuzzer.h"
17 #define private public
18 #include "quick_fix_boot_scanner.h"
19 #include "securec.h"
20
21 using namespace OHOS::AppExecFwk;
22 namespace OHOS {
23 constexpr size_t FOO_MAX_LEN = 1024;
24 constexpr size_t U32_AT_SIZE = 4;
25 constexpr size_t MESSAGE_SIZE = 4;
26 constexpr size_t DCAMERA_SHIFT_24 = 24;
27 constexpr size_t DCAMERA_SHIFT_16 = 16;
28 constexpr size_t DCAMERA_SHIFT_8 = 8;
29
GetU32Data(const char * ptr)30 uint32_t GetU32Data(const char* ptr)
31 {
32 return (ptr[0] << DCAMERA_SHIFT_24) | (ptr[1] << DCAMERA_SHIFT_16) | (ptr[2] << DCAMERA_SHIFT_8) | (ptr[3]);
33 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)34 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
35 {
36 QuickFixBootScanner quickFixBootScanner;
37 quickFixBootScanner.ProcessQuickFixBootUp();
38 std::shared_ptr<QuickFixState> state_ = nullptr;
39 quickFixBootScanner.SetQuickFixState(state_);
40 quickFixBootScanner.ProcessState();
41 quickFixBootScanner.RestoreQuickFix();
42 std::string bundlePath (reinterpret_cast<const char*>(data), size);
43 std::string realPath (reinterpret_cast<const char*>(data), size);
44 std::vector<std::string> fileDir;
45 fileDir.push_back(bundlePath);
46 quickFixBootScanner.ProcessQuickFixDir(fileDir);
47 quickFixBootScanner.ReprocessQuickFix(realPath, bundlePath);
48 std::string bundleName(data, size);
49 std::string quickFixPath(data, size);
50 ApplicationInfo info;
51 quickFixBootScanner.GetApplicationInfo(bundleName, quickFixPath, info);
52 int32_t quickFixVersion = reinterpret_cast<uintptr_t>(data);
53 int32_t fileVersion = reinterpret_cast<uintptr_t>(data);
54 quickFixBootScanner.ProcessWithBundleHasQuickFixInfo(bundleName, quickFixPath, quickFixVersion, fileVersion);
55 quickFixBootScanner.RemoveInvalidDir();
56 return true;
57 }
58 }
59
60 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)61 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
62 {
63 /* Run your code on data */
64 if (data == nullptr) {
65 return 0;
66 }
67
68 if (size < OHOS::U32_AT_SIZE) {
69 return 0;
70 }
71
72 /* Validate the length of size */
73 if (size > OHOS::FOO_MAX_LEN) {
74 return 0;
75 }
76
77 char* ch = static_cast<char*>(malloc(size + 1));
78 if (ch == nullptr) {
79 return 0;
80 }
81
82 (void)memset_s(ch, size + 1, 0x00, size + 1);
83 if (memcpy_s(ch, size, data, size) != EOK) {
84 free(ch);
85 ch = nullptr;
86 return 0;
87 }
88 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
89 free(ch);
90 ch = nullptr;
91 return 0;
92 }