• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "arknativeengine_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 
RunBufferScript(std::vector<uint8_t> & buffer)37     void RunBufferScript(std::vector<uint8_t>& buffer)
38     {
39         arkNativeEngine_->RunBufferScript(buffer);
40     }
41 
RunScriptPath(const char * path)42     void RunScriptPath(const char* path)
43     {
44         arkNativeEngine_->RunScriptPath(path);
45     }
46 
~Engine()47     ~Engine()
48     {
49         delete arkNativeEngine_;
50         JSNApi::DestroyJSVM(vm_);
51     }
52 private:
53     EcmaVM* vm_ {nullptr};
54     ArkNativeEngine* arkNativeEngine_ {nullptr};
55 };
56 
57 static Engine g_nativeEngine;
58 
59 namespace OHOS {
ArkNativeEngineFuzzTest(const uint8_t * data,size_t size)60     void ArkNativeEngineFuzzTest(const uint8_t* data, size_t size)
61     {
62         if (size <= 0) {
63             return;
64         }
65         double input = 0;
66         if (size > MAXBYTELEN) {
67             size = MAXBYTELEN;
68         }
69         if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
70             std::cout << "memcpy_s failed!" << std::endl;
71             UNREACHABLE();
72         }
73 
74         std::string result(reinterpret_cast<const char*>(data), size);
75         g_nativeEngine.RunScriptPath(result.c_str());
76         std::vector<uint8_t> vec(size, *data);
77         g_nativeEngine.RunBufferScript(vec);
78     }
79 }
80 
81 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)82 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
83 {
84     // Run your code on data.
85     OHOS::ArkNativeEngineFuzzTest(data, size);
86     return 0;
87 }