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 "setiteratorrefget_fuzzer.h"
17 
18 #include "ecmascript/base/string_helper.h"
19 #include "ecmascript/ecma_vm.h"
20 #include "ecmascript/global_env.h"
21 #include "ecmascript/js_handle.h"
22 #include "ecmascript/js_set.h"
23 #include "ecmascript/js_set_iterator.h"
24 #include "ecmascript/js_tagged_value.h"
25 #include "ecmascript/linked_hash_table.h"
26 #include "ecmascript/napi/include/jsnapi.h"
27 #include "ecmascript/napi/jsnapi_helper.h"
28 #include "ecmascript/object_factory.h"
29 
30 using namespace panda;
31 using namespace panda::ecmascript;
32 
33 namespace OHOS {
CreateJSSet(JSThread *thread)34 JSSet *CreateJSSet(JSThread *thread)
35 {
36     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
37     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
38     JSHandle<JSTaggedValue> constructor = env->GetBuiltinsSetFunction();
39     JSHandle<JSSet> set =
40         JSHandle<JSSet>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
41     JSHandle<LinkedHashSet> hashSet = LinkedHashSet::Create(thread);
42     set->SetLinkedSet(thread, hashSet);
43     return JSSet::Cast(set.GetTaggedValue().GetTaggedObject());
44 }
45 
SetIteratorRefGetFuzztest([[maybe_unused]]const uint8_t *data, size_t size)46 void SetIteratorRefGetFuzztest([[maybe_unused]]const uint8_t *data, size_t size)
47 {
48     RuntimeOption option;
49     option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
50     EcmaVM *vm = JSNApi::CreateJSVM(option);
51     {
52         JsiFastNativeScope scope(vm);
53         if (size <= 0) {
54             LOG_ECMA(ERROR) << "illegal input!";
55             return;
56         }
57         auto thread = vm->GetAssociatedJSThread();
58         JSHandle<JSSet> jsSet(thread, CreateJSSet(thread));
59         JSHandle<JSTaggedValue> jsTagSetIterator =
60             JSSetIterator::CreateSetIterator(thread, JSHandle<JSTaggedValue>(jsSet), IterationKind::KEY);
61         JSHandle<JSSetIterator> jsSetIterator1(jsTagSetIterator);
62         JSTaggedValue::SameValue(jsSetIterator1->GetIteratedSet(), jsSet->GetLinkedSet());
63         Local<SetIteratorRef> setIterator = JSNApiHelper::ToLocal<SetIteratorRef>(jsTagSetIterator);
64         Local<JSValueRef> setIterator1 = setIterator;
65         setIterator1->IsSetIterator(vm);
66         setIterator->GetIndex();
67         setIterator->GetKind(vm);
68     }
69     JSNApi::DestroyJSVM(vm);
70 }
71 }
72 
73 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)74 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
75 {
76     // Run your code on data.
77     OHOS::SetIteratorRefGetFuzztest(data, size);
78     return 0;
79 }