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