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 #define private public
17 #include "hmpbundleinstaller_fuzzer.h"
18
19 #include "hmp_bundle_installer.h"
20 #include "inner_bundle_info.h"
21 #include "securec.h"
22
23 using namespace OHOS::AppExecFwk;
24 namespace OHOS {
25 constexpr size_t U32_AT_SIZE = 4;
26 constexpr uint8_t ENABLE = 2;
DoSomethingInterestingWithMyAPI(const char * data,size_t size)27 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
28 {
29 HmpBundleInstaller hmpBundleInstaller;
30 std::set<std::string> hapList;
31 std::set<std::string> systemHspList;
32 std::unordered_map<std::string, InnerBundleInfo> infos;
33 hmpBundleInstaller.InstallSystemHspInHmp(std::string(data, size));
34 hmpBundleInstaller.InstallNormalAppInHmp(std::string(data, size));
35 std::set<int32_t> userIds;
36 hmpBundleInstaller.GetRequiredUserIds(std::string(data, size), userIds);
37 hmpBundleInstaller.RollbackHmpBundle(systemHspList, hapList);
38 hmpBundleInstaller.UpdateBundleInfo(std::string(data, size), std::string(data, size), std::string(data, size));
39 hmpBundleInstaller.GetHmpBundleList(std::string(data, size));
40 hmpBundleInstaller.UpdateInnerBundleInfo(std::string(data, size), infos);
41 hmpBundleInstaller.ParseInfos(std::string(data, size), std::string(data, size), infos);
42 hmpBundleInstaller.ParseHapFiles(std::string(data, size), infos);
43 hmpBundleInstaller.UninstallSystemBundle(std::string(data, size), std::string(data, size));
44 hmpBundleInstaller.CheckUninstallSystemHsp(std::string(data, size));
45 hmpBundleInstaller.UpdatePreInfoInDb(std::string(data, size), infos);
46 hmpBundleInstaller.UpdateBundleInfoForHmp(std::string(data, size), hapList, systemHspList);
47 return true;
48 }
49 } // namespace OHOS
50
51 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)52 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
53 {
54 /* Run your code on data */
55 if (data == nullptr) {
56 return 0;
57 }
58
59 /* Validate the length of size */
60 if (size < OHOS::U32_AT_SIZE) {
61 return 0;
62 }
63
64 char* ch = (char*)malloc(size + 1);
65 if (ch == nullptr) {
66 std::cout << "malloc failed." << std::endl;
67 return 0;
68 }
69
70 (void)memset_s(ch, size + 1, 0x00, size + 1);
71 if (memcpy_s(ch, size + 1, data, size) != EOK) {
72 std::cout << "copy failed." << std::endl;
73 free(ch);
74 ch = nullptr;
75 return 0;
76 }
77
78 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
79 free(ch);
80 ch = nullptr;
81 return 0;
82 }