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 "locatorcallbackhost_fuzzer.h"
17
18 #include "accesstoken_kit.h"
19 #include "if_system_ability_manager.h"
20 #include "iservice_registry.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23 #include "nativetoken_kit.h"
24 #include "system_ability_definition.h"
25 #include "token_setproc.h"
26 #include "locator_ability.h"
27
28 #include "locator_callback_host.h"
29
30 namespace OHOS {
31 using namespace OHOS::Location;
32 const int32_t MAX_MEM_SIZE = 4 * 1024 * 1024;
33
ParseData(const uint8_t * data,size_t size)34 char* ParseData(const uint8_t* data, size_t size)
35 {
36 if (data == nullptr) {
37 return nullptr;
38 }
39
40 if (size > MAX_MEM_SIZE) {
41 return nullptr;
42 }
43
44 char* ch = (char *)malloc(size + 1);
45 if (ch == nullptr) {
46 return nullptr;
47 }
48
49 (void)memset_s(ch, size + 1, 0x00, size + 1);
50 if (memcpy_s(ch, size, data, size) != EOK) {
51 free(ch);
52 ch = nullptr;
53 return nullptr;
54 }
55 return ch;
56 }
57
LocatorCallbackHostFuzzTest(const char * data,size_t size)58 bool LocatorCallbackHostFuzzTest(const char* data, size_t size)
59 {
60 MessageParcel requestParcel;
61 requestParcel.WriteInterfaceToken(u"location.ILocatorCallback");
62 requestParcel.WriteBuffer(data, size);
63 requestParcel.RewindRead(0);
64
65 MessageParcel reply;
66 MessageOption option;
67
68 auto callback1 = sptr<LocatorCallbackHost>(new (std::nothrow) LocatorCallbackHost());
69 callback1->OnRemoteRequest(ILocatorCallback::RECEIVE_LOCATION_INFO_EVENT, requestParcel, reply, option);
70 auto callback2 = sptr<LocatorCallbackHost>(new (std::nothrow) LocatorCallbackHost());
71 callback2->OnRemoteRequest(ILocatorCallback::RECEIVE_LOCATION_STATUS_EVENT, requestParcel, reply, option);
72 auto callback3 = sptr<LocatorCallbackHost>(new (std::nothrow) LocatorCallbackHost());
73 callback3->OnRemoteRequest(ILocatorCallback::RECEIVE_ERROR_INFO_EVENT, requestParcel, reply, option);
74
75 return true;
76 }
77 } // namespace OHOS
78
79 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)80 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
81 {
82 char* ch = OHOS::ParseData(data, size);
83 if (ch != nullptr) {
84 OHOS::LocatorCallbackHostFuzzTest(ch, size);
85 free(ch);
86 ch = nullptr;
87 }
88 return 0;
89 }
90
91