• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "getsimstate_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 int32_t ICC_STATUS_NUM = 12;
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 
GetSimStateFunc(const uint8_t * data,size_t size)53 void GetSimStateFunc(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     int32_t slotId = static_cast<int32_t>(*data % SLOT_NUM);
59     [[maybe_unused]] int32_t slotCount = GetInt(data, size, index++);
60     int32_t voiceMailCount = GetInt(data, size, index++);
61     int32_t simState = *data % SIM_STATUS_NUM + 1;
62     int32_t iccStatus = *data % ICC_STATUS_NUM + 1;
63     SimState simEnum = static_cast<SimState>(simState);
64     IccSimStatus iccEnum = static_cast<IccSimStatus>(iccStatus);
65     bool hasOperatorPrivileges = true;
66     simManager->GetSimState(slotId, simEnum);
67     simManager->GetSimIccStatus(slotId, iccEnum);
68     simManager->SetModemInit(slotId, true);
69     simManager->RefreshSimState(slotId);
70     simManager->SendSimMatchedOperatorInfo(slotId, simState, std::string(reinterpret_cast<const char *>(data), size),
71         std::string(reinterpret_cast<const char *>(data), size));
72     simManager->SetVoiceMailCount(slotId, voiceMailCount);
73     simManager->GetVoiceMailCount(slotId, voiceMailCount);
74     simManager->ObtainSpnCondition(
75         slotId, hasOperatorPrivileges, std::string(reinterpret_cast<const char *>(data), size));
76     std::string pdu(reinterpret_cast<const char*>(data), size);
77     std::string smsc(reinterpret_cast<const char*>(data), size);
78     simManager->AddSmsToIcc(slotId, static_cast<int32_t>(simState), pdu, smsc);
79     simManager->IsCTSimCard(slotId, hasOperatorPrivileges);
80     simManager->IsValidSlotIdForDefault(slotId);
81     simManager->GetSimIst(slotId);
82     simManager->NotifySimSlotsMapping(slotId);
83 }
84 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)85 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
86 {
87     if (data == nullptr || size == 0) {
88         return;
89     }
90 
91     GetSimStateFunc(data, size);
92     auto telRilManager = std::static_pointer_cast<TelRilManager>(
93          DelayedSingleton<CoreService>::GetInstance()->telRilManager_);
94     if (telRilManager == nullptr || telRilManager->handler_ == nullptr) {
95         return;
96     }
97     auto handler = telRilManager->handler_;
98     if (handler != nullptr) {
99         handler->RemoveAllEvents();
100         usleep(SLEEP_TIME_SECONDS);
101     }
102     telRilManager->handler_->ClearFfrt(false);
103     telRilManager->handler_->queue_ = nullptr;
104     return;
105 }
106 } // namespace OHOS
107 
108 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)109 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
110 {
111     OHOS::AddCoreServiceTokenFuzzer token;
112     return 0;
113 }
114 
115 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)116 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
117 {
118     /* Run your code on data */
119     OHOS::DoSomethingInterestingWithMyAPI(data, size);
120     return 0;
121 }