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 "bundleresrdb_fuzzer.h"
18
19 #include <memory>
20
21 #include "bundle_resource_rdb.h"
22 #include "bundle_resource_register.h"
23 #include "securec.h"
24
25 using namespace OHOS::AppExecFwk;
26 namespace OHOS {
27 constexpr size_t U32_AT_SIZE = 4;
28 constexpr uint32_t CODE_MAX = 8;
29 const int32_t USERID = 100;
30 const std::string MODULE_NAME = "entry";
31 const std::string ABILITY_NAME = "com.example.bmsaccesstoken1.MainAbility";
32 namespace {
33 class SingletonRdb {
34 public:
GetInstance()35 static SingletonRdb& GetInstance()
36 {
37 return *instance_;
38 }
39
40 SingletonRdb& operator=(const SingletonRdb&) = delete;
41 SingletonRdb(const SingletonRdb&) = delete;
42
43 BundleResourceRdb bundleResourceRdb;
44 private:
45 SingletonRdb() = default;
46 ~SingletonRdb() = default;
47 static std::unique_ptr<SingletonRdb> instance_;
48 };
49 }
50
51 std::unique_ptr<SingletonRdb> SingletonRdb::instance_ = std::make_unique<SingletonRdb>();
52
DoSomethingInterestingWithMyAPI(const char * data,size_t size)53 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
54 {
55 SingletonRdb& rdb = SingletonRdb::GetInstance();
56 ResourceInfo resourceInfo;
57 std::string bundleName(data, size);
58 resourceInfo.bundleName_ = bundleName;
59 rdb.bundleResourceRdb.AddResourceInfo(resourceInfo);
60 rdb.bundleResourceRdb.DeleteResourceInfo(resourceInfo.GetKey());
61 std::vector<ResourceInfo> resourceInfos;
62 resourceInfos.push_back(resourceInfo);
63 rdb.bundleResourceRdb.AddResourceInfos(resourceInfos);
64 rdb.bundleResourceRdb.DeleteAllResourceInfo();
65 std::vector<std::string> keyNames;
66 rdb.bundleResourceRdb.GetAllResourceName(keyNames);
67 int32_t appIndex = 1;
68 rdb.bundleResourceRdb.GetResourceNameByBundleName(bundleName, appIndex, keyNames);
69 BundleResourceInfo info;
70 rdb.bundleResourceRdb.GetBundleResourceInfo(resourceInfo.bundleName_,
71 static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_LABEL), info);
72 std::vector<LauncherAbilityResourceInfo> launcherInfos;
73 rdb.bundleResourceRdb.GetLauncherAbilityResourceInfo(resourceInfo.bundleName_,
74 static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_LABEL), launcherInfos);
75 std::vector<BundleResourceInfo> infos;
76 rdb.bundleResourceRdb.GetAllBundleResourceInfo(
77 static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_LABEL), infos);
78 rdb.bundleResourceRdb.GetAllLauncherAbilityResourceInfo(
79 static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_LABEL), launcherInfos);
80 rdb.bundleResourceRdb.UpdateResourceForSystemStateChanged(resourceInfos);
81 std::string systemState(data, size);
82 rdb.bundleResourceRdb.GetCurrentSystemState(systemState);
83 rdb.bundleResourceRdb.DeleteNotExistResourceInfo();
84 rdb.bundleResourceRdb.ParseKey(resourceInfo.GetKey(), info);
85 LauncherAbilityResourceInfo launcherInfo;
86 rdb.bundleResourceRdb.ParseKey(resourceInfo.GetKey(), launcherInfo);
87 BundleResourceRegister::RegisterConfigurationObserver();
88 BundleResourceRegister::RegisterCommonEventSubscriber();
89 resourceInfo.ConvertFromBundleResourceInfo(info);
90 resourceInfo.ConvertFromLauncherAbilityResourceInfo(launcherInfo);
91 resourceInfo.InnerParseAppIndex(resourceInfo.GetKey());
92 return true;
93 }
94 }
95
96 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)97 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
98 {
99 /* Run your code on data */
100 if (data == nullptr) {
101 return 0;
102 }
103
104 if (size < OHOS::U32_AT_SIZE) {
105 return 0;
106 }
107
108 char* ch = static_cast<char*>(malloc(size + 1));
109 if (ch == nullptr) {
110 return 0;
111 }
112
113 (void)memset_s(ch, size + 1, 0x00, size + 1);
114 if (memcpy_s(ch, size, data, size) != EOK) {
115 free(ch);
116 ch = nullptr;
117 return 0;
118 }
119 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
120 free(ch);
121 ch = nullptr;
122 return 0;
123 }