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 "backendsinglestep_fuzzer.h"
17 #include "ecmascript/napi/include/jsnapi.h"
18 #include "agent/debugger_impl.h"
19 #include "tooling/backend/js_pt_hooks.h"
20 
21 using namespace panda;
22 using namespace panda::ecmascript;
23 using namespace panda::ecmascript::tooling;
24 
25 #define MAXBYTELEN sizeof(int32_t)
26 
27 namespace OHOS {
BackendSingleStepFuzzTest(const uint8_t* data, size_t size)28     void BackendSingleStepFuzzTest(const uint8_t* data, size_t size)
29     {
30         int32_t input = 0;
31         RuntimeOption option;
32         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
33         auto vm = JSNApi::CreateJSVM(option);
34         {
35             if (size <= 0) {
36                 return;
37             }
38             if (size > MAXBYTELEN) {
39                 size = MAXBYTELEN;
40             }
41             if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
42                 std::cout << "memcpy_s failed!";
43                 UNREACHABLE();
44             }
45             const int32_t MaxMemory = 1073741824;
46             if (input > MaxMemory) {
47                 input = MaxMemory;
48             }
49             using JSPtLocation = tooling::JSPtLocation;
50             EntityId methodId(input);
51             uint32_t bytecodeOffset = 0;
52             auto debugger = std::make_unique<DebuggerImpl>(vm, nullptr, nullptr);
53             std::unique_ptr<JSPtHooks> jspthooks = std::make_unique<JSPtHooks>(debugger.get());
54             JSPtLocation ptLocation1(nullptr, methodId, bytecodeOffset);
55             jspthooks->SingleStep(ptLocation1);
56         }
57         JSNApi::DestroyJSVM(vm);
58     }
59 }
60 
61 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)62 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
63 {
64     // Run your code on data.
65     OHOS::BackendSingleStepFuzzTest(data, size);
66     return 0;
67 }