• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef CONTAINERSLINKEDLISTCOMMON_FUZZER_H
17 #define CONTAINERSLINKEDLISTCOMMON_FUZZER_H
18 
19 #include "ecmascript/containers/containers_linked_list.h"
20 #include "ecmascript/containers/containers_private.h"
21 #include "ecmascript/ecma_string-inl.h"
22 #include "ecmascript/ecma_vm.h"
23 #include "ecmascript/global_env.h"
24 #include "ecmascript/js_api/js_api_linked_list.h"
25 #include "ecmascript/js_handle.h"
26 #include "ecmascript/napi/include/jsnapi.h"
27 #include "ecmascript/ecma_runtime_call_info.h"
28 #include "ecmascript/js_thread.h"
29 
30 #define MAXBYTELEN sizeof(int)
31 
32 namespace panda::ecmascript {
33 class ContainersLinkedListFuzzTestHelper {
34 public:
35     class TestClass : public base::BuiltinsBase {
36     public:
TestForEachFunc(EcmaRuntimeCallInfo * argv)37         static JSTaggedValue TestForEachFunc(EcmaRuntimeCallInfo *argv)
38         {
39             JSThread *thread = argv->GetThread();
40             JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
41             JSHandle<JSTaggedValue> index = GetCallArg(argv, 1);
42             JSHandle<JSTaggedValue> list = GetCallArg(argv, 2); // 2 means the secode arg
43             if (!list->IsUndefined()) {
44                 if (index->IsNumber() && value->IsNumber()) {
45                     JSHandle<JSTaggedValue> newValue(thread, JSTaggedValue(value->GetInt() * 2)); // 2 means mul by 2
46                     JSAPILinkedList::Set(thread, JSHandle<JSAPILinkedList>::Cast(list), index->GetInt(), newValue);
47                 }
48             }
49             return JSTaggedValue::True();
50         }
51     };
JSObjectCreate(JSThread * thread)52     static JSFunction *JSObjectCreate(JSThread *thread)
53     {
54         EcmaVM *ecmaVM = thread->GetEcmaVM();
55         JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
56         return globalEnv->GetObjectFunction().GetObject<JSFunction>();
57     }
58 
CreateEcmaRuntimeCallInfo(JSThread * thread,uint32_t numArgs)59     static EcmaRuntimeCallInfo *CreateEcmaRuntimeCallInfo(JSThread *thread, uint32_t numArgs)
60     {
61         auto factory = thread->GetEcmaVM()->GetFactory();
62         JSHandle<JSTaggedValue> hclass(thread, JSObjectCreate(thread));
63         JSHandle<JSTaggedValue> callee(factory->NewJSObjectByConstructor(JSHandle<JSFunction>::Cast(hclass), hclass));
64         JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
65         EcmaRuntimeCallInfo *objCallInfo =
66             EcmaInterpreter::NewRuntimeCallInfo(thread, undefined, callee, undefined, numArgs);
67         return objCallInfo;
68     }
69 
CreateJSAPILinkedList(JSThread * thread)70     static JSHandle<JSAPILinkedList> CreateJSAPILinkedList(JSThread *thread)
71     {
72         auto factory = thread->GetEcmaVM()->GetFactory();
73         JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
74         JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
75         JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
76         JSHandle<JSTaggedValue> value =
77             JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
78 
79         auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length
80         objCallInfo->SetFunction(JSTaggedValue::Undefined());
81         objCallInfo->SetThis(value.GetTaggedValue());
82         objCallInfo->SetCallArg(0,
83             JSTaggedValue(static_cast<int>(containers::ContainerTag::LinkedList))); // 0 means the argument
84         JSTaggedValue result = containers::ContainersPrivate::Load(objCallInfo);
85 
86         JSHandle<JSFunction> newTarget(thread, result);
87         auto objCallInfo2 = CreateEcmaRuntimeCallInfo(thread, 6);
88         objCallInfo2->SetFunction(newTarget.GetTaggedValue());
89         objCallInfo2->SetNewTarget(newTarget.GetTaggedValue());
90         objCallInfo2->SetThis(JSTaggedValue::Undefined());
91         objCallInfo2->SetCallArg(0, JSTaggedValue::Undefined());
92 
93         JSTaggedValue list = containers::ContainersLinkedList::LinkedListConstructor(objCallInfo2);
94         JSHandle<JSAPILinkedList> linkedList(thread, list);
95         return linkedList;
96     }
97 
LinkedListAdd(JSHandle<JSAPILinkedList> & linkedList,JSTaggedValue value,JSThread * thread)98     static void LinkedListAdd(JSHandle<JSAPILinkedList> &linkedList, JSTaggedValue value, JSThread *thread)
99     {
100         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
101         callInfo->SetFunction(JSTaggedValue::Undefined());
102         callInfo->SetThis(linkedList.GetTaggedValue());
103         callInfo->SetCallArg(0, value);
104         containers::ContainersLinkedList::Add(callInfo);
105     }
106 
ContainersLinkedListAddFuzzTest(const uint8_t * data,size_t size)107     static void ContainersLinkedListAddFuzzTest(const uint8_t* data, size_t size)
108     {
109         RuntimeOption option;
110         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
111         EcmaVM *vm = JSNApi::CreateJSVM(option);
112         auto thread = vm->GetAssociatedJSThread();
113         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
114         int value = 0;
115         if (size <= 0) {
116             return;
117         }
118         if (size > MAXBYTELEN) {
119             size = MAXBYTELEN;
120         }
121         if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
122             std::cout << "memcpy_s failed!";
123             UNREACHABLE();
124         }
125         LinkedListAdd(linkedList, JSTaggedValue(value), thread);
126         JSNApi::DestroyJSVM(vm);
127     }
128 
ContainersLinkedListGetFirstFuzzTest(const uint8_t * data,size_t size)129     static void ContainersLinkedListGetFirstFuzzTest(const uint8_t* data, size_t size)
130     {
131         RuntimeOption option;
132         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
133         EcmaVM *vm = JSNApi::CreateJSVM(option);
134         auto thread = vm->GetAssociatedJSThread();
135         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
136         int value = 0;
137         if (size <= 0) {
138             return;
139         }
140         if (size > MAXBYTELEN) {
141             size = MAXBYTELEN;
142         }
143         if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
144             std::cout << "memcpy_s failed!";
145             UNREACHABLE();
146         }
147         LinkedListAdd(linkedList, JSTaggedValue(value), thread);
148 
149         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4);
150         callInfo->SetFunction(JSTaggedValue::Undefined());
151         callInfo->SetThis(linkedList.GetTaggedValue());
152         containers::ContainersLinkedList::GetFirst(callInfo);
153         JSNApi::DestroyJSVM(vm);
154     }
155 
ContainersLinkedListGetLastFuzzTest(const uint8_t * data,size_t size)156     static void ContainersLinkedListGetLastFuzzTest(const uint8_t* data, size_t size)
157     {
158         RuntimeOption option;
159         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
160         EcmaVM *vm = JSNApi::CreateJSVM(option);
161         auto thread = vm->GetAssociatedJSThread();
162         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
163         int value = 0;
164         if (size <= 0) {
165             return;
166         }
167         if (size > MAXBYTELEN) {
168             size = MAXBYTELEN;
169         }
170         if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
171             std::cout << "memcpy_s failed!";
172             UNREACHABLE();
173         }
174         LinkedListAdd(linkedList, JSTaggedValue(value), thread);
175 
176         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4);
177         callInfo->SetFunction(JSTaggedValue::Undefined());
178         callInfo->SetThis(linkedList.GetTaggedValue());
179         containers::ContainersLinkedList::GetLast(callInfo);
180         JSNApi::DestroyJSVM(vm);
181     }
182 
ContainersLinkedListAddFirstFuzzTest(const uint8_t * data,size_t size)183     static void ContainersLinkedListAddFirstFuzzTest(const uint8_t* data, size_t size)
184     {
185         RuntimeOption option;
186         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
187         EcmaVM *vm = JSNApi::CreateJSVM(option);
188         auto thread = vm->GetAssociatedJSThread();
189         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
190         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
191         callInfo->SetFunction(JSTaggedValue::Undefined());
192         callInfo->SetThis(linkedList.GetTaggedValue());
193         int value = 0;
194         if (size <= 0) {
195             return;
196         }
197         if (size > MAXBYTELEN) {
198             size = MAXBYTELEN;
199         }
200         if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
201             std::cout << "memcpy_s failed!";
202             UNREACHABLE();
203         }
204         callInfo->SetCallArg(0, JSTaggedValue(value));
205 
206         containers::ContainersLinkedList::AddFirst(callInfo);
207         JSNApi::DestroyJSVM(vm);
208     }
209 
ContainersLinkedListClearFuzzTest(const uint8_t * data,size_t size)210     static void ContainersLinkedListClearFuzzTest(const uint8_t* data, size_t size)
211     {
212         RuntimeOption option;
213         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
214         EcmaVM *vm = JSNApi::CreateJSVM(option);
215         auto thread = vm->GetAssociatedJSThread();
216         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
217         int value = 0;
218         if (size <= 0) {
219             return;
220         }
221         if (size > MAXBYTELEN) {
222             size = MAXBYTELEN;
223         }
224         if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
225             std::cout << "memcpy_s failed!";
226             UNREACHABLE();
227         }
228         LinkedListAdd(linkedList, JSTaggedValue(value), thread);
229 
230         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4);
231         callInfo->SetFunction(JSTaggedValue::Undefined());
232         callInfo->SetThis(linkedList.GetTaggedValue());
233         containers::ContainersLinkedList::Clear(callInfo);
234         JSNApi::DestroyJSVM(vm);
235     }
236 
ContainersLinkedListCloneFuzzTest(const uint8_t * data,size_t size)237     static void ContainersLinkedListCloneFuzzTest(const uint8_t* data, size_t size)
238     {
239         RuntimeOption option;
240         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
241         EcmaVM *vm = JSNApi::CreateJSVM(option);
242         auto thread = vm->GetAssociatedJSThread();
243         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
244         int value = 0;
245         if (size <= 0) {
246             return;
247         }
248         if (size > MAXBYTELEN) {
249             size = MAXBYTELEN;
250         }
251         if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
252             std::cout << "memcpy_s failed!";
253             UNREACHABLE();
254         }
255         LinkedListAdd(linkedList, JSTaggedValue(value), thread);
256 
257         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4);
258         callInfo->SetFunction(JSTaggedValue::Undefined());
259         callInfo->SetThis(linkedList.GetTaggedValue());
260         containers::ContainersLinkedList::Clone(callInfo);
261         JSNApi::DestroyJSVM(vm);
262     }
263 
ContainersLinkedListGetFuzzTest(const uint8_t * data,size_t size)264     static void ContainersLinkedListGetFuzzTest(const uint8_t* data, size_t size)
265     {
266         RuntimeOption option;
267         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
268         EcmaVM *vm = JSNApi::CreateJSVM(option);
269         auto thread = vm->GetAssociatedJSThread();
270         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
271         int value = 0;
272         if (size <= 0) {
273             return;
274         }
275         if (size > MAXBYTELEN) {
276             size = MAXBYTELEN;
277         }
278         if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
279             std::cout << "memcpy_s failed!";
280             UNREACHABLE();
281         }
282         LinkedListAdd(linkedList, JSTaggedValue(value), thread);
283 
284         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
285         callInfo->SetFunction(JSTaggedValue::Undefined());
286         callInfo->SetThis(linkedList.GetTaggedValue());
287         int index = 0;
288         if (size <= 0) {
289             return;
290         }
291         if (size > MAXBYTELEN) {
292             size = MAXBYTELEN;
293         }
294         if (memcpy_s(&index, MAXBYTELEN, data, size) != 0) {
295             std::cout << "memcpy_s failed!";
296             UNREACHABLE();
297         }
298         callInfo->SetCallArg(0, JSTaggedValue(index));
299         containers::ContainersLinkedList::Get(callInfo);
300         JSNApi::DestroyJSVM(vm);
301     }
302 
ContainersLinkedListGetIndexOfFuzzTest(const uint8_t * data,size_t size)303     static void ContainersLinkedListGetIndexOfFuzzTest(const uint8_t* data, size_t size)
304     {
305         RuntimeOption option;
306         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
307         EcmaVM *vm = JSNApi::CreateJSVM(option);
308         auto thread = vm->GetAssociatedJSThread();
309         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
310         int value = 0;
311         if (size <= 0) {
312             return;
313         }
314         if (size > MAXBYTELEN) {
315             size = MAXBYTELEN;
316         }
317         if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
318             std::cout << "memcpy_s failed!";
319             UNREACHABLE();
320         }
321         LinkedListAdd(linkedList, JSTaggedValue(value), thread);
322 
323         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
324         callInfo->SetFunction(JSTaggedValue::Undefined());
325         callInfo->SetThis(linkedList.GetTaggedValue());
326         callInfo->SetCallArg(0, JSTaggedValue(value));
327         containers::ContainersLinkedList::GetIndexOf(callInfo);
328         JSNApi::DestroyJSVM(vm);
329     }
330 
ContainersLinkedListGetLastIndexOfFuzzTest(const uint8_t * data,size_t size)331     static void ContainersLinkedListGetLastIndexOfFuzzTest(const uint8_t* data, size_t size)
332     {
333         RuntimeOption option;
334         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
335         EcmaVM *vm = JSNApi::CreateJSVM(option);
336         auto thread = vm->GetAssociatedJSThread();
337         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
338         int value = 0;
339         if (size <= 0) {
340             return;
341         }
342         if (size > MAXBYTELEN) {
343             size = MAXBYTELEN;
344         }
345         if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
346             std::cout << "memcpy_s failed!";
347             UNREACHABLE();
348         }
349         LinkedListAdd(linkedList, JSTaggedValue(value), thread);
350 
351         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
352         callInfo->SetFunction(JSTaggedValue::Undefined());
353         callInfo->SetThis(linkedList.GetTaggedValue());
354         callInfo->SetCallArg(0, JSTaggedValue(value));
355         containers::ContainersLinkedList::GetLastIndexOf(callInfo);
356         JSNApi::DestroyJSVM(vm);
357     }
358 
ContainersLinkedListHasFuzzTest(const uint8_t * data,size_t size)359     static void ContainersLinkedListHasFuzzTest(const uint8_t* data, size_t size)
360     {
361         RuntimeOption option;
362         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
363         EcmaVM *vm = JSNApi::CreateJSVM(option);
364         auto thread = vm->GetAssociatedJSThread();
365         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
366         int value = 0;
367         if (size <= 0) {
368             return;
369         }
370         if (size > MAXBYTELEN) {
371             size = MAXBYTELEN;
372         }
373         if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
374             std::cout << "memcpy_s failed!";
375             UNREACHABLE();
376         }
377         LinkedListAdd(linkedList, JSTaggedValue(value), thread);
378 
379         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
380         callInfo->SetFunction(JSTaggedValue::Undefined());
381         callInfo->SetThis(linkedList.GetTaggedValue());
382         callInfo->SetCallArg(0, JSTaggedValue(value));
383         containers::ContainersLinkedList::Has(callInfo);
384         JSNApi::DestroyJSVM(vm);
385     }
386 
ContainersLinkedListInsertFuzzTest(const uint8_t * data,size_t size)387     static void ContainersLinkedListInsertFuzzTest(const uint8_t* data, size_t size)
388     {
389         RuntimeOption option;
390         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
391         EcmaVM *vm = JSNApi::CreateJSVM(option);
392         auto thread = vm->GetAssociatedJSThread();
393         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
394         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 8);
395         callInfo->SetFunction(JSTaggedValue::Undefined());
396         callInfo->SetThis(linkedList.GetTaggedValue());
397         int index = 0;
398         if (size <= 0) {
399             return;
400         }
401         if (size > MAXBYTELEN) {
402             size = MAXBYTELEN;
403         }
404         if (memcpy_s(&index, MAXBYTELEN, data, size) != 0) {
405             std::cout << "memcpy_s failed!";
406             UNREACHABLE();
407         }
408         callInfo->SetCallArg(0, JSTaggedValue(index));
409         callInfo->SetCallArg(1, JSTaggedValue(index + 1));
410 
411         containers::ContainersLinkedList::Insert(callInfo);
412         JSNApi::DestroyJSVM(vm);
413     }
414 
ContainersLinkedListRemoveByIndexFuzzTest(const uint8_t * data,size_t size)415     static void ContainersLinkedListRemoveByIndexFuzzTest(const uint8_t* data, size_t size)
416     {
417         RuntimeOption option;
418         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
419         EcmaVM *vm = JSNApi::CreateJSVM(option);
420         auto thread = vm->GetAssociatedJSThread();
421         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
422         int value = 0;
423         if (size <= 0) {
424             return;
425         }
426         if (size > MAXBYTELEN) {
427             size = MAXBYTELEN;
428         }
429         if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
430             std::cout << "memcpy_s failed!";
431             UNREACHABLE();
432         }
433         LinkedListAdd(linkedList, JSTaggedValue(value), thread);
434         int index = 0;
435         if (size <= 0) {
436             return;
437         }
438         if (size > MAXBYTELEN) {
439             size = MAXBYTELEN;
440         }
441         if (memcpy_s(&index, MAXBYTELEN, data, size) != 0) {
442             std::cout << "memcpy_s failed!";
443             UNREACHABLE();
444         }
445         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
446         callInfo->SetFunction(JSTaggedValue::Undefined());
447         callInfo->SetThis(linkedList.GetTaggedValue());
448         callInfo->SetCallArg(0, JSTaggedValue(index));
449         containers::ContainersLinkedList::RemoveByIndex(callInfo);
450         JSNApi::DestroyJSVM(vm);
451     }
452 
ContainersLinkedListRemoveFuzzTest(const uint8_t * data,size_t size)453     static void ContainersLinkedListRemoveFuzzTest(const uint8_t* data, size_t size)
454     {
455         RuntimeOption option;
456         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
457         EcmaVM *vm = JSNApi::CreateJSVM(option);
458         auto thread = vm->GetAssociatedJSThread();
459         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
460         int value = 0;
461         if (size <= 0) {
462             return;
463         }
464         if (size > MAXBYTELEN) {
465             size = MAXBYTELEN;
466         }
467         if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
468             std::cout << "memcpy_s failed!";
469             UNREACHABLE();
470         }
471         LinkedListAdd(linkedList, JSTaggedValue(value), thread);
472 
473         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
474         callInfo->SetFunction(JSTaggedValue::Undefined());
475         callInfo->SetThis(linkedList.GetTaggedValue());
476         callInfo->SetCallArg(0, JSTaggedValue(value));
477         containers::ContainersLinkedList::Remove(callInfo);
478         JSNApi::DestroyJSVM(vm);
479     }
480 
ContainersLinkedListRemoveFirstFuzzTest(const uint8_t * data,size_t size)481     static void ContainersLinkedListRemoveFirstFuzzTest(const uint8_t* data, size_t size)
482     {
483         RuntimeOption option;
484         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
485         EcmaVM *vm = JSNApi::CreateJSVM(option);
486         auto thread = vm->GetAssociatedJSThread();
487         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
488         int value = 0;
489         if (size <= 0) {
490             return;
491         }
492         if (size > MAXBYTELEN) {
493             size = MAXBYTELEN;
494         }
495         if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
496             std::cout << "memcpy_s failed!";
497             UNREACHABLE();
498         }
499         LinkedListAdd(linkedList, JSTaggedValue(value), thread);
500 
501         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4);
502         callInfo->SetFunction(JSTaggedValue::Undefined());
503         callInfo->SetThis(linkedList.GetTaggedValue());
504         containers::ContainersLinkedList::RemoveFirst(callInfo);
505         JSNApi::DestroyJSVM(vm);
506     }
507 
ContainersLinkedListRemoveLastFuzzTest(const uint8_t * data,size_t size)508     static void ContainersLinkedListRemoveLastFuzzTest(const uint8_t* data, size_t size)
509     {
510         RuntimeOption option;
511         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
512         EcmaVM *vm = JSNApi::CreateJSVM(option);
513         auto thread = vm->GetAssociatedJSThread();
514         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
515         int value = 0;
516         if (size <= 0) {
517             return;
518         }
519         if (size > MAXBYTELEN) {
520             size = MAXBYTELEN;
521         }
522         if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
523             std::cout << "memcpy_s failed!";
524             UNREACHABLE();
525         }
526         LinkedListAdd(linkedList, JSTaggedValue(value), thread);
527 
528         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4);
529         callInfo->SetFunction(JSTaggedValue::Undefined());
530         callInfo->SetThis(linkedList.GetTaggedValue());
531         containers::ContainersLinkedList::RemoveLast(callInfo);
532         JSNApi::DestroyJSVM(vm);
533     }
534 
ContainersLinkedListRemoveFirstFoundFuzzTest(const uint8_t * data,size_t size)535     static void ContainersLinkedListRemoveFirstFoundFuzzTest(const uint8_t* data, size_t size)
536     {
537         RuntimeOption option;
538         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
539         EcmaVM *vm = JSNApi::CreateJSVM(option);
540         auto thread = vm->GetAssociatedJSThread();
541         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
542         int value = 0;
543         if (size <= 0) {
544             return;
545         }
546         if (size > MAXBYTELEN) {
547             size = MAXBYTELEN;
548         }
549         if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
550             std::cout << "memcpy_s failed!";
551             UNREACHABLE();
552         }
553         LinkedListAdd(linkedList, JSTaggedValue(value), thread);
554 
555         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
556         callInfo->SetFunction(JSTaggedValue::Undefined());
557         callInfo->SetThis(linkedList.GetTaggedValue());
558         callInfo->SetCallArg(0, JSTaggedValue(value));
559         containers::ContainersLinkedList::RemoveFirstFound(callInfo);
560         JSNApi::DestroyJSVM(vm);
561     }
562 
ContainersLinkedListRemoveLastFoundFuzzTest(const uint8_t * data,size_t size)563     static void ContainersLinkedListRemoveLastFoundFuzzTest(const uint8_t* data, size_t size)
564     {
565         RuntimeOption option;
566         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
567         EcmaVM *vm = JSNApi::CreateJSVM(option);
568         auto thread = vm->GetAssociatedJSThread();
569         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
570         int value = 0;
571         if (size <= 0) {
572             return;
573         }
574         if (size > MAXBYTELEN) {
575             size = MAXBYTELEN;
576         }
577         if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
578             std::cout << "memcpy_s failed!";
579             UNREACHABLE();
580         }
581         LinkedListAdd(linkedList, JSTaggedValue(value), thread);
582 
583         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
584         callInfo->SetFunction(JSTaggedValue::Undefined());
585         callInfo->SetThis(linkedList.GetTaggedValue());
586         callInfo->SetCallArg(0, JSTaggedValue(value));
587         containers::ContainersLinkedList::RemoveLastFound(callInfo);
588         JSNApi::DestroyJSVM(vm);
589     }
590 
ContainersLinkedListSetFuzzTest(const uint8_t * data,size_t size)591     static void ContainersLinkedListSetFuzzTest(const uint8_t* data, size_t size)
592     {
593         RuntimeOption option;
594         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
595         EcmaVM *vm = JSNApi::CreateJSVM(option);
596         auto thread = vm->GetAssociatedJSThread();
597         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
598         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 8);
599         callInfo->SetFunction(JSTaggedValue::Undefined());
600         callInfo->SetThis(linkedList.GetTaggedValue());
601         int index = 0;
602         if (size <= 0) {
603             return;
604         }
605         if (size > MAXBYTELEN) {
606             size = MAXBYTELEN;
607         }
608         if (memcpy_s(&index, MAXBYTELEN, data, size) != 0) {
609             std::cout << "memcpy_s failed!";
610             UNREACHABLE();
611         }
612         callInfo->SetCallArg(0, JSTaggedValue(index));
613         callInfo->SetCallArg(1, JSTaggedValue(index + 1));
614 
615         containers::ContainersLinkedList::Set(callInfo);
616         JSNApi::DestroyJSVM(vm);
617     }
618 
ContainersLinkedListLengthFuzzTest(const uint8_t * data,size_t size)619     static void ContainersLinkedListLengthFuzzTest(const uint8_t* data, size_t size)
620     {
621         RuntimeOption option;
622         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
623         EcmaVM *vm = JSNApi::CreateJSVM(option);
624         auto thread = vm->GetAssociatedJSThread();
625         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
626         int value = 0;
627         if (size <= 0) {
628             return;
629         }
630         if (size > MAXBYTELEN) {
631             size = MAXBYTELEN;
632         }
633         if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
634             std::cout << "memcpy_s failed!";
635             UNREACHABLE();
636         }
637         LinkedListAdd(linkedList, JSTaggedValue(value), thread);
638 
639         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4);
640         callInfo->SetFunction(JSTaggedValue::Undefined());
641         callInfo->SetThis(linkedList.GetTaggedValue());
642         containers::ContainersLinkedList::Length(callInfo);
643         JSNApi::DestroyJSVM(vm);
644     }
645 
ContainersLinkedListConvertToArrayFuzzTest(const uint8_t * data,size_t size)646     static void ContainersLinkedListConvertToArrayFuzzTest(const uint8_t* data, size_t size)
647     {
648         RuntimeOption option;
649         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
650         EcmaVM *vm = JSNApi::CreateJSVM(option);
651         auto thread = vm->GetAssociatedJSThread();
652         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
653         int value = 0;
654         if (size <= 0) {
655             return;
656         }
657         if (size > MAXBYTELEN) {
658             size = MAXBYTELEN;
659         }
660         if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
661             std::cout << "memcpy_s failed!";
662             UNREACHABLE();
663         }
664         LinkedListAdd(linkedList, JSTaggedValue(value), thread);
665 
666         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4);
667         callInfo->SetFunction(JSTaggedValue::Undefined());
668         callInfo->SetThis(linkedList.GetTaggedValue());
669         containers::ContainersLinkedList::ConvertToArray(callInfo);
670         JSNApi::DestroyJSVM(vm);
671     }
672 
ContainersLinkedListForEachFuzzTest(const uint8_t * data,size_t size)673     static void ContainersLinkedListForEachFuzzTest(const uint8_t* data, size_t size)
674     {
675         RuntimeOption option;
676         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
677         EcmaVM *vm = JSNApi::CreateJSVM(option);
678         auto thread = vm->GetAssociatedJSThread();
679         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
680         int value = 0;
681         if (size <= 0) {
682             return;
683         }
684         if (size > MAXBYTELEN) {
685             size = MAXBYTELEN;
686         }
687         if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
688             std::cout << "memcpy_s failed!";
689             UNREACHABLE();
690         }
691         LinkedListAdd(linkedList, JSTaggedValue(value), thread);
692 
693         JSHandle<JSAPILinkedList> newLinkedlist = CreateJSAPILinkedList(thread);
694         auto callInfo2 = CreateEcmaRuntimeCallInfo(thread, 8);
695         JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
696         auto factory = vm->GetFactory();
697         JSHandle<JSFunction> func = factory->NewJSFunction(env, reinterpret_cast<void *>(TestClass::TestForEachFunc));
698         callInfo2->SetFunction(JSTaggedValue::Undefined());
699         callInfo2->SetThis(linkedList.GetTaggedValue());
700         callInfo2->SetCallArg(0, func.GetTaggedValue());
701         callInfo2->SetCallArg(1, newLinkedlist.GetTaggedValue());
702         containers::ContainersLinkedList::ForEach(callInfo2);
703         JSNApi::DestroyJSVM(vm);
704     }
705 
ContainersLinkedListGetIteratorObjFuzzTest(const uint8_t * data,size_t size)706     static void ContainersLinkedListGetIteratorObjFuzzTest(const uint8_t* data, size_t size)
707     {
708         RuntimeOption option;
709         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
710         EcmaVM *vm = JSNApi::CreateJSVM(option);
711         auto thread = vm->GetAssociatedJSThread();
712         JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
713         int value = 0;
714         if (size <= 0) {
715             return;
716         }
717         if (size > MAXBYTELEN) {
718             size = MAXBYTELEN;
719         }
720         if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
721             std::cout << "memcpy_s failed!";
722             UNREACHABLE();
723         }
724         LinkedListAdd(linkedList, JSTaggedValue(value), thread);
725 
726         auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4);
727         callInfo->SetFunction(JSTaggedValue::Undefined());
728         callInfo->SetThis(linkedList.GetTaggedValue());
729         containers::ContainersLinkedList::GetIteratorObj(callInfo);
730         JSNApi::DestroyJSVM(vm);
731     }
732 };
733 }
734 #endif