1 /*
2  * Copyright (c) 2021-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  * miscservices under the License is miscservices 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 "screenlockdump_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <string_ex.h>
21 
22 #include "message_parcel.h"
23 #include "screenlock_callback.h"
24 #include "screenlock_manager_interface.h"
25 #include "screenlock_system_ability.h"
26 
27 using namespace OHOS::ScreenLock;
28 using namespace OHOS::Rosen;
29 
30 namespace OHOS {
31 constexpr int32_t THRESHOLD = 4;
32 const std::u16string SCREENLOCK_SYSTEMABILITY_INTERFACE_TOKEN = u"OHOS.ScreenLock.ScreenLockSystemAbilityInterface";
33 
ConvertToUint32(const uint8_t * ptr)34 uint32_t ConvertToUint32(const uint8_t *ptr)
35 {
36     if (ptr == nullptr) {
37         return 0;
38     }
39     uint32_t bigvar = (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
40     return bigvar;
41 }
42 
FuzzScreenlockCallback(const uint8_t * rawData,size_t size)43 bool FuzzScreenlockCallback(const uint8_t *rawData, size_t size)
44 {
45     uint32_t code = ConvertToUint32(rawData);
46 
47     EventListener mEventListener;
48     MessageParcel data;
49     data.WriteInterfaceToken(SCREENLOCK_SYSTEMABILITY_INTERFACE_TOKEN);
50     data.WriteBuffer(rawData, size);
51     data.RewindRead(0);
52     MessageParcel reply;
53     MessageOption option;
54 
55     sptr<ScreenlockCallback> mScreenlock = new ScreenlockCallback(mEventListener);
56     mScreenlock->OnRemoteRequest(code, data, reply, option);
57 
58     return true;
59 }
60 
FuzzScreenlockDisplayPowerEvent(const uint8_t * rawData,size_t size)61 bool FuzzScreenlockDisplayPowerEvent(const uint8_t *rawData, size_t size)
62 {
63     uint32_t event = ConvertToUint32(rawData);
64     uint32_t status = ConvertToUint32(rawData);
65 
66     sptr<ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener> displayPowerEventListener_;
67     displayPowerEventListener_ = new ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener();
68     displayPowerEventListener_->OnDisplayPowerEvent(
69         static_cast<DisplayPowerEvent>(event), static_cast<EventStatus>(status));
70     return true;
71 }
72 
FuzzScreenlockDump(const uint8_t * rawData,size_t size)73 bool FuzzScreenlockDump(const uint8_t *rawData, size_t size)
74 {
75     std::vector<std::u16string> args;
76     std::string str(reinterpret_cast<const char *>(rawData), size);
77     args.push_back(Str8ToStr16(str));
78     int fd = 0;
79     ScreenLockSystemAbility::GetInstance()->Dump(fd, args);
80     return true;
81 }
82 } // namespace OHOS
83 
84 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)85 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
86 {
87     if (size < OHOS::THRESHOLD) {
88         return 0;
89     }
90 
91     /* Run your code on data */
92     OHOS::FuzzScreenlockCallback(data, size);
93     OHOS::FuzzScreenlockDisplayPowerEvent(data, size);
94     OHOS::FuzzScreenlockDump(data, size);
95     ScreenLockSystemAbility::GetInstance()->ResetFfrtQueue();
96     return 0;
97 }