1 /*
2 * Copyright (c) 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 * 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 "createnweb_fuzzer.h"
17
18 #include <cstring>
19 #include <securec.h>
20 #include <ui/rs_surface_node.h>
21
22 #include "nweb.h"
23 #include "nweb_adapter_helper.h"
24 #include "nweb_handler_impl_test.h"
25
26 #define private public
27 #include "nweb_surface_adapter.h"
28 #include "window.h"
29
30 using namespace OHOS::NWeb;
31 using namespace OHOS::Rosen;
32
33 namespace OHOS {
34 namespace {
35 std::unordered_map<std::string, std::string> g_argsMap;
36 const std::string ARG_URL = "--url";
37 const std::string ARG_DUMP = "--dump-path";
38 const std::string ARG_FRAME_INFO = "--frame-info";
39 const std::string ARG_ADD_WEB_ENGINE_ARG = "--add-args";
40 const std::string ARG_DELETE_WEB_ENGINE_ARG = "--delete-args";
41 const std::string ARG_MULTI_RENDER_PROCESS = "--multi-renderer-process";
42 const std::string ARG_NWEB_TEST_MOCK_BUNDLEPATH = "--bundle-installation-dir";
43 const std::string MOCK_INSTALLATION_DIR = "/data/app/el1/bundle/public/com.ohos.nweb";
44 const std::string ARG_WIDTH = "--width";
45 const std::string ARG_HEIGHT = "--height";
46 const int DEFAULT_WIDTH = 2560;
47 const int DEFAULT_HEIGHT = 1396;
48 }
49
HasArg(const std::string & arg)50 bool HasArg(const std::string &arg)
51 {
52 return (!g_argsMap.empty()) && (g_argsMap.find(arg) != g_argsMap.end());
53 }
54
GetArgValue(const std::string & arg)55 std::string GetArgValue(const std::string &arg)
56 {
57 if (!HasArg(arg)) {
58 return "";
59 }
60 return g_argsMap.at(arg);
61 }
62
GetNumFromArgs(const std::string & arg)63 uint32_t GetNumFromArgs(const std::string &arg)
64 {
65 if (!HasArg(arg)) {
66 return 0;
67 }
68 return std::stoi(GetArgValue(arg));
69 }
70
GetWebEngineArgs(const std::string & arg)71 std::list<std::string> GetWebEngineArgs(const std::string &arg)
72 {
73 std::string webEngineArgValue = GetArgValue(arg);
74 std::list<std::string> webEngineArgList;
75 if (webEngineArgValue.empty()) {
76 return webEngineArgList;
77 }
78 uint32_t start = 0;
79 uint32_t pos = 0;
80 while (pos < webEngineArgValue.size()) {
81 if (webEngineArgValue[pos] == ',') {
82 webEngineArgList.emplace_back(webEngineArgValue.substr(start, pos - start));
83 pos++;
84 start = pos;
85 } else {
86 pos++;
87 }
88 }
89 webEngineArgList.emplace_back(webEngineArgValue.substr(start, pos - start));
90 webEngineArgList.emplace_back(ARG_NWEB_TEST_MOCK_BUNDLEPATH + "=" + MOCK_INSTALLATION_DIR);
91 return webEngineArgList;
92 }
93
GetInitArgs()94 OHOS::NWeb::NWebInitArgs GetInitArgs()
95 {
96 OHOS::NWeb::NWebInitArgs initArgs = {
97 .dump_path = GetArgValue(ARG_DUMP),
98 .frame_info_dump = HasArg(ARG_FRAME_INFO) ? true : false,
99 .web_engine_args_to_add = GetWebEngineArgs(ARG_ADD_WEB_ENGINE_ARG),
100 .web_engine_args_to_delete = GetWebEngineArgs(ARG_DELETE_WEB_ENGINE_ARG),
101 .multi_renderer_process = HasArg(ARG_MULTI_RENDER_PROCESS) ? true : false,
102 };
103 return initArgs;
104 }
105
CreateWindow()106 sptr<Rosen::Window> CreateWindow()
107 {
108 sptr<Rosen::WindowOption> option = new Rosen::WindowOption();
109 if (option == nullptr) {
110 return nullptr;
111 }
112 int width = HasArg(ARG_WIDTH) ? GetNumFromArgs(ARG_WIDTH) : DEFAULT_WIDTH;
113 int height = HasArg(ARG_HEIGHT) ? GetNumFromArgs(ARG_HEIGHT) : DEFAULT_HEIGHT;
114 option->SetWindowRect({0, 0, width, height});
115 auto window = Rosen::Window::Create("nweb_test_window", option);
116 return window;
117 }
118
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)119 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
120 {
121 if ((data == nullptr) || (size == 0)) {
122 return false;
123 }
124 sptr<Rosen::Window> window = CreateWindow();
125 if (window == nullptr) {
126 return false;
127 }
128 auto nweb = NWebAdapterHelper::Instance();
129 nweb.CreateNWeb(window.GetRefPtr(), GetInitArgs());
130 return true;
131 }
132 }
133
134 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)135 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
136 {
137 /* Run your code on data */
138 OHOS::DoSomethingInterestingWithMyAPI(data, size);
139 return 0;
140 }
141