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#include "containerslightweightsethashash_fuzzer.h" 17 18#include "ecmascript/containers/containers_lightweightset.h" 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 26using namespace panda; 27using namespace panda::test; 28using namespace panda::ecmascript; 29using namespace panda::ecmascript::containers; 30 31namespace OHOS { 32 33 JSFunction *JSObjectCreate(JSThread *thread) 34 { 35 EcmaVM *ecmaVM = thread->GetEcmaVM(); 36 JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv(); 37 return globalEnv->GetObjectFunction().GetObject<JSFunction>(); 38 } 39 40 EcmaRuntimeCallInfo *CreateEcmaRuntimeCallInfo(JSThread *thread, uint32_t numArgs) 41 { 42 auto factory = thread->GetEcmaVM()->GetFactory(); 43 JSHandle<JSTaggedValue> hclass(thread, JSObjectCreate(thread)); 44 JSHandle<JSTaggedValue> callee(factory->NewJSObjectByConstructor(JSHandle<JSFunction>::Cast(hclass), hclass)); 45 JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined(); 46 EcmaRuntimeCallInfo *objCallInfo = 47 EcmaInterpreter::NewRuntimeCallInfo(thread, undefined, callee, undefined, numArgs); 48 return objCallInfo; 49 } 50 51 JSTaggedValue InitializeLightWeightSetConstructor(JSThread *thread) 52 { 53 auto factory = thread->GetEcmaVM()->GetFactory(); 54 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv(); 55 JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject(); 56 JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate")); 57 JSHandle<JSTaggedValue> value = 58 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue(); 59 auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 60 objCallInfo->SetFunction(JSTaggedValue::Undefined()); 61 objCallInfo->SetThis(value.GetTaggedValue()); 62 objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(ContainerTag::LightWeightSet))); 63 JSTaggedValue result = ContainersPrivate::Load(objCallInfo); 64 return result; 65 } 66 67 JSHandle<JSAPILightWeightSet> CreateJSAPILightWeightSet(JSThread *thread) 68 { 69 JSHandle<JSFunction> newTarget(thread, InitializeLightWeightSetConstructor(thread)); 70 auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 4); 71 objCallInfo->SetFunction(newTarget.GetTaggedValue()); 72 objCallInfo->SetNewTarget(newTarget.GetTaggedValue()); 73 objCallInfo->SetThis(JSTaggedValue::Undefined()); 74 JSTaggedValue result = ContainersLightWeightSet::LightWeightSetConstructor(objCallInfo); 75 JSHandle<JSAPILightWeightSet> map(thread, result); 76 return map; 77 } 78 79 void ContainersLightWeightSetFuzzTest(const uint8_t* data, size_t size) 80 { 81 RuntimeOption option; 82 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 83 EcmaVM *vm = JSNApi::CreateJSVM(option); 84 { 85 JsiFastNativeScope scope(vm); 86 auto thread = vm->GetAssociatedJSThread(); 87 if (size <= 0) { 88 return; 89 } 90 double input = 0; 91 const double maxByteLen = 4; 92 if (size > maxByteLen) { 93 size = maxByteLen; 94 } 95 if (memcpy_s(&input, maxByteLen, data, size) != 0) { 96 std::cout << "memcpy_s failed!"; 97 UNREACHABLE(); 98 } 99 JSHandle<JSAPILightWeightSet> lightWeightSet = CreateJSAPILightWeightSet(thread); 100 EcmaRuntimeCallInfo *callInfo = CreateEcmaRuntimeCallInfo(thread, 6); // 6 : means the argv length 101 callInfo->SetFunction(JSTaggedValue::Undefined()); 102 callInfo->SetThis(lightWeightSet.GetTaggedValue()); 103 callInfo->SetCallArg(0, JSTaggedValue(input)); 104 ContainersLightWeightSet::HasHash(callInfo); 105 } 106 JSNApi::DestroyJSVM(vm); 107 } 108} 109 110// Fuzzer entry point. 111extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) 112{ 113 // Run your code on data. 114 OHOS::ContainersLightWeightSetFuzzTest(data, size); 115 return 0; 116}