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 "arrayref_fuzzer.h"
17 #include "ecmascript/base/string_helper.h"
18 #include "ecmascript/napi/include/dfx_jsnapi.h"
19 #include "ecmascript/napi/include/jsnapi.h"
20
21 using namespace panda;
22 using namespace panda::ecmascript;
23
24 namespace OHOS {
ArrayRefNewFuzzerTest(const uint8_t * data,size_t size)25 void ArrayRefNewFuzzerTest([[maybe_unused]]const uint8_t* data, size_t size)
26 {
27 RuntimeOption option;
28 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
29 EcmaVM *vm = JSNApi::CreateJSVM(option);
30 if (size <= 0) {
31 LOG_ECMA(ERROR) << "illegal input!";
32 return;
33 }
34 ArrayRef::New(vm, (uint32_t)size);
35 JSNApi::DestroyJSVM(vm);
36 }
37
ArrayRefGetValueAtFuzzerTest(const uint8_t * data,size_t size)38 void ArrayRefGetValueAtFuzzerTest([[maybe_unused]]const uint8_t* data, size_t size)
39 {
40 RuntimeOption option;
41 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
42 EcmaVM *vm = JSNApi::CreateJSVM(option);
43 Local<ObjectRef> globalObject = JSNApi::GetGlobalObject(vm);
44 Local<ArrayRef> property = ArrayRef::New(vm, (uint32_t)size);
45 if (size <= 0) {
46 LOG_ECMA(ERROR) << "illegal input!";
47 return;
48 }
49 [[maybe_unused]]Local<JSValueRef> value = property->GetValueAt(vm, globalObject, (uint32_t)size);
50 JSNApi::DestroyJSVM(vm);
51 }
52
ArrayRefLengthFuzzerTest(const uint8_t * data,size_t size)53 void ArrayRefLengthFuzzerTest([[maybe_unused]]const uint8_t* data, size_t size)
54 {
55 RuntimeOption option;
56 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
57 EcmaVM *vm = JSNApi::CreateJSVM(option);
58 if (size <= 0) {
59 LOG_ECMA(ERROR) << "illegal input!";
60 return;
61 }
62 Local<ArrayRef> property = ArrayRef::New(vm, (uint32_t)size);
63 property->Length(vm);
64 JSNApi::DestroyJSVM(vm);
65 }
66
ArrayRefSetValueAtFuzzerTest(const uint8_t * data,size_t size)67 void ArrayRefSetValueAtFuzzerTest([[maybe_unused]]const uint8_t* data, size_t size)
68 {
69 RuntimeOption option;
70 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
71 EcmaVM *vm = JSNApi::CreateJSVM(option);
72 if (size <= 0) {
73 LOG_ECMA(ERROR) << "illegal input!";
74 return;
75 }
76 Local<ObjectRef> globalObject = JSNApi::GetGlobalObject(vm);
77 Local<ArrayRef> property = ArrayRef::New(vm, (uint32_t)size);
78 Local<JSValueRef> value = ObjectRef::New(vm);
79 [[maybe_unused]]bool result = property->SetValueAt(vm, globalObject, (uint32_t)size, value);
80 JSNApi::DestroyJSVM(vm);
81 }
82 }
83
84 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)85 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
86 {
87 // Run your code on data.
88 OHOS::ArrayRefNewFuzzerTest(data, size);
89 OHOS::ArrayRefGetValueAtFuzzerTest(data, size);
90 OHOS::ArrayRefLengthFuzzerTest(data, size);
91 OHOS::ArrayRefSetValueAtFuzzerTest(data, size);
92 return 0;
93 }