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