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 "quickfixdeployer_fuzzer.h"
17 #define private public
18 #include "quick_fix_deployer.h"
19 #include "securec.h"
20 #include "inner_bundle_info.h"
21
22 using namespace OHOS::AppExecFwk;
23 namespace OHOS {
24 constexpr size_t U32_AT_SIZE = 4;
25 const std::string BUNDLE_NAME = "com.example.bmsaccesstoken1";
26 const uint32_t QUICK_FIX_VERSION_CODE = 1;
27 const uint32_t BUNDLE_VERSION_CODE = 1;
28 const std::string QUICK_FIX_VERSION_NAME = "1.0";
29 const std::string BUNDLE_VERSION_NAME = "1.0";
30
CreateAppQuickFix()31 AppQuickFix CreateAppQuickFix()
32 {
33 AppqfInfo appInfo;
34 appInfo.versionCode = QUICK_FIX_VERSION_CODE;
35 appInfo.versionName = QUICK_FIX_VERSION_NAME;
36 appInfo.type = QuickFixType::PATCH;
37 HqfInfo hqfInfo;
38 hqfInfo.moduleName = "entry";
39 hqfInfo.type = QuickFixType::PATCH;
40 appInfo.hqfInfos.push_back(hqfInfo);
41 AppQuickFix appQuickFix;
42 appQuickFix.bundleName = BUNDLE_NAME;
43 appQuickFix.versionCode = BUNDLE_VERSION_CODE;
44 appQuickFix.versionName = BUNDLE_VERSION_NAME;
45 appQuickFix.deployingAppqfInfo = appInfo;
46 return appQuickFix;
47 }
48
DoSomethingInterestingWithMyAPI(const char * data,size_t size)49 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
50 {
51 std::string targetPath(data, size);
52 nlohmann::json jsonObject;
53 std::vector<std::string> bundlePaths;
54 QuickFixDeployer quickFixDeployer(bundlePaths, false, targetPath);
55 std::unordered_map<std::string, AppQuickFix> infos;
56 InnerAppQuickFix oldInnerAppQuickFix;
57 InnerAppQuickFix newInnerAppQuickFix;
58 std::vector<std::string> bundleFilePaths;
59 bundleFilePaths.push_back(targetPath);
60 quickFixDeployer.GetDeployQuickFixResult();
61 quickFixDeployer.DeployQuickFix();
62 quickFixDeployer.GetQuickFixDataMgr();
63 quickFixDeployer.SaveToInnerBundleInfo(newInnerAppQuickFix);
64 quickFixDeployer.ToDeployEndStatus(newInnerAppQuickFix, oldInnerAppQuickFix);
65 quickFixDeployer.ToDeployStartStatus(bundleFilePaths, newInnerAppQuickFix, oldInnerAppQuickFix);
66 quickFixDeployer.ParseAndCheckAppQuickFixInfos(bundleFilePaths, infos);
67 quickFixDeployer.ToInnerAppQuickFix(infos, oldInnerAppQuickFix, newInnerAppQuickFix);
68 BundleInfo bundleInfo;
69 std::string bundleName(data, size);
70 quickFixDeployer.GetBundleInfo(bundleName, bundleInfo);
71 quickFixDeployer.ProcessPatchDeployStart(bundleFilePaths, bundleInfo, infos);
72 std::unordered_map<std::string, AppQuickFix> infos1;
73 const AppQuickFix appQuickFix = CreateAppQuickFix();
74 quickFixDeployer.ProcessHotReloadDeployStart(bundleInfo, appQuickFix);
75 quickFixDeployer.ProcessPatchDeployEnd(appQuickFix, targetPath);
76 quickFixDeployer.ProcessHotReloadDeployEnd(appQuickFix, targetPath);
77 AppQuickFix newAppQuickFix = CreateAppQuickFix();
78 AppQuickFix oldAppQuickFix = CreateAppQuickFix();
79 quickFixDeployer.CheckPatchVersionCode(newAppQuickFix, oldAppQuickFix);
80 QuickFixMark mark;
81 mark.bundleName = appQuickFix.bundleName;
82 mark.status = QuickFixStatus::DEPLOY_START;
83 InnerAppQuickFix innerAppQuickFix(appQuickFix, mark);
84 quickFixDeployer.SaveAppQuickFix(innerAppQuickFix);
85 quickFixDeployer.MoveHqfFiles(innerAppQuickFix, targetPath);
86 std::vector<std::string> realPaths;
87 quickFixDeployer.ProcessBundleFilePaths(bundleFilePaths, realPaths);
88 quickFixDeployer.ToDeployQuickFixResult(appQuickFix);
89 quickFixDeployer.ProcessNativeLibraryPath(targetPath, innerAppQuickFix);
90 quickFixDeployer.ResetNativeSoAttrs(infos1);
91 return true;
92 }
93 }
94
95 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)96 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
97 {
98 /* Run your code on data */
99 if (data == nullptr) {
100 return 0;
101 }
102
103 if (size < OHOS::U32_AT_SIZE) {
104 return 0;
105 }
106
107 char* ch = static_cast<char*>(malloc(size + 1));
108 if (ch == nullptr) {
109 return 0;
110 }
111
112 (void)memset_s(ch, size + 1, 0x00, size + 1);
113 if (memcpy_s(ch, size, data, size) != EOK) {
114 free(ch);
115 ch = nullptr;
116 return 0;
117 }
118 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
119 free(ch);
120 ch = nullptr;
121 return 0;
122 }