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 CONTAINERSPRIVATECOMMON_FUZZER_H 17#define CONTAINERSPRIVATECOMMON_FUZZER_H 18 19#include "ecmascript/containers/containers_private.h" 20#include "ecmascript/ecma_string-inl.h" 21#include "ecmascript/ecma_vm.h" 22#include "ecmascript/global_env.h" 23#include "ecmascript/js_handle.h" 24#include "ecmascript/napi/include/jsnapi.h" 25#include "ecmascript/ecma_runtime_call_info.h" 26#include "ecmascript/js_thread.h" 27 28namespace panda::ecmascript { 29class ContainersPrivateFuzzTestHelper { 30public: 31 static JSFunction *JSObjectCreate(JSThread *thread) 32 { 33 EcmaVM *ecmaVM = thread->GetEcmaVM(); 34 JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv(); 35 return globalEnv->GetObjectFunction().GetObject<JSFunction>(); 36 } 37 38 static EcmaRuntimeCallInfo *CreateEcmaRuntimeCallInfo(JSThread *thread, uint32_t numArgs) 39 { 40 auto factory = thread->GetEcmaVM()->GetFactory(); 41 JSHandle<JSTaggedValue> hclass(thread, JSObjectCreate(thread)); 42 JSHandle<JSTaggedValue> callee(factory->NewJSObjectByConstructor(JSHandle<JSFunction>::Cast(hclass), hclass)); 43 JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined(); 44 EcmaRuntimeCallInfo *objCallInfo = 45 EcmaInterpreter::NewRuntimeCallInfo(thread, undefined, callee, undefined, numArgs); 46 return objCallInfo; 47 } 48 49 static void ContainersPrivateCommonFuzzTest([[maybe_unused]] const uint8_t* data, size_t size, uint8_t tag) 50 { 51 RuntimeOption option; 52 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 53 EcmaVM *vm = JSNApi::CreateJSVM(option); 54 { 55 JsiFastNativeScope scope(vm); 56 auto thread = vm->GetAssociatedJSThread(); 57 58 if (size <= 0) { 59 return; 60 } 61 62 auto factory = thread->GetEcmaVM()->GetFactory(); 63 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 64 JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject(); 65 JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate")); 66 JSHandle<JSTaggedValue> value = 67 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue(); 68 69 auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 70 objCallInfo->SetFunction(JSTaggedValue::Undefined()); 71 objCallInfo->SetThis(value.GetTaggedValue()); 72 objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(tag))); // 0 means the argument 73 containers::ContainersPrivate::Load(objCallInfo); 74 } 75 JSNApi::DestroyJSVM(vm); 76 } 77}; 78} 79#endif