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 "cachedlocationcallbackhost_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
27 #ifdef FEATURE_GNSS_SUPPORT
28 #include "cached_locations_callback_napi.h"
29 #endif
30 #include "common_utils.h"
31 #include "constant_definition.h"
32 #include "country_code_callback_napi.h"
33 #include "i_locator_callback.h"
34 #include "location.h"
35 #include "locator.h"
36 #include "location_switch_callback_napi.h"
37 #include "locator.h"
38 #include "locator_ability.h"
39 #include "locator_callback_napi.h"
40 #include "location_log.h"
41
42 namespace OHOS {
43 using namespace OHOS::Location;
44 const int32_t MAX_MEM_SIZE = 4 * 1024 * 1024;
45
ParseData(const uint8_t * data,size_t size)46 char* ParseData(const uint8_t* data, size_t size)
47 {
48 if (data == nullptr) {
49 return nullptr;
50 }
51
52 if (size > MAX_MEM_SIZE) {
53 return nullptr;
54 }
55
56 char* ch = (char *)malloc(size + 1);
57 if (ch == nullptr) {
58 return nullptr;
59 }
60
61 (void)memset_s(ch, size + 1, 0x00, size + 1);
62 if (memcpy_s(ch, size, data, size) != EOK) {
63 free(ch);
64 ch = nullptr;
65 return nullptr;
66 }
67 return ch;
68 }
69
CachedLocationsCallbackHostFuzzTest(const char * data,size_t size)70 bool CachedLocationsCallbackHostFuzzTest(const char* data, size_t size)
71 {
72 MessageParcel requestParcel;
73 requestParcel.WriteInterfaceToken(u"location.ICachedLocationsCallback");
74 requestParcel.WriteBuffer(data, size);
75 requestParcel.RewindRead(0);
76
77 MessageParcel reply;
78 MessageOption option;
79 auto callback = sptr<CachedLocationsCallbackNapi>(new (std::nothrow) CachedLocationsCallbackNapi());
80 callback->OnRemoteRequest(CachedLocationsCallbackNapi::RECEIVE_CACHED_LOCATIONS_EVENT,
81 requestParcel, reply, option);
82 return true;
83 }
84 } // namespace OHOS
85
86 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)87 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
88 {
89 char* ch = OHOS::ParseData(data, size);
90 if (ch != nullptr) {
91 OHOS::CachedLocationsCallbackHostFuzzTest(ch, size);
92 free(ch);
93 ch = nullptr;
94 }
95 return 0;
96 }
97
98