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 "runscriptbuffer_fuzzer.h"
17 #include "native_engine/impl/ark/ark_native_engine.h"
18 #include "securec.h"
19
20 using namespace panda;
21 using namespace panda::ecmascript;
22 using panda::RuntimeOption;
23
24
25 #define MAXBYTELEN sizeof(uint32_t)
26
27 class Engine {
28 public:
Engine()29 Engine()
30 {
31 RuntimeOption option;
32 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
33 vm_ = JSNApi::CreateJSVM(option);
34 arkNativeEngine_ = new ArkNativeEngine(vm_, nullptr);
35 }
36
RunScriptBuffer(const char * path,std::vector<uint8_t> & buffer,bool isBundle)37 void RunScriptBuffer(const char* path, std::vector<uint8_t>& buffer, bool isBundle)
38 {
39 arkNativeEngine_->RunScriptBuffer(path, buffer, isBundle);
40 }
41
RunScriptBuffer(const std::string & path,uint8_t * buffer,size_t size,bool isBundle)42 void RunScriptBuffer(const std::string& path, uint8_t* buffer, size_t size, bool isBundle)
43 {
44 arkNativeEngine_->RunScriptBuffer(path, buffer, size, isBundle);
45 }
46
~Engine()47 ~Engine()
48 {
49 if (arkNativeEngine_ != nullptr) {
50 delete arkNativeEngine_;
51 arkNativeEngine_ = nullptr;
52 }
53 if (vm_ != nullptr) {
54 JSNApi::DestroyJSVM(vm_);
55 vm_ = nullptr;
56 }
57 }
58 private:
59 EcmaVM* vm_ {nullptr};
60 ArkNativeEngine* arkNativeEngine_ {nullptr};
61 };
62
63 static Engine g_nativeEngine;
64
65 namespace OHOS {
RunScriptBufferFuzzTest(const uint8_t * data,size_t size)66 void RunScriptBufferFuzzTest(const uint8_t* data, size_t size)
67 {
68 const std::string path = "test.abc";
69 if (size <= 0) {
70 return;
71 }
72 double input = 0;
73 if (size > MAXBYTELEN) {
74 size = MAXBYTELEN;
75 }
76 if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
77 std::cout << "memcpy_s failed!" << std::endl;
78 UNREACHABLE();
79 }
80
81 std::vector<uint8_t> vec(size, *data);
82 g_nativeEngine.RunScriptBuffer(path.c_str(), vec, true);
83 g_nativeEngine.RunScriptBuffer(path.c_str(), vec, false);
84 uint8_t* buffer = const_cast<uint8_t*>(data);
85 g_nativeEngine.RunScriptBuffer(path, buffer, size, true);
86 g_nativeEngine.RunScriptBuffer(path, buffer, size, false);
87 }
88 }
89
90 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
92 {
93 // Run your code on data.
94 OHOS::RunScriptBufferFuzzTest(data, size);
95 return 0;
96 }