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 {
37 JsiFastNativeScope scope(vm);
38 if (size <= 0) {
39 return;
40 }
41 JSThread *thread = vm->GetJSThread();
42 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
43 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
44 JSHandle<JSTaggedValue> constructor = env->GetBuiltinsWeakSetFunction();
45 auto obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor);
46 JSHandle<JSWeakSet> weakSet = JSHandle<JSWeakSet>::Cast(obj);
47 JSHandle<LinkedHashSet> hashSet = LinkedHashSet::Create(thread);
48 weakSet->SetLinkedSet(thread, hashSet);
49 JSHandle<JSTaggedValue> weakSetTag = JSHandle<JSTaggedValue>::Cast(weakSet);
50 Local<WeakSetRef> set = JSNApiHelper::ToLocal<WeakSetRef>(weakSetTag);
51 JSHandle<JSTaggedValue> value(factory->NewFromASCII("value"));
52 JSWeakSet::Add(thread, weakSet, value);
53 set->GetSize(vm);
54 }
55 JSNApi::DestroyJSVM(vm);
56 return;
57 }
58
WeakSetRefGetTotalElementsFuzzTest(const uint8_t * data,size_t size)59 void WeakSetRefGetTotalElementsFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)
60 {
61 RuntimeOption option;
62 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
63 EcmaVM *vm = JSNApi::CreateJSVM(option);
64 {
65 JsiFastNativeScope scope(vm);
66 if (size <= 0) {
67 return;
68 }
69 JSThread *thread = vm->GetJSThread();
70 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
71 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
72 JSHandle<JSTaggedValue> constructor = env->GetBuiltinsWeakSetFunction();
73 auto obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor);
74 JSHandle<JSWeakSet> weakSet = JSHandle<JSWeakSet>::Cast(obj);
75 JSHandle<LinkedHashSet> hashSet = LinkedHashSet::Create(thread);
76 weakSet->SetLinkedSet(thread, hashSet);
77 JSHandle<JSTaggedValue> weakSetTag = JSHandle<JSTaggedValue>::Cast(weakSet);
78 Local<WeakSetRef> set = JSNApiHelper::ToLocal<WeakSetRef>(weakSetTag);
79 JSHandle<JSTaggedValue> value(factory->NewFromASCII("value"));
80 JSWeakSet::Add(thread, weakSet, value);
81 set->GetTotalElements(vm);
82 }
83 JSNApi::DestroyJSVM(vm);
84 return;
85 }
86
WeakSetRefGetValueFuzzTest(const uint8_t * data,size_t size)87 void WeakSetRefGetValueFuzzTest([[maybe_unused]]const uint8_t *data, size_t size)
88 {
89 RuntimeOption option;
90 option.SetLogLevel(RuntimeOption::LOG_LEVEL::ERROR);
91 EcmaVM *vm = JSNApi::CreateJSVM(option);
92 {
93 JsiFastNativeScope scope(vm);
94 if (size <= 0) {
95 return;
96 }
97 JSThread *thread = vm->GetJSThread();
98 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
99 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
100 JSHandle<JSTaggedValue> constructor = env->GetBuiltinsWeakSetFunction();
101 auto obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor);
102 JSHandle<JSWeakSet> weakSet = JSHandle<JSWeakSet>::Cast(obj);
103 JSHandle<LinkedHashSet> hashSet = LinkedHashSet::Create(thread);
104 weakSet->SetLinkedSet(thread, hashSet);
105 JSHandle<JSTaggedValue> weakSetTag = JSHandle<JSTaggedValue>::Cast(weakSet);
106 Local<WeakSetRef> set = JSNApiHelper::ToLocal<WeakSetRef>(weakSetTag);
107 JSHandle<JSTaggedValue> value(factory->NewFromASCII("value"));
108 JSWeakSet::Add(thread, weakSet, value);
109 set->GetValue(vm, 0);
110 }
111 JSNApi::DestroyJSVM(vm);
112 return;
113 }
114 }
115
116 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)117 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
118 {
119 // Run your code on data.
120 OHOS::WeakSetRefGetSizeFuzzTest(data, size);
121 OHOS::WeakSetRefGetTotalElementsFuzzTest(data, size);
122 OHOS::WeakSetRefGetValueFuzzTest(data, size);
123 return 0;
124 }