• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "startserviceextensionability_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "ability_context_impl.h"
22 #include "parcel.h"
23 #include "want.h"
24 #include "securec.h"
25 
26 using namespace OHOS::AAFwk;
27 using namespace OHOS::AppExecFwk;
28 
29 namespace OHOS {
30 namespace {
31 constexpr size_t U32_AT_SIZE = 4;
32 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)33 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
34 {
35     std::shared_ptr<AbilityRuntime::AbilityContextImpl> context =
36         std::make_shared<AbilityRuntime::AbilityContextImpl>();
37 
38     int32_t accountId = 100;
39     if (!context) {
40         return false;
41     }
42 
43     // fuzz for want
44     Parcel wantParcel;
45     Want* want = nullptr;
46     if (wantParcel.WriteBuffer(data, size)) {
47         want = Want::Unmarshalling(wantParcel);
48         if (want) {
49             context->StartServiceExtensionAbility(*want);
50             context->StartServiceExtensionAbility(*want, accountId);
51         }
52     }
53 
54     if (want) {
55         delete want;
56         want = nullptr;
57     }
58 
59     return true;
60 }
61 }
62 
63 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)64 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
65 {
66     /* Run your code on data */
67     if (data == nullptr) {
68         std::cout << "invalid data" << std::endl;
69         return 0;
70     }
71 
72     /* Validate the length of size */
73     if (size < OHOS::U32_AT_SIZE) {
74         return 0;
75     }
76 
77     char* ch = static_cast<char*>(malloc(size + 1));
78     if (ch == nullptr) {
79         std::cout << "malloc failed." << std::endl;
80         return 0;
81     }
82 
83     (void)memset_s(ch, size + 1, 0x00, size + 1);
84     if (memcpy_s(ch, size, data, size) != EOK) {
85         std::cout << "copy failed." << std::endl;
86         free(ch);
87         ch = nullptr;
88         return 0;
89     }
90 
91     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
92     free(ch);
93     ch = nullptr;
94     return 0;
95 }
96 
97