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 "voicemailconstants_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 SLEEP_TIME_SECONDS = 100000;
39
GetInt(const uint8_t * data,size_t size,int index=0)40 [[maybe_unused]] static int32_t GetInt(const uint8_t *data, size_t size, int index = 0)
41 {
42 size_t typeSize = sizeof(int32_t);
43 uintptr_t align = reinterpret_cast<uintptr_t>(data) % typeSize;
44 const uint8_t *base = data + (align > 0 ? typeSize - align : 0);
45 if (size - align < typeSize * index + (typeSize - align)) {
46 return 0;
47 }
48 return *reinterpret_cast<const int32_t*>(base + index * typeSize);
49 }
50
VoiceMailConstantseFunc(const uint8_t * data,size_t size)51 void VoiceMailConstantseFunc(const uint8_t *data, size_t size)
52 {
53 int32_t slotId = static_cast<int32_t>(*data % SLOT_NUM);
54 auto voiceMailConstants = std::make_shared<VoiceMailConstants>(slotId);
55 std::string key(reinterpret_cast<const char *>(data), size);
56 voiceMailConstants->GetStringValueFromCust(slotId, key);
57 voiceMailConstants->ResetVoiceMailLoadedFlag();
58 voiceMailConstants->GetVoiceMailFixed(key);
59 voiceMailConstants->GetVoiceMailNumber(key);
60 voiceMailConstants->GetVoiceMailTag(key);
61 voiceMailConstants->LoadVoiceMailConfigFromCard(key, key);
62 voiceMailConstants->ContainsCarrier(key);
63 }
64
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)65 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
66 {
67 if (data == nullptr || size == 0) {
68 return;
69 }
70
71 VoiceMailConstantseFunc(data, size);
72 auto telRilManager = std::static_pointer_cast<TelRilManager>(
73 DelayedSingleton<CoreService>::GetInstance()->telRilManager_);
74 if (telRilManager == nullptr || telRilManager->handler_ == nullptr) {
75 return;
76 }
77 auto handler = telRilManager->handler_;
78 if (handler != nullptr) {
79 handler->RemoveAllEvents();
80 usleep(SLEEP_TIME_SECONDS);
81 }
82 telRilManager->handler_->ClearFfrt(false);
83 telRilManager->handler_->queue_ = nullptr;
84 return;
85 }
86 } // namespace OHOS
87
88 /* Fuzzer entry point */
LLVMFuzzerInitialize(int * argc,char *** argv)89 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
90 {
91 OHOS::AddCoreServiceTokenFuzzer token;
92 return 0;
93 }
94
95 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)96 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
97 {
98 /* Run your code on data */
99 OHOS::DoSomethingInterestingWithMyAPI(data, size);
100 return 0;
101 }