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 <fuzzer/FuzzedDataProvider.h>
17 #include "ecmascript/ecma_string-inl.h"
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/js_function.h"
20 #include "ecmascript/js_generator_object.h"
21 #include "ecmascript/js_handle.h"
22 #include "ecmascript/napi/include/jsnapi.h"
23 #include "ecmascript/napi/jsnapi_helper.h"
24 #include "publicapigeneratorobjectref_fuzzer.h"
25
26 using namespace panda;
27 using namespace panda::ecmascript;
28
29 namespace OHOS {
GetGeneratorReceiverFuzzTest(const uint8_t * data,size_t size)30 void GetGeneratorReceiverFuzzTest(const uint8_t *data, size_t size)
31 {
32 RuntimeOption option;
33 option.SetLogLevel(common::LOG_LEVEL::ERROR);
34 EcmaVM *vm = JSNApi::CreateJSVM(option);
35 {
36 JsiFastNativeScope scope(vm);
37 if (size <= 0) {
38 LOG_ECMA(ERROR) << "illegal input!";
39 return;
40 }
41 JSThread *thread = vm->GetJSThread();
42 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
43 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
44 JSHandle<JSTaggedValue> genFunc = env->GetGeneratorFunctionFunction();
45 JSHandle<JSGeneratorObject> genObjHandleVal = factory->NewJSGeneratorObject(genFunc);
46 JSHandle<JSHClass> hclass = JSHandle<JSHClass>::Cast(env->GetGeneratorFunctionClass());
47 JSHandle<JSFunction> generatorFunc = JSHandle<JSFunction>::Cast(factory->NewJSObject(hclass));
48 JSFunction::InitializeJSFunction(thread, generatorFunc, FunctionKind::GENERATOR_FUNCTION);
49 JSHandle<GeneratorContext> generatorContext = factory->NewGeneratorContext();
50 generatorContext->SetMethod(thread, generatorFunc.GetTaggedValue());
51 JSHandle<JSTaggedValue> generatorContextVal = JSHandle<JSTaggedValue>::Cast(generatorContext);
52 genObjHandleVal->SetGeneratorContext(thread, generatorContextVal.GetTaggedValue());
53 FuzzedDataProvider fdp(data, size);
54 auto state = fdp.PickValueInArray({
55 JSGeneratorState::UNDEFINED,
56 JSGeneratorState::SUSPENDED_START,
57 JSGeneratorState::SUSPENDED_YIELD,
58 JSGeneratorState::EXECUTING,
59 JSGeneratorState::COMPLETED,
60 });
61 genObjHandleVal->SetGeneratorState(state);
62 JSHandle<JSTaggedValue> genObjTagHandleVal = JSHandle<JSTaggedValue>::Cast(genObjHandleVal);
63 Local<GeneratorObjectRef> object = JSNApiHelper::ToLocal<GeneratorObjectRef>(genObjTagHandleVal);
64 object->GetGeneratorReceiver(vm);
65 }
66 JSNApi::DestroyJSVM(vm);
67 return;
68 }
69 }
70
71 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)72 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
73 {
74 // Run your code on data.
75 OHOS::GetGeneratorReceiverFuzzTest(data, size);
76 return 0;
77 }