1 /*
2 * Copyright (c) 2025 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 "locatorhandler_fuzzer.h"
17
18 #include "event_handler.h"
19 #include "accesstoken_kit.h"
20 #include "if_system_ability_manager.h"
21 #include "iservice_registry.h"
22 #include "message_option.h"
23 #include "message_parcel.h"
24 #include "nativetoken_kit.h"
25 #include "system_ability_definition.h"
26 #include "token_setproc.h"
27 #define private public
28 #include "locator_ability.h"
29 #undef private
30
31 #include "i_locator_callback.h"
32
33 namespace OHOS {
34 using namespace OHOS::Location;
35 const int32_t MAX_MEM_SIZE = 4 * 1024 * 1024;
36 const int32_t CODE_MAX = 50;
37 const int32_t CODE_MIN = 1;
38 const int32_t MIN_SIZE_NUM = 4;
39
GetU32Data(const char * ptr)40 uint32_t GetU32Data(const char* ptr)
41 {
42 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
43 }
44
ParseData(const uint8_t * data,size_t size)45 char* ParseData(const uint8_t* data, size_t size)
46 {
47 if (data == nullptr) {
48 return nullptr;
49 }
50
51 if (size > MAX_MEM_SIZE) {
52 return nullptr;
53 }
54
55 char* ch = static_cast<char*>(malloc(size + 1));
56 if (ch == nullptr) {
57 return nullptr;
58 }
59
60 (void)memset_s(ch, size + 1, 0x00, size + 1);
61 if (memcpy_s(ch, size, data, size) != EOK) {
62 free(ch);
63 ch = nullptr;
64 return nullptr;
65 }
66 return ch;
67 }
68
LocatorHandlerFuzzTest(const char * data,size_t size)69 bool LocatorHandlerFuzzTest(const char* data, size_t size)
70 {
71 uint32_t code = GetU32Data(data) % (CODE_MAX - CODE_MIN + 1) + CODE_MIN;
72 std::shared_ptr<AppExecFwk::EventRunner> runner;
73 auto locatorHandler = new LocatorHandler(runner);
74 AppExecFwk::InnerEvent::Pointer event = AppExecFwk::InnerEvent::Get(code, code);
75 locatorHandler->ProcessEvent(event);
76 locatorHandler->UpdateSaEvent(event);
77 locatorHandler->InitRequestManagerEvent(event);
78 locatorHandler->ApplyRequirementsEvent(event);
79 locatorHandler->RetryRegisterActionEvent(event);
80 locatorHandler->ReportLocationMessageEvent(event);
81 locatorHandler->SendSwitchStateToHifenceEvent(event);
82 locatorHandler->UnloadSaEvent(event);
83 locatorHandler->StartLocatingEvent(event);
84 locatorHandler->StopLocatingEvent(event);
85 locatorHandler->GetCachedLocationSuccess(event);
86 locatorHandler->GetCachedLocationFailed(event);
87 locatorHandler->StartScanBluetoothDeviceEvent(event);
88 locatorHandler->StopScanBluetoothDeviceEvent(event);
89 locatorHandler->RegLocationErrorEvent(event);
90 locatorHandler->UnRegLocationErrorEvent(event);
91 locatorHandler->ReportNetworkLocatingErrorEvent(event);
92 locatorHandler->RequestCheckEvent(event);
93 locatorHandler->SyncStillMovementState(event);
94 locatorHandler->SyncIdleState(event);
95 locatorHandler->SendGeoRequestEvent(event);
96 locatorHandler->SyncSwitchStatus(event);
97 locatorHandler->InitMonitorManagerEvent(event);
98 locatorHandler->IsStandByEvent(event);
99 locatorHandler->SetLocationWorkingStateEvent(event);
100 locatorHandler->SetSwitchStateToDbEvent(event);
101 locatorHandler->SetSwitchStateToDbForUserEvent(event);
102 locatorHandler->WatchSwitchParameter(event);
103 return true;
104 }
105 } // namespace OHOS
106
107 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)108 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
109 {
110 if (size < OHOS::MIN_SIZE_NUM) {
111 return 0;
112 }
113 char* ch = OHOS::ParseData(data, size);
114 if (ch != nullptr) {
115 OHOS::LocatorHandlerFuzzTest(ch, size);
116 free(ch);
117 ch = nullptr;
118 }
119 return 0;
120 }
121
122