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 #include "abilitychildprocessrecord_fuzzer.h"
16 
17 #define private public
18 #include "app_running_record.h"
19 #include "child_process_record.h"
20 #undef private
21 #include "child_process_request.h"
22 #include <cstddef>
23 #include <cstdint>
24 #include <iostream>
25 #include "securec.h"
26 #include "configuration.h"
27 using namespace OHOS::AppExecFwk;
28 using namespace OHOS::AAFwk;
29 namespace OHOS {
30 namespace {
31 constexpr size_t FOO_MAX_LEN = 1024;
32 constexpr size_t U32_AT_SIZE = 4;
33 constexpr int INPUT_ZERO = 0;
34 constexpr int INPUT_ONE = 1;
35 constexpr int INPUT_THREE = 3;
36 constexpr uint8_t ENABLE = 2;
37 constexpr size_t OFFSET_ZERO = 24;
38 constexpr size_t OFFSET_ONE = 16;
39 constexpr size_t OFFSET_TWO = 8;
40 }
41 
42 
GetU32Data(const char * ptr)43 uint32_t GetU32Data(const char* ptr)
44 {
45     // convert fuzz input data to an integer
46     return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[ENABLE] << OFFSET_TWO) |
47         ptr[INPUT_THREE];
48 }
49 
50 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)51 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
52 {
53     int32_t hostPid = static_cast<int32_t>(GetU32Data(data));
54     ChildProcessRequest request;
55     std::shared_ptr<AppRunningRecord> hostRecord;
56     ChildProcessRecord::CreateChildProcessRecord(hostPid, request, hostRecord);
57     std::string stringParam(data, size);
58     sptr<IRemoteObject> mainProcessCb;
59     int32_t childProcessCount = static_cast<int32_t>(GetU32Data(data));
60     bool isStartWithDebug = *data % ENABLE;
61     ChildProcessRecord::CreateNativeChildProcessRecord(hostPid, stringParam, hostRecord, mainProcessCb,
62         childProcessCount, isStartWithDebug);
63     std::shared_ptr<ApplicationInfo> appInfo = std::make_shared<ApplicationInfo>();
64     int32_t recordId = static_cast<int32_t>(GetU32Data(data));
65     auto appRecord = std::make_shared<AppRunningRecord>(appInfo, recordId, stringParam);
66     auto childRecord = std::make_shared<ChildProcessRecord>(hostPid, request, appRecord);
67     childRecord->SetPid(hostPid);
68     childRecord->GetPid();
69     childRecord->GetHostPid();
70     int32_t uid = static_cast<int32_t>(GetU32Data(data));
71     childRecord->SetUid(uid);
72     childRecord->GetUid();
73     childRecord->GetProcessName();
74     childRecord->GetSrcEntry();
75     childRecord->GetHostRecord();
76     sptr<IChildScheduler> scheduler;
77     childRecord->SetScheduler(scheduler);
78     sptr<AppDeathRecipient> recipient;
79     childRecord->SetDeathRecipient(recipient);
80     childRecord->RegisterDeathRecipient();
81     childRecord-> RemoveDeathRecipient();
82     childRecord-> ScheduleExitProcessSafely();
83     childRecord->isStartWithDebug();
84     childRecord-> GetChildProcessType();
85     childRecord->GetMainProcessCallback();
86     childRecord->ClearMainProcessCallback();
87     std::string entryParams(data, size);
88     childRecord->GetEntryParams();
89     childRecord->MakeProcessName(hostRecord);
90     return true;
91 }
92 }
93 
94 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)95 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
96 {
97     /* Run your code on data */
98     if (data == nullptr) {
99         std::cout << "invalid data" << std::endl;
100         return 0;
101     }
102 
103     /* Validate the length of size */
104     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
105         return 0;
106     }
107 
108     char* ch = (char*)malloc(size + 1);
109     if (ch == nullptr) {
110         std::cout << "malloc failed." << std::endl;
111         return 0;
112     }
113 
114     (void)memset_s(ch, size + 1, 0x00, size + 1);
115     if (memcpy_s(ch, size, data, size) != EOK) {
116         std::cout << "copy failed." << std::endl;
117         free(ch);
118         ch = nullptr;
119         return 0;
120     }
121 
122     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
123     free(ch);
124     ch = nullptr;
125     return 0;
126 }
127 
128