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 "ispassseqcheck_fuzzer.h"
17 #include <cstddef>
18 #include "softbus_sequence_verification.h"
19 
20 namespace OHOS {
21     constexpr size_t THRESHOLD = 10;
22     constexpr uint32_t NINE = 9;
23     constexpr int32_t MINSEQ = 2;
24     constexpr int32_t MAXSEQ = -2;
25     constexpr int32_t OFFSET = 4;
26     SeqVerifyInfo seqInfo;
27     enum  CmdId {
28         CMD_SOFTBUS_ONE,
29         CMD_SOFTBUS_TWO,
30         CMD_SOFTBUS_THREE,
31     };
32 
Convert2Uint32(const uint8_t * ptr)33     uint32_t Convert2Uint32(const uint8_t *ptr)
34     {
35         if (ptr == nullptr) {
36             return 0;
37         }
38         /*
39         * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left,
40         * and the third digit no left
41         */
42         return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
43     }
44 
IsPassSeqCheckSwitch(uint32_t cmd,const uint8_t * rawData)45     static void IsPassSeqCheckSwitch(uint32_t cmd, const uint8_t *rawData)
46     {
47         int32_t seq = *const_cast<int32_t *>(reinterpret_cast<const int32_t *>(rawData));
48         uint64_t bit = *const_cast<uint64_t *>(reinterpret_cast<const uint64_t *>(rawData));
49         cmd = cmd % NINE;
50         switch (cmd) {
51             case CMD_SOFTBUS_ONE: {
52                 seqInfo.minSeq = MINSEQ;
53                 seqInfo.maxSeq = seq;
54                 seqInfo.recvBitmap = bit;
55                 IsPassSeqCheck(&seqInfo, seq);
56                 break;
57             }
58             case CMD_SOFTBUS_TWO: {
59                 seqInfo.minSeq = seq;
60                 seqInfo.maxSeq = MAXSEQ;
61                 seqInfo.recvBitmap = bit;
62                 IsPassSeqCheck(&seqInfo, seq);
63                 break;
64             }
65             case CMD_SOFTBUS_THREE: {
66                 seqInfo.minSeq = seq;
67                 seqInfo.maxSeq = seq;
68                 seqInfo.recvBitmap = bit;
69                 IsPassSeqCheck(&seqInfo, seq);
70                 break;
71             }
72             default:
73                 break;
74         }
75     }
76 
DoSomethingInterestingWithMyAPI(const uint8_t * rawData,size_t size)77     bool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)
78     {
79         (void)size;
80 
81         if (rawData == nullptr) {
82             return false;
83         }
84         uint32_t cmd = Convert2Uint32(rawData);
85         rawData = rawData + OFFSET;
86 
87         IsPassSeqCheckSwitch(cmd, rawData);
88         return true;
89     }
90 } // namespace OHOS
91 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)92 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
93 {
94     if (size < OHOS::THRESHOLD) {
95         return 0;
96     }
97 
98     /* Run your code on data */
99     OHOS::DoSomethingInterestingWithMyAPI(data, size);
100     return 0;
101 }