• 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 "startusertest_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "ability_manager_client.h"
22 #include "parcel.h"
23 #include "want.h"
24 #include "iremote_object.h"
25 #include "bundle_info.h"
26 #include "app_launch_data.h"
27 #include "app_running_record.h"
28 #include "app_running_manager.h"
29 #include "test_observer.h"
30 
31 using namespace OHOS::AAFwk;
32 using namespace OHOS::AppExecFwk;
33 namespace OHOS {
34 namespace {
35 constexpr size_t FOO_MAX_LEN = 1024;
36 constexpr size_t U32_AT_SIZE = 4;
37 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)38 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
39 {
40     auto abilityMgr = AbilityManagerClient::GetInstance();
41     if (!abilityMgr) {
42         std::cout << "Get ability manager client failed." << std::endl;
43         return false;
44     }
45 
46     // get observer
47     sptr<TestObserver> observer = new (std::nothrow) TestObserver();
48 
49     // fuzz for want
50     Parcel wantParcel;
51     Want *want = nullptr;
52     if (wantParcel.WriteBuffer(data, size)) {
53         want = Want::Unmarshalling(wantParcel);
54         if (want) {
55             abilityMgr->StartUserTest(*want, observer->AsObject());
56         }
57     }
58 
59     if (want) {
60         delete want;
61         want = nullptr;
62     }
63 
64     return true;
65 }
66 }
67 
68 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)69 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
70 {
71     /* Run your code on data */
72     if (data == nullptr) {
73         std::cout << "invalid data" << std::endl;
74         return 0;
75     }
76 
77     /* Validate the length of size */
78     if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
79         return 0;
80     }
81 
82     char* ch = (char *)malloc(size + 1);
83     if (ch == nullptr) {
84         std::cout << "malloc failed." << std::endl;
85         return 0;
86     }
87 
88     (void)memset_s(ch, size + 1, 0x00, size + 1);
89     if (memcpy_s(ch, size, data, size) != EOK) {
90         std::cout << "copy failed." << std::endl;
91         free(ch);
92         ch = nullptr;
93         return 0;
94     }
95 
96     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
97     free(ch);
98     ch = nullptr;
99     return 0;
100 }
101