1/* 2 * Copyright (c) 2023 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 "integerrefnewvalue_fuzzer.h" 17#include "ecmascript/base/string_helper.h" 18#include "ecmascript/ecma_string-inl.h" 19#include "ecmascript/napi/include/jsnapi.h" 20 21using namespace panda; 22using namespace panda::ecmascript; 23 24#define MAXBYTELEN sizeof(uint32_t) 25#define MAXBYTELENS sizeof(int32_t) 26 27namespace OHOS { 28void IntegerRefNewValueFuzzTest(const uint8_t *data, size_t size) 29{ 30 RuntimeOption option; 31 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR); 32 EcmaVM *vm = JSNApi::CreateJSVM(option); 33 unsigned int number = 123; 34 if (data == nullptr || size <= 0) { 35 std::cout << "illegal input!"; 36 return; 37 } 38 if (size > MAXBYTELEN) { 39 size = MAXBYTELEN; 40 } 41 if (memcpy_s(&number, MAXBYTELEN, data, size) != 0) { 42 std::cout << "memcpy_s failed!"; 43 UNREACHABLE(); 44 } 45 Local<JSValueRef> targetUInt = IntegerRef::NewFromUnsigned(vm, number); 46 targetUInt->Uint32Value(vm); 47 48 int value = 0; 49 if (memcpy_s(&value, MAXBYTELENS, data, size) != 0) { 50 std::cout << "memcpy_s failed!"; 51 UNREACHABLE(); 52 } 53 Local<IntegerRef> res = IntegerRef::New(vm, value); 54 res->Value(); 55 JSNApi::DestroyJSVM(vm); 56} 57} 58 59// Fuzzer entry point. 60extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) 61{ 62 // Run your code on data. 63 OHOS::IntegerRefNewValueFuzzTest(data, size); 64 return 0; 65}