1 /*
2 * Copyright (c) 2023 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 "registerquickfixqueryfunc_fuzzer.h"
17 #include "ecmascript/base/string_helper.h"
18 #include "ecmascript/napi/include/dfx_jsnapi.h"
19 #include "ecmascript/napi/include/jsnapi.h"
20
21 using namespace panda;
22 using namespace panda::ecmascript;
23
24 namespace OHOS {
QuickFixQueryFunc(std::string baseFileName,std::string & patchFileName,void ** patchBuffer,size_t & patchBufferSize)25 bool QuickFixQueryFunc(std::string baseFileName, std::string &patchFileName, void **patchBuffer,
26 size_t &patchBufferSize)
27 {
28 if (baseFileName != "multi_file/base/merge.abc") {
29 return false;
30 }
31 patchFileName = "__index.pa";
32 const char *data = R"(
33 .function void foo() {}
34 )";
35 char *bufferData = new char[strlen(data) + 1];
36 size_t dataSize = strlen(data) + 1;
37 strcpy_s(bufferData, dataSize, data);
38 *patchBuffer = reinterpret_cast<void *>(bufferData);
39 patchBufferSize = strlen(data);
40 return true;
41 }
RegisterQuickFixQueryFuncFuzzerTest(const uint8_t * data,size_t size)42 void RegisterQuickFixQueryFuncFuzzerTest([[maybe_unused]]const uint8_t* data, size_t size)
43 {
44 RuntimeOption option;
45 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
46 EcmaVM *vm = JSNApi::CreateJSVM(option);
47 if (size <= 0) {
48 LOG_ECMA(ERROR) << "illegal input!";
49 return;
50 }
51 JSNApi::RegisterQuickFixQueryFunc(vm, QuickFixQueryFunc);
52 JSNApi::DestroyJSVM(vm);
53 }
54 }
55
56 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)57 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
58 {
59 // Run your code on data.
60 OHOS::RegisterQuickFixQueryFuncFuzzerTest(data, size);
61 return 0;
62 }