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 #include "systemabilityhelper_fuzzer.h"
16
17 #include <iostream>
18
19 #include "securec.h"
20 #include "system_ability_helper.h"
21
22 using namespace OHOS::AppExecFwk;
23 namespace OHOS {
24 constexpr size_t U32_AT_SIZE = 4;
25 constexpr uint8_t ENABLE = 2;
DoSomethingInterestingWithMyAPI(const char * data,size_t size)26 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
27 {
28 SystemAbilityHelper helper;
29 int32_t systemAbilityId = reinterpret_cast<uintptr_t>(data);
30 helper.GetSystemAbility(systemAbilityId);
31 helper.RemoveSystemAbility(systemAbilityId);
32 sptr<IRemoteObject> systemAbility;
33 helper.AddSystemAbility(systemAbilityId, systemAbility);
34 std::string bundleName(data, size);
35 int32_t uid = reinterpret_cast<uintptr_t>(data);
36 helper.UninstallApp(bundleName, uid, systemAbilityId);
37 helper.UpgradeApp(bundleName, uid, systemAbilityId);
38 helper.UnloadSystemAbility(systemAbilityId);
39 return true;
40 }
41 } // namespace OHOS
42
43 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)44 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
45 {
46 /* Run your code on data */
47 if (data == nullptr) {
48 return 0;
49 }
50
51 /* Validate the length of size */
52 if (size < OHOS::U32_AT_SIZE) {
53 return 0;
54 }
55
56 char* ch = (char*)malloc(size + 1);
57 if (ch == nullptr) {
58 std::cout << "malloc failed." << std::endl;
59 return 0;
60 }
61
62 (void)memset_s(ch, size + 1, 0x00, size + 1);
63 if (memcpy_s(ch, size + 1, data, size) != EOK) {
64 std::cout << "copy failed." << std::endl;
65 free(ch);
66 ch = nullptr;
67 return 0;
68 }
69
70 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
71 free(ch);
72 ch = nullptr;
73 return 0;
74 }