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         // 0 means the argument
83         objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::LinkedList)));
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         {
113             JsiFastNativeScope scope(vm);
114             auto thread = vm->GetAssociatedJSThread();
115             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
116             int value = 0;
117             if (size <= 0) {
118                 return;
119             }
120             if (size > MAXBYTELEN) {
121                 size = MAXBYTELEN;
122             }
123             if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
124                 std::cout << "memcpy_s failed!";
125                 UNREACHABLE();
126             }
127             LinkedListAdd(linkedList, JSTaggedValue(value), thread);
128         }
129         JSNApi::DestroyJSVM(vm);
130     }
131 
ContainersLinkedListGetFirstFuzzTest(const uint8_t* data, size_t size)132     static void ContainersLinkedListGetFirstFuzzTest(const uint8_t* data, size_t size)
133     {
134         RuntimeOption option;
135         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
136         EcmaVM *vm = JSNApi::CreateJSVM(option);
137         {
138             JsiFastNativeScope scope(vm);
139             auto thread = vm->GetAssociatedJSThread();
140             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
141             int value = 0;
142             if (size <= 0) {
143                 return;
144             }
145             if (size > MAXBYTELEN) {
146                 size = MAXBYTELEN;
147             }
148             if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
149                 std::cout << "memcpy_s failed!";
150                 UNREACHABLE();
151             }
152             LinkedListAdd(linkedList, JSTaggedValue(value), thread);
153 
154             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4);
155             callInfo->SetFunction(JSTaggedValue::Undefined());
156             callInfo->SetThis(linkedList.GetTaggedValue());
157             containers::ContainersLinkedList::GetFirst(callInfo);
158         }
159         JSNApi::DestroyJSVM(vm);
160     }
161 
ContainersLinkedListGetLastFuzzTest(const uint8_t* data, size_t size)162     static void ContainersLinkedListGetLastFuzzTest(const uint8_t* data, size_t size)
163     {
164         RuntimeOption option;
165         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
166         EcmaVM *vm = JSNApi::CreateJSVM(option);
167         {
168             JsiFastNativeScope scope(vm);
169             auto thread = vm->GetAssociatedJSThread();
170             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
171             int value = 0;
172             if (size <= 0) {
173                 return;
174             }
175             if (size > MAXBYTELEN) {
176                 size = MAXBYTELEN;
177             }
178             if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
179                 std::cout << "memcpy_s failed!";
180                 UNREACHABLE();
181             }
182             LinkedListAdd(linkedList, JSTaggedValue(value), thread);
183 
184             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4);
185             callInfo->SetFunction(JSTaggedValue::Undefined());
186             callInfo->SetThis(linkedList.GetTaggedValue());
187             containers::ContainersLinkedList::GetLast(callInfo);
188         }
189         JSNApi::DestroyJSVM(vm);
190     }
191 
ContainersLinkedListAddFirstFuzzTest(const uint8_t* data, size_t size)192     static void ContainersLinkedListAddFirstFuzzTest(const uint8_t* data, size_t size)
193     {
194         RuntimeOption option;
195         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
196         EcmaVM *vm = JSNApi::CreateJSVM(option);
197         {
198             JsiFastNativeScope scope(vm);
199             auto thread = vm->GetAssociatedJSThread();
200             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
201             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
202             callInfo->SetFunction(JSTaggedValue::Undefined());
203             callInfo->SetThis(linkedList.GetTaggedValue());
204             int value = 0;
205             if (size <= 0) {
206                 return;
207             }
208             if (size > MAXBYTELEN) {
209                 size = MAXBYTELEN;
210             }
211             if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
212                 std::cout << "memcpy_s failed!";
213                 UNREACHABLE();
214             }
215             callInfo->SetCallArg(0, JSTaggedValue(value));
216 
217             containers::ContainersLinkedList::AddFirst(callInfo);
218             }
219         JSNApi::DestroyJSVM(vm);
220     }
221 
ContainersLinkedListClearFuzzTest(const uint8_t* data, size_t size)222     static void ContainersLinkedListClearFuzzTest(const uint8_t* data, size_t size)
223     {
224         RuntimeOption option;
225         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
226         EcmaVM *vm = JSNApi::CreateJSVM(option);
227         {
228             JsiFastNativeScope scope(vm);
229             auto thread = vm->GetAssociatedJSThread();
230             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
231             int value = 0;
232             if (size <= 0) {
233                 return;
234             }
235             if (size > MAXBYTELEN) {
236                 size = MAXBYTELEN;
237             }
238             if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
239                 std::cout << "memcpy_s failed!";
240                 UNREACHABLE();
241             }
242             LinkedListAdd(linkedList, JSTaggedValue(value), thread);
243 
244             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4);
245             callInfo->SetFunction(JSTaggedValue::Undefined());
246             callInfo->SetThis(linkedList.GetTaggedValue());
247             containers::ContainersLinkedList::Clear(callInfo);
248         }
249         JSNApi::DestroyJSVM(vm);
250     }
251 
ContainersLinkedListCloneFuzzTest(const uint8_t* data, size_t size)252     static void ContainersLinkedListCloneFuzzTest(const uint8_t* data, size_t size)
253     {
254         RuntimeOption option;
255         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
256         EcmaVM *vm = JSNApi::CreateJSVM(option);
257         {
258             JsiFastNativeScope scope(vm);
259             auto thread = vm->GetAssociatedJSThread();
260             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
261             int value = 0;
262             if (size <= 0) {
263                 return;
264             }
265             if (size > MAXBYTELEN) {
266                 size = MAXBYTELEN;
267             }
268             if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
269                 std::cout << "memcpy_s failed!";
270                 UNREACHABLE();
271             }
272             LinkedListAdd(linkedList, JSTaggedValue(value), thread);
273 
274             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4);
275             callInfo->SetFunction(JSTaggedValue::Undefined());
276             callInfo->SetThis(linkedList.GetTaggedValue());
277             containers::ContainersLinkedList::Clone(callInfo);
278         }
279         JSNApi::DestroyJSVM(vm);
280     }
281 
ContainersLinkedListGetFuzzTest(const uint8_t* data, size_t size)282     static void ContainersLinkedListGetFuzzTest(const uint8_t* data, size_t size)
283     {
284         RuntimeOption option;
285         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
286         EcmaVM *vm = JSNApi::CreateJSVM(option);
287         {
288             JsiFastNativeScope scope(vm);
289             auto thread = vm->GetAssociatedJSThread();
290             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
291             int value = 0;
292             if (size <= 0) {
293                 return;
294             }
295             if (size > MAXBYTELEN) {
296                 size = MAXBYTELEN;
297             }
298             if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
299                 std::cout << "memcpy_s failed!";
300                 UNREACHABLE();
301             }
302             LinkedListAdd(linkedList, JSTaggedValue(value), thread);
303 
304             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
305             callInfo->SetFunction(JSTaggedValue::Undefined());
306             callInfo->SetThis(linkedList.GetTaggedValue());
307             int index = 0;
308             if (size <= 0) {
309                 return;
310             }
311             if (size > MAXBYTELEN) {
312                 size = MAXBYTELEN;
313             }
314             if (memcpy_s(&index, MAXBYTELEN, data, size) != 0) {
315                 std::cout << "memcpy_s failed!";
316                 UNREACHABLE();
317             }
318             callInfo->SetCallArg(0, JSTaggedValue(index));
319             containers::ContainersLinkedList::Get(callInfo);
320         }
321         JSNApi::DestroyJSVM(vm);
322     }
323 
ContainersLinkedListGetIndexOfFuzzTest(const uint8_t* data, size_t size)324     static void ContainersLinkedListGetIndexOfFuzzTest(const uint8_t* data, size_t size)
325     {
326         RuntimeOption option;
327         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
328         EcmaVM *vm = JSNApi::CreateJSVM(option);
329         {
330             JsiFastNativeScope scope(vm);
331             auto thread = vm->GetAssociatedJSThread();
332             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
333             int value = 0;
334             if (size <= 0) {
335                 return;
336             }
337             if (size > MAXBYTELEN) {
338                 size = MAXBYTELEN;
339             }
340             if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
341                 std::cout << "memcpy_s failed!";
342                 UNREACHABLE();
343             }
344             LinkedListAdd(linkedList, JSTaggedValue(value), thread);
345 
346             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
347             callInfo->SetFunction(JSTaggedValue::Undefined());
348             callInfo->SetThis(linkedList.GetTaggedValue());
349             callInfo->SetCallArg(0, JSTaggedValue(value));
350             containers::ContainersLinkedList::GetIndexOf(callInfo);
351         }
352         JSNApi::DestroyJSVM(vm);
353     }
354 
ContainersLinkedListGetLastIndexOfFuzzTest(const uint8_t* data, size_t size)355     static void ContainersLinkedListGetLastIndexOfFuzzTest(const uint8_t* data, size_t size)
356     {
357         RuntimeOption option;
358         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
359         EcmaVM *vm = JSNApi::CreateJSVM(option);
360         {
361             JsiFastNativeScope scope(vm);
362             auto thread = vm->GetAssociatedJSThread();
363             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
364             int value = 0;
365             if (size <= 0) {
366                 return;
367             }
368             if (size > MAXBYTELEN) {
369                 size = MAXBYTELEN;
370             }
371             if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
372                 std::cout << "memcpy_s failed!";
373                 UNREACHABLE();
374             }
375             LinkedListAdd(linkedList, JSTaggedValue(value), thread);
376 
377             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
378             callInfo->SetFunction(JSTaggedValue::Undefined());
379             callInfo->SetThis(linkedList.GetTaggedValue());
380             callInfo->SetCallArg(0, JSTaggedValue(value));
381             containers::ContainersLinkedList::GetLastIndexOf(callInfo);
382         }
383         JSNApi::DestroyJSVM(vm);
384     }
385 
ContainersLinkedListHasFuzzTest(const uint8_t* data, size_t size)386     static void ContainersLinkedListHasFuzzTest(const uint8_t* data, size_t size)
387     {
388         RuntimeOption option;
389         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
390         EcmaVM *vm = JSNApi::CreateJSVM(option);
391         {
392             JsiFastNativeScope scope(vm);
393             auto thread = vm->GetAssociatedJSThread();
394             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
395             int value = 0;
396             if (size <= 0) {
397                 return;
398             }
399             if (size > MAXBYTELEN) {
400                 size = MAXBYTELEN;
401             }
402             if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
403                 std::cout << "memcpy_s failed!";
404                 UNREACHABLE();
405             }
406             LinkedListAdd(linkedList, JSTaggedValue(value), thread);
407 
408             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
409             callInfo->SetFunction(JSTaggedValue::Undefined());
410             callInfo->SetThis(linkedList.GetTaggedValue());
411             callInfo->SetCallArg(0, JSTaggedValue(value));
412             containers::ContainersLinkedList::Has(callInfo);
413         }
414         JSNApi::DestroyJSVM(vm);
415     }
416 
ContainersLinkedListInsertFuzzTest(const uint8_t* data, size_t size)417     static void ContainersLinkedListInsertFuzzTest(const uint8_t* data, size_t size)
418     {
419         RuntimeOption option;
420         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
421         EcmaVM *vm = JSNApi::CreateJSVM(option);
422         {
423             JsiFastNativeScope scope(vm);
424             auto thread = vm->GetAssociatedJSThread();
425             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
426             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 8);
427             callInfo->SetFunction(JSTaggedValue::Undefined());
428             callInfo->SetThis(linkedList.GetTaggedValue());
429             int index = 0;
430             if (size <= 0) {
431                 return;
432             }
433             if (size > MAXBYTELEN) {
434                 size = MAXBYTELEN;
435             }
436             if (memcpy_s(&index, MAXBYTELEN, data, size) != 0) {
437                 std::cout << "memcpy_s failed!";
438                 UNREACHABLE();
439             }
440             callInfo->SetCallArg(0, JSTaggedValue(index));
441             callInfo->SetCallArg(1, JSTaggedValue(index + 1));
442 
443             containers::ContainersLinkedList::Insert(callInfo);
444         }
445         JSNApi::DestroyJSVM(vm);
446     }
447 
ContainersLinkedListRemoveByIndexFuzzTest(const uint8_t* data, size_t size)448     static void ContainersLinkedListRemoveByIndexFuzzTest(const uint8_t* data, size_t size)
449     {
450         RuntimeOption option;
451         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
452         EcmaVM *vm = JSNApi::CreateJSVM(option);
453         {
454             JsiFastNativeScope scope(vm);
455             auto thread = vm->GetAssociatedJSThread();
456             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
457             int value = 0;
458             if (size <= 0) {
459                 return;
460             }
461             if (size > MAXBYTELEN) {
462                 size = MAXBYTELEN;
463             }
464             if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
465                 std::cout << "memcpy_s failed!";
466                 UNREACHABLE();
467             }
468             LinkedListAdd(linkedList, JSTaggedValue(value), thread);
469             int index = 0;
470             if (size <= 0) {
471                 return;
472             }
473             if (size > MAXBYTELEN) {
474                 size = MAXBYTELEN;
475             }
476             if (memcpy_s(&index, MAXBYTELEN, data, size) != 0) {
477                 std::cout << "memcpy_s failed!";
478                 UNREACHABLE();
479             }
480             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
481             callInfo->SetFunction(JSTaggedValue::Undefined());
482             callInfo->SetThis(linkedList.GetTaggedValue());
483             callInfo->SetCallArg(0, JSTaggedValue(index));
484             containers::ContainersLinkedList::RemoveByIndex(callInfo);
485         }
486         JSNApi::DestroyJSVM(vm);
487     }
488 
ContainersLinkedListRemoveFuzzTest(const uint8_t* data, size_t size)489     static void ContainersLinkedListRemoveFuzzTest(const uint8_t* data, size_t size)
490     {
491         RuntimeOption option;
492         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
493         EcmaVM *vm = JSNApi::CreateJSVM(option);
494         {
495             JsiFastNativeScope scope(vm);
496             auto thread = vm->GetAssociatedJSThread();
497             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
498             int value = 0;
499             if (size <= 0) {
500                 return;
501             }
502             if (size > MAXBYTELEN) {
503                 size = MAXBYTELEN;
504             }
505             if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
506                 std::cout << "memcpy_s failed!";
507                 UNREACHABLE();
508             }
509             LinkedListAdd(linkedList, JSTaggedValue(value), thread);
510 
511             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
512             callInfo->SetFunction(JSTaggedValue::Undefined());
513             callInfo->SetThis(linkedList.GetTaggedValue());
514             callInfo->SetCallArg(0, JSTaggedValue(value));
515             containers::ContainersLinkedList::Remove(callInfo);
516         }
517         JSNApi::DestroyJSVM(vm);
518     }
519 
ContainersLinkedListRemoveFirstFuzzTest(const uint8_t* data, size_t size)520     static void ContainersLinkedListRemoveFirstFuzzTest(const uint8_t* data, size_t size)
521     {
522         RuntimeOption option;
523         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
524         EcmaVM *vm = JSNApi::CreateJSVM(option);
525         {
526             JsiFastNativeScope scope(vm);
527             auto thread = vm->GetAssociatedJSThread();
528             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
529             int value = 0;
530             if (size <= 0) {
531                 return;
532             }
533             if (size > MAXBYTELEN) {
534                 size = MAXBYTELEN;
535             }
536             if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
537                 std::cout << "memcpy_s failed!";
538                 UNREACHABLE();
539             }
540             LinkedListAdd(linkedList, JSTaggedValue(value), thread);
541 
542             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4);
543             callInfo->SetFunction(JSTaggedValue::Undefined());
544             callInfo->SetThis(linkedList.GetTaggedValue());
545             containers::ContainersLinkedList::RemoveFirst(callInfo);
546         }
547         JSNApi::DestroyJSVM(vm);
548     }
549 
ContainersLinkedListRemoveLastFuzzTest(const uint8_t* data, size_t size)550     static void ContainersLinkedListRemoveLastFuzzTest(const uint8_t* data, size_t size)
551     {
552         RuntimeOption option;
553         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
554         EcmaVM *vm = JSNApi::CreateJSVM(option);
555         {
556             JsiFastNativeScope scope(vm);
557             auto thread = vm->GetAssociatedJSThread();
558             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
559             int value = 0;
560             if (size <= 0) {
561                 return;
562             }
563             if (size > MAXBYTELEN) {
564                 size = MAXBYTELEN;
565             }
566             if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
567                 std::cout << "memcpy_s failed!";
568                 UNREACHABLE();
569             }
570             LinkedListAdd(linkedList, JSTaggedValue(value), thread);
571 
572             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4);
573             callInfo->SetFunction(JSTaggedValue::Undefined());
574             callInfo->SetThis(linkedList.GetTaggedValue());
575             containers::ContainersLinkedList::RemoveLast(callInfo);
576         }
577         JSNApi::DestroyJSVM(vm);
578     }
579 
ContainersLinkedListRemoveFirstFoundFuzzTest(const uint8_t* data, size_t size)580     static void ContainersLinkedListRemoveFirstFoundFuzzTest(const uint8_t* data, size_t size)
581     {
582         RuntimeOption option;
583         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
584         EcmaVM *vm = JSNApi::CreateJSVM(option);
585         {
586             JsiFastNativeScope scope(vm);
587             auto thread = vm->GetAssociatedJSThread();
588             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
589             int value = 0;
590             if (size <= 0) {
591                 return;
592             }
593             if (size > MAXBYTELEN) {
594                 size = MAXBYTELEN;
595             }
596             if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
597                 std::cout << "memcpy_s failed!";
598                 UNREACHABLE();
599             }
600             LinkedListAdd(linkedList, JSTaggedValue(value), thread);
601 
602             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
603             callInfo->SetFunction(JSTaggedValue::Undefined());
604             callInfo->SetThis(linkedList.GetTaggedValue());
605             callInfo->SetCallArg(0, JSTaggedValue(value));
606             containers::ContainersLinkedList::RemoveFirstFound(callInfo);
607         }
608         JSNApi::DestroyJSVM(vm);
609     }
610 
ContainersLinkedListRemoveLastFoundFuzzTest(const uint8_t* data, size_t size)611     static void ContainersLinkedListRemoveLastFoundFuzzTest(const uint8_t* data, size_t size)
612     {
613         RuntimeOption option;
614         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
615         EcmaVM *vm = JSNApi::CreateJSVM(option);
616         {
617             JsiFastNativeScope scope(vm);
618             auto thread = vm->GetAssociatedJSThread();
619             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
620             int value = 0;
621             if (size <= 0) {
622                 return;
623             }
624             if (size > MAXBYTELEN) {
625                 size = MAXBYTELEN;
626             }
627             if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
628                 std::cout << "memcpy_s failed!";
629                 UNREACHABLE();
630             }
631             LinkedListAdd(linkedList, JSTaggedValue(value), thread);
632 
633             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
634             callInfo->SetFunction(JSTaggedValue::Undefined());
635             callInfo->SetThis(linkedList.GetTaggedValue());
636             callInfo->SetCallArg(0, JSTaggedValue(value));
637             containers::ContainersLinkedList::RemoveLastFound(callInfo);
638         }
639         JSNApi::DestroyJSVM(vm);
640     }
641 
ContainersLinkedListSetFuzzTest(const uint8_t* data, size_t size)642     static void ContainersLinkedListSetFuzzTest(const uint8_t* data, size_t size)
643     {
644         RuntimeOption option;
645         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
646         EcmaVM *vm = JSNApi::CreateJSVM(option);
647         {
648             JsiFastNativeScope scope(vm);
649             auto thread = vm->GetAssociatedJSThread();
650             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
651             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 8);
652             callInfo->SetFunction(JSTaggedValue::Undefined());
653             callInfo->SetThis(linkedList.GetTaggedValue());
654             int index = 0;
655             if (size <= 0) {
656                 return;
657             }
658             if (size > MAXBYTELEN) {
659                 size = MAXBYTELEN;
660             }
661             if (memcpy_s(&index, MAXBYTELEN, data, size) != 0) {
662                 std::cout << "memcpy_s failed!";
663                 UNREACHABLE();
664             }
665             callInfo->SetCallArg(0, JSTaggedValue(index));
666             callInfo->SetCallArg(1, JSTaggedValue(index + 1));
667 
668             containers::ContainersLinkedList::Set(callInfo);
669         }
670         JSNApi::DestroyJSVM(vm);
671     }
672 
ContainersLinkedListLengthFuzzTest(const uint8_t* data, size_t size)673     static void ContainersLinkedListLengthFuzzTest(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         {
679             JsiFastNativeScope scope(vm);
680             auto thread = vm->GetAssociatedJSThread();
681             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
682             int value = 0;
683             if (size <= 0) {
684                 return;
685             }
686             if (size > MAXBYTELEN) {
687                 size = MAXBYTELEN;
688             }
689             if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
690                 std::cout << "memcpy_s failed!";
691                 UNREACHABLE();
692             }
693             LinkedListAdd(linkedList, JSTaggedValue(value), thread);
694 
695             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4);
696             callInfo->SetFunction(JSTaggedValue::Undefined());
697             callInfo->SetThis(linkedList.GetTaggedValue());
698             containers::ContainersLinkedList::Length(callInfo);
699         }
700         JSNApi::DestroyJSVM(vm);
701     }
702 
ContainersLinkedListConvertToArrayFuzzTest(const uint8_t* data, size_t size)703     static void ContainersLinkedListConvertToArrayFuzzTest(const uint8_t* data, size_t size)
704     {
705         RuntimeOption option;
706         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
707         EcmaVM *vm = JSNApi::CreateJSVM(option);
708         {
709             JsiFastNativeScope scope(vm);
710             auto thread = vm->GetAssociatedJSThread();
711             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
712             int value = 0;
713             if (size <= 0) {
714                 return;
715             }
716             if (size > MAXBYTELEN) {
717                 size = MAXBYTELEN;
718             }
719             if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
720                 std::cout << "memcpy_s failed!";
721                 UNREACHABLE();
722             }
723             LinkedListAdd(linkedList, JSTaggedValue(value), thread);
724 
725             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4);
726             callInfo->SetFunction(JSTaggedValue::Undefined());
727             callInfo->SetThis(linkedList.GetTaggedValue());
728             containers::ContainersLinkedList::ConvertToArray(callInfo);
729         }
730         JSNApi::DestroyJSVM(vm);
731     }
732 
ContainersLinkedListForEachFuzzTest(const uint8_t* data, size_t size)733     static void ContainersLinkedListForEachFuzzTest(const uint8_t* data, size_t size)
734     {
735         RuntimeOption option;
736         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
737         EcmaVM *vm = JSNApi::CreateJSVM(option);
738         {
739             JsiFastNativeScope scope(vm);
740             auto thread = vm->GetAssociatedJSThread();
741             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
742             int value = 0;
743             if (size <= 0) {
744                 return;
745             }
746             if (size > MAXBYTELEN) {
747                 size = MAXBYTELEN;
748             }
749             if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
750                 std::cout << "memcpy_s failed!";
751                 UNREACHABLE();
752             }
753             LinkedListAdd(linkedList, JSTaggedValue(value), thread);
754 
755             JSHandle<JSAPILinkedList> newLinkedlist = CreateJSAPILinkedList(thread);
756             auto callInfo2 = CreateEcmaRuntimeCallInfo(thread, 8);
757             JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
758             auto factory = vm->GetFactory();
759             JSHandle<JSFunction> func =
760                 factory->NewJSFunction(env, reinterpret_cast<void *>(TestClass::TestForEachFunc));
761             callInfo2->SetFunction(JSTaggedValue::Undefined());
762             callInfo2->SetThis(linkedList.GetTaggedValue());
763             callInfo2->SetCallArg(0, func.GetTaggedValue());
764             callInfo2->SetCallArg(1, newLinkedlist.GetTaggedValue());
765             containers::ContainersLinkedList::ForEach(callInfo2);
766         }
767         JSNApi::DestroyJSVM(vm);
768     }
769 
ContainersLinkedListGetIteratorObjFuzzTest(const uint8_t* data, size_t size)770     static void ContainersLinkedListGetIteratorObjFuzzTest(const uint8_t* data, size_t size)
771     {
772         RuntimeOption option;
773         option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
774         EcmaVM *vm = JSNApi::CreateJSVM(option);
775         {
776             JsiFastNativeScope scope(vm);
777             auto thread = vm->GetAssociatedJSThread();
778             JSHandle<JSAPILinkedList> linkedList = CreateJSAPILinkedList(thread);
779             int value = 0;
780             if (size <= 0) {
781                 return;
782             }
783             if (size > MAXBYTELEN) {
784                 size = MAXBYTELEN;
785             }
786             if (memcpy_s(&value, MAXBYTELEN, data, size) != 0) {
787                 std::cout << "memcpy_s failed!";
788                 UNREACHABLE();
789             }
790             LinkedListAdd(linkedList, JSTaggedValue(value), thread);
791 
792             auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4);
793             callInfo->SetFunction(JSTaggedValue::Undefined());
794             callInfo->SetThis(linkedList.GetTaggedValue());
795             containers::ContainersLinkedList::GetIteratorObj(callInfo);
796         }
797         JSNApi::DestroyJSVM(vm);
798     }
799 };
800 }
801 #endif