• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "executejavascriptcode_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 "nweb_value_callback.h"
32 #include "nweb_web_message.h"
33 
34 using namespace OHOS::Rosen;
35 
36 namespace OHOS {
37     std::shared_ptr<OHOS::NWeb::NWeb> g_nweb = nullptr;
38     sptr<Surface> g_surface = nullptr;
39     std::unordered_map<std::string, std::string> g_argsMap;
40     const std::string ARG_URL = "--url";
41     const std::string ARG_DUMP = "--dump-path";
42     const std::string ARG_FRAME_INFO = "--frame-info";
43     const std::string ARG_ADD_WEB_ENGINE_ARG = "--add-args";
44     const std::string ARG_DELETE_WEB_ENGINE_ARG = "--delete-args";
45     const std::string ARG_MULTI_RENDER_PROCESS = "--multi-renderer-process";
46     const std::string ARG_NWEB_TEST_MOCK_BUNDLEPATH = "--bundle-installation-dir";
47     const std::string MOCK_INSTALLATION_DIR = "/data/app/el1/bundle/public/com.ohos.nweb";
48     const std::string ARG_WIDTH = "--width";
49     const std::string ARG_HEIGHT = "--height";
50 
HasArg(const std::string & arg)51     bool HasArg(const std::string &arg)
52     {
53         return (!g_argsMap.empty()) && (g_argsMap.find(arg) != g_argsMap.end());
54     }
55 
GetArgValue(const std::string & arg)56     std::string GetArgValue(const std::string &arg)
57     {
58         if (!HasArg(arg)) {
59             return "";
60         }
61         return g_argsMap.at(arg);
62     }
63 
GetWebEngineArgs(const std::string & arg)64     std::list<std::string> GetWebEngineArgs(const std::string &arg)
65     {
66         std::string webEngineArgValue = GetArgValue(arg);
67         std::list<std::string> webEngineArgList;
68         if (webEngineArgValue.empty()) {
69             return webEngineArgList;
70         }
71         uint32_t start = 0;
72         uint32_t pos = 0;
73         while (pos < webEngineArgValue.size()) {
74             if (webEngineArgValue[pos] == ',') {
75                 webEngineArgList.emplace_back(webEngineArgValue.substr(start, pos - start));
76                 pos++;
77                 start = pos;
78             } else {
79                 pos++;
80             }
81         }
82         webEngineArgList.emplace_back(webEngineArgValue.substr(start, pos - start));
83         webEngineArgList.emplace_back(ARG_NWEB_TEST_MOCK_BUNDLEPATH + "=" + MOCK_INSTALLATION_DIR);
84         return webEngineArgList;
85     }
86 
GetInitArgs()87     OHOS::NWeb::NWebInitArgs GetInitArgs()
88     {
89         OHOS::NWeb::NWebInitArgs initArgs = {
90             .dump_path = GetArgValue(ARG_DUMP),
91             .frame_info_dump = HasArg(ARG_FRAME_INFO) ? true : false,
92             .web_engine_args_to_add = GetWebEngineArgs(ARG_ADD_WEB_ENGINE_ARG),
93             .web_engine_args_to_delete = GetWebEngineArgs(ARG_DELETE_WEB_ENGINE_ARG),
94             .multi_renderer_process = HasArg(ARG_MULTI_RENDER_PROCESS) ? true : false,
95         };
96         return initArgs;
97     }
98 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)99     bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
100     {
101         if ((data == nullptr) || (size == 0)) {
102             return true;
103         }
104         if (!g_surface) {
105             RSSurfaceNodeConfig config;
106             config.SurfaceNodeName = "webTestSurfaceName";
107             auto surfaceNode = RSSurfaceNode::Create(config, false);
108             if (surfaceNode == nullptr) {
109                 return false;
110             }
111             g_surface = surfaceNode->GetSurface();
112             if (g_surface== nullptr) {
113                 return false;
114             }
115         }
116         g_nweb = NWeb::NWebAdapterHelper::Instance().CreateNWeb(g_surface, GetInitArgs());
117         if (g_nweb == nullptr) {
118             return true;
119         }
120         std::string code((const char *)data, size);
121         std::shared_ptr<NWeb::NWebValueCallback<std::shared_ptr<NWeb::NWebMessage>>> callback;
122         g_nweb->ExecuteJavaScript(code, callback, false);
123         return true;
124     }
125 }
126 
127 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)128 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
129 {
130     /* Run your code on data */
131     OHOS::DoSomethingInterestingWithMyAPI(data, size);
132     return 0;
133 }
134