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 "getsimmessage_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 #include "sim_manager.h"
32 #include "operator_config_cache.h"
33 #include "voice_mail_constants.h"
34
35 using namespace OHOS::Telephony;
36 namespace OHOS {
37 constexpr int32_t SLOT_NUM = 2;
38 constexpr int32_t SIM_STATUS_NUM = 5;
39 constexpr const char *DEFAULT_SLOT_COUNT = "1";
40 constexpr int32_t SLEEP_TIME_SECONDS = 100000;
41
GetInt(const uint8_t * data,size_t size,int index=0)42 static int32_t GetInt(const uint8_t *data, size_t size, int index = 0)
43 {
44 size_t typeSize = sizeof(int32_t);
45 uintptr_t align = reinterpret_cast<uintptr_t>(data) % typeSize;
46 const uint8_t *base = data + (align > 0 ? typeSize - align : 0);
47 if (size - align < typeSize * index + (typeSize - align)) {
48 return 0;
49 }
50 return *reinterpret_cast<const int32_t*>(base + index * typeSize);
51 }
52
GetSimMessageFunc(const uint8_t * data,size_t size)53 void GetSimMessageFunc(const uint8_t *data, size_t size)
54 {
55 int index = 0;
56 auto telRilManager = std::make_shared<TelRilManager>();
57 auto simManager = std::make_shared<SimManager>(telRilManager);
58 std::vector<std::shared_ptr<Telephony::SimStateManager>> simStateManager = { nullptr, nullptr };
59 std::vector<std::shared_ptr<Telephony::SimFileManager>> simFileManager = { nullptr, nullptr };
60 simManager->multiSimController_ =std::make_shared<MultiSimController>(
61 telRilManager, simStateManager, simFileManager);
62 int32_t slotId = static_cast<int32_t>(*data % SLOT_NUM);
63 int32_t enable = GetInt(data, size, index++);
64 int32_t simState = *data % SIM_STATUS_NUM + 1;
65 std::string pin(reinterpret_cast<const char *>(data), size);
66 std::u16string number(reinterpret_cast<const char16_t *>(data), size);
67 simManager->GetIMSI(slotId, number);
68 simManager->GetLocaleFromDefaultSim(slotId);
69 simManager->GetSimGid1(slotId, number);
70 simManager->GetSimGid1(slotId, number);
71 simManager->GetSimTelephoneNumber(slotId, number);
72 simManager->GetSimTeleNumberIdentifier(slotId);
73 simManager->GetVoiceMailIdentifier(slotId, number);
74 simManager->GetVoiceMailNumber(slotId, number);
75 simManager->SetVoiceCallForwarding(slotId, true, pin);
76 simManager->SetVoiceMailInfo(slotId, number, number);
77 simManager->UpdateSmsIcc(slotId, static_cast<int>(enable), static_cast<int>(simState), pin, pin);
78 simManager->DelSmsIcc(slotId, static_cast<int>(enable));
79 simManager->ObtainAllSmsOfIcc(slotId);
80 simManager->GetDefaultMainSlotByIccId();
81 simManager->multiSimController_ = nullptr;
82 simManager->SetActiveSim(slotId, enable);
83 simManager->SetActiveSimSatellite(slotId, enable);
84 simManager->SetShowNumber(slotId, number);
85 simManager->SetShowName(slotId, number);
86 simManager->GetShowNumber(slotId, number);
87 simManager->GetShowName(slotId, number);
88 simManager->GetSimTelephoneNumber(slotId, number);
89 simManager->GetDefaultMainSlotByIccId();
90 simManager->slotCount_ = std::atoi(DEFAULT_SLOT_COUNT);
91 simManager->GetDsdsMode(enable);
92 }
93
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)94 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
95 {
96 if (data == nullptr || size == 0) {
97 return;
98 }
99
100 GetSimMessageFunc(data, size);
101 auto telRilManager = std::static_pointer_cast<TelRilManager>(
102 DelayedSingleton<CoreService>::GetInstance()->telRilManager_);
103 if (telRilManager == nullptr || telRilManager->handler_ == nullptr) {
104 return;
105 }
106 auto handler = telRilManager->handler_;
107 if (handler != nullptr) {
108 handler->RemoveAllEvents();
109 usleep(SLEEP_TIME_SECONDS);
110 }
111 telRilManager->handler_->ClearFfrt(false);
112 telRilManager->handler_->queue_ = nullptr;
113 return;
114 }
115 } // namespace OHOS
116
117 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)118 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
119 {
120 OHOS::AddCoreServiceTokenFuzzer token;
121 return 0;
122 }
123
124 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)125 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
126 {
127 /* Run your code on data */
128 OHOS::DoSomethingInterestingWithMyAPI(data, size);
129 return 0;
130 }