1/* 2 * Copyright (c) 2022-2023 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 "splitmessage_fuzzer.h" 17 18#define private public 19#include "addsmstoken_fuzzer.h" 20#include "cdma_sms_message.h" 21#include "core_manager_inner.h" 22#include "sms_service.h" 23 24using namespace OHOS::Telephony; 25namespace OHOS { 26static bool g_isInited = false; 27static int32_t SLOT_NUM = 2; 28 29bool IsServiceInited() 30{ 31 if (!g_isInited) { 32 CoreManagerInner::GetInstance().isInitAllObj_ = true; 33 DelayedSingleton<SmsService>::GetInstance()->registerToService_ = true; 34 DelayedSingleton<SmsService>::GetInstance()->WaitCoreServiceToInit(); 35 DelayedSingleton<SmsService>::GetInstance()->OnStart(); 36 if (DelayedSingleton<SmsService>::GetInstance()->GetServiceRunningState() == 37 static_cast<int32_t>(Telephony::ServiceRunningState::STATE_RUNNING)) { 38 g_isInited = true; 39 } 40 } 41 return g_isInited; 42} 43 44void SplitMessage(const uint8_t *data, size_t size) 45{ 46 MessageParcel dataParcel; 47 MessageParcel replyParcel; 48 MessageOption option(MessageOption::TF_SYNC); 49 50 std::string message(reinterpret_cast<const char *>(data), size); 51 auto messageU16 = Str8ToStr16(message); 52 dataParcel.WriteString16(messageU16); 53 dataParcel.RewindRead(0); 54 55 DelayedSingleton<SmsService>::GetInstance()->OnSplitMessage(dataParcel, replyParcel, option); 56 57 int32_t slotId = static_cast<int32_t>(size % SLOT_NUM); 58 std::shared_ptr<SmsInterfaceManager> interfaceManager = std::make_shared<SmsInterfaceManager>(slotId); 59 if (interfaceManager == nullptr) { 60 TELEPHONY_LOGE("interfaceManager nullptr error"); 61 return; 62 } 63 interfaceManager->InitInterfaceManager(); 64 std::string messageData(reinterpret_cast<const char *>(data), size); 65 std::vector<std::u16string> splitMessage; 66 interfaceManager->SplitMessage(messageData, splitMessage); 67 auto smsSendManager = std::make_unique<SmsSendManager>(slotId); 68 if (smsSendManager == nullptr) { 69 TELEPHONY_LOGE("failed to create SmsSendManager"); 70 return; 71 } 72 smsSendManager->Init(); 73 smsSendManager->InitNetworkHandle(); 74 smsSendManager->SplitMessage(messageData, splitMessage); 75 76 DataCodingScheme codingType; 77 std::vector<struct SplitInfo> cellsInfos; 78 GsmSmsMessage gsmSmsMessage; 79 gsmSmsMessage.SplitMessage(cellsInfos, messageData, false, codingType, false, ""); 80 CdmaSmsMessage cdmaSmsMessage; 81 cdmaSmsMessage.SplitMessage(cellsInfos, messageData, false, codingType, false, ""); 82} 83 84void GetImsShortMessageFormat(const uint8_t *data, size_t size) 85{ 86 MessageParcel dataParcel; 87 MessageParcel replyParcel; 88 MessageOption option(MessageOption::TF_SYNC); 89 90 dataParcel.WriteBuffer(data, size); 91 dataParcel.RewindRead(0); 92 DelayedSingleton<SmsService>::GetInstance()->OnGetImsShortMessageFormat(dataParcel, replyParcel, option); 93 94 int32_t slotId = static_cast<int32_t>(size % SLOT_NUM); 95 std::shared_ptr<SmsInterfaceManager> interfaceManager = std::make_shared<SmsInterfaceManager>(slotId); 96 if (interfaceManager == nullptr) { 97 TELEPHONY_LOGE("interfaceManager nullptr error"); 98 return; 99 } 100 std::u16string format; 101 interfaceManager->InitInterfaceManager(); 102 interfaceManager->GetImsShortMessageFormat(format); 103 104 auto smsSendManager = std::make_unique<SmsSendManager>(slotId); 105 if (smsSendManager == nullptr) { 106 TELEPHONY_LOGE("failed to create SmsSendManager"); 107 return; 108 } 109 smsSendManager->Init(); 110 smsSendManager->InitNetworkHandle(); 111 smsSendManager->GetImsShortMessageFormat(format); 112} 113 114void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size) 115{ 116 if (data == nullptr || size == 0) { 117 return; 118 } 119 120 if (!IsServiceInited()) { 121 return; 122 } 123 124 SplitMessage(data, size); 125 GetImsShortMessageFormat(data, size); 126 DelayedSingleton<SmsService>::DestroyInstance(); 127} 128} // namespace OHOS 129 130/* Fuzzer entry point */ 131extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 132{ 133 /* Run your code on data */ 134 OHOS::AddSmsTokenFuzzer token; 135 OHOS::DoSomethingInterestingWithMyAPI(data, size); 136 return 0; 137} 138