• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "callemergencynotice_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21 
22 #include "hril_call.h"
23 #include "hril_manager.h"
24 #include "hril_notification.h"
25 #include "system_ability_definition.h"
26 
27 using namespace OHOS::Telephony;
28 namespace OHOS {
29 constexpr int32_t MIN_SLOT_ID = -1;
30 constexpr int32_t MAX_SLOT_ID = 4;
31 constexpr const char *NUMBER = "123";
32 
GetRandomInt(int min,int max,const uint8_t * data,size_t size)33 int32_t GetRandomInt(int min, int max, const uint8_t *data, size_t size)
34 {
35     FuzzedDataProvider fdp(data, size);
36     return fdp.ConsumeIntegralInRange<int32_t>(min, max);
37 }
38 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)39 void DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
40 {
41     if (data == nullptr || size == 0) {
42         return;
43     }
44 
45     int32_t slotId = GetRandomInt(MIN_SLOT_ID, MAX_SLOT_ID, data, size);
46     int32_t minErrCode = -1;
47     int32_t maxErrCode = static_cast<int32_t>(HRIL_ERR_HDF_IPC_FAILURE) + 1;
48     HRilErrNumber error = static_cast<HRilErrNumber>(GetRandomInt(minErrCode, maxErrCode, data, size));
49     HRilEmergencyInfo info;
50     int32_t offset = 0;
51     info.index = static_cast<int32_t>(*data + offset);
52     offset += sizeof(int32_t);
53     info.total = static_cast<int32_t>(*data + offset);
54     offset += sizeof(int32_t);
55     info.eccNum = const_cast<char *>(NUMBER);
56     info.category = static_cast<int32_t>(*data + offset);
57     offset += sizeof(int32_t);
58     info.simpresent = static_cast<int32_t>(*data + offset);
59     offset += sizeof(int32_t);
60     info.mcc = const_cast<char *>(NUMBER);
61     info.abnormalService = static_cast<int32_t>(*data + offset);
62     struct ReportInfo report;
63     report.error = error;
64     report.notifyId = HNOTI_CALL_EMERGENCY_NUMBER_REPORT;
65     report.type = HRIL_NOTIFICATION;
66     HRilManager::GetInstance().OnCallReport(slotId, &report, (const uint8_t *)&info, sizeof(HRilEmergencyInfo));
67     return;
68 }
69 } // namespace OHOS
70 
71 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)72 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
73 {
74     /* Run your code on data */
75     OHOS::DoSomethingInterestingWithMyAPI(data, size);
76     return 0;
77 }
78