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 "objectref_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 using FunctionForRef = Local<JSValueRef>(*)(JsiRuntimeCallInfo*);
24 namespace OHOS {
FuncRefNewCallbackForTest(JsiRuntimeCallInfo * info)25 Local<JSValueRef> FuncRefNewCallbackForTest(JsiRuntimeCallInfo* info)
26 {
27 LOG_ECMA(ERROR) << "runing FuncRefNewCallbackForTest";
28 EscapeLocalScope scope(info->GetVM());
29 return scope.Escape(ArrayRef::New(info->GetVM(), info->GetArgsNumber()));
30 }
SetAccessorPropertyFuzzTest(const uint8_t * data,size_t size)31 void SetAccessorPropertyFuzzTest(const uint8_t* data, size_t size)
32 {
33 RuntimeOption option;
34 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
35 EcmaVM *vm = JSNApi::CreateJSVM(option);
36 if (data == nullptr || size <= 0) {
37 LOG_ECMA(ERROR) << "illegal input!";
38 return;
39 }
40 Local<JSValueRef> key = StringRef::NewFromUtf8(vm, (char*)data, (int)size);
41 FunctionForRef nativeFunc = FuncRefNewCallbackForTest;
42 Local<FunctionRef> getter = FunctionRef::New(vm, nativeFunc);
43 Local<FunctionRef> setter = FunctionRef::New(vm, nativeFunc);
44 Local<ObjectRef> object = ObjectRef::New(vm);
45 object->SetAccessorProperty(vm, key, getter, setter);
46 JSNApi::DestroyJSVM(vm);
47 }
48
DefinePropertyFuzzTest(const uint8_t * data,size_t size)49 void DefinePropertyFuzzTest(const uint8_t* data, size_t size)
50 {
51 RuntimeOption option;
52 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
53 EcmaVM *vm = JSNApi::CreateJSVM(option);
54 if (data == nullptr || size <= 0) {
55 LOG_ECMA(ERROR) << "illegal input!";
56 return;
57 }
58 Local<ObjectRef> object = ObjectRef::New(vm);
59 Local<JSValueRef> key = StringRef::NewFromUtf8(vm, (char*)data, (int)size);
60 Local<JSValueRef> value = ObjectRef::New(vm);
61 PropertyAttribute attribute(value, true, true, true);
62 object->DefineProperty(vm, key, attribute);
63 JSNApi::DestroyJSVM(vm);
64 }
65 }
66
67 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)68 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
69 {
70 // Run your code on data.
71 OHOS::SetAccessorPropertyFuzzTest(data, size);
72 OHOS::DefinePropertyFuzzTest(data, size);
73 return 0;
74 }