• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "medialibrarymediaphotoassetproxy_fuzzer.h"
17 
18 #include <cstdint>
19 #include <memory>
20 #include <string>
21 #include <fuzzer/FuzzedDataProvider.h>
22 
23 #define private public
24 #include "media_photo_asset_proxy.h"
25 #undef private
26 
27 #include "ability_context_impl.h"
28 #include "datashare_predicates.h"
29 #include "iservice_registry.h"
30 #include "media_log.h"
31 #include "medialibrary_command.h"
32 #include "medialibrary_data_manager.h"
33 #include "medialibrary_errno.h"
34 #include "medialibrary_unistore.h"
35 #include "medialibrary_unistore_manager.h"
36 #include "rdb_store.h"
37 #include "system_ability_definition.h"
38 #include "userfilemgr_uri.h"
39 #include "medialibrary_kvstore_manager.h"
40 
41 namespace OHOS {
42 using namespace std;
43 using namespace DataShare;
44 static const int32_t MAX_PHOTO_QUALITY_FUZZER_LISTS = 1;
45 static const int32_t MAX_CAMERA_SHOT_TYPE_FUZZER_LISTS = 3;
46 static const int32_t MAX_PHOTO_FORMAT_FUZZER_LISTS = 3;
47 constexpr int FUZZ_STORAGE_MANAGER_MANAGER_ID = 5003;
48 std::shared_ptr<DataShare::DataShareHelper> sDataShareHelper_ = nullptr;
49 std::shared_ptr<Media::MediaLibraryRdbStore> g_rdbStore;
50 FuzzedDataProvider *provider = nullptr;
51 
FuzzCameraShotType()52 static inline Media::CameraShotType FuzzCameraShotType()
53 {
54     uint8_t data = provider->ConsumeIntegralInRange<uint8_t>(0, MAX_CAMERA_SHOT_TYPE_FUZZER_LISTS);
55     return Media::CameraShotType_FUZZER_LISTS[data];
56 }
57 
FuzzPhotoFormat()58 static inline Media::PhotoFormat FuzzPhotoFormat()
59 {
60     uint8_t data = provider->ConsumeIntegralInRange<uint8_t>(0, MAX_PHOTO_FORMAT_FUZZER_LISTS);
61     return Media::PhotoFormat_FUZZER_LISTS[data];
62 }
63 
FuzzPhotoQuality()64 static inline Media::PhotoQuality FuzzPhotoQuality()
65 {
66     uint8_t data = provider->ConsumeIntegralInRange<uint8_t>(0, MAX_PHOTO_QUALITY_FUZZER_LISTS);
67     return Media::PhotoQuality_FUZZER_LISTS[data];
68 }
69 
CreateDataHelper(int32_t systemAbilityId)70 void CreateDataHelper(int32_t systemAbilityId)
71 {
72     auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
73     if (saManager == nullptr) {
74         MEDIA_ERR_LOG("Get system ability mgr failed.");
75         return;
76     }
77     auto remoteObj = saManager->GetSystemAbility(systemAbilityId);
78     if (remoteObj == nullptr) {
79         MEDIA_ERR_LOG("GetSystemAbility Service Failed.");
80         return;
81     }
82 
83     if (sDataShareHelper_ == nullptr) {
84         const sptr<IRemoteObject> &token = remoteObj;
85         sDataShareHelper_ = DataShare::DataShareHelper::Creator(token, Media::MEDIALIBRARY_DATA_URI);
86     }
87 }
88 
Init()89 static shared_ptr<Media::PhotoAssetProxy> Init()
90 {
91     shared_ptr<Media::PhotoAssetProxy> photoAssetProxy = make_shared<Media::PhotoAssetProxy>(sDataShareHelper_,
92         FuzzCameraShotType(), provider->ConsumeIntegral<int32_t>(), provider->ConsumeIntegral<int32_t>(),
93         provider->ConsumeIntegral<uint32_t>());
94     return photoAssetProxy;
95 }
96 
FuzzPhotoAssetProxy()97 static sptr<Media::PhotoProxyFuzzTest> FuzzPhotoAssetProxy()
98 {
99     sptr<Media::PhotoProxyFuzzTest> photoProxyFuzzTest = new(std::nothrow) Media::PhotoProxyFuzzTest();
100     if (photoProxyFuzzTest == nullptr) {
101         return nullptr;
102     }
103     photoProxyFuzzTest->SetFormat(FuzzPhotoFormat());
104     photoProxyFuzzTest->SetPhotoQuality(FuzzPhotoQuality());
105 
106     return photoProxyFuzzTest;
107 }
108 
MediaLibraryMediaPhotoAssetProxyTest()109 static void MediaLibraryMediaPhotoAssetProxyTest()
110 {
111     if (sDataShareHelper_ == nullptr) {
112         CreateDataHelper(FUZZ_STORAGE_MANAGER_MANAGER_ID);
113     }
114     shared_ptr<Media::PhotoAssetProxy> photoAssetProxy = Init();
115     if (photoAssetProxy == nullptr) {
116         return;
117     }
118     sptr<Media::PhotoProxyFuzzTest> photoProxyFuzzTest = FuzzPhotoAssetProxy();
119     photoAssetProxy->AddPhotoProxy((sptr<Media::PhotoProxy>&)photoProxyFuzzTest);
120     photoAssetProxy->GetVideoFd();
121     photoAssetProxy->NotifyVideoSaveFinished();
122 }
123 
SetTables()124 void SetTables()
125 {
126     vector<string> createTableSqlList = {
127         Media::PhotoColumn::CREATE_PHOTO_TABLE,
128     };
129     for (auto &createTableSql : createTableSqlList) {
130         int32_t ret = g_rdbStore->ExecuteSql(createTableSql);
131         if (ret != NativeRdb::E_OK) {
132             MEDIA_ERR_LOG("Execute sql %{private}s failed", createTableSql.c_str());
133             return;
134         }
135         MEDIA_DEBUG_LOG("Execute sql %{private}s success", createTableSql.c_str());
136     }
137 }
138 
RdbStoreInit()139 static void RdbStoreInit()
140 {
141     auto stageContext = std::make_shared<AbilityRuntime::ContextImpl>();
142     auto abilityContextImpl = std::make_shared<OHOS::AbilityRuntime::AbilityContextImpl>();
143     abilityContextImpl->SetStageContext(stageContext);
144     int32_t sceneCode = 0;
145     auto ret = Media::MediaLibraryDataManager::GetInstance()->InitMediaLibraryMgr(abilityContextImpl,
146         abilityContextImpl, sceneCode);
147     CHECK_AND_RETURN_LOG(ret == NativeRdb::E_OK, "InitMediaLibraryMgr failed, ret: %{public}d", ret);
148 
149     auto rdbStore = Media::MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
150     if (rdbStore == nullptr) {
151         return;
152     }
153     g_rdbStore = rdbStore;
154     SetTables();
155 }
156 
ClearKvStore()157 static inline void ClearKvStore()
158 {
159     Media::MediaLibraryKvStoreManager::GetInstance().CloseAllKvStore();
160 }
161 } // namespace OHOS
162 
LLVMFuzzerInitialize(int * argc,char *** argv)163 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
164 {
165     OHOS::RdbStoreInit();
166     return 0;
167 }
168 
169 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)170 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
171 {
172     /* Run your code on data */
173     FuzzedDataProvider fdp(data, size);
174     OHOS::provider = &fdp;
175     if (data == nullptr) {
176         return 0;
177     }
178     OHOS::MediaLibraryMediaPhotoAssetProxyTest();
179     OHOS::ClearKvStore();
180     return 0;
181 }