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 "marshalling_helper.h"
17 #include <securec.h>
18 #include <iremote_broker.h>
19 #include "ability_context.h"
20 #include "ability_context_impl.h"
21 #include "pip_fuzzer.h"
22 #include "picture_in_picture_controller.h"
23 #include "picture_in_picture_manager.h"
24 #include "picture_in_picture_option.h"
25 
26 using namespace OHOS::Rosen;
27 
28 namespace OHOS {
29 namespace {
30 constexpr size_t DATA_MIN_SIZE = 2;
31 }
32 
33 template<class T>
GetObject(T & object,const uint8_t * data,size_t size)34 size_t GetObject(T& object, const uint8_t* data, size_t size)
35 {
36     size_t objectSize = sizeof(object);
37     if (objectSize > size) {
38         return 0;
39     }
40     return memcpy_s(&object, objectSize, data, objectSize) == EOK ? objectSize : 0;
41 }
42 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)43 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
44 {
45     if (data == nullptr || size < DATA_MIN_SIZE) {
46         return false;
47     }
48     size_t startPos = 0;
49     sptr<PipOption> option = new PipOption();
50     AbilityRuntime::Context* context = nullptr;
51     startPos += GetObject(context, data + startPos, size - startPos);
52     option->SetContext(static_cast<void*>(context));
53     std::string navigationId = "";
54     option->SetNavigationId(navigationId);
55     std::shared_ptr<XComponentController> xComponentController = nullptr;
56     option->SetXComponentController(xComponentController);
57     uint32_t templateType = 0;
58     startPos += GetObject<uint32_t>(templateType, data + startPos, size - startPos);
59     option->SetPipTemplate(templateType);
60     uint32_t height = 0;
61     startPos += GetObject<uint32_t>(height, data + startPos, size - startPos);
62     uint32_t width = 0;
63     startPos += GetObject<uint32_t>(width, data + startPos, size - startPos);
64     option->SetContentSize(width, height);
65     int32_t windowId;
66     startPos += GetObject(windowId, data + startPos, size - startPos);
67     napi_env env = nullptr;
68     startPos += GetObject(env, data + startPos, size - startPos);
69     sptr<Window> window = new Window();
70     sptr<PictureInPictureController> controller = new PictureInPictureController(option, window, windowId, env);
71     if (controller == nullptr) {
72         return false;
73     }
74     bool boolVal = false;
75     startPos += GetObject(boolVal, data + startPos, size - startPos);
76     controller->SetAutoStartEnabled(boolVal);
77     uint32_t w = 0;
78     startPos += GetObject<uint32_t>(w, data + startPos, size - startPos);
79     uint32_t h = 0;
80     startPos += GetObject<uint32_t>(h, data + startPos, size - startPos);
81     controller->UpdateContentSize(w, h);
82     return true;
83 }
84 } // namespace.OHOS
85 
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::DoSomethingInterestingWithMyAPI(data, size);
91     return 0;
92 }