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(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 if (size <= 0) {
52 LOG_ECMA(ERROR) << "illegal input!";
53 return;
54 }
55 auto thread = vm->GetAssociatedJSThread();
56 JSHandle<JSSet> jsSet(thread, CreateJSSet(thread));
57 JSHandle<JSTaggedValue> jsTagSetIterator =
58 JSSetIterator::CreateSetIterator(thread, JSHandle<JSTaggedValue>(jsSet), IterationKind::KEY);
59 JSHandle<JSSetIterator> jsSetIterator1(jsTagSetIterator);
60 JSTaggedValue::SameValue(jsSetIterator1->GetIteratedSet(), jsSet->GetLinkedSet());
61 Local<SetIteratorRef> setIterator = JSNApiHelper::ToLocal<SetIteratorRef>(jsTagSetIterator);
62 Local<JSValueRef> setIterator1 = setIterator;
63 setIterator1->IsSetIterator();
64 setIterator->GetIndex();
65 setIterator->GetKind(vm);
66 JSNApi::DestroyJSVM(vm);
67 }
68 }
69
70 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)71 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
72 {
73 // Run your code on data.
74 OHOS::SetIteratorRefGetFuzztest(data, size);
75 return 0;
76 }