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 "bundleresparser_fuzzer.h"
18
19 #include "bundle_resource_parser.h"
20 #include "bundle_resource_process.h"
21 #include "securec.h"
22
23 using namespace OHOS::AppExecFwk;
24 namespace OHOS {
25 constexpr size_t U32_AT_SIZE = 4;
26 constexpr uint32_t CODE_MAX = 8;
27 const int32_t USERID = 100;
28 const std::string HAP_NOT_EXIST = "not exist";
29 const std::string MODULE_NAME = "entry";
30
DoSomethingInterestingWithMyAPI(const char * data,size_t size)31 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
32 {
33 std::vector<ResourceInfo> resourceInfos;
34 std::string bundleName(data, size);
35 BundleResourceProcess::GetResourceInfoByBundleName(bundleName, USERID, resourceInfos);
36 ResourceInfo resourceInfo;
37 resourceInfo.bundleName_ = bundleName;
38 resourceInfo.moduleName_ = MODULE_NAME;
39 resourceInfo.label_ = bundleName;
40 resourceInfo.labelNeedParse_ = false;
41 resourceInfo.iconNeedParse_ = false;
42 BundleResourceParser parser;
43 if (!resourceInfos.empty()) {
44 parser.ParseResourceInfo(USERID, resourceInfos[0]);
45 resourceInfos[0].label_ = "";
46 resourceInfos[0].icon_ = "";
47 parser.ParseResourceInfos(USERID, resourceInfos);
48 parser.ParseIconResourceByPath(resourceInfos[0].hapPath_, resourceInfos[0].iconId_, resourceInfo);
49 }
50 int32_t appIndex = 1;
51 parser.ParserCloneResourceInfo(appIndex, resourceInfos);
52 parser.ParseResourceInfoWithSameHap(USERID, resourceInfo);
53 std::string label(data, size);
54 parser.ParseLabelResourceByPath(HAP_NOT_EXIST, 0, label);
55 std::shared_ptr<Global::Resource::ResourceManager> resourceManager(Global::Resource::CreateResourceManager());
56 parser.ParseResourceInfoByResourceManager(resourceManager, resourceInfo);
57 parser.ParseLabelResourceByResourceManager(resourceManager, 0, label);
58 parser.ParseIconResourceByResourceManager(resourceManager, resourceInfo);
59 nlohmann::json layeredImagedJson = R"(
60 {
61 "layered" : {
62 "background" : "$media:1",
63 "foreground" : "$media:2"
64 }
65 }
66 )"_json;
67 std::string jsonBuff(layeredImagedJson.dump());
68 parser.ParseForegroundAndBackgroundResource(resourceManager, jsonBuff, 0, resourceInfo);
69 uint32_t foregroundId = 0;
70 uint32_t backgroundId = 0;
71 parser.ParseIconIdFromJson(jsonBuff, foregroundId, backgroundId);
72 uint32_t iconId = 1;
73 int32_t density = 0;
74 std::vector<uint8_t> datas;
75 parser.GetMediaDataById(resourceManager, iconId, density, datas);
76 ResourceInfo newResourceInfo;
77 parser.IsNeedToParseResourceInfo(newResourceInfo, resourceInfo);
78 return true;
79 }
80 }
81
82 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
84 {
85 /* Run your code on data */
86 if (data == nullptr) {
87 return 0;
88 }
89
90 if (size < OHOS::U32_AT_SIZE) {
91 return 0;
92 }
93
94 char* ch = static_cast<char*>(malloc(size + 1));
95 if (ch == nullptr) {
96 return 0;
97 }
98
99 (void)memset_s(ch, size + 1, 0x00, size + 1);
100 if (memcpy_s(ch, size, data, size) != EOK) {
101 free(ch);
102 ch = nullptr;
103 return 0;
104 }
105 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
106 free(ch);
107 ch = nullptr;
108 return 0;
109 }