/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "containerstreesetremove_fuzzer.h" #include "ecmascript/containers/containers_private.h" #include "ecmascript/containers/containers_treeset.h" #include "ecmascript/ecma_string-inl.h" #include "ecmascript/ecma_vm.h" #include "ecmascript/global_env.h" #include "ecmascript/js_handle.h" #include "ecmascript/napi/include/jsnapi.h" using namespace panda; using namespace panda::ecmascript; using namespace panda::ecmascript::containers; namespace OHOS { JSFunction *JSObjectCreate(JSThread *thread) { EcmaVM *ecmaVM = thread->GetEcmaVM(); JSHandle globalEnv = ecmaVM->GetGlobalEnv(); return globalEnv->GetObjectFunction().GetObject(); } EcmaRuntimeCallInfo *CreateEcmaRuntimeCallInfo(JSThread *thread, uint32_t numArgs) { auto factory = thread->GetEcmaVM()->GetFactory(); JSHandle hclass(thread, JSObjectCreate(thread)); JSHandle callee(factory->NewJSObjectByConstructor(JSHandle::Cast(hclass), hclass)); JSHandle undefined = thread->GlobalConstants()->GetHandledUndefined(); EcmaRuntimeCallInfo *objCallInfo = EcmaInterpreter::NewRuntimeCallInfo(thread, undefined, callee, undefined, numArgs); return objCallInfo; } JSTaggedValue InitializeTreeSetConstructor(JSThread *thread) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle env = thread->GetEcmaVM()->GetGlobalEnv(); JSHandle globalObject = env->GetJSGlobalObject(); JSHandle key(factory->NewFromASCII("ArkPrivate")); JSHandle value = JSObject::GetProperty(thread, JSHandle(globalObject), key).GetValue(); auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6); objCallInfo->SetFunction(JSTaggedValue::Undefined()); objCallInfo->SetThis(value.GetTaggedValue()); objCallInfo->SetCallArg(0, JSTaggedValue(static_cast(ContainerTag::TreeSet))); JSTaggedValue result = ContainersPrivate::Load(objCallInfo); return result; } JSHandle CreateJSAPITreeSet(JSThread *thread) { JSHandle newTarget(thread, InitializeTreeSetConstructor(thread)); auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6); objCallInfo->SetFunction(newTarget.GetTaggedValue()); objCallInfo->SetNewTarget(newTarget.GetTaggedValue()); objCallInfo->SetThis(JSTaggedValue::Undefined()); JSTaggedValue result = ContainersTreeSet::TreeSetConstructor(objCallInfo); JSHandle set(thread, result); return set; } void ContainersTreeSetRemoveFuzzTest(const uint8_t* data, size_t size) { RuntimeOption option; option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); EcmaVM *vm = JSNApi::CreateJSVM(option); JSThread *thread = vm->GetJSThread(); if (size <= 0) { return; } int32_t key = 0; size_t maxByteLen = 4; if (size > maxByteLen) { size = maxByteLen; } if (memcpy_s(&key, maxByteLen, data, size) != EOK) { std::cout << "memcpy_s failed!"; UNREACHABLE(); } JSHandle tset = CreateJSAPITreeSet(thread); auto callInfo = CreateEcmaRuntimeCallInfo(thread, 6); callInfo->SetFunction(JSTaggedValue::Undefined()); callInfo->SetThis(tset.GetTaggedValue()); callInfo->SetCallArg(0, JSTaggedValue(key)); ContainersTreeSet::Add(callInfo); auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6); objCallInfo->SetFunction(JSTaggedValue::Undefined()); objCallInfo->SetThis(tset.GetTaggedValue()); objCallInfo->SetCallArg(0, JSTaggedValue(key)); ContainersTreeSet::Remove(objCallInfo); JSNApi::DestroyJSVM(vm); } } // Fuzzer entry point. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { // Run your code on data. OHOS::ContainersTreeSetRemoveFuzzTest(data, size); return 0; }