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 "setdefaultslotid_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 const char *DEFAULT_SLOT_COUNT = "1";
39 constexpr int32_t SLEEP_TIME_SECONDS = 100000;
40
GetInt(const uint8_t * data,size_t size,int index=0)41 static int32_t GetInt(const uint8_t *data, size_t size, int index = 0)
42 {
43 size_t typeSize = sizeof(int32_t);
44 uintptr_t align = reinterpret_cast<uintptr_t>(data) % typeSize;
45 const uint8_t *base = data + (align > 0 ? typeSize - align : 0);
46 if (size - align < typeSize * index + (typeSize - align)) {
47 return 0;
48 }
49 return *reinterpret_cast<const int32_t*>(base + index * typeSize);
50 }
51
SetDefaultSlotIdFunc(const uint8_t * data,size_t size)52 void SetDefaultSlotIdFunc(const uint8_t *data, size_t size)
53 {
54 int index = 0;
55 auto telRilManager = std::make_shared<TelRilManager>();
56 auto simManager = std::make_shared<SimManager>(telRilManager);
57 int32_t slotId = static_cast<int32_t>(*data % SLOT_NUM);
58 int32_t voiceMailCount = GetInt(data, size, index++);
59 simManager->multiSimController_ = nullptr;
60 simManager->SetDefaultVoiceSlotId(slotId);
61 simManager->SetDefaultSmsSlotId(slotId);
62 simManager->SetDefaultCellularDataSlotId(slotId);
63 simManager->SetPrimarySlotId(slotId, true);
64 simManager->GetDefaultVoiceSimId(slotId);
65 simManager->GetDefaultSmsSlotId();
66 simManager->GetDefaultSmsSimId(slotId);
67 simManager->GetDefaultCellularDataSlotId();
68 simManager->GetDefaultCellularDataSimId(slotId);
69 simManager->GetPrimarySlotId(slotId);
70 simManager->InsertEsimData(std::string(reinterpret_cast<const char *>(data), size), voiceMailCount,
71 std::string(reinterpret_cast<const char *>(data), size));
72 simManager->SetSimLabelIndex(std::string(reinterpret_cast<const char *>(data), size), voiceMailCount);
73 simManager->slotCount_ = std::atoi(DEFAULT_SLOT_COUNT);
74 simManager->GetDefaultSmsSlotId();
75 simManager->GetDefaultCellularDataSlotId();
76 simManager->GetPrimarySlotId(slotId);
77 }
78
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)79 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
80 {
81 if (data == nullptr || size == 0) {
82 return;
83 }
84
85 SetDefaultSlotIdFunc(data, size);
86 auto telRilManager = std::static_pointer_cast<TelRilManager>(
87 DelayedSingleton<CoreService>::GetInstance()->telRilManager_);
88 if (telRilManager == nullptr || telRilManager->handler_ == nullptr) {
89 return;
90 }
91 auto handler = telRilManager->handler_;
92 if (handler != nullptr) {
93 handler->RemoveAllEvents();
94 usleep(SLEEP_TIME_SECONDS);
95 }
96 telRilManager->handler_->ClearFfrt(false);
97 telRilManager->handler_->queue_ = nullptr;
98 return;
99 }
100 } // namespace OHOS
101
102 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)103 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
104 {
105 OHOS::AddCoreServiceTokenFuzzer token;
106 return 0;
107 }
108
109 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)110 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
111 {
112 /* Run your code on data */
113 OHOS::DoSomethingInterestingWithMyAPI(data, size);
114 return 0;
115 }