1e509ee18Sopenharmony_ci/* 2e509ee18Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3e509ee18Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4e509ee18Sopenharmony_ci * you may not use this file except in compliance with the License. 5e509ee18Sopenharmony_ci * You may obtain a copy of the License at 6e509ee18Sopenharmony_ci * 7e509ee18Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8e509ee18Sopenharmony_ci * 9e509ee18Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10e509ee18Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11e509ee18Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12e509ee18Sopenharmony_ci * See the License for the specific language governing permissions and 13e509ee18Sopenharmony_ci * limitations under the License. 14e509ee18Sopenharmony_ci */ 15e509ee18Sopenharmony_ci 16e509ee18Sopenharmony_ci#include "backendloadmodule_fuzzer.h" 17e509ee18Sopenharmony_ci#include "ecmascript/napi/include/jsnapi.h" 18e509ee18Sopenharmony_ci#include "agent/debugger_impl.h" 19e509ee18Sopenharmony_ci#include "tooling/backend/js_pt_hooks.h" 20e509ee18Sopenharmony_ci 21e509ee18Sopenharmony_ciusing namespace panda; 22e509ee18Sopenharmony_ciusing namespace panda::ecmascript; 23e509ee18Sopenharmony_ciusing namespace panda::ecmascript::tooling; 24e509ee18Sopenharmony_ci 25e509ee18Sopenharmony_ci#define MAXBYTELEN sizeof(int32_t) 26e509ee18Sopenharmony_ci 27e509ee18Sopenharmony_cinamespace OHOS { 28e509ee18Sopenharmony_ci void BackendLoadModuleFuzzTest(const uint8_t* data, size_t size) 29e509ee18Sopenharmony_ci { 30e509ee18Sopenharmony_ci int32_t input = 0; 31e509ee18Sopenharmony_ci RuntimeOption option; 32e509ee18Sopenharmony_ci option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 33e509ee18Sopenharmony_ci auto vm = JSNApi::CreateJSVM(option); 34e509ee18Sopenharmony_ci { 35e509ee18Sopenharmony_ci if (size <= 0) { 36e509ee18Sopenharmony_ci return; 37e509ee18Sopenharmony_ci } 38e509ee18Sopenharmony_ci if (size > MAXBYTELEN) { 39e509ee18Sopenharmony_ci size = MAXBYTELEN; 40e509ee18Sopenharmony_ci } 41e509ee18Sopenharmony_ci if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) { 42e509ee18Sopenharmony_ci std::cout << "memcpy_s failed!"; 43e509ee18Sopenharmony_ci UNREACHABLE(); 44e509ee18Sopenharmony_ci } 45e509ee18Sopenharmony_ci const int32_t MaxMemory = 1073741824; 46e509ee18Sopenharmony_ci if (input > MaxMemory) { 47e509ee18Sopenharmony_ci input = MaxMemory; 48e509ee18Sopenharmony_ci } 49e509ee18Sopenharmony_ci using JSPtLocation = tooling::JSPtLocation; 50e509ee18Sopenharmony_ci EntityId methodId(input); 51e509ee18Sopenharmony_ci uint32_t bytecodeOffset = 0; 52e509ee18Sopenharmony_ci auto debugger = std::make_unique<DebuggerImpl>(vm, nullptr, nullptr); 53e509ee18Sopenharmony_ci std::unique_ptr<JSPtHooks> jspthooks = std::make_unique<JSPtHooks>(debugger.get()); 54e509ee18Sopenharmony_ci JSPtLocation ptLocation1(nullptr, methodId, bytecodeOffset); 55e509ee18Sopenharmony_ci jspthooks->LoadModule("pandafile/test.abc", "func_main_0"); 56e509ee18Sopenharmony_ci } 57e509ee18Sopenharmony_ci JSNApi::DestroyJSVM(vm); 58e509ee18Sopenharmony_ci } 59e509ee18Sopenharmony_ci} 60e509ee18Sopenharmony_ci 61e509ee18Sopenharmony_ci// Fuzzer entry point. 62e509ee18Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 63e509ee18Sopenharmony_ci{ 64e509ee18Sopenharmony_ci // Run your code on data. 65e509ee18Sopenharmony_ci OHOS::BackendLoadModuleFuzzTest(data, size); 66e509ee18Sopenharmony_ci return 0; 67e509ee18Sopenharmony_ci}