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 "addiccdiallingnumbers_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <thread>
21
22 #define private public
23 #include "addcoreservicetoken_fuzzer.h"
24 #include "core_service.h"
25 #include "napi_util.h"
26 #include "system_ability_definition.h"
27 #include "tel_event_handler.h"
28 #include "unistd.h"
29 #include "tel_ril_manager.h"
30 #include "sim_state_type.h"
31
32 using namespace OHOS::Telephony;
33 namespace OHOS {
34 static bool g_isInited = false;
35 constexpr int32_t SIZE_LIMIT = 4;
36 constexpr uint32_t FUCTION_SIZE = 100;
37 constexpr int32_t SLEEP_TIME_SECONDS = 100000;
38
IsServiceInited()39 bool IsServiceInited()
40 {
41 if (!g_isInited) {
42 DelayedSingleton<CoreService>::GetInstance()->OnStart();
43 if (DelayedSingleton<CoreService>::GetInstance()->GetServiceRunningState() ==
44 static_cast<int32_t>(ServiceRunningState::STATE_RUNNING)) {
45 g_isInited = true;
46 }
47 }
48 return g_isInited;
49 }
50
OnRemoteRequest(const uint8_t * data,size_t size)51 void OnRemoteRequest(const uint8_t *data, size_t size)
52 {
53 if (!IsServiceInited()) {
54 return;
55 }
56
57 if (size < SIZE_LIMIT) {
58 return;
59 }
60
61 MessageParcel dataMessageParcel;
62 if (!dataMessageParcel.WriteInterfaceToken(CoreServiceStub::GetDescriptor())) {
63 return;
64 }
65 dataMessageParcel.WriteBuffer(data, size);
66 dataMessageParcel.RewindRead(0);
67
68 uint32_t code = (static_cast<uint32_t>(data[0]) << 24) | (static_cast<uint32_t>(data[1]) << 16) |
69 (static_cast<uint32_t>(data[2]) << 8) | (static_cast<uint32_t>(data[3])) % FUCTION_SIZE;
70
71 MessageParcel reply;
72 MessageOption option;
73 DelayedSingleton<CoreService>::GetInstance()->OnRemoteRequest(code, dataMessageParcel, reply, option);
74 }
75
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)76 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
77 {
78 if (data == nullptr || size == 0) {
79 return;
80 }
81
82 OnRemoteRequest(data, size);
83 auto telRilManager = std::static_pointer_cast<TelRilManager>(
84 DelayedSingleton<CoreService>::GetInstance()->telRilManager_);
85 if (telRilManager == nullptr || telRilManager->handler_ == nullptr) {
86 return;
87 }
88 auto handler = telRilManager->handler_;
89 if (handler != nullptr) {
90 handler->RemoveAllEvents();
91 usleep(SLEEP_TIME_SECONDS);
92 }
93 telRilManager->handler_->ClearFfrt(false);
94 telRilManager->handler_->queue_ = nullptr;
95 return;
96 }
97 } // namespace OHOS
98
99 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)100 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
101 {
102 OHOS::AddCoreServiceTokenFuzzer token;
103 return 0;
104 }
105
106 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)107 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
108 {
109 /* Run your code on data */
110 OHOS::DoSomethingInterestingWithMyAPI(data, size);
111 if (OHOS::g_isInited) {
112 OHOS::DelayedSingleton<CoreService>::GetInstance()->OnStop();
113 OHOS::g_isInited = false;
114 }
115 usleep(OHOS::SLEEP_TIME_SECONDS);
116 return 0;
117 }
118