1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#include "abilitymanagerservicefifth_fuzzer.h"
17eace7efcSopenharmony_ci
18eace7efcSopenharmony_ci#include <cstddef>
19eace7efcSopenharmony_ci#include <cstdint>
20eace7efcSopenharmony_ci
21eace7efcSopenharmony_ci#define private public
22eace7efcSopenharmony_ci#define protected public
23eace7efcSopenharmony_ci#include "ability_manager_service.h"
24eace7efcSopenharmony_ci#undef protected
25eace7efcSopenharmony_ci#undef private
26eace7efcSopenharmony_ci
27eace7efcSopenharmony_ci#include "ability_record.h"
28eace7efcSopenharmony_ci
29eace7efcSopenharmony_ciusing namespace OHOS::AAFwk;
30eace7efcSopenharmony_ciusing namespace OHOS::AppExecFwk;
31eace7efcSopenharmony_ci
32eace7efcSopenharmony_ci#define DISABLE_FUZZ
33eace7efcSopenharmony_cinamespace OHOS {
34eace7efcSopenharmony_cinamespace {
35eace7efcSopenharmony_ciconstexpr int INPUT_ZERO = 0;
36eace7efcSopenharmony_ciconstexpr int INPUT_ONE = 1;
37eace7efcSopenharmony_ciconstexpr int INPUT_TWO = 2;
38eace7efcSopenharmony_ciconstexpr int INPUT_THREE = 3;
39eace7efcSopenharmony_ciconstexpr size_t FOO_MAX_LEN = 1024;
40eace7efcSopenharmony_ciconstexpr size_t U32_AT_SIZE = 4;
41eace7efcSopenharmony_ciconstexpr int OFFSET_ZERO = 24;
42eace7efcSopenharmony_ciconstexpr int OFFSET_ONE = 16;
43eace7efcSopenharmony_ciconstexpr int OFFSET_TWO = 8;
44eace7efcSopenharmony_ci}
45eace7efcSopenharmony_ci
46eace7efcSopenharmony_ciuint32_t GetU32Data(const char* ptr)
47eace7efcSopenharmony_ci{
48eace7efcSopenharmony_ci    // convert fuzz input data to an integer
49eace7efcSopenharmony_ci    return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[INPUT_TWO] << OFFSET_TWO) |
50eace7efcSopenharmony_ci        ptr[INPUT_THREE];
51eace7efcSopenharmony_ci}
52eace7efcSopenharmony_ci
53eace7efcSopenharmony_cisptr<Token> GetFuzzAbilityToken()
54eace7efcSopenharmony_ci{
55eace7efcSopenharmony_ci    sptr<Token> token = nullptr;
56eace7efcSopenharmony_ci    AbilityRequest abilityRequest;
57eace7efcSopenharmony_ci    abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
58eace7efcSopenharmony_ci    abilityRequest.abilityInfo.name = "MainAbility";
59eace7efcSopenharmony_ci    abilityRequest.abilityInfo.type = AbilityType::DATA;
60eace7efcSopenharmony_ci    std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
61eace7efcSopenharmony_ci    if (abilityRecord) {
62eace7efcSopenharmony_ci        token = abilityRecord->GetToken();
63eace7efcSopenharmony_ci    }
64eace7efcSopenharmony_ci    return token;
65eace7efcSopenharmony_ci}
66eace7efcSopenharmony_ci
67eace7efcSopenharmony_cibool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
68eace7efcSopenharmony_ci{
69eace7efcSopenharmony_ci    int32_t int32Param = static_cast<int32_t>(GetU32Data(data));
70eace7efcSopenharmony_ci    uint32_t uint32Param = GetU32Data(data);
71eace7efcSopenharmony_ci    std::string stringParam(data, size);
72eace7efcSopenharmony_ci    Parcel wantParcel;
73eace7efcSopenharmony_ci    Want* want = nullptr;
74eace7efcSopenharmony_ci    if (wantParcel.WriteBuffer(data, size)) {
75eace7efcSopenharmony_ci        want = Want::Unmarshalling(wantParcel);
76eace7efcSopenharmony_ci        if (!want) {
77eace7efcSopenharmony_ci            return false;
78eace7efcSopenharmony_ci        }
79eace7efcSopenharmony_ci    }
80eace7efcSopenharmony_ci    sptr<IRemoteObject> token = GetFuzzAbilityToken();
81eace7efcSopenharmony_ci
82eace7efcSopenharmony_ci    // fuzz for AbilityManagerService
83eace7efcSopenharmony_ci    auto abilityms = std::make_shared<AbilityManagerService>();
84eace7efcSopenharmony_ci    abilityms->GetTopAbility(token);
85eace7efcSopenharmony_ci    abilityms->DelegatorDoAbilityForeground(token);
86eace7efcSopenharmony_ci    abilityms->DelegatorDoAbilityBackground(token);
87eace7efcSopenharmony_ci    abilityms->DoAbilityForeground(token, uint32Param);
88eace7efcSopenharmony_ci    abilityms->DoAbilityBackground(token, uint32Param);
89eace7efcSopenharmony_ci    abilityms->DelegatorMoveMissionToFront(int32Param);
90eace7efcSopenharmony_ci    abilityms->OpenLink(*want, token, int32Param, int32Param);
91eace7efcSopenharmony_ci    abilityms->JudgeMultiUserConcurrency(int32Param);
92eace7efcSopenharmony_ci#ifdef ABILITY_COMMAND_FOR_TEST
93eace7efcSopenharmony_ci    abilityms->ForceTimeoutForTest(stringParam, stringParam);
94eace7efcSopenharmony_ci#endif
95eace7efcSopenharmony_ci    AppExecFwk::AbilityRequest abilityRequest;
96eace7efcSopenharmony_ci    abilityms->CheckStaticCfgPermission(abilityRequest, false, -1);
97eace7efcSopenharmony_ci    if (want) {
98eace7efcSopenharmony_ci        delete want;
99eace7efcSopenharmony_ci        want = nullptr;
100eace7efcSopenharmony_ci    }
101eace7efcSopenharmony_ci    return true;
102eace7efcSopenharmony_ci}
103eace7efcSopenharmony_ci}
104eace7efcSopenharmony_ci
105eace7efcSopenharmony_ci/* Fuzzer entry point */
106eace7efcSopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
107eace7efcSopenharmony_ci{
108eace7efcSopenharmony_ci    /* Run your code on data */
109eace7efcSopenharmony_ci    if (data == nullptr) {
110eace7efcSopenharmony_ci        return 0;
111eace7efcSopenharmony_ci    }
112eace7efcSopenharmony_ci
113eace7efcSopenharmony_ci    /* Validate the length of size */
114eace7efcSopenharmony_ci    if (size < OHOS::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) {
115eace7efcSopenharmony_ci        return 0;
116eace7efcSopenharmony_ci    }
117eace7efcSopenharmony_ci
118eace7efcSopenharmony_ci    char* ch = (char*)malloc(size + 1);
119eace7efcSopenharmony_ci    if (ch == nullptr) {
120eace7efcSopenharmony_ci        std::cout << "malloc failed." << std::endl;
121eace7efcSopenharmony_ci        return 0;
122eace7efcSopenharmony_ci    }
123eace7efcSopenharmony_ci
124eace7efcSopenharmony_ci    (void)memset_s(ch, size + 1, 0x00, size + 1);
125eace7efcSopenharmony_ci    if (memcpy_s(ch, size, data, size) != EOK) {
126eace7efcSopenharmony_ci        std::cout << "copy failed." << std::endl;
127eace7efcSopenharmony_ci        free(ch);
128eace7efcSopenharmony_ci        ch = nullptr;
129eace7efcSopenharmony_ci        return 0;
130eace7efcSopenharmony_ci    }
131eace7efcSopenharmony_ci
132eace7efcSopenharmony_ci#ifndef DISABLE_FUZZ
133eace7efcSopenharmony_ci    OHOS::DoSomethingInterestingWithMyAPI(ch, size);
134eace7efcSopenharmony_ci#endif
135eace7efcSopenharmony_ci    free(ch);
136eace7efcSopenharmony_ci    ch = nullptr;
137eace7efcSopenharmony_ci    return 0;
138eace7efcSopenharmony_ci}
139eace7efcSopenharmony_ci
140