/* * 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 #include "containersfastbuffercompare_fuzzer.h" #include "ecmascript/containers/containers_buffer.h" #include "ecmascript/containers/containers_private.h" #include "ecmascript/ecma_string-inl.h" #include "ecmascript/ecma_vm.h" #include "ecmascript/global_env.h" #include "ecmascript/js_api/js_api_buffer.h" #include "ecmascript/js_handle.h" #include "ecmascript/object_factory.h" using namespace panda; using namespace panda::ecmascript; using namespace panda::ecmascript::containers; #define MAXBYTELEN sizeof(uint32_t) 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 InitializeFastBufferConstructor(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::FastBuffer))); JSTaggedValue result = ContainersPrivate::Load(objCallInfo); return result; } JSHandle CreateJSAPIFastBuffer(JSThread *thread, uint32_t length) { JSHandle newTarget(thread, InitializeFastBufferConstructor(thread)); auto objCallInfo = CreateEcmaRuntimeCallInfo(thread, 6); objCallInfo->SetFunction(newTarget.GetTaggedValue()); objCallInfo->SetNewTarget(newTarget.GetTaggedValue()); objCallInfo->SetThis(JSTaggedValue::Undefined()); objCallInfo->SetCallArg(0, JSTaggedValue(length)); JSTaggedValue result = ContainersBuffer::BufferConstructor(objCallInfo); JSHandle buffer(thread, result); return buffer; } void ContainersFastBufferCompareFuzzTest(const uint8_t* data, size_t size) { constexpr size_t STRING_MAX_LENGTH = 100; RuntimeOption option; option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); EcmaVM *vm = JSNApi::CreateJSVM(option); { JsiFastNativeScope scope(vm); auto thread = vm->GetAssociatedJSThread(); const int32_t MaxMenory = 1024; if (size > MaxMenory) { size = MaxMenory; } FuzzedDataProvider fdp(data, size); ObjectFactory *factory = vm->GetFactory(); int32_t targetStart = fdp.ConsumeIntegral(); int32_t targetEnd = fdp.ConsumeIntegral(); int32_t sourceStart = fdp.ConsumeIntegral(); int32_t sourceEnd = fdp.ConsumeIntegral(); std::string rdStr = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH); JSHandle str = factory->NewFromStdString(rdStr); JSHandle buf1 = CreateJSAPIFastBuffer(thread, STRING_MAX_LENGTH); JSHandle buf2 = CreateJSAPIFastBuffer(thread, STRING_MAX_LENGTH); EcmaRuntimeCallInfo *callInfo1 = CreateEcmaRuntimeCallInfo(thread, 6); callInfo1->SetFunction(JSTaggedValue::Undefined()); callInfo1->SetThis(buf1.GetTaggedValue()); // 0 : means the first parameter callInfo1->SetCallArg(0, JSTaggedValue(*str)); [[maybe_unused]] JSTaggedValue ret = ContainersBuffer::Write(callInfo1); EcmaRuntimeCallInfo *callInfo2 = CreateEcmaRuntimeCallInfo(thread, 6); callInfo2->SetFunction(JSTaggedValue::Undefined()); callInfo2->SetThis(buf2.GetTaggedValue()); // 0 : means the first parameter callInfo2->SetCallArg(0, JSTaggedValue(*str)); ret = ContainersBuffer::Write(callInfo2); EcmaRuntimeCallInfo *callInfo3 = CreateEcmaRuntimeCallInfo(thread, 14); callInfo3->SetFunction(JSTaggedValue::Undefined()); callInfo3->SetThis(buf2.GetTaggedValue()); // 0 : means the first parameter callInfo3->SetCallArg(0, buf1.GetTaggedValue()); // 1 : means the second parameter callInfo3->SetCallArg(1, JSTaggedValue(targetStart)); // 2 : means the third parameter callInfo3->SetCallArg(2, JSTaggedValue(targetEnd)); // 3 : means the fourth parameter callInfo3->SetCallArg(3, JSTaggedValue(sourceStart)); // 4 : means the fifth parameter callInfo3->SetCallArg(4, JSTaggedValue(sourceEnd)); ret = ContainersBuffer::Compare(callInfo3); } JSNApi::DestroyJSVM(vm); } } // Fuzzer entry point. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { // Run your code on data. OHOS::ContainersFastBufferCompareFuzzTest(data, size); return 0; }