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 "atomicservicestatuscallbackstub_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "atomic_service_status_callback_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.IAtomicServiceStatusCallback";
32 }
33
34 class AtomicServiceStatusCallbackStubFuzzTest : public AtomicServiceStatusCallbackStub {
35 public:
36 AtomicServiceStatusCallbackStubFuzzTest() = default;
~AtomicServiceStatusCallbackStubFuzzTest()37 virtual ~AtomicServiceStatusCallbackStubFuzzTest()
38 {};
OnInstallFinished(int resultCode,const Want & want,int32_t userId)39 void OnInstallFinished(int resultCode, const Want &want, int32_t userId) override
40 {}
OnRemoteInstallFinished(int resultCode,const Want & want,int32_t userId)41 void OnRemoteInstallFinished(int resultCode, const Want &want, int32_t userId) override
42 {}
43 };
44
GetU32Data(const char * ptr)45 uint32_t GetU32Data(const char* ptr)
46 {
47 // convert fuzz input data to an integer
48 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
49 }
50
DoSomethingInterestingWithMyAPI(const char * data,size_t size)51 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
52 {
53 uint32_t code = GetU32Data(data);
54
55 MessageParcel parcel;
56 parcel.WriteInterfaceToken(ABILITYMGR_INTERFACE_TOKEN);
57 parcel.WriteBuffer(data, size);
58 parcel.RewindRead(0);
59 MessageParcel reply;
60 MessageOption option;
61
62 std::shared_ptr<AtomicServiceStatusCallbackStub> atomicservicestatuscallbackstub
63 = std::make_shared<AtomicServiceStatusCallbackStubFuzzTest>();
64
65 atomicservicestatuscallbackstub->OnRemoteRequest(code, parcel, reply, option);
66
67 return true;
68 }
69 }
70
71 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)72 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
73 {
74 /* Run your code on data */
75 if (data == nullptr) {
76 std::cout << "invalid data" << std::endl;
77 return 0;
78 }
79
80 /* Validate the length of size */
81 if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
82 return 0;
83 }
84
85 char* ch = (char *)malloc(size + 1);
86 if (ch == nullptr) {
87 std::cout << "malloc failed." << std::endl;
88 return 0;
89 }
90
91 (void)memset_s(ch, size + 1, 0x00, size + 1);
92 if (memcpy_s(ch, size, data, size) != EOK) {
93 std::cout << "copy failed." << std::endl;
94 free(ch);
95 ch = nullptr;
96 return 0;
97 }
98
99 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
100 free(ch);
101 ch = nullptr;
102 return 0;
103 }
104
105