14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2023 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#include "jsvaluerefishashmap_fuzzer.h"
174514f5e3Sopenharmony_ci#include "ecmascript/containers/containers_list.h"
184514f5e3Sopenharmony_ci#include "ecmascript/containers/containers_private.h"
194514f5e3Sopenharmony_ci#include "ecmascript/ecma_string-inl.h"
204514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h"
214514f5e3Sopenharmony_ci#include "ecmascript/global_env.h"
224514f5e3Sopenharmony_ci#include "ecmascript/js_handle.h"
234514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h"
244514f5e3Sopenharmony_ci#include "ecmascript/napi/include/jsnapi.h"
254514f5e3Sopenharmony_ci#include "ecmascript/js_thread.h"
264514f5e3Sopenharmony_ci#include "ecmascript/js_global_object.h"
274514f5e3Sopenharmony_ci#include "ecmascript/napi/jsnapi_helper.h"
284514f5e3Sopenharmony_ci#include "ecmascript/linked_hash_table.h"
294514f5e3Sopenharmony_ci#include "ecmascript/ecma_runtime_call_info.h"
304514f5e3Sopenharmony_ci#include "ecmascript/common.h"
314514f5e3Sopenharmony_ci#include "ecmascript/frames.h"
324514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h"
334514f5e3Sopenharmony_ci#include "ecmascript/js_set.h"
344514f5e3Sopenharmony_ci#include "ecmascript/js_set_iterator.h"
354514f5e3Sopenharmony_ci#include "ecmascript/js_map.h"
364514f5e3Sopenharmony_ci#include "ecmascript/js_weak_container.h"
374514f5e3Sopenharmony_ci#include "ecmascript/js_map_iterator.h"
384514f5e3Sopenharmony_ci#include "ecmascript/containers/containers_arraylist.h"
394514f5e3Sopenharmony_ci#include "ecmascript/js_api/js_api_arraylist.h"
404514f5e3Sopenharmony_ci
414514f5e3Sopenharmony_ciusing namespace panda;
424514f5e3Sopenharmony_ciusing namespace panda::test;
434514f5e3Sopenharmony_ciusing namespace panda::ecmascript;
444514f5e3Sopenharmony_ciusing namespace panda::ecmascript::containers;
454514f5e3Sopenharmony_ci
464514f5e3Sopenharmony_cinamespace OHOS {
474514f5e3Sopenharmony_ciEcmaRuntimeCallInfo *CreateEcmaRuntimeCallInfo(JSThread *thread, JSTaggedValue newTgt, uint32_t argvLength)
484514f5e3Sopenharmony_ci{
494514f5e3Sopenharmony_ci    const uint8_t testDecodedSize = 2;
504514f5e3Sopenharmony_ci    int32_t numActualArgs = argvLength / testDecodedSize + 1;
514514f5e3Sopenharmony_ci    JSTaggedType *sp = const_cast<JSTaggedType *>(thread->GetCurrentSPFrame());
524514f5e3Sopenharmony_ci
534514f5e3Sopenharmony_ci    size_t frameSize = 0;
544514f5e3Sopenharmony_ci    if (thread->IsAsmInterpreter()) {
554514f5e3Sopenharmony_ci        frameSize = InterpretedEntryFrame::NumOfMembers() + numActualArgs;
564514f5e3Sopenharmony_ci    } else {
574514f5e3Sopenharmony_ci        frameSize = InterpretedFrame::NumOfMembers() + numActualArgs;
584514f5e3Sopenharmony_ci    }
594514f5e3Sopenharmony_ci    JSTaggedType *newSp = sp - frameSize; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
604514f5e3Sopenharmony_ci    for (int i = numActualArgs; i > 0; i--) {
614514f5e3Sopenharmony_ci        newSp[i - 1] = JSTaggedValue::Undefined().GetRawData();
624514f5e3Sopenharmony_ci    }
634514f5e3Sopenharmony_ci    EcmaRuntimeCallInfo *ecmaRuntimeCallInfo = reinterpret_cast<EcmaRuntimeCallInfo *>(newSp - 2);
644514f5e3Sopenharmony_ci    *(--newSp) = numActualArgs;
654514f5e3Sopenharmony_ci    *(--newSp) = panda::ecmascript::ToUintPtr(thread);
664514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetNewTarget(newTgt);
674514f5e3Sopenharmony_ci    return ecmaRuntimeCallInfo;
684514f5e3Sopenharmony_ci}
694514f5e3Sopenharmony_ci
704514f5e3Sopenharmony_cistatic JSTaggedType *SetupFrame(JSThread *thread, EcmaRuntimeCallInfo *info)
714514f5e3Sopenharmony_ci{
724514f5e3Sopenharmony_ci    JSTaggedType *sp = const_cast<JSTaggedType *>(thread->GetCurrentSPFrame());
734514f5e3Sopenharmony_ci    size_t frameSize = 0;
744514f5e3Sopenharmony_ci    const int num = 2;
754514f5e3Sopenharmony_ci    // 2 means thread and numArgs
764514f5e3Sopenharmony_ci    if (thread->IsAsmInterpreter()) {
774514f5e3Sopenharmony_ci        frameSize = InterpretedEntryFrame::NumOfMembers() + info->GetArgsNumber() + NUM_MANDATORY_JSFUNC_ARGS + num;
784514f5e3Sopenharmony_ci    } else {
794514f5e3Sopenharmony_ci        frameSize = InterpretedFrame::NumOfMembers() + info->GetArgsNumber() + NUM_MANDATORY_JSFUNC_ARGS + num;
804514f5e3Sopenharmony_ci    }
814514f5e3Sopenharmony_ci    JSTaggedType *newSp = sp - frameSize; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
824514f5e3Sopenharmony_ci
834514f5e3Sopenharmony_ci    InterpretedEntryFrame *state = reinterpret_cast<InterpretedEntryFrame *>(newSp) - 1;
844514f5e3Sopenharmony_ci    state->base.type = ecmascript::FrameType::INTERPRETER_ENTRY_FRAME;
854514f5e3Sopenharmony_ci    state->base.prev = sp;
864514f5e3Sopenharmony_ci    state->pc = nullptr;
874514f5e3Sopenharmony_ci    thread->SetCurrentSPFrame(newSp);
884514f5e3Sopenharmony_ci    return sp;
894514f5e3Sopenharmony_ci}
904514f5e3Sopenharmony_ci
914514f5e3Sopenharmony_civoid TearDownFrame(JSThread *thread, JSTaggedType *prev)
924514f5e3Sopenharmony_ci{
934514f5e3Sopenharmony_ci    thread->SetCurrentSPFrame(prev);
944514f5e3Sopenharmony_ci}
954514f5e3Sopenharmony_ci
964514f5e3Sopenharmony_ciJSHandle<JSAPIHashMap> ConstructobjectHashMap(JSThread *thread)
974514f5e3Sopenharmony_ci{
984514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
994514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1004514f5e3Sopenharmony_ci
1014514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
1024514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
1034514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> value =
1044514f5e3Sopenharmony_ci        JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
1054514f5e3Sopenharmony_ci    auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
1064514f5e3Sopenharmony_ci    objCallInfo->SetFunction(JSTaggedValue::Undefined());
1074514f5e3Sopenharmony_ci    objCallInfo->SetThis(value.GetTaggedValue());
1084514f5e3Sopenharmony_ci    objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::HashMap)));
1094514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = SetupFrame(thread, objCallInfo);
1104514f5e3Sopenharmony_ci    JSTaggedValue result = containers::ContainersPrivate::Load(objCallInfo);
1114514f5e3Sopenharmony_ci    TearDownFrame(thread, prev);
1124514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> constructor(thread, result);
1134514f5e3Sopenharmony_ci    JSHandle<JSAPIHashMap> map(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
1144514f5e3Sopenharmony_ci    return map;
1154514f5e3Sopenharmony_ci}
1164514f5e3Sopenharmony_ci
1174514f5e3Sopenharmony_ci
1184514f5e3Sopenharmony_civoid JSValueRefIsHashMapFuzzTest([[maybe_unused]] const uint8_t *data, size_t size)
1194514f5e3Sopenharmony_ci{
1204514f5e3Sopenharmony_ci    RuntimeOption option;
1214514f5e3Sopenharmony_ci    option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
1224514f5e3Sopenharmony_ci    EcmaVM *vm = JSNApi::CreateJSVM(option);
1234514f5e3Sopenharmony_ci    {
1244514f5e3Sopenharmony_ci        JsiFastNativeScope scope(vm);
1254514f5e3Sopenharmony_ci        if (size <= 0) {
1264514f5e3Sopenharmony_ci            LOG_ECMA(ERROR) << "Parameter out of range..";
1274514f5e3Sopenharmony_ci            return;
1284514f5e3Sopenharmony_ci        }
1294514f5e3Sopenharmony_ci        auto thread = vm->GetAssociatedJSThread();
1304514f5e3Sopenharmony_ci        JSHandle<JSAPIHashMap> map = ConstructobjectHashMap(thread);
1314514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> jshashmap = JSHandle<JSTaggedValue>::Cast(map);
1324514f5e3Sopenharmony_ci        Local<JSValueRef> tag = JSNApiHelper::ToLocal<JSValueRef>(jshashmap);
1334514f5e3Sopenharmony_ci        tag->IsHashMap(vm);
1344514f5e3Sopenharmony_ci    }
1354514f5e3Sopenharmony_ci    JSNApi::DestroyJSVM(vm);
1364514f5e3Sopenharmony_ci}
1374514f5e3Sopenharmony_ci}
1384514f5e3Sopenharmony_ci
1394514f5e3Sopenharmony_ci// Fuzzer entry point.
1404514f5e3Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
1414514f5e3Sopenharmony_ci{
1424514f5e3Sopenharmony_ci    // Run your code on data.
1434514f5e3Sopenharmony_ci    OHOS::JSValueRefIsHashMapFuzzTest(data, size);
1444514f5e3Sopenharmony_ci    return 0;
1454514f5e3Sopenharmony_ci}