• 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 "templateclassglobal_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 {
TemplateGlobalFuzzerTest(const uint8_t * data,size_t size)25     void TemplateGlobalFuzzerTest([[maybe_unused]]const uint8_t* data, size_t size)
26     {
27         RuntimeOption option;
28         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
29         EcmaVM *vm = JSNApi::CreateJSVM(option);
30         if (size <= 0) {
31             LOG_ECMA(ERROR) << "illegal input!";
32             return;
33         }
34         Local<BooleanRef> current = BooleanRef::New(vm, true);
35         [[maybe_unused]]Global<JSValueRef> global(vm, current);
36         JSNApi::DestroyJSVM(vm);
37     }
38 
TemplateToLocalFuzzerTest(const uint8_t * data,size_t size)39     void TemplateToLocalFuzzerTest([[maybe_unused]]const uint8_t* data, size_t size)
40     {
41         RuntimeOption option;
42         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
43         EcmaVM *vm = JSNApi::CreateJSVM(option);
44         if (size <= 0) {
45             LOG_ECMA(ERROR) << "illegal input!";
46             return;
47         }
48         Global<BooleanRef> global(vm, BooleanRef::New(vm, true));
49         [[maybe_unused]]Local<JSValueRef> local = global.ToLocal();
50         JSNApi::DestroyJSVM(vm);
51     }
52 
TemplateToLocalFromVMFuzzerTest(const uint8_t * data,size_t size)53     void TemplateToLocalFromVMFuzzerTest([[maybe_unused]]const uint8_t* data, size_t size)
54     {
55         RuntimeOption option;
56         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
57         EcmaVM *vm = JSNApi::CreateJSVM(option);
58         if (size <= 0) {
59             LOG_ECMA(ERROR) << "illegal input!";
60             return;
61         }
62         Global<BooleanRef> global(vm, BooleanRef::New(vm, true));
63         [[maybe_unused]]Local<JSValueRef> local = global.ToLocal(vm);
64         JSNApi::DestroyJSVM(vm);
65     }
66 
TemplateEmptyFuzzerTest(const uint8_t * data,size_t size)67     void TemplateEmptyFuzzerTest([[maybe_unused]]const uint8_t* data, size_t size)
68     {
69         RuntimeOption option;
70         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
71         EcmaVM *vm = JSNApi::CreateJSVM(option);
72         if (size <= 0) {
73             LOG_ECMA(ERROR) << "illegal input!";
74             return;
75         }
76         Global<BooleanRef> global(vm, BooleanRef::New(vm, true));
77         global.Empty();
78         JSNApi::DestroyJSVM(vm);
79     }
80 
TemplateFreeGlobalHandleAddrFuzzerTest(const uint8_t * data,size_t size)81     void TemplateFreeGlobalHandleAddrFuzzerTest([[maybe_unused]]const uint8_t* data, size_t size)
82     {
83         RuntimeOption option;
84         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
85         EcmaVM *vm = JSNApi::CreateJSVM(option);
86         if (size <= 0) {
87             LOG_ECMA(ERROR) << "illegal input!";
88             return;
89         }
90         Global<BooleanRef> global(vm, BooleanRef::New(vm, true));
91         global.FreeGlobalHandleAddr();
92         JSNApi::DestroyJSVM(vm);
93     }
94 
GlobalOperatorStarFuzzerTest(const uint8_t * data,size_t size)95     void GlobalOperatorStarFuzzerTest([[maybe_unused]]const uint8_t* data, size_t size)
96     {
97         RuntimeOption option;
98         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
99         EcmaVM *vm = JSNApi::CreateJSVM(option);
100         if (size <= 0) {
101             LOG_ECMA(ERROR) << "illegal input!";
102             return;
103         }
104         Global<BooleanRef> global(vm, BooleanRef::New(vm, true));
105         [[maybe_unused]]bool b = (*global)->BooleaValue(vm);
106         JSNApi::DestroyJSVM(vm);
107     }
108 
GlobalOperatorPointToFuzzerTest(const uint8_t * data,size_t size)109     void GlobalOperatorPointToFuzzerTest([[maybe_unused]]const uint8_t* data, size_t size)
110     {
111         RuntimeOption option;
112         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
113         EcmaVM *vm = JSNApi::CreateJSVM(option);
114         if (size <= 0) {
115             LOG_ECMA(ERROR) << "illegal input!";
116             return;
117         }
118         Global<BooleanRef> global(vm, BooleanRef::New(vm, true));
119         [[maybe_unused]]bool b = global->BooleaValue(vm);
120         JSNApi::DestroyJSVM(vm);
121     }
122 
GlobalIsEmptyFuzzerTest(const uint8_t * data,size_t size)123     void GlobalIsEmptyFuzzerTest([[maybe_unused]]const uint8_t* data, size_t size)
124     {
125         RuntimeOption option;
126         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
127         EcmaVM *vm = JSNApi::CreateJSVM(option);
128         if (size <= 0) {
129             LOG_ECMA(ERROR) << "illegal input!";
130             return;
131         }
132         Global<JSValueRef> global;
133         [[maybe_unused]]bool b = global.IsEmpty();
134         JSNApi::DestroyJSVM(vm);
135     }
136 
GlobalSetWeakFuzzerTest(const uint8_t * data,size_t size)137     void GlobalSetWeakFuzzerTest([[maybe_unused]]const uint8_t* data, size_t size)
138     {
139         RuntimeOption option;
140         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
141         EcmaVM *vm = JSNApi::CreateJSVM(option);
142         if (size <= 0) {
143             LOG_ECMA(ERROR) << "illegal input!";
144             return;
145         }
146         Global<JSValueRef> global(vm, BooleanRef::New(vm, true));
147         global.SetWeak();
148         bool res = global.IsWeak();
149         if (!res) {
150             LOG_ECMA(ERROR) << "SetWeak failed";
151             return;
152         }
153 
154         JSNApi::DestroyJSVM(vm);
155     }
156 
GlobalClearWeakFuzzerTest(const uint8_t * data,size_t size)157     void GlobalClearWeakFuzzerTest([[maybe_unused]]const uint8_t* data, size_t size)
158     {
159         RuntimeOption option;
160         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
161         EcmaVM *vm = JSNApi::CreateJSVM(option);
162         if (size <= 0) {
163             LOG_ECMA(ERROR) << "illegal input!";
164             return;
165         }
166         Global<JSValueRef> global(vm, BooleanRef::New(vm, true));
167         global.ClearWeak();
168         JSNApi::DestroyJSVM(vm);
169     }
170 
GlobalIsWeakFuzzerTest(const uint8_t * data,size_t size)171     void GlobalIsWeakFuzzerTest([[maybe_unused]]const uint8_t* data, size_t size)
172     {
173         RuntimeOption option;
174         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
175         EcmaVM *vm = JSNApi::CreateJSVM(option);
176         if (size <= 0) {
177             LOG_ECMA(ERROR) << "illegal input!";
178             return;
179         }
180         Global<JSValueRef> global(vm, BooleanRef::New(vm, true));
181         global.SetWeak();
182         [[maybe_unused]]bool b = global.IsWeak();
183         JSNApi::DestroyJSVM(vm);
184     }
185 }
186 
187 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)188 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
189 {
190     // Run your code on data.
191     OHOS::TemplateGlobalFuzzerTest(data, size);
192     OHOS::TemplateToLocalFuzzerTest(data, size);
193     OHOS::TemplateToLocalFromVMFuzzerTest(data, size);
194     OHOS::TemplateEmptyFuzzerTest(data, size);
195     OHOS::TemplateFreeGlobalHandleAddrFuzzerTest(data, size);
196     OHOS::GlobalOperatorStarFuzzerTest(data, size);
197     OHOS::GlobalOperatorPointToFuzzerTest(data, size);
198     OHOS::GlobalIsEmptyFuzzerTest(data, size);
199     OHOS::GlobalSetWeakFuzzerTest(data, size);
200     OHOS::GlobalClearWeakFuzzerTest(data, size);
201     OHOS::GlobalIsWeakFuzzerTest(data, size);
202     return 0;
203 }