1 /*
2  * Copyright (c) 2024 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 "pasteboardobserver_fuzzer.h"
17 
18 #include "message_parcel.h"
19 
20 #include <map>
21 
22 #include "pasteboard_error.h"
23 #include "pasteboard_observer.h"
24 #include "pasteboard_serv_ipc_interface_code.h"
25 
26 namespace OHOS {
27 using namespace OHOS::Security::PasteboardServ;
28 using namespace OHOS::MiscServices;
29 constexpr size_t THRESHOLD = 2;
30 const std::u16string PASTEBOARDSERVICE_INTERFACE_TOKEN = u"ohos.miscservices.pasteboard.IPasteboardService";
31 
32 template<class T>
TypeCast(const uint8_t * data,int * pos=nullptr)33 T TypeCast(const uint8_t *data, int *pos = nullptr)
34 {
35     if (pos) {
36         *pos += sizeof(T);
37     }
38     return *(reinterpret_cast<const T*>(data));
39 }
40 
FuzzPasteboardObserver(const uint8_t * rawData,size_t size)41 bool FuzzPasteboardObserver(const uint8_t *rawData, size_t size)
42 {
43     if (rawData == nullptr || size < sizeof(uint32_t)) {
44         return true;
45     }
46     uint32_t code = TypeCast<uint32_t>(rawData);
47     MessageParcel data;
48     data.WriteInterfaceToken(PASTEBOARDSERVICE_INTERFACE_TOKEN);
49     MessageParcel reply;
50     MessageOption option;
51     std::make_shared<PasteboardObserver>()->OnRemoteRequest(
52         static_cast<uint32_t>(code % THRESHOLD), data, reply, option);
53     return true;
54 }
55 
FuzzPasteboardObserverOnPasteboardChangedStub(const uint8_t * rawData,size_t size)56 bool FuzzPasteboardObserverOnPasteboardChangedStub(const uint8_t *rawData, size_t size)
57 {
58     MessageParcel data;
59     data.WriteInterfaceToken(PasteboardObserverStub::GetDescriptor());
60     MessageParcel reply;
61     MessageOption option;
62     std::make_shared<PasteboardObserver>()->OnRemoteRequest(
63         static_cast<uint32_t>(PasteboardObserverInterfaceCode::ON_PASTE_BOARD_CHANGE), data, reply, option);
64     return true;
65 }
66 
FuzzPasteboardObserverOnPasteboardEventStub(const uint8_t * rawData,size_t size)67 bool FuzzPasteboardObserverOnPasteboardEventStub(const uint8_t *rawData, size_t size)
68 {
69     if (rawData == nullptr || size < sizeof(int32_t)) {
70         return true;
71     }
72 
73     MessageParcel data;
74     data.WriteInterfaceToken(PasteboardObserverStub::GetDescriptor());
75     MessageParcel reply;
76     MessageOption option;
77     int pos = 0;
78     int32_t status = TypeCast<int32_t>(rawData, &pos);
79     data.WriteString(std::string(reinterpret_cast<const char*>(rawData + pos), size - pos));
80     data.WriteInt32(status);
81     std::make_shared<PasteboardObserver>()->OnRemoteRequest(
82         static_cast<uint32_t>(PasteboardObserverInterfaceCode::ON_PASTE_BOARD_EVENT), data, reply, option);
83     return true;
84 }
85 } // namespace OHOS
86 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)87 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
88 {
89     /* Run your code on data */
90     OHOS::FuzzPasteboardObserver(data, size);
91     OHOS::FuzzPasteboardObserverOnPasteboardChangedStub(data, size);
92     OHOS::FuzzPasteboardObserverOnPasteboardEventStub(data, size);
93     return 0;
94 }