1/*
2 * Copyright (c) 2023-2024 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 "functionrefinherit_fuzzer.h"
17#include "ecmascript/base/utf_helper.h"
18#include "ecmascript/ecma_string-inl.h"
19#include "ecmascript/global_env.h"
20#include "ecmascript/js_function.h"
21#include "ecmascript/napi/include/jsnapi.h"
22#include "ecmascript/napi/jsnapi_helper.h"
23
24using namespace panda;
25using namespace panda::ecmascript;
26using namespace panda::ecmascript::base::utf_helper;
27
28namespace OHOS {
29void FunctionRefInheritFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)
30{
31    RuntimeOption option;
32    option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
33    EcmaVM *vm = JSNApi::CreateJSVM(option);
34    {
35        JsiFastNativeScope scope(vm);
36        if (size <= 0) {
37            return;
38        }
39        JSThread *thread = vm->GetJSThread();
40        JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
41        JSHandle<JSTaggedValue> set = env->GetBuiltinsSetFunction();
42        Local<FunctionRef> setLocal = JSNApiHelper::ToLocal<FunctionRef>(set);
43        JSHandle<JSTaggedValue> map = env->GetBuiltinsMapFunction();
44        Local<FunctionRef> mapLocal = JSNApiHelper::ToLocal<FunctionRef>(map);
45        JSHandle<JSTaggedValue> setPrototype(thread, JSHandle<JSFunction>::Cast(set)->GetFunctionPrototype());
46        JSHandle<JSTaggedValue> mapPrototype(thread, JSHandle<JSFunction>::Cast(map)->GetFunctionPrototype());
47        JSHandle<JSTaggedValue> mapPrototypeProto(thread, JSTaggedValue::GetPrototype(thread, mapPrototype));
48        JSTaggedValue::SameValue(setPrototype, mapPrototypeProto);
49        mapLocal->Inherit(vm, setLocal);
50    }
51    JSNApi::DestroyJSVM(vm);
52    return;
53}
54}
55
56// Fuzzer entry point.
57extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
58{
59    // Run your code on data.
60    OHOS::FunctionRefInheritFuzzTest(data, size);
61    return 0;
62}