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 "ecmascript/ecma_string-inl.h"
17 #include "ecmascript/global_env.h"
18 #include "ecmascript/js_function.h"
19 #include "ecmascript/js_generator_object.h"
20 #include "ecmascript/js_handle.h"
21 #include "ecmascript/napi/include/jsnapi.h"
22 #include "ecmascript/napi/jsnapi_helper.h"
23 #include "publicapigeneratorobjectref_fuzzer.h"
24
25 using namespace panda;
26 using namespace panda::ecmascript;
27
28 namespace OHOS {
GetGeneratorReceiverFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)29 void GetGeneratorReceiverFuzzTest([[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 LOG_ECMA(ERROR) << "illegal input!";
38 return;
39 }
40 JSThread *thread = vm->GetJSThread();
41 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
42 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
43 JSHandle<JSTaggedValue> genFunc = env->GetGeneratorFunctionFunction();
44 JSHandle<JSGeneratorObject> genObjHandleVal = factory->NewJSGeneratorObject(genFunc);
45 JSHandle<JSHClass> hclass = JSHandle<JSHClass>::Cast(env->GetGeneratorFunctionClass());
46 JSHandle<JSFunction> generatorFunc = JSHandle<JSFunction>::Cast(factory->NewJSObject(hclass));
47 JSFunction::InitializeJSFunction(thread, generatorFunc, FunctionKind::GENERATOR_FUNCTION);
48 JSHandle<GeneratorContext> generatorContext = factory->NewGeneratorContext();
49 generatorContext->SetMethod(thread, generatorFunc.GetTaggedValue());
50 JSHandle<JSTaggedValue> generatorContextVal = JSHandle<JSTaggedValue>::Cast(generatorContext);
51 genObjHandleVal->SetGeneratorContext(thread, generatorContextVal.GetTaggedValue());
52 genObjHandleVal->SetGeneratorState(JSGeneratorState::COMPLETED);
53 JSHandle<JSTaggedValue> genObjTagHandleVal = JSHandle<JSTaggedValue>::Cast(genObjHandleVal);
54 Local<GeneratorObjectRef> object = JSNApiHelper::ToLocal<GeneratorObjectRef>(genObjTagHandleVal);
55 object->GetGeneratorReceiver(vm);
56 }
57 JSNApi::DestroyJSVM(vm);
58 return;
59 }
60 }
61
62 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)63 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
64 {
65 // Run your code on data.
66 OHOS::GetGeneratorReceiverFuzzTest(data, size);
67 return 0;
68 }