• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "setshownumber_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <string_ex.h>
21 #include <thread>
22 
23 #define private public
24 #include "addcoreservicetoken_fuzzer.h"
25 #include "core_service.h"
26 #include "napi_util.h"
27 #include "system_ability_definition.h"
28 #include "unistd.h"
29 
30 using namespace OHOS::Telephony;
31 namespace OHOS {
32 static bool g_isInited = false;
33 constexpr int32_t SLOT_NUM = 2;
34 constexpr int32_t SLEEP_TIME_SECONDS = 10;
35 
IsServiceInited()36 bool IsServiceInited()
37 {
38     if (!g_isInited) {
39         auto onStart = [] { DelayedSingleton<CoreService>::GetInstance()->OnStart(); };
40         std::thread startThread(onStart);
41         pthread_setname_np(startThread.native_handle(), "setshownumber_fuzzer");
42         startThread.join();
43 
44         sleep(SLEEP_TIME_SECONDS);
45         if (DelayedSingleton<CoreService>::GetInstance()->GetServiceRunningState() ==
46             static_cast<int32_t>(ServiceRunningState::STATE_RUNNING)) {
47             g_isInited = true;
48         }
49     }
50     return g_isInited;
51 }
52 
GetShowNumber(const uint8_t * data,size_t size)53 void GetShowNumber(const uint8_t *data, size_t size)
54 {
55     if (!IsServiceInited()) {
56         return;
57     }
58 
59     int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
60     MessageParcel dataMessageParcel;
61     dataMessageParcel.WriteInt32(slotId);
62     dataMessageParcel.WriteBuffer(data, size);
63     dataMessageParcel.RewindRead(0);
64     MessageParcel reply;
65     DelayedSingleton<CoreService>::GetInstance()->OnGetShowNumber(dataMessageParcel, reply);
66 }
67 
GetSlotId(const uint8_t * data,size_t size)68 void GetSlotId(const uint8_t *data, size_t size)
69 {
70     if (!IsServiceInited()) {
71         return;
72     }
73 
74     int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
75     MessageParcel dataMessageParcel;
76     dataMessageParcel.WriteInt32(slotId);
77     dataMessageParcel.WriteBuffer(data, size);
78     dataMessageParcel.RewindRead(0);
79     MessageParcel reply;
80     DelayedSingleton<CoreService>::GetInstance()->OnGetSlotId(dataMessageParcel, reply);
81 }
82 
GetSimId(const uint8_t * data,size_t size)83 void GetSimId(const uint8_t *data, size_t size)
84 {
85     if (!IsServiceInited()) {
86         return;
87     }
88 
89     int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
90     MessageParcel dataMessageParcel;
91     dataMessageParcel.WriteInt32(slotId);
92     dataMessageParcel.WriteBuffer(data, size);
93     dataMessageParcel.RewindRead(0);
94     MessageParcel reply;
95     DelayedSingleton<CoreService>::GetInstance()->OnGetSimId(dataMessageParcel, reply);
96 }
97 
GetLocaleFromDefaultSim(const uint8_t * data,size_t size)98 void GetLocaleFromDefaultSim(const uint8_t *data, size_t size)
99 {
100     if (!IsServiceInited()) {
101         return;
102     }
103 
104     MessageParcel dataMessageParcel;
105     dataMessageParcel.WriteBuffer(data, size);
106     dataMessageParcel.RewindRead(0);
107     MessageParcel reply;
108     DelayedSingleton<CoreService>::GetInstance()->OnGetLocaleFromDefaultSim(dataMessageParcel, reply);
109 }
110 
SetShowNumber(const uint8_t * data,size_t size)111 void SetShowNumber(const uint8_t *data, size_t size)
112 {
113     if (!IsServiceInited()) {
114         return;
115     }
116 
117     int32_t slotId = static_cast<int32_t>(size % SLOT_NUM);
118     std::string number(reinterpret_cast<const char *>(data), size);
119     auto numberU16 = Str8ToStr16(number);
120     MessageParcel dataMessageParcel;
121     dataMessageParcel.WriteInt32(slotId);
122     dataMessageParcel.WriteString16(numberU16);
123     dataMessageParcel.RewindRead(0);
124     MessageParcel reply;
125     DelayedSingleton<CoreService>::GetInstance()->OnSetShowNumber(dataMessageParcel, reply);
126 }
127 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)128 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
129 {
130     if (data == nullptr || size == 0) {
131         return;
132     }
133 
134     GetShowNumber(data, size);
135     GetSlotId(data, size);
136     GetSimId(data, size);
137     GetLocaleFromDefaultSim(data, size);
138     SetShowNumber(data, size);
139     return;
140 }
141 } // namespace OHOS
142 
143 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)144 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
145 {
146     OHOS::AddCoreServiceTokenFuzzer token;
147     /* Run your code on data */
148     OHOS::DoSomethingInterestingWithMyAPI(data, size);
149     return 0;
150 }
151