• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "devicestatusstub_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "securec.h"
22 #include "singleton.h"
23 
24 #define private public
25 #include "devicestatus_service.h"
26 #include "message_parcel.h"
27 
28 using namespace OHOS::Msdp::DeviceStatus;
29 
30 namespace OHOS {
31 constexpr size_t FOO_MAX_LEN { 1024 };
32 constexpr size_t MIN_SIZE { 4 };
33 const std::u16string FORMMGR_DEVICE_TOKEN { u"ohos.msdp.Idevicestatus" };
34 
GetU32Data(const char * ch,size_t size)35 uint32_t GetU32Data(const char* ch, size_t size)
36 {
37     if (ch == nullptr) {
38         return 0;
39     }
40     if (size < MIN_SIZE) {
41         return 0;
42     }
43     // 将第0个数字左移24位,将第1个数字左移16位,将第2个数字左移8位,第3个数字不左移
44     return (ch[0] << 24) | (ch[1] << 16) | (ch[2] << 8) | (ch[3]);
45 }
46 
DoSomethingWithMyAPI(const char * data,size_t size)47 bool DoSomethingWithMyAPI(const char* data, size_t size)
48 {
49     uint32_t code = GetU32Data(data, size);
50     MessageParcel datas;
51     datas.WriteInterfaceToken(FORMMGR_DEVICE_TOKEN);
52     datas.WriteBuffer(data, size);
53     datas.RewindRead(0);
54     MessageParcel reply;
55     MessageOption option;
56     DelayedSingleton<DeviceStatusService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
57     return true;
58 }
59 }
60 
61 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)62 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
63 {
64     /* Run your code on data */
65     if (data == nullptr) {
66         return 0;
67     }
68 
69     /* Validate the length of size */
70     if ((size < OHOS::MIN_SIZE) || (size > OHOS::FOO_MAX_LEN)) {
71         return 0;
72     }
73     const char* ch = reinterpret_cast<const char*>(data);
74     OHOS::DoSomethingWithMyAPI(ch, size);
75     return 0;
76 }
77 
78