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