• 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 "weaksetref_fuzzer.h"
17 #include "ecmascript/base/utf_helper.h"
18 #include "ecmascript/ecma_string-inl.h"
19 #include "ecmascript/global_env.h"
20 #include "ecmascript/js_handle.h"
21 #include "ecmascript/js_weak_container.h"
22 #include "ecmascript/napi/include/jsnapi.h"
23 #include "ecmascript/napi/jsnapi_helper.h"
24 #include "ecmascript/linked_hash_table.h"
25 
26 using namespace panda;
27 using namespace panda::ecmascript;
28 using namespace panda::ecmascript::base::utf_helper;
29 
30 namespace OHOS {
WeakSetRefGetSizeFuzzTest(const uint8_t * data,size_t size)31 void WeakSetRefGetSizeFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)
32 {
33     RuntimeOption option;
34     option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
35     EcmaVM *vm = JSNApi::CreateJSVM(option);
36     if (size <= 0) {
37         return;
38     }
39     JSThread *thread = vm->GetJSThread();
40     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
41     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
42     JSHandle<JSTaggedValue> constructor = env->GetBuiltinsWeakSetFunction();
43     JSHandle<JSWeakSet> weakSet =
44         JSHandle<JSWeakSet>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
45     JSHandle<LinkedHashSet> hashSet = LinkedHashSet::Create(thread);
46     weakSet->SetLinkedSet(thread, hashSet);
47     JSHandle<JSTaggedValue> weakSetTag = JSHandle<JSTaggedValue>::Cast(weakSet);
48     Local<WeakSetRef> set = JSNApiHelper::ToLocal<WeakSetRef>(weakSetTag);
49     JSHandle<JSTaggedValue> value(factory->NewFromASCII("value"));
50     JSWeakSet::Add(thread, weakSet, value);
51     set->GetSize();
52     JSNApi::DestroyJSVM(vm);
53     return;
54 }
55 
WeakSetRefGetTotalElementsFuzzTest(const uint8_t * data,size_t size)56 void WeakSetRefGetTotalElementsFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)
57 {
58     RuntimeOption option;
59     option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
60     EcmaVM *vm = JSNApi::CreateJSVM(option);
61     if (size <= 0) {
62         return;
63     }
64     JSThread *thread = vm->GetJSThread();
65     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
66     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
67     JSHandle<JSTaggedValue> constructor = env->GetBuiltinsWeakSetFunction();
68     JSHandle<JSWeakSet> weakSet =
69         JSHandle<JSWeakSet>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
70     JSHandle<LinkedHashSet> hashSet = LinkedHashSet::Create(thread);
71     weakSet->SetLinkedSet(thread, hashSet);
72     JSHandle<JSTaggedValue> weakSetTag = JSHandle<JSTaggedValue>::Cast(weakSet);
73     Local<WeakSetRef> set = JSNApiHelper::ToLocal<WeakSetRef>(weakSetTag);
74     JSHandle<JSTaggedValue> value(factory->NewFromASCII("value"));
75     JSWeakSet::Add(thread, weakSet, value);
76     set->GetTotalElements();
77     JSNApi::DestroyJSVM(vm);
78     return;
79 }
80 
WeakSetRefGetValueFuzzTest(const uint8_t * data,size_t size)81 void WeakSetRefGetValueFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)
82 {
83     RuntimeOption option;
84     option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
85     EcmaVM *vm = JSNApi::CreateJSVM(option);
86     if (size <= 0) {
87         return;
88     }
89     JSThread *thread = vm->GetJSThread();
90     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
91     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
92     JSHandle<JSTaggedValue> constructor = env->GetBuiltinsWeakSetFunction();
93     JSHandle<JSWeakSet> weakSet =
94         JSHandle<JSWeakSet>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
95     JSHandle<LinkedHashSet> hashSet = LinkedHashSet::Create(thread);
96     weakSet->SetLinkedSet(thread, hashSet);
97     JSHandle<JSTaggedValue> weakSetTag = JSHandle<JSTaggedValue>::Cast(weakSet);
98     Local<WeakSetRef> set = JSNApiHelper::ToLocal<WeakSetRef>(weakSetTag);
99     JSHandle<JSTaggedValue> value(factory->NewFromASCII("value"));
100     JSWeakSet::Add(thread, weakSet, value);
101     set->GetValue(vm, 0);
102     JSNApi::DestroyJSVM(vm);
103     return;
104 }
105 }
106 
107 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)108 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
109 {
110     // Run your code on data.
111     OHOS::WeakSetRefGetSizeFuzzTest(data, size);
112     OHOS::WeakSetRefGetTotalElementsFuzzTest(data, size);
113     OHOS::WeakSetRefGetValueFuzzTest(data, size);
114     return 0;
115 }