• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "abilitymgrrest_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #include "ability_interceptor.h"
23 #include "implicit_start_processor.h"
24 #include "system_dialog_scheduler.h"
25 #undef private
26 #include "inner_mission_info.h"
27 #include "parcel.h"
28 #include "securec.h"
29 
30 using namespace OHOS::AAFwk;
31 using namespace OHOS::AppExecFwk;
32 
33 namespace OHOS {
34 namespace {
35 constexpr size_t FOO_MAX_LEN = 1024;
36 constexpr size_t U32_AT_SIZE = 4;
37 constexpr size_t OFFSET_ZERO = 24;
38 constexpr size_t OFFSET_ONE = 16;
39 constexpr size_t OFFSET_TWO = 8;
40 }
GetU32Data(const char * ptr)41 uint32_t GetU32Data(const char* ptr)
42 {
43     // convert fuzz input data to an integer
44     return (ptr[0] << OFFSET_ZERO) | (ptr[1] << OFFSET_ONE) | (ptr[2] << OFFSET_TWO) | ptr[3];
45 }
GetFuzzAbilityToken()46 sptr<Token> GetFuzzAbilityToken()
47 {
48     sptr<Token> token = nullptr;
49 
50     AbilityRequest abilityRequest;
51     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
52     abilityRequest.abilityInfo.name = "MainAbility";
53     abilityRequest.abilityInfo.type = AbilityType::PAGE;
54     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
55     if (abilityRecord) {
56         token = abilityRecord->GetToken();
57     }
58 
59     return token;
60 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)61 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
62 {
63     int pid = static_cast<int>(GetU32Data(data));
64     std::string bundleName(data, size);
65     Parcel wantParcel;
66     Want *want = nullptr;
67     if (wantParcel.WriteBuffer(data, size)) {
68         want = Want::Unmarshalling(wantParcel);
69     }
70     int32_t userId = static_cast<int32_t>(GetU32Data(data));
71     std::shared_ptr<SystemDialogScheduler> systemDialogScheduler = std::make_shared<SystemDialogScheduler>();
72     systemDialogScheduler->GetANRDialogWant(static_cast<int>(userId), pid, *want);
73     std::vector<DialogAppInfo> dialogAppInfos;
74     systemDialogScheduler->GetSelectorParams(dialogAppInfos);
75     int32_t labelId = static_cast<int32_t>(GetU32Data(data));
76     std::string appName(data, size);
77     systemDialogScheduler->GetAppNameFromResource(labelId, bundleName, userId, appName);
78     InnerMissionInfo innerMissionInfo;
79     innerMissionInfo.ToJsonStr();
80     std::string jsonStr(data, size);
81     innerMissionInfo.FromJsonStr(jsonStr);
82     std::vector<std::string> info;
83     innerMissionInfo.Dump(info);
84     nlohmann::json value;
85     std::string node(data, size);
86     JsonType jsonType = JsonType::STRING;
87     return innerMissionInfo.CheckJsonNode(value, node, jsonType);
88 }
89 }
90 
91 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)92 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
93 {
94     /* Run your code on data */
95     if (data == nullptr) {
96         std::cout << "invalid data" << std::endl;
97         return 0;
98     }
99 
100     /* Validate the length of size */
101     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
102         return 0;
103     }
104 
105     char* ch = (char *)malloc(size + 1);
106     if (ch == nullptr) {
107         std::cout << "malloc failed." << std::endl;
108         return 0;
109     }
110 
111     (void)memset_s(ch, size + 1, 0x00, size + 1);
112     if (memcpy_s(ch, size, data, size) != EOK) {
113         std::cout << "copy failed." << std::endl;
114         free(ch);
115         ch = nullptr;
116         return 0;
117     }
118 
119     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
120     free(ch);
121     ch = nullptr;
122     return 0;
123 }
124 
125