• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "securec.h"
16 #include "devicestandby_fuzzer.h"
17 
18 #include "standby_service.h"
19 
20 namespace OHOS {
21 namespace DevStandbyMgr {
22     constexpr size_t U32_AT_SIZE = 4;
23     constexpr uint32_t MAX_CODE = 11;
24     constexpr uint8_t TWENTYFOUR = 24;
25     constexpr uint8_t SIXTEEN = 16;
26     constexpr uint8_t EIGHT = 8;
27     constexpr uint8_t TWO = 2;
28     constexpr int32_t THREE = 3;
29     const std::u16string DEVICE_STANDBY_TOKEN = u"ohos.resourceschedule.IStandbyService";
30 
GetU32Data(const char * ptr)31     uint32_t GetU32Data(const char* ptr)
32     {
33         return (ptr[0] << TWENTYFOUR) | (ptr[1] << SIXTEEN) | (ptr[TWO] << EIGHT) | (ptr[THREE]);
34     }
35 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)36     bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
37     {
38         uint32_t code = GetU32Data(data);
39         MessageParcel datas;
40         datas.WriteInterfaceToken(DEVICE_STANDBY_TOKEN);
41         datas.WriteBuffer(data, size);
42         datas.RewindRead(0);
43         MessageParcel reply;
44         MessageOption option;
45         uint32_t inputCode = code % MAX_CODE;
46         if (inputCode > 0) {
47             DelayedSingleton<StandbyService>::GetInstance()->OnRemoteRequest(inputCode,
48                 datas, reply, option);
49         }
50         return true;
51     }
52 } // namespace DevStandbyMgr
53 } // namespace OHOS
54 
55 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)56 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
57 {
58     /* Run your code on data */
59     if (data == nullptr) {
60         return 0;
61     }
62 
63     if (size < OHOS::DevStandbyMgr::U32_AT_SIZE) {
64         return 0;
65     }
66 
67     char* ch = (char *)malloc(size + 1);
68     if (ch == nullptr) {
69         return 0;
70     }
71 
72     (void)memset_s(ch, size + 1, 0x00, size + 1);
73     if (memcpy_s(ch, size, data, size) != EOK) {
74         free(ch);
75         ch = nullptr;
76         return 0;
77     }
78 
79     OHOS::DevStandbyMgr::DoSomethingInterestingWithMyAPI(ch, size);
80     free(ch);
81     ch = nullptr;
82     return 0;
83 }
84