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 "jsvaluerefislightweightmapandset_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/containers/containers_hashset.h" 314514f5e3Sopenharmony_ci#include "ecmascript/common.h" 324514f5e3Sopenharmony_ci#include "ecmascript/frames.h" 334514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h" 344514f5e3Sopenharmony_ci#include "ecmascript/js_set.h" 354514f5e3Sopenharmony_ci#include "ecmascript/js_set_iterator.h" 364514f5e3Sopenharmony_ci#include "ecmascript/js_map.h" 374514f5e3Sopenharmony_ci#include "ecmascript/js_weak_container.h" 384514f5e3Sopenharmony_ci#include "ecmascript/js_map_iterator.h" 394514f5e3Sopenharmony_ci#include "ecmascript/containers/containers_arraylist.h" 404514f5e3Sopenharmony_ci#include "ecmascript/js_api/js_api_arraylist.h" 414514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_function.h" 424514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins.h" 434514f5e3Sopenharmony_ci#include "ecmascript/ecma_global_storage.h" 444514f5e3Sopenharmony_ci#include "ecmascript/js_bigint.h" 454514f5e3Sopenharmony_ci#include "ecmascript/js_runtime_options.h" 464514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h" 474514f5e3Sopenharmony_ci#include "ecmascript/tagged_array.h" 484514f5e3Sopenharmony_ci#include "ecmascript/js_generator_object.h" 494514f5e3Sopenharmony_ci#include "ecmascript/js_string_iterator.h" 504514f5e3Sopenharmony_ci#include "ecmascript/js_date_time_format.h" 514514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_number.h" 524514f5e3Sopenharmony_ci#include "ecmascript/js_api/js_api_hashmap.h" 534514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_regexp.h" 544514f5e3Sopenharmony_ci#include "ecmascript/js_regexp.h" 554514f5e3Sopenharmony_ci#include "ecmascript/tagged_hash_array.h" 564514f5e3Sopenharmony_ci#include "ecmascript/containers/containers_lightweightmap.h" 574514f5e3Sopenharmony_ci#include "ecmascript/containers/containers_lightweightset.h" 584514f5e3Sopenharmony_ci 594514f5e3Sopenharmony_ciusing namespace panda; 604514f5e3Sopenharmony_ciusing namespace panda::test; 614514f5e3Sopenharmony_ciusing namespace panda::ecmascript; 624514f5e3Sopenharmony_ciusing namespace panda::ecmascript::containers; 634514f5e3Sopenharmony_ci 644514f5e3Sopenharmony_cinamespace OHOS { 654514f5e3Sopenharmony_ciEcmaRuntimeCallInfo *CreateEcmaRuntimeCallInfo(JSThread *thread, JSTaggedValue newTgt, uint32_t argvLength) 664514f5e3Sopenharmony_ci{ 674514f5e3Sopenharmony_ci const uint8_t testDecodedSize = 2; 684514f5e3Sopenharmony_ci int32_t numActualArgs = argvLength / testDecodedSize + 1; 694514f5e3Sopenharmony_ci JSTaggedType *sp = const_cast<JSTaggedType *>(thread->GetCurrentSPFrame()); 704514f5e3Sopenharmony_ci 714514f5e3Sopenharmony_ci size_t frameSize = 0; 724514f5e3Sopenharmony_ci if (thread->IsAsmInterpreter()) { 734514f5e3Sopenharmony_ci frameSize = InterpretedEntryFrame::NumOfMembers() + numActualArgs; 744514f5e3Sopenharmony_ci } else { 754514f5e3Sopenharmony_ci frameSize = InterpretedFrame::NumOfMembers() + numActualArgs; 764514f5e3Sopenharmony_ci } 774514f5e3Sopenharmony_ci JSTaggedType *newSp = sp - frameSize; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 784514f5e3Sopenharmony_ci for (int i = numActualArgs; i > 0; i--) { 794514f5e3Sopenharmony_ci newSp[i - 1] = JSTaggedValue::Undefined().GetRawData(); 804514f5e3Sopenharmony_ci } 814514f5e3Sopenharmony_ci EcmaRuntimeCallInfo *ecmaRuntimeCallInfo = reinterpret_cast<EcmaRuntimeCallInfo *>(newSp - 2); 824514f5e3Sopenharmony_ci *(--newSp) = numActualArgs; 834514f5e3Sopenharmony_ci *(--newSp) = panda::ecmascript::ToUintPtr(thread); 844514f5e3Sopenharmony_ci ecmaRuntimeCallInfo->SetNewTarget(newTgt); 854514f5e3Sopenharmony_ci return ecmaRuntimeCallInfo; 864514f5e3Sopenharmony_ci} 874514f5e3Sopenharmony_ci 884514f5e3Sopenharmony_cistatic JSTaggedType *SetupFrame(JSThread *thread, EcmaRuntimeCallInfo *info) 894514f5e3Sopenharmony_ci{ 904514f5e3Sopenharmony_ci JSTaggedType *sp = const_cast<JSTaggedType *>(thread->GetCurrentSPFrame()); 914514f5e3Sopenharmony_ci size_t frameSize = 0; 924514f5e3Sopenharmony_ci const int num = 2; 934514f5e3Sopenharmony_ci // 2 means thread and numArgs 944514f5e3Sopenharmony_ci if (thread->IsAsmInterpreter()) { 954514f5e3Sopenharmony_ci frameSize = InterpretedEntryFrame::NumOfMembers() + info->GetArgsNumber() + NUM_MANDATORY_JSFUNC_ARGS + num; 964514f5e3Sopenharmony_ci } else { 974514f5e3Sopenharmony_ci frameSize = InterpretedFrame::NumOfMembers() + info->GetArgsNumber() + NUM_MANDATORY_JSFUNC_ARGS + num; 984514f5e3Sopenharmony_ci } 994514f5e3Sopenharmony_ci JSTaggedType *newSp = sp - frameSize; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) 1004514f5e3Sopenharmony_ci 1014514f5e3Sopenharmony_ci InterpretedEntryFrame *state = reinterpret_cast<InterpretedEntryFrame *>(newSp) - 1; 1024514f5e3Sopenharmony_ci state->base.type = ecmascript::FrameType::INTERPRETER_ENTRY_FRAME; 1034514f5e3Sopenharmony_ci state->base.prev = sp; 1044514f5e3Sopenharmony_ci state->pc = nullptr; 1054514f5e3Sopenharmony_ci thread->SetCurrentSPFrame(newSp); 1064514f5e3Sopenharmony_ci return sp; 1074514f5e3Sopenharmony_ci} 1084514f5e3Sopenharmony_ci 1094514f5e3Sopenharmony_civoid TearDownFrame(JSThread *thread, JSTaggedType *prev) 1104514f5e3Sopenharmony_ci{ 1114514f5e3Sopenharmony_ci thread->SetCurrentSPFrame(prev); 1124514f5e3Sopenharmony_ci} 1134514f5e3Sopenharmony_ci 1144514f5e3Sopenharmony_ciJSHandle<JSAPILightWeightMap> ConstructobjectLightWeightMap(JSThread *thread) 1154514f5e3Sopenharmony_ci{ 1164514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1174514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 1184514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject(); 1194514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate")); 1204514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value = 1214514f5e3Sopenharmony_ci JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue(); 1224514f5e3Sopenharmony_ci auto objCallInfo1 = CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 1234514f5e3Sopenharmony_ci objCallInfo1->SetFunction(JSTaggedValue::Undefined()); 1244514f5e3Sopenharmony_ci objCallInfo1->SetThis(value.GetTaggedValue()); 1254514f5e3Sopenharmony_ci objCallInfo1->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::LightWeightMap))); 1264514f5e3Sopenharmony_ci [[maybe_unused]] auto prev1 = SetupFrame(thread, objCallInfo1); 1274514f5e3Sopenharmony_ci JSTaggedValue result1 = ecmascript::containers::ContainersPrivate::Load(objCallInfo1); 1284514f5e3Sopenharmony_ci JSHandle<JSFunction> newTarget(thread, result1); 1294514f5e3Sopenharmony_ci auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4); 1304514f5e3Sopenharmony_ci objCallInfo->SetFunction(newTarget.GetTaggedValue()); 1314514f5e3Sopenharmony_ci objCallInfo->SetNewTarget(newTarget.GetTaggedValue()); 1324514f5e3Sopenharmony_ci objCallInfo->SetThis(JSTaggedValue::Undefined()); 1334514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = SetupFrame(thread, objCallInfo); 1344514f5e3Sopenharmony_ci JSTaggedValue result = ecmascript::containers::ContainersLightWeightMap::LightWeightMapConstructor(objCallInfo); 1354514f5e3Sopenharmony_ci TearDownFrame(thread, prev); 1364514f5e3Sopenharmony_ci JSHandle<JSAPILightWeightMap> mapHandle(thread, result); 1374514f5e3Sopenharmony_ci return mapHandle; 1384514f5e3Sopenharmony_ci} 1394514f5e3Sopenharmony_ci 1404514f5e3Sopenharmony_ciJSHandle<JSAPILightWeightSet> ConstructobjectLightWeightSet(JSThread *thread) 1414514f5e3Sopenharmony_ci{ 1424514f5e3Sopenharmony_ci ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 1434514f5e3Sopenharmony_ci JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 1444514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject(); 1454514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate")); 1464514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> value = 1474514f5e3Sopenharmony_ci JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue(); 1484514f5e3Sopenharmony_ci auto objCallInfo1 = CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6); 1494514f5e3Sopenharmony_ci objCallInfo1->SetFunction(JSTaggedValue::Undefined()); 1504514f5e3Sopenharmony_ci objCallInfo1->SetThis(value.GetTaggedValue()); 1514514f5e3Sopenharmony_ci objCallInfo1->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::LightWeightSet))); 1524514f5e3Sopenharmony_ci [[maybe_unused]] auto prev1 = SetupFrame(thread, objCallInfo1); 1534514f5e3Sopenharmony_ci JSTaggedValue result1 = ecmascript::containers::ContainersPrivate::Load(objCallInfo1); 1544514f5e3Sopenharmony_ci JSHandle<JSFunction> newTarget(thread, result1); 1554514f5e3Sopenharmony_ci auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4); 1564514f5e3Sopenharmony_ci objCallInfo->SetFunction(newTarget.GetTaggedValue()); 1574514f5e3Sopenharmony_ci objCallInfo->SetNewTarget(newTarget.GetTaggedValue()); 1584514f5e3Sopenharmony_ci objCallInfo->SetThis(JSTaggedValue::Undefined()); 1594514f5e3Sopenharmony_ci [[maybe_unused]] auto prev = SetupFrame(thread, objCallInfo); 1604514f5e3Sopenharmony_ci JSTaggedValue result = ecmascript::containers::ContainersLightWeightSet::LightWeightSetConstructor(objCallInfo); 1614514f5e3Sopenharmony_ci TearDownFrame(thread, prev); 1624514f5e3Sopenharmony_ci JSHandle<JSAPILightWeightSet> mapHandle(thread, result); 1634514f5e3Sopenharmony_ci return mapHandle; 1644514f5e3Sopenharmony_ci} 1654514f5e3Sopenharmony_ci 1664514f5e3Sopenharmony_civoid JSValueRefIsLightWeightMapFuzzTest([[maybe_unused]] const uint8_t *data, size_t size) 1674514f5e3Sopenharmony_ci{ 1684514f5e3Sopenharmony_ci RuntimeOption option; 1694514f5e3Sopenharmony_ci option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 1704514f5e3Sopenharmony_ci EcmaVM *vm = JSNApi::CreateJSVM(option); 1714514f5e3Sopenharmony_ci { 1724514f5e3Sopenharmony_ci JsiFastNativeScope scope(vm); 1734514f5e3Sopenharmony_ci if (size <= 0) { 1744514f5e3Sopenharmony_ci LOG_ECMA(ERROR) << "Parameter out of range.."; 1754514f5e3Sopenharmony_ci return; 1764514f5e3Sopenharmony_ci } 1774514f5e3Sopenharmony_ci auto thread = vm->GetAssociatedJSThread(); 1784514f5e3Sopenharmony_ci JSHandle<JSAPILightWeightMap> mapHandle = ConstructobjectLightWeightMap(thread); 1794514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> jshashmap = JSHandle<JSTaggedValue>::Cast(mapHandle); 1804514f5e3Sopenharmony_ci Local<JSValueRef> tag = JSNApiHelper::ToLocal<JSValueRef>(jshashmap); 1814514f5e3Sopenharmony_ci tag->IsLightWeightMap(vm); 1824514f5e3Sopenharmony_ci } 1834514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(vm); 1844514f5e3Sopenharmony_ci} 1854514f5e3Sopenharmony_ci 1864514f5e3Sopenharmony_civoid JSValueRefIsLightWeightSetFuzzTest([[maybe_unused]] const uint8_t *data, size_t size) 1874514f5e3Sopenharmony_ci{ 1884514f5e3Sopenharmony_ci RuntimeOption option; 1894514f5e3Sopenharmony_ci option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 1904514f5e3Sopenharmony_ci EcmaVM *vm = JSNApi::CreateJSVM(option); 1914514f5e3Sopenharmony_ci { 1924514f5e3Sopenharmony_ci JsiFastNativeScope scope(vm); 1934514f5e3Sopenharmony_ci if (size <= 0) { 1944514f5e3Sopenharmony_ci LOG_ECMA(ERROR) << "Parameter out of range.."; 1954514f5e3Sopenharmony_ci return; 1964514f5e3Sopenharmony_ci } 1974514f5e3Sopenharmony_ci auto thread = vm->GetAssociatedJSThread(); 1984514f5e3Sopenharmony_ci JSHandle<JSAPILightWeightSet> mapHandle = ConstructobjectLightWeightSet(thread); 1994514f5e3Sopenharmony_ci JSHandle<JSTaggedValue> jshashmap = JSHandle<JSTaggedValue>::Cast(mapHandle); 2004514f5e3Sopenharmony_ci Local<JSValueRef> tag = JSNApiHelper::ToLocal<JSValueRef>(jshashmap); 2014514f5e3Sopenharmony_ci tag->IsLightWeightSet(vm); 2024514f5e3Sopenharmony_ci } 2034514f5e3Sopenharmony_ci JSNApi::DestroyJSVM(vm); 2044514f5e3Sopenharmony_ci} 2054514f5e3Sopenharmony_ci} 2064514f5e3Sopenharmony_ci 2074514f5e3Sopenharmony_ci// Fuzzer entry point. 2084514f5e3Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) 2094514f5e3Sopenharmony_ci{ 2104514f5e3Sopenharmony_ci // Run your code on data. 2114514f5e3Sopenharmony_ci OHOS::JSValueRefIsLightWeightMapFuzzTest(data, size); 2124514f5e3Sopenharmony_ci OHOS::JSValueRefIsLightWeightSetFuzzTest(data, size); 2134514f5e3Sopenharmony_ci return 0; 2144514f5e3Sopenharmony_ci}