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 FOO_MAX_LEN = 1024;
32 constexpr size_t U32_AT_SIZE = 4;
33 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)34 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
35 {
36 std::shared_ptr<AbilityRuntime::AbilityContextImpl> context =
37 std::make_shared<AbilityRuntime::AbilityContextImpl>();
38
39 int32_t accountId = 100;
40 if (!context) {
41 return false;
42 }
43
44 // fuzz for want
45 Parcel wantParcel;
46 Want* want = nullptr;
47 if (wantParcel.WriteBuffer(data, size)) {
48 want = Want::Unmarshalling(wantParcel);
49 if (want) {
50 context->StartServiceExtensionAbility(*want);
51 context->StartServiceExtensionAbility(*want, accountId);
52 }
53 }
54
55 if (want) {
56 delete want;
57 want = nullptr;
58 }
59
60 return true;
61 }
62 }
63
64 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)65 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
66 {
67 /* Run your code on data */
68 if (data == nullptr) {
69 std::cout << "invalid data" << std::endl;
70 return 0;
71 }
72
73 /* Validate the length of size */
74 if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
75 return 0;
76 }
77
78 char* ch = static_cast<char*>(malloc(size + 1));
79 if (ch == nullptr) {
80 std::cout << "malloc failed." << std::endl;
81 return 0;
82 }
83
84 (void)memset_s(ch, size + 1, 0x00, size + 1);
85 if (memcpy_s(ch, size, data, size) != EOK) {
86 std::cout << "copy failed." << std::endl;
87 free(ch);
88 ch = nullptr;
89 return 0;
90 }
91
92 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
93 free(ch);
94 ch = nullptr;
95 return 0;
96 }
97
98