• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "abilitydebugdealfirst_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21 
22 #define private public
23 #include "ability_debug_deal.h"
24 #undef private
25 #include "ability_record.h"
26 #include "ability_debug_response_stub.h"
27 #include "securec.h"
28 
29 using namespace OHOS::AAFwk;
30 using namespace OHOS::AppExecFwk;
31 
32 namespace OHOS {
33 namespace {
34 constexpr size_t STRING_MAX_LENGTH = 128;
35 }
GetFuzzAbilityRecord()36 std::shared_ptr<AbilityRecord> GetFuzzAbilityRecord()
37 {
38     sptr<Token> token = nullptr;
39     AbilityRequest abilityRequest;
40     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
41     abilityRequest.abilityInfo.name = "MainAbility";
42     abilityRequest.abilityInfo.type = AbilityType::DATA;
43     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
44     if (!abilityRecord) {
45         return nullptr;
46     }
47     return abilityRecord;
48 }
49 
GetFuzzAbilityToken()50 sptr<Token> GetFuzzAbilityToken()
51 {
52     sptr<Token> token = nullptr;
53     std::shared_ptr<AbilityRecord> abilityRecord = GetFuzzAbilityRecord();
54     if (abilityRecord) {
55         token = abilityRecord->GetToken();
56     }
57     return token;
58 }
59 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)60 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
61 {
62     bool boolParam;
63     sptr<IRemoteObject> token = GetFuzzAbilityToken();
64     std::vector<sptr<IRemoteObject>> tokens;
65     std::weak_ptr<AbilityDebugDeal> deal;
66     FuzzedDataProvider fdp(data, size);
67     boolParam = fdp.ConsumeBool();
68     auto abilityDebugDeal = std::make_shared<AbilityDebugDeal>();
69     abilityDebugDeal->RegisterAbilityDebugResponse();
70     abilityDebugDeal->OnAbilitysDebugStarted(tokens);
71     abilityDebugDeal->OnAbilitysDebugStoped(tokens);
72     abilityDebugDeal->OnAbilitysAssertDebugChange(tokens, boolParam);
73     Token::GetAbilityRecordByToken(token) = nullptr;
74     abilityDebugDeal->OnAbilitysDebugStarted(tokens);
75     abilityDebugDeal->OnAbilitysDebugStoped(tokens);
76     abilityDebugDeal->OnAbilitysAssertDebugChange(tokens, boolParam);
77 
78     auto abilityDebugResponse = std::make_shared<AbilityDebugResponse>(deal);
79     abilityDebugResponse->OnAbilitysDebugStarted(tokens);
80     abilityDebugResponse->OnAbilitysDebugStoped(tokens);
81     abilityDebugResponse->OnAbilitysAssertDebugChange(tokens, boolParam);
82     abilityDebugResponse->abilityDebugDeal_.lock() = nullptr;
83     abilityDebugResponse->OnAbilitysDebugStarted(tokens);
84     abilityDebugResponse->OnAbilitysDebugStoped(tokens);
85     abilityDebugResponse->OnAbilitysAssertDebugChange(tokens, boolParam);
86     return true;
87 }
88 }
89 
90 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
92 {
93     // Run your code on data.
94     OHOS::DoSomethingInterestingWithMyAPI(data, size);
95     return 0;
96 }