/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "load_fuzzer.h" #include #include #include #include "nweb.h" #include "nweb_adapter_helper.h" using namespace OHOS::Rosen; namespace OHOS { std::shared_ptr g_nweb = nullptr; sptr g_surface = nullptr; std::unordered_map g_argsMap; const std::string ARG_URL = "--url"; const std::string ARG_DUMP = "--dump-path"; const std::string ARG_FRAME_INFO = "--frame-info"; const std::string ARG_ADD_WEB_ENGINE_ARG = "--add-args"; const std::string ARG_DELETE_WEB_ENGINE_ARG = "--delete-args"; const std::string ARG_MULTI_RENDER_PROCESS = "--multi-renderer-process"; const std::string ARG_NWEB_TEST_MOCK_BUNDLEPATH = "--bundle-installation-dir"; const std::string MOCK_INSTALLATION_DIR = "/data/app/el1/bundle/public/com.ohos.nweb"; const std::string ARG_WIDTH = "--width"; const std::string ARG_HEIGHT = "--height"; bool HasArg(const std::string &arg) { return (!g_argsMap.empty()) && (g_argsMap.find(arg) != g_argsMap.end()); } std::string GetArgValue(const std::string &arg) { if (!HasArg(arg)) { return ""; } return g_argsMap.at(arg); } std::list GetWebEngineArgs(const std::string &arg) { std::string webEngineArgValue = GetArgValue(arg); std::list webEngineArgList; if (webEngineArgValue.empty()) { return webEngineArgList; } uint32_t start = 0; uint32_t pos = 0; while (pos < webEngineArgValue.size()) { if (webEngineArgValue[pos] == ',') { webEngineArgList.emplace_back(webEngineArgValue.substr(start, pos - start)); pos++; start = pos; } else { pos++; } } webEngineArgList.emplace_back(webEngineArgValue.substr(start, pos - start)); webEngineArgList.emplace_back(ARG_NWEB_TEST_MOCK_BUNDLEPATH + "=" + MOCK_INSTALLATION_DIR); return webEngineArgList; } OHOS::NWeb::NWebInitArgs GetInitArgs() { OHOS::NWeb::NWebInitArgs initArgs = { .dump_path = GetArgValue(ARG_DUMP), .frame_info_dump = HasArg(ARG_FRAME_INFO) ? true : false, .web_engine_args_to_add = GetWebEngineArgs(ARG_ADD_WEB_ENGINE_ARG), .web_engine_args_to_delete = GetWebEngineArgs(ARG_DELETE_WEB_ENGINE_ARG), .multi_renderer_process = HasArg(ARG_MULTI_RENDER_PROCESS) ? true : false, }; return initArgs; } bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size) { if ((data == nullptr) || (size == 0)) { return true; } if (!g_surface) { RSSurfaceNodeConfig config; config.SurfaceNodeName = "webTestSurfaceName"; auto surfaceNode = RSSurfaceNode::Create(config, false); if (surfaceNode == nullptr) { return false; } g_surface = surfaceNode->GetSurface(); if (g_surface== nullptr) { return false; } } g_nweb = NWeb::NWebAdapterHelper::Instance().CreateNWeb(g_surface, GetInitArgs()); if (g_nweb == nullptr) { return true; } std::string url((const char *)data, size); g_nweb->Load(url); return true; } } /* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { /* Run your code on data */ OHOS::DoSomethingInterestingWithMyAPI(data, size); return 0; }