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 "wantsenderstub_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "want_sender_stub.h"
22 #include "message_parcel.h"
23 #include "securec.h"
24
25 using namespace OHOS::AAFwk;
26
27 namespace OHOS {
28 namespace {
29 constexpr size_t FOO_MAX_LEN = 1024;
30 constexpr size_t U32_AT_SIZE = 4;
31 const std::u16string ABILITYMGR_INTERFACE_TOKEN = u"ohos.aafwk.WantSender";
32 }
33
34 class WantSenderStubFuzzTest : public WantSenderStub {
35 public:
36 WantSenderStubFuzzTest() = default;
~WantSenderStubFuzzTest()37 virtual ~WantSenderStubFuzzTest()
38 {}
Send(SenderInfo & senderInfo)39 void Send(SenderInfo &senderInfo) override
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[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
47 }
48
DoSomethingInterestingWithMyAPI(const char * data,size_t size)49 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
50 {
51 uint32_t code = GetU32Data(data);
52
53 MessageParcel parcel;
54 parcel.WriteInterfaceToken(ABILITYMGR_INTERFACE_TOKEN);
55 parcel.WriteBuffer(data, size);
56 parcel.RewindRead(0);
57 MessageParcel reply;
58 MessageOption option;
59
60 std::shared_ptr<WantSenderStub> wantsenderstub = std::make_shared<WantSenderStubFuzzTest>();
61
62 wantsenderstub->OnRemoteRequest(code, parcel, reply, option);
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
102