14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef CONTAINERSDEQUECOMMON_FUZZER_H
174514f5e3Sopenharmony_ci#define CONTAINERSDEQUECOMMON_FUZZER_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/containers/containers_deque.h"
204514f5e3Sopenharmony_ci#include "ecmascript/containers/containers_private.h"
214514f5e3Sopenharmony_ci#include "ecmascript/ecma_string-inl.h"
224514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h"
234514f5e3Sopenharmony_ci#include "ecmascript/global_env.h"
244514f5e3Sopenharmony_ci#include "ecmascript/js_api/js_api_deque.h"
254514f5e3Sopenharmony_ci#include "ecmascript/js_api/js_api_deque_iterator.h"
264514f5e3Sopenharmony_ci#include "ecmascript/js_handle.h"
274514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value-inl.h"
284514f5e3Sopenharmony_ci#include "ecmascript/napi/include/jsnapi.h"
294514f5e3Sopenharmony_ci#include "ecmascript/ecma_runtime_call_info.h"
304514f5e3Sopenharmony_ci#include "ecmascript/js_thread.h"
314514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h"
324514f5e3Sopenharmony_ci
334514f5e3Sopenharmony_ci#define MAXBYTELEN sizeof(unsigned int)
344514f5e3Sopenharmony_ci
354514f5e3Sopenharmony_cinamespace panda::ecmascript {
364514f5e3Sopenharmony_ciusing namespace panda::ecmascript::containers;
374514f5e3Sopenharmony_ciclass ContainersDequeFuzzTestHelper {
384514f5e3Sopenharmony_cipublic:
394514f5e3Sopenharmony_ci    static JSFunction *JSObjectCreate(JSThread *thread)
404514f5e3Sopenharmony_ci    {
414514f5e3Sopenharmony_ci        EcmaVM *ecmaVM = thread->GetEcmaVM();
424514f5e3Sopenharmony_ci        JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
434514f5e3Sopenharmony_ci        return globalEnv->GetObjectFunction().GetObject<JSFunction>();
444514f5e3Sopenharmony_ci    }
454514f5e3Sopenharmony_ci
464514f5e3Sopenharmony_ci    static EcmaRuntimeCallInfo *CreateEcmaRuntimeCallInfo(JSThread *thread, uint32_t numArgs)
474514f5e3Sopenharmony_ci    {
484514f5e3Sopenharmony_ci        auto factory = thread->GetEcmaVM()->GetFactory();
494514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> hclass(thread, JSObjectCreate(thread));
504514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> callee(factory->NewJSObjectByConstructor(JSHandle<JSFunction>::Cast(hclass), hclass));
514514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
524514f5e3Sopenharmony_ci        EcmaRuntimeCallInfo *objCallInfo =
534514f5e3Sopenharmony_ci            EcmaInterpreter::NewRuntimeCallInfo(thread, undefined, callee, undefined, numArgs);
544514f5e3Sopenharmony_ci        return objCallInfo;
554514f5e3Sopenharmony_ci    }
564514f5e3Sopenharmony_ci
574514f5e3Sopenharmony_ci    static JSHandle<JSAPIDeque> CreateJSAPIDeque(JSThread *thread)
584514f5e3Sopenharmony_ci    {
594514f5e3Sopenharmony_ci        auto factory = thread->GetEcmaVM()->GetFactory();
604514f5e3Sopenharmony_ci        JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
614514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
624514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
634514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> value =
644514f5e3Sopenharmony_ci            JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
654514f5e3Sopenharmony_ci
664514f5e3Sopenharmony_ci        auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length
674514f5e3Sopenharmony_ci        objCallInfo->SetFunction(JSTaggedValue::Undefined());
684514f5e3Sopenharmony_ci        objCallInfo->SetThis(value.GetTaggedValue());
694514f5e3Sopenharmony_ci        objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(ContainerTag::Deque))); // 0 means the argument
704514f5e3Sopenharmony_ci        JSTaggedValue result = ContainersPrivate::Load(objCallInfo);
714514f5e3Sopenharmony_ci
724514f5e3Sopenharmony_ci        JSHandle<JSFunction> newTarget(thread, result);
734514f5e3Sopenharmony_ci        auto objCallInfo2 = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length
744514f5e3Sopenharmony_ci        objCallInfo2->SetFunction(newTarget.GetTaggedValue());
754514f5e3Sopenharmony_ci        objCallInfo2->SetNewTarget(newTarget.GetTaggedValue());
764514f5e3Sopenharmony_ci        objCallInfo2->SetThis(JSTaggedValue::Undefined());
774514f5e3Sopenharmony_ci        objCallInfo2->SetCallArg(0, JSTaggedValue::Undefined());
784514f5e3Sopenharmony_ci
794514f5e3Sopenharmony_ci        JSTaggedValue list = ContainersDeque::DequeConstructor(objCallInfo2);
804514f5e3Sopenharmony_ci        JSHandle<JSAPIDeque> deque(thread, list);
814514f5e3Sopenharmony_ci        return deque;
824514f5e3Sopenharmony_ci    }
834514f5e3Sopenharmony_ci
844514f5e3Sopenharmony_ci    static void ContainersDequeInsertFrontFuzzTest(const uint8_t* data, size_t size)
854514f5e3Sopenharmony_ci    {
864514f5e3Sopenharmony_ci        RuntimeOption option;
874514f5e3Sopenharmony_ci        option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
884514f5e3Sopenharmony_ci        EcmaVM *vm = JSNApi::CreateJSVM(option);
894514f5e3Sopenharmony_ci        {
904514f5e3Sopenharmony_ci            JsiFastNativeScope scope(vm);
914514f5e3Sopenharmony_ci            auto thread = vm->GetAssociatedJSThread();
924514f5e3Sopenharmony_ci            JSHandle<JSAPIDeque> deque = CreateJSAPIDeque(thread);
934514f5e3Sopenharmony_ci
944514f5e3Sopenharmony_ci            auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6);
954514f5e3Sopenharmony_ci            callInfo->SetFunction(JSTaggedValue::Undefined());
964514f5e3Sopenharmony_ci            callInfo->SetThis(deque.GetTaggedValue());
974514f5e3Sopenharmony_ci            ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
984514f5e3Sopenharmony_ci            std::string str(data, data + size);
994514f5e3Sopenharmony_ci            JSTaggedValue value = factory->NewFromStdString(str).GetTaggedValue();
1004514f5e3Sopenharmony_ci            callInfo->SetCallArg(0, value);
1014514f5e3Sopenharmony_ci            ContainersDeque::InsertFront(callInfo);
1024514f5e3Sopenharmony_ci        }
1034514f5e3Sopenharmony_ci        JSNApi::DestroyJSVM(vm);
1044514f5e3Sopenharmony_ci    }
1054514f5e3Sopenharmony_ci
1064514f5e3Sopenharmony_ci    class TestClass : public base::BuiltinsBase {
1074514f5e3Sopenharmony_ci        public:
1084514f5e3Sopenharmony_ci            static JSTaggedValue TestForEachFunc(EcmaRuntimeCallInfo *argv)
1094514f5e3Sopenharmony_ci            {
1104514f5e3Sopenharmony_ci                JSThread *thread = argv->GetThread();
1114514f5e3Sopenharmony_ci                JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
1124514f5e3Sopenharmony_ci                JSHandle<JSTaggedValue> index = GetCallArg(argv, 1);
1134514f5e3Sopenharmony_ci                JSHandle<JSTaggedValue> deque = GetCallArg(argv, 2); // 2 means the secode arg
1144514f5e3Sopenharmony_ci                if (!deque->IsUndefined()) {
1154514f5e3Sopenharmony_ci                    if (index->IsNumber() && value->IsNumber()) {
1164514f5e3Sopenharmony_ci                        JSHandle<JSAPIDeque>::Cast(deque)->Set(thread, index->GetInt(),
1174514f5e3Sopenharmony_ci                            JSTaggedValue(value->GetInt() * 2));    // 2 means mul by 2
1184514f5e3Sopenharmony_ci                    }
1194514f5e3Sopenharmony_ci                }
1204514f5e3Sopenharmony_ci                return JSTaggedValue::True();
1214514f5e3Sopenharmony_ci            }
1224514f5e3Sopenharmony_ci    };
1234514f5e3Sopenharmony_ci
1244514f5e3Sopenharmony_ci    static void ContainersDequeForEachFuzzTest(const uint8_t* data, size_t size)
1254514f5e3Sopenharmony_ci    {
1264514f5e3Sopenharmony_ci        uint32_t input = 0;
1274514f5e3Sopenharmony_ci        if (size <= 0) {
1284514f5e3Sopenharmony_ci            return;
1294514f5e3Sopenharmony_ci        }
1304514f5e3Sopenharmony_ci        if (size > MAXBYTELEN) {
1314514f5e3Sopenharmony_ci            size = MAXBYTELEN;
1324514f5e3Sopenharmony_ci        }
1334514f5e3Sopenharmony_ci        if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
1344514f5e3Sopenharmony_ci            std::cout << "memcpy_s failed!";
1354514f5e3Sopenharmony_ci            UNREACHABLE();
1364514f5e3Sopenharmony_ci        }
1374514f5e3Sopenharmony_ci
1384514f5e3Sopenharmony_ci        RuntimeOption option;
1394514f5e3Sopenharmony_ci        option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
1404514f5e3Sopenharmony_ci        EcmaVM *vm = JSNApi::CreateJSVM(option);
1414514f5e3Sopenharmony_ci        {
1424514f5e3Sopenharmony_ci            JsiFastNativeScope scope(vm);
1434514f5e3Sopenharmony_ci            auto thread = vm->GetAssociatedJSThread();
1444514f5e3Sopenharmony_ci
1454514f5e3Sopenharmony_ci            constexpr uint32_t NODE_NUMBERS = 8;
1464514f5e3Sopenharmony_ci            JSHandle<JSAPIDeque> deque = CreateJSAPIDeque(thread);
1474514f5e3Sopenharmony_ci            for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
1484514f5e3Sopenharmony_ci                auto callInfo = CreateEcmaRuntimeCallInfo(thread, 8);
1494514f5e3Sopenharmony_ci                callInfo->SetFunction(JSTaggedValue::Undefined());
1504514f5e3Sopenharmony_ci                callInfo->SetThis(deque.GetTaggedValue());
1514514f5e3Sopenharmony_ci                callInfo->SetCallArg(0, JSTaggedValue(i + input));
1524514f5e3Sopenharmony_ci                ContainersDeque::InsertFront(callInfo);
1534514f5e3Sopenharmony_ci            }
1544514f5e3Sopenharmony_ci
1554514f5e3Sopenharmony_ci            JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1564514f5e3Sopenharmony_ci            JSHandle<JSFunction> func = thread->GetEcmaVM()->GetFactory()->NewJSFunction(env,
1574514f5e3Sopenharmony_ci                reinterpret_cast<void *>(TestClass::TestForEachFunc));
1584514f5e3Sopenharmony_ci            auto callInfo = CreateEcmaRuntimeCallInfo(thread, 8);
1594514f5e3Sopenharmony_ci            callInfo->SetFunction(JSTaggedValue::Undefined());
1604514f5e3Sopenharmony_ci            callInfo->SetThis(deque.GetTaggedValue());
1614514f5e3Sopenharmony_ci            callInfo->SetCallArg(0, func.GetTaggedValue());
1624514f5e3Sopenharmony_ci            callInfo->SetCallArg(1, deque.GetTaggedValue());
1634514f5e3Sopenharmony_ci            ContainersDeque::ForEach(callInfo);
1644514f5e3Sopenharmony_ci        }
1654514f5e3Sopenharmony_ci        JSNApi::DestroyJSVM(vm);
1664514f5e3Sopenharmony_ci    }
1674514f5e3Sopenharmony_ci
1684514f5e3Sopenharmony_ci    static void ContainersDequeGetFirstFuzzTest(const uint8_t* data, size_t size)
1694514f5e3Sopenharmony_ci    {
1704514f5e3Sopenharmony_ci        RuntimeOption option;
1714514f5e3Sopenharmony_ci        option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
1724514f5e3Sopenharmony_ci        EcmaVM *vm = JSNApi::CreateJSVM(option);
1734514f5e3Sopenharmony_ci        {
1744514f5e3Sopenharmony_ci            JsiFastNativeScope scope(vm);
1754514f5e3Sopenharmony_ci            auto thread = vm->GetAssociatedJSThread();
1764514f5e3Sopenharmony_ci
1774514f5e3Sopenharmony_ci            JSHandle<JSAPIDeque> deque = CreateJSAPIDeque(thread);
1784514f5e3Sopenharmony_ci            auto callInfo = CreateEcmaRuntimeCallInfo(thread, 8);
1794514f5e3Sopenharmony_ci            callInfo->SetFunction(JSTaggedValue::Undefined());
1804514f5e3Sopenharmony_ci            callInfo->SetThis(deque.GetTaggedValue());
1814514f5e3Sopenharmony_ci
1824514f5e3Sopenharmony_ci            unsigned int input = 0;
1834514f5e3Sopenharmony_ci            if (size <= 0) {
1844514f5e3Sopenharmony_ci                return;
1854514f5e3Sopenharmony_ci            }
1864514f5e3Sopenharmony_ci            if (size > MAXBYTELEN) {
1874514f5e3Sopenharmony_ci                size = MAXBYTELEN;
1884514f5e3Sopenharmony_ci            }
1894514f5e3Sopenharmony_ci            if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
1904514f5e3Sopenharmony_ci                std::cout << "memcpy_s failed!";
1914514f5e3Sopenharmony_ci                UNREACHABLE();
1924514f5e3Sopenharmony_ci            }
1934514f5e3Sopenharmony_ci            callInfo->SetCallArg(0, JSTaggedValue(input));
1944514f5e3Sopenharmony_ci            ContainersDeque::InsertFront(callInfo);
1954514f5e3Sopenharmony_ci            auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 4);
1964514f5e3Sopenharmony_ci            callInfo1->SetFunction(JSTaggedValue::Undefined());
1974514f5e3Sopenharmony_ci            callInfo1->SetThis(deque.GetTaggedValue());
1984514f5e3Sopenharmony_ci            ContainersDeque::GetFirst(callInfo1);
1994514f5e3Sopenharmony_ci        }
2004514f5e3Sopenharmony_ci        JSNApi::DestroyJSVM(vm);
2014514f5e3Sopenharmony_ci        return;
2024514f5e3Sopenharmony_ci    }
2034514f5e3Sopenharmony_ci
2044514f5e3Sopenharmony_ci    static void ContainersDequeGetLastFuzzTest(const uint8_t* data, size_t size)
2054514f5e3Sopenharmony_ci    {
2064514f5e3Sopenharmony_ci        RuntimeOption option;
2074514f5e3Sopenharmony_ci        option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
2084514f5e3Sopenharmony_ci        EcmaVM *vm = JSNApi::CreateJSVM(option);
2094514f5e3Sopenharmony_ci        {
2104514f5e3Sopenharmony_ci            JsiFastNativeScope scope(vm);
2114514f5e3Sopenharmony_ci            auto thread = vm->GetAssociatedJSThread();
2124514f5e3Sopenharmony_ci
2134514f5e3Sopenharmony_ci            JSHandle<JSAPIDeque> deque = CreateJSAPIDeque(thread);
2144514f5e3Sopenharmony_ci            auto callInfo = CreateEcmaRuntimeCallInfo(thread, 8);
2154514f5e3Sopenharmony_ci            callInfo->SetFunction(JSTaggedValue::Undefined());
2164514f5e3Sopenharmony_ci            callInfo->SetThis(deque.GetTaggedValue());
2174514f5e3Sopenharmony_ci
2184514f5e3Sopenharmony_ci            unsigned int input = 0;
2194514f5e3Sopenharmony_ci            if (size <= 0) {
2204514f5e3Sopenharmony_ci                return;
2214514f5e3Sopenharmony_ci            }
2224514f5e3Sopenharmony_ci            if (size > MAXBYTELEN) {
2234514f5e3Sopenharmony_ci                size = MAXBYTELEN;
2244514f5e3Sopenharmony_ci            }
2254514f5e3Sopenharmony_ci            if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
2264514f5e3Sopenharmony_ci                std::cout << "memcpy_s failed!";
2274514f5e3Sopenharmony_ci                UNREACHABLE();
2284514f5e3Sopenharmony_ci            }
2294514f5e3Sopenharmony_ci            callInfo->SetCallArg(0, JSTaggedValue(input));
2304514f5e3Sopenharmony_ci
2314514f5e3Sopenharmony_ci            ContainersDeque::InsertFront(callInfo);
2324514f5e3Sopenharmony_ci
2334514f5e3Sopenharmony_ci            auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 4);
2344514f5e3Sopenharmony_ci            callInfo1->SetFunction(JSTaggedValue::Undefined());
2354514f5e3Sopenharmony_ci            callInfo1->SetThis(deque.GetTaggedValue());
2364514f5e3Sopenharmony_ci            ContainersDeque::GetLast(callInfo1);
2374514f5e3Sopenharmony_ci        }
2384514f5e3Sopenharmony_ci        JSNApi::DestroyJSVM(vm);
2394514f5e3Sopenharmony_ci        return;
2404514f5e3Sopenharmony_ci    }
2414514f5e3Sopenharmony_ci
2424514f5e3Sopenharmony_ci    static void ContainersDequeInsertEndFuzzTest(const uint8_t* data, size_t size)
2434514f5e3Sopenharmony_ci    {
2444514f5e3Sopenharmony_ci        RuntimeOption option;
2454514f5e3Sopenharmony_ci        option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
2464514f5e3Sopenharmony_ci        EcmaVM *vm = JSNApi::CreateJSVM(option);
2474514f5e3Sopenharmony_ci        {
2484514f5e3Sopenharmony_ci            JsiFastNativeScope scope(vm);
2494514f5e3Sopenharmony_ci            auto thread = vm->GetAssociatedJSThread();
2504514f5e3Sopenharmony_ci
2514514f5e3Sopenharmony_ci            JSHandle<JSAPIDeque> deque = CreateJSAPIDeque(thread);
2524514f5e3Sopenharmony_ci            auto callInfo = CreateEcmaRuntimeCallInfo(thread, 8);
2534514f5e3Sopenharmony_ci            callInfo->SetFunction(JSTaggedValue::Undefined());
2544514f5e3Sopenharmony_ci            callInfo->SetThis(deque.GetTaggedValue());
2554514f5e3Sopenharmony_ci
2564514f5e3Sopenharmony_ci            unsigned int input = 0;
2574514f5e3Sopenharmony_ci            if (size <= 0) {
2584514f5e3Sopenharmony_ci                return;
2594514f5e3Sopenharmony_ci            }
2604514f5e3Sopenharmony_ci            if (size > MAXBYTELEN) {
2614514f5e3Sopenharmony_ci                size = MAXBYTELEN;
2624514f5e3Sopenharmony_ci            }
2634514f5e3Sopenharmony_ci            if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
2644514f5e3Sopenharmony_ci                std::cout << "memcpy_s failed!";
2654514f5e3Sopenharmony_ci                UNREACHABLE();
2664514f5e3Sopenharmony_ci            }
2674514f5e3Sopenharmony_ci            callInfo->SetCallArg(0, JSTaggedValue(input));
2684514f5e3Sopenharmony_ci            ContainersDeque::InsertEnd(callInfo);
2694514f5e3Sopenharmony_ci        }
2704514f5e3Sopenharmony_ci        JSNApi::DestroyJSVM(vm);
2714514f5e3Sopenharmony_ci        return;
2724514f5e3Sopenharmony_ci    }
2734514f5e3Sopenharmony_ci
2744514f5e3Sopenharmony_ci    static void ContainersDequeHasFuzzTest(const uint8_t* data, size_t size)
2754514f5e3Sopenharmony_ci    {
2764514f5e3Sopenharmony_ci        RuntimeOption option;
2774514f5e3Sopenharmony_ci        option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
2784514f5e3Sopenharmony_ci        EcmaVM *vm = JSNApi::CreateJSVM(option);
2794514f5e3Sopenharmony_ci        {
2804514f5e3Sopenharmony_ci            JsiFastNativeScope scope(vm);
2814514f5e3Sopenharmony_ci            auto thread = vm->GetAssociatedJSThread();
2824514f5e3Sopenharmony_ci
2834514f5e3Sopenharmony_ci            JSHandle<JSAPIDeque> deque = CreateJSAPIDeque(thread);
2844514f5e3Sopenharmony_ci            auto callInfo = CreateEcmaRuntimeCallInfo(thread, 8);
2854514f5e3Sopenharmony_ci            callInfo->SetFunction(JSTaggedValue::Undefined());
2864514f5e3Sopenharmony_ci            callInfo->SetThis(deque.GetTaggedValue());
2874514f5e3Sopenharmony_ci
2884514f5e3Sopenharmony_ci            unsigned int input = 0;
2894514f5e3Sopenharmony_ci            if (size <= 0) {
2904514f5e3Sopenharmony_ci                return;
2914514f5e3Sopenharmony_ci            }
2924514f5e3Sopenharmony_ci            if (size > MAXBYTELEN) {
2934514f5e3Sopenharmony_ci                size = MAXBYTELEN;
2944514f5e3Sopenharmony_ci            }
2954514f5e3Sopenharmony_ci            if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
2964514f5e3Sopenharmony_ci                std::cout << "memcpy_s failed!";
2974514f5e3Sopenharmony_ci                UNREACHABLE();
2984514f5e3Sopenharmony_ci            }
2994514f5e3Sopenharmony_ci            callInfo->SetCallArg(0, JSTaggedValue(input));
3004514f5e3Sopenharmony_ci
3014514f5e3Sopenharmony_ci            ContainersDeque::InsertEnd(callInfo);
3024514f5e3Sopenharmony_ci
3034514f5e3Sopenharmony_ci            auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 8);
3044514f5e3Sopenharmony_ci            callInfo1->SetFunction(JSTaggedValue::Undefined());
3054514f5e3Sopenharmony_ci            callInfo1->SetThis(deque.GetTaggedValue());
3064514f5e3Sopenharmony_ci            callInfo1->SetCallArg(0, JSTaggedValue(input));
3074514f5e3Sopenharmony_ci            ContainersDeque::Has(callInfo1);
3084514f5e3Sopenharmony_ci        }
3094514f5e3Sopenharmony_ci        JSNApi::DestroyJSVM(vm);
3104514f5e3Sopenharmony_ci        return;
3114514f5e3Sopenharmony_ci    }
3124514f5e3Sopenharmony_ci
3134514f5e3Sopenharmony_ci    static void ContainersDequePopFirstFuzzTest(const uint8_t* data, size_t size)
3144514f5e3Sopenharmony_ci    {
3154514f5e3Sopenharmony_ci        RuntimeOption option;
3164514f5e3Sopenharmony_ci        option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
3174514f5e3Sopenharmony_ci        EcmaVM *vm = JSNApi::CreateJSVM(option);
3184514f5e3Sopenharmony_ci        {
3194514f5e3Sopenharmony_ci            JsiFastNativeScope scope(vm);
3204514f5e3Sopenharmony_ci            auto thread = vm->GetAssociatedJSThread();
3214514f5e3Sopenharmony_ci
3224514f5e3Sopenharmony_ci            JSHandle<JSAPIDeque> deque = CreateJSAPIDeque(thread);
3234514f5e3Sopenharmony_ci            auto callInfo = CreateEcmaRuntimeCallInfo(thread, 8);
3244514f5e3Sopenharmony_ci            callInfo->SetFunction(JSTaggedValue::Undefined());
3254514f5e3Sopenharmony_ci            callInfo->SetThis(deque.GetTaggedValue());
3264514f5e3Sopenharmony_ci
3274514f5e3Sopenharmony_ci            unsigned int input = 0;
3284514f5e3Sopenharmony_ci            if (size <= 0) {
3294514f5e3Sopenharmony_ci                return;
3304514f5e3Sopenharmony_ci            }
3314514f5e3Sopenharmony_ci            if (size > MAXBYTELEN) {
3324514f5e3Sopenharmony_ci                size = MAXBYTELEN;
3334514f5e3Sopenharmony_ci            }
3344514f5e3Sopenharmony_ci            if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
3354514f5e3Sopenharmony_ci                std::cout << "memcpy_s failed!";
3364514f5e3Sopenharmony_ci                UNREACHABLE();
3374514f5e3Sopenharmony_ci            }
3384514f5e3Sopenharmony_ci            callInfo->SetCallArg(0, JSTaggedValue(input));
3394514f5e3Sopenharmony_ci            ContainersDeque::InsertFront(callInfo);
3404514f5e3Sopenharmony_ci            auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 4);
3414514f5e3Sopenharmony_ci            callInfo1->SetFunction(JSTaggedValue::Undefined());
3424514f5e3Sopenharmony_ci            callInfo1->SetThis(deque.GetTaggedValue());
3434514f5e3Sopenharmony_ci            ContainersDeque::PopFirst(callInfo1);
3444514f5e3Sopenharmony_ci        }
3454514f5e3Sopenharmony_ci        JSNApi::DestroyJSVM(vm);
3464514f5e3Sopenharmony_ci        return;
3474514f5e3Sopenharmony_ci    }
3484514f5e3Sopenharmony_ci
3494514f5e3Sopenharmony_ci    static void ContainersDequePopLastFuzzTest(const uint8_t* data, size_t size)
3504514f5e3Sopenharmony_ci    {
3514514f5e3Sopenharmony_ci        RuntimeOption option;
3524514f5e3Sopenharmony_ci        option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
3534514f5e3Sopenharmony_ci        EcmaVM *vm = JSNApi::CreateJSVM(option);
3544514f5e3Sopenharmony_ci        {
3554514f5e3Sopenharmony_ci            JsiFastNativeScope scope(vm);
3564514f5e3Sopenharmony_ci            auto thread = vm->GetAssociatedJSThread();
3574514f5e3Sopenharmony_ci
3584514f5e3Sopenharmony_ci            JSHandle<JSAPIDeque> deque = CreateJSAPIDeque(thread);
3594514f5e3Sopenharmony_ci            auto callInfo = CreateEcmaRuntimeCallInfo(thread, 8);
3604514f5e3Sopenharmony_ci            callInfo->SetFunction(JSTaggedValue::Undefined());
3614514f5e3Sopenharmony_ci            callInfo->SetThis(deque.GetTaggedValue());
3624514f5e3Sopenharmony_ci
3634514f5e3Sopenharmony_ci            unsigned int input = 0;
3644514f5e3Sopenharmony_ci            if (size <= 0) {
3654514f5e3Sopenharmony_ci                return;
3664514f5e3Sopenharmony_ci            }
3674514f5e3Sopenharmony_ci            if (size > MAXBYTELEN) {
3684514f5e3Sopenharmony_ci                size = MAXBYTELEN;
3694514f5e3Sopenharmony_ci            }
3704514f5e3Sopenharmony_ci            if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
3714514f5e3Sopenharmony_ci                std::cout << "memcpy_s failed!";
3724514f5e3Sopenharmony_ci                UNREACHABLE();
3734514f5e3Sopenharmony_ci            }
3744514f5e3Sopenharmony_ci            callInfo->SetCallArg(0, JSTaggedValue(input));
3754514f5e3Sopenharmony_ci            ContainersDeque::InsertFront(callInfo);
3764514f5e3Sopenharmony_ci            auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 4);
3774514f5e3Sopenharmony_ci            callInfo1->SetFunction(JSTaggedValue::Undefined());
3784514f5e3Sopenharmony_ci            callInfo1->SetThis(deque.GetTaggedValue());
3794514f5e3Sopenharmony_ci            ContainersDeque::PopLast(callInfo1);
3804514f5e3Sopenharmony_ci        }
3814514f5e3Sopenharmony_ci        JSNApi::DestroyJSVM(vm);
3824514f5e3Sopenharmony_ci        return;
3834514f5e3Sopenharmony_ci    }
3844514f5e3Sopenharmony_ci
3854514f5e3Sopenharmony_ci    static void ContainersDequeIteratorFuzzTest(const uint8_t* data, size_t size)
3864514f5e3Sopenharmony_ci    {
3874514f5e3Sopenharmony_ci        RuntimeOption option;
3884514f5e3Sopenharmony_ci        option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
3894514f5e3Sopenharmony_ci        EcmaVM *vm = JSNApi::CreateJSVM(option);
3904514f5e3Sopenharmony_ci        {
3914514f5e3Sopenharmony_ci            JsiFastNativeScope scope(vm);
3924514f5e3Sopenharmony_ci            auto thread = vm->GetAssociatedJSThread();
3934514f5e3Sopenharmony_ci
3944514f5e3Sopenharmony_ci            uint32_t input = 0;
3954514f5e3Sopenharmony_ci            if (size <= 0) {
3964514f5e3Sopenharmony_ci                return;
3974514f5e3Sopenharmony_ci            }
3984514f5e3Sopenharmony_ci            if (size > MAXBYTELEN) {
3994514f5e3Sopenharmony_ci                size = MAXBYTELEN;
4004514f5e3Sopenharmony_ci            }
4014514f5e3Sopenharmony_ci            if (memcpy_s(&input, MAXBYTELEN, data, size) != 0) {
4024514f5e3Sopenharmony_ci                std::cout << "memcpy_s failed!";
4034514f5e3Sopenharmony_ci                UNREACHABLE();
4044514f5e3Sopenharmony_ci            }
4054514f5e3Sopenharmony_ci
4064514f5e3Sopenharmony_ci            constexpr uint32_t NODE_NUMBERS = 8;
4074514f5e3Sopenharmony_ci            JSHandle<JSAPIDeque> deque = CreateJSAPIDeque(thread);
4084514f5e3Sopenharmony_ci            for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
4094514f5e3Sopenharmony_ci                auto callInfo = CreateEcmaRuntimeCallInfo(thread, 8);
4104514f5e3Sopenharmony_ci                callInfo->SetFunction(JSTaggedValue::Undefined());
4114514f5e3Sopenharmony_ci                callInfo->SetThis(deque.GetTaggedValue());
4124514f5e3Sopenharmony_ci                callInfo->SetCallArg(0, JSTaggedValue(i + input));
4134514f5e3Sopenharmony_ci
4144514f5e3Sopenharmony_ci                ContainersDeque::InsertEnd(callInfo);
4154514f5e3Sopenharmony_ci            }
4164514f5e3Sopenharmony_ci
4174514f5e3Sopenharmony_ci            auto callInfo1 = CreateEcmaRuntimeCallInfo(thread, 4);
4184514f5e3Sopenharmony_ci            callInfo1->SetFunction(JSTaggedValue::Undefined());
4194514f5e3Sopenharmony_ci            callInfo1->SetThis(deque.GetTaggedValue());
4204514f5e3Sopenharmony_ci            JSHandle<JSTaggedValue> iterValues(thread, ContainersDeque::GetIteratorObj(callInfo1));
4214514f5e3Sopenharmony_ci
4224514f5e3Sopenharmony_ci            JSMutableHandle<JSTaggedValue> result(thread, JSTaggedValue::Undefined());
4234514f5e3Sopenharmony_ci            for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
4244514f5e3Sopenharmony_ci                auto callInfo = CreateEcmaRuntimeCallInfo(thread, 4);
4254514f5e3Sopenharmony_ci                callInfo->SetFunction(JSTaggedValue::Undefined());
4264514f5e3Sopenharmony_ci                callInfo->SetThis(iterValues.GetTaggedValue());
4274514f5e3Sopenharmony_ci                result.Update(JSAPIDequeIterator::Next(callInfo));
4284514f5e3Sopenharmony_ci            }
4294514f5e3Sopenharmony_ci        }
4304514f5e3Sopenharmony_ci        JSNApi::DestroyJSVM(vm);
4314514f5e3Sopenharmony_ci        return;
4324514f5e3Sopenharmony_ci    }
4334514f5e3Sopenharmony_ci};
4344514f5e3Sopenharmony_ci}
4354514f5e3Sopenharmony_ci#endif