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